|
| 1 | +import { useColorMode } from "@docusaurus/theme-common"; |
| 2 | +import Heading from "@theme/Heading"; |
| 3 | +import { |
| 4 | + BarElement, |
| 5 | + CategoryScale, |
| 6 | + Chart as ChartJS, |
| 7 | + Colors, |
| 8 | + Legend, |
| 9 | + LineElement, |
| 10 | + LinearScale, |
| 11 | + PointElement, |
| 12 | + Title, |
| 13 | + Tooltip, |
| 14 | +} from "chart.js"; |
| 15 | +import React from "react"; |
| 16 | +import { Bar, Line } from "react-chartjs-2"; |
| 17 | +import styles from "./styles.module.css"; |
| 18 | + |
| 19 | +ChartJS.register( |
| 20 | + CategoryScale, |
| 21 | + LinearScale, |
| 22 | + PointElement, |
| 23 | + LineElement, |
| 24 | + Title, |
| 25 | + Tooltip, |
| 26 | + Legend, |
| 27 | + Colors, |
| 28 | + BarElement |
| 29 | +); |
| 30 | + |
| 31 | +export function BenchmarkGraphs() { |
| 32 | + const [data, setData] = React.useState([]); |
| 33 | + const colorMode = useColorMode(); |
| 34 | + ChartJS.defaults.color = colorMode.colorMode === "light" ? "#666" : "white"; |
| 35 | + |
| 36 | + React.useEffect(() => { |
| 37 | + Promise.all([ |
| 38 | + buildChartFromBenchmark("Crypto"), |
| 39 | + buildChartFromBenchmark("DeltaBlue"), |
| 40 | + buildChartFromBenchmark("EarleyBoyer"), |
| 41 | + buildChartFromBenchmark("NavierStokes"), |
| 42 | + buildChartFromBenchmark("RayTrace"), |
| 43 | + buildChartFromBenchmark("RegExp"), |
| 44 | + buildChartFromBenchmark("Richards"), |
| 45 | + buildChartFromBenchmark("Splay"), |
| 46 | + buildChartFromBenchmark("score"), |
| 47 | + ]).then((charts) => setData(charts)); |
| 48 | + }, [colorMode.colorMode]); |
| 49 | + |
| 50 | + return data && data.map((chart) => chart); |
| 51 | +} |
| 52 | + |
| 53 | +const buildChartFromBenchmark = async (name: string) => { |
| 54 | + const data = await fetchData( |
| 55 | + `https://raw.githubusercontent.com/boa-dev/data/main/bench/results/${name}.json` |
| 56 | + ); |
| 57 | + |
| 58 | + const barData = getBarChartData(data); |
| 59 | + |
| 60 | + return ( |
| 61 | + <div key={name}> |
| 62 | + <div className={`card__header ${styles["benchmark-card-header"]}`}> |
| 63 | + <Heading as="h2">{name}</Heading> |
| 64 | + </div> |
| 65 | + <div className={styles["cards-wrap"]}> |
| 66 | + <div className={`card ${styles["benchmark-card"]}`}> |
| 67 | + <Line data={data}></Line> |
| 68 | + </div> |
| 69 | + <div className={`card ${styles["benchmark-card"]}`}> |
| 70 | + <Bar data={barData}></Bar> |
| 71 | + </div> |
| 72 | + </div> |
| 73 | + </div> |
| 74 | + ); |
| 75 | +}; |
| 76 | + |
| 77 | +const fetchData = async (url: string) => { |
| 78 | + const response = await fetch(url); |
| 79 | + const data = await response.json(); |
| 80 | + return { |
| 81 | + labels: data.labels.map((epoch: number) => |
| 82 | + new Date(epoch).toLocaleDateString() |
| 83 | + ), |
| 84 | + datasets: [ |
| 85 | + { |
| 86 | + label: "boa", |
| 87 | + data: data.results["boa"], |
| 88 | + fill: false, |
| 89 | + }, |
| 90 | + { |
| 91 | + label: "v8-jitless", |
| 92 | + data: data.results["v8-jitless"], |
| 93 | + fill: false, |
| 94 | + }, |
| 95 | + { |
| 96 | + label: "libjs", |
| 97 | + data: data.results["libjs"], |
| 98 | + fill: false, |
| 99 | + }, |
| 100 | + { |
| 101 | + label: "duktape", |
| 102 | + data: data.results["duktape"], |
| 103 | + fill: false, |
| 104 | + }, |
| 105 | + { |
| 106 | + label: "quickjs", |
| 107 | + data: data.results["quickjs"], |
| 108 | + fill: false, |
| 109 | + }, |
| 110 | + ], |
| 111 | + }; |
| 112 | +}; |
| 113 | + |
| 114 | +const getBarChartData = (data) => { |
| 115 | + // We only want the last value from each dataset |
| 116 | + return { |
| 117 | + labels: [data.labels[data.labels.length - 1]], |
| 118 | + datasets: data.datasets.map((dataset) => { |
| 119 | + return { |
| 120 | + label: dataset.label, |
| 121 | + data: [dataset.data[dataset.data.length - 1]], |
| 122 | + }; |
| 123 | + }), |
| 124 | + }; |
| 125 | +}; |
0 commit comments