@@ -12,7 +12,7 @@ import {
12
12
Title ,
13
13
Tooltip ,
14
14
} from "chart.js" ;
15
- import React from "react" ;
15
+ import { useEffect , useState } from "react" ;
16
16
import { Bar , Line } from "react-chartjs-2" ;
17
17
import styles from "./styles.module.css" ;
18
18
@@ -30,6 +30,7 @@ ChartJS.register(
30
30
31
31
interface BenchmarkGraphsProps {
32
32
selectedEngines : string [ ] ;
33
+ range : string ;
33
34
}
34
35
35
36
const ChartOptions = {
@@ -42,75 +43,51 @@ const ChartOptions = {
42
43
43
44
export const BenchmarkGraphs : React . FC < BenchmarkGraphsProps > = ( {
44
45
selectedEngines,
46
+ range
45
47
} ) => {
46
48
// Control the state of which engines are displayed using a Set
47
49
48
- const [ data , setData ] = React . useState ( [ ] ) ;
50
+ const [ charts , setCharts ] = useState ( [ ] ) ;
51
+ const [ data , setData ] = useState ( { } ) ;
49
52
const colorMode = useColorMode ( ) ;
50
53
ChartJS . defaults . color = colorMode . colorMode === "light" ? "#666" : "white" ;
51
54
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
- } ;
68
55
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 ] ) ;
74
64
75
- const barData = getBarChartData ( data ) ;
65
+ useEffect ( ( ) => {
66
+ setCharts ( buildChartFromBenchmark ( data ) ) ;
67
+ } , [ data ] ) ;
76
68
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 ) ;
92
71
} ;
93
72
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
+
98
80
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
+ } ) ) ,
111
87
} ;
112
88
} ;
113
89
90
+
114
91
const getBarChartData = ( data ) => {
115
92
// We only want the last value from each dataset
116
93
return {
@@ -123,3 +100,33 @@ const getBarChartData = (data) => {
123
100
} ) ,
124
101
} ;
125
102
} ;
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