@@ -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,51 @@ 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+ console . log ( "range changed" , range ) ;
58+ fetch ( `https://boa-api.jason-williams.co.uk/benchmarks?months=${ range } &engines=${ selectedEngines . join ( ',' ) } ` ) . then (
59+ ( res ) => res . json ( ) )
60+ . then ( respData => {
61+ setData ( respData )
62+ } ) ;
63+ } , [ selectedEngines , range ] ) ;
7464
75- const barData = getBarChartData ( data ) ;
65+ useEffect ( ( ) => {
66+ setCharts ( buildChartFromBenchmark ( data ) ) ;
67+ } , [ data ] ) ;
7668
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- ) ;
69+
70+ return charts && charts . map ( ( chart ) => chart ) ;
9271} ;
9372
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
73+ const normalizeBenchmarkData = ( benchmarkData : any [ ] ) => {
74+ const labels = benchmarkData . map ( ( entry : any ) =>
75+ new Date ( entry . date ) . toLocaleDateString ( ) ,
76+ ) ;
77+
78+ const engines = Object . keys ( benchmarkData [ 0 ] ) . filter ( key => key != "date" ) ;
79+
9880 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- } ) ,
81+ labels,
82+ datasets : engines . map ( engine => ( {
83+ label : engine ,
84+ data : benchmarkData . map ( entry => entry [ engine ] ) ,
85+ fill : false
86+ } ) ) ,
11187 } ;
11288} ;
11389
90+
11491const getBarChartData = ( data ) => {
11592 // We only want the last value from each dataset
11693 return {
@@ -123,3 +100,33 @@ const getBarChartData = (data) => {
123100 } ) ,
124101 } ;
125102} ;
103+
104+ const buildChartFromBenchmark = ( data : any ) : any [ ] => {
105+
106+ let charts = [ ] ;
107+ for ( const benchmark in data ) {
108+ const normalizedData = normalizeBenchmarkData ( data [ benchmark ] )
109+ const barData = getBarChartData ( normalizedData ) ;
110+ charts . push ( (
111+ < div key = { benchmark } >
112+ < div className = { `card__header ${ styles [ "benchmark-card-header" ] } ` } >
113+ < Heading as = "h2" > { benchmark } </ Heading >
114+ </ div >
115+ < div className = { styles [ "cards-wrap" ] } >
116+ < div className = { `card ${ styles [ "benchmark-card" ] } ` } >
117+ < Line data = { normalizedData } options = { ChartOptions } > </ Line >
118+ </ div >
119+ < div className = { `card ${ styles [ "benchmark-card" ] } ` } >
120+ < Bar data = { barData } options = { ChartOptions } > </ Bar >
121+ </ div >
122+ </ div >
123+ </ div >
124+ ) ) ;
125+ } ;
126+
127+ return charts ;
128+ } ;
129+
130+
131+
132+
0 commit comments