@@ -18,36 +18,36 @@ def generate_html(
1818 benchmark_runs .sort (key = lambda run : run .date , reverse = True )
1919 serializable_metadata = {k : v .__dict__ for k , v in metadata .items ()}
2020
21+ serializable_runs = [json .loads (run .to_json ()) for run in benchmark_runs ]
22+
23+ data = {
24+ "runs" : serializable_runs ,
25+ "metadata" : serializable_metadata ,
26+ "defaultCompareNames" : compare_names ,
27+ }
28+
2129 if options .output_html == "local" :
2230 data_path = os .path .join (html_path , "data.js" )
23- # Write data to js file
24- # We can't store this as a standalone json file because it needs to be inline in the html
2531 with open (data_path , "w" ) as f :
26- f .write ("benchmarkRuns = [\n " )
27- # it might be tempting to just to create a list and convert
28- # that to a json, but that leads to json being serialized twice.
29- for i , run in enumerate (benchmark_runs ):
30- if i > 0 :
31- f .write (",\n " )
32- f .write (run .to_json ())
33-
34- f .write ("\n ];\n \n " ) # terminates benchmarkRuns
32+ # For local format, we need to write JavaScript variable assignments
33+ f .write ("benchmarkRuns = " )
34+ json .dump (data ["runs" ], f , indent = 2 )
35+ f .write (";\n \n " )
3536
3637 f .write ("benchmarkMetadata = " )
37- json .dump (serializable_metadata , f )
38-
39- f .write (";\n \n " ) # terminates benchmarkMetadata
38+ json .dump (data ["metadata" ], f , indent = 2 )
39+ f .write (";\n \n " )
4040
4141 f .write ("defaultCompareNames = " )
42- json .dump (compare_names , f )
43- f .write (";\n " ) # terminates defaultCompareNames
42+ json .dump (data [ "defaultCompareNames" ] , f , indent = 2 )
43+ f .write (";\n " )
4444
4545 print (f"See { os .getcwd ()} /html/index.html for the results." )
4646 else :
47+ # For remote format, we write a single JSON file
4748 data_path = os .path .join (html_path , "data.json" )
4849 with open (data_path , "w" ) as f :
49- json_data = {"runs" : benchmark_runs , "metadata" : serializable_metadata }
50- json .dump (json_data , f , indent = 2 )
50+ json .dump (data , f , indent = 2 )
5151
5252 print (
5353 f"Upload { data_path } to a location set in config.js remoteDataUrl argument."
0 commit comments