@@ -12,7 +12,7 @@ import {
1212 Title ,
1313 Tooltip ,
1414} from "chart.js" ;
15- import React from "react" ;
15+ import { useEffect , useState } from "react" ;
1616import { Bar , Line } from "react-chartjs-2" ;
1717import styles from "./styles.module.css" ;
1818
@@ -30,6 +30,7 @@ ChartJS.register(
3030
3131interface BenchmarkGraphsProps {
3232 selectedEngines : string [ ] ;
33+ range : string ;
3334}
3435
3536const ChartOptions = {
@@ -42,75 +43,50 @@ const ChartOptions = {
4243
4344export const BenchmarkGraphs : React . FC < BenchmarkGraphsProps > = ( {
4445 selectedEngines,
46+ range
4547} ) => {
4648 // Control the state of which engines are displayed using a Set
4749
48- const [ data , setData ] = React . useState ( [ ] ) ;
50+ const [ charts , setCharts ] = useState ( [ ] ) ;
51+ const [ data , setData ] = useState ( { } ) ;
4952 const colorMode = useColorMode ( ) ;
5053 ChartJS . defaults . color = colorMode . colorMode === "light" ? "#666" : "white" ;
5154
52- React . useEffect ( ( ) => {
53- Promise . all ( [
54- buildChartFromBenchmark ( "Crypto" , selectedEngines ) ,
55- buildChartFromBenchmark ( "DeltaBlue" , selectedEngines ) ,
56- buildChartFromBenchmark ( "EarleyBoyer" , selectedEngines ) ,
57- buildChartFromBenchmark ( "NavierStokes" , selectedEngines ) ,
58- buildChartFromBenchmark ( "RayTrace" , selectedEngines ) ,
59- buildChartFromBenchmark ( "RegExp" , selectedEngines ) ,
60- buildChartFromBenchmark ( "Richards" , selectedEngines ) ,
61- buildChartFromBenchmark ( "Splay" , selectedEngines ) ,
62- buildChartFromBenchmark ( "score" , selectedEngines ) ,
63- ] ) . then ( ( charts ) => setData ( charts ) ) ;
64- } , [ selectedEngines ] ) ;
65-
66- return data && data . map ( ( chart ) => chart ) ;
67- } ;
6855
69- const buildChartFromBenchmark = async ( name : string , engines : string [ ] ) => {
70- const data = await fetchData (
71- `https://raw.githubusercontent.com/boa-dev/data/main/bench/results/${ name } .json` ,
72- engines ,
73- ) ;
56+ useEffect ( ( ) => {
57+ fetch ( `https://boa-api.jason-williams.co.uk/benchmarks?months=${ range } &engines=${ selectedEngines . join ( ',' ) } ` ) . then (
58+ ( res ) => res . json ( ) )
59+ . then ( respData => {
60+ setData ( respData )
61+ } ) ;
62+ } , [ selectedEngines , range ] ) ;
7463
75- const barData = getBarChartData ( data ) ;
64+ useEffect ( ( ) => {
65+ setCharts ( buildChartFromBenchmark ( data ) ) ;
66+ } , [ data ] ) ;
7667
77- return (
78- < div key = { name } >
79- < div className = { `card__header ${ styles [ "benchmark-card-header" ] } ` } >
80- < Heading as = "h2" > { name } </ Heading >
81- </ div >
82- < div className = { styles [ "cards-wrap" ] } >
83- < div className = { `card ${ styles [ "benchmark-card" ] } ` } >
84- < Line data = { data } options = { ChartOptions } > </ Line >
85- </ div >
86- < div className = { `card ${ styles [ "benchmark-card" ] } ` } >
87- < Bar data = { barData } options = { ChartOptions } > </ Bar >
88- </ div >
89- </ div >
90- </ div >
91- ) ;
68+
69+ return charts && charts . map ( ( chart ) => chart ) ;
9270} ;
9371
94- const fetchData = async ( url : string , engines : string [ ] ) => {
95- const response = await fetch ( url ) ;
96- const data = await response . json ( ) ;
97- // Add the dataset if the engine is enabled
72+ const normalizeBenchmarkData = ( benchmarkData : any [ ] ) => {
73+ const labels = benchmarkData . map ( ( entry : any ) =>
74+ new Date ( entry . date ) . toLocaleDateString ( ) ,
75+ ) ;
76+
77+ const engines = Object . keys ( benchmarkData [ 0 ] ) . filter ( key => key != "date" ) ;
78+
9879 return {
99- labels : data . labels . map ( ( epoch : number ) =>
100- new Date ( epoch ) . toLocaleDateString ( ) ,
101- ) ,
102- datasets : Object . keys ( data . results )
103- . filter ( ( engine ) => engines . includes ( engine ) )
104- . map ( ( engine ) => {
105- return {
106- label : engine ,
107- data : data . results [ engine ] ,
108- fill : false ,
109- } ;
110- } ) ,
80+ labels,
81+ datasets : engines . map ( engine => ( {
82+ label : engine ,
83+ data : benchmarkData . map ( entry => entry [ engine ] ) ,
84+ fill : false
85+ } ) ) ,
11186 } ;
11287} ;
11388
89+
11490const getBarChartData = ( data ) => {
11591 // We only want the last value from each dataset
11692 return {
@@ -123,3 +99,33 @@ const getBarChartData = (data) => {
12399 } ) ,
124100 } ;
125101} ;
102+
103+ const buildChartFromBenchmark = ( data : any ) : any [ ] => {
104+
105+ let charts = [ ] ;
106+ for ( const benchmark in data ) {
107+ const normalizedData = normalizeBenchmarkData ( data [ benchmark ] )
108+ const barData = getBarChartData ( normalizedData ) ;
109+ charts . push ( (
110+ < div key = { benchmark } >
111+ < div className = { `card__header ${ styles [ "benchmark-card-header" ] } ` } >
112+ < Heading as = "h2" > { benchmark } </ Heading >
113+ </ div >
114+ < div className = { styles [ "cards-wrap" ] } >
115+ < div className = { `card ${ styles [ "benchmark-card" ] } ` } >
116+ < Line data = { normalizedData } options = { ChartOptions } > </ Line >
117+ </ div >
118+ < div className = { `card ${ styles [ "benchmark-card" ] } ` } >
119+ < Bar data = { barData } options = { ChartOptions } > </ Bar >
120+ </ div >
121+ </ div >
122+ </ div >
123+ ) ) ;
124+ } ;
125+
126+ return charts ;
127+ } ;
128+
129+
130+
131+
0 commit comments