11#!/usr/bin/env ruby 
22
3- require  'csv' 
3+ require_relative  'stats' 
4+ require  'json' 
45begin 
56  require  'gruff' 
67rescue  LoadError 
910  require  'gruff' 
1011end 
1112
12- def  render_graph ( csv_path ,  png_path ,  title_font_size : 16.0 ,  legend_font_size : 12.0 ,  marker_font_size : 10.0 ) 
13-   ruby_descriptions_csv ,  table_csv  =  File . read ( csv_path ) . split ( "\n \n " ,  2 ) 
14-   ruby_descriptions  =  CSV . parse ( ruby_descriptions_csv ) . to_h 
15-   table  =  CSV . parse ( table_csv ) 
16-   bench_names  =  table . drop ( 1 ) . map ( &:first ) 
13+ def  render_graph ( json_path ,  png_path ,  title_font_size : 16.0 ,  legend_font_size : 12.0 ,  marker_font_size : 10.0 ) 
14+   json  =  JSON . load_file ( json_path ) 
15+   ruby_descriptions  =  json . fetch ( "metadata" ) 
16+   data  =  json . fetch ( "raw_data" ) 
17+   baseline  =  ruby_descriptions . first . first 
18+   bench_names  =  data . first . last . keys 
1719
1820  # ruby_descriptions, bench_names, table 
1921  g  =  Gruff ::Bar . new ( 1600 ) 
@@ -32,14 +34,13 @@ def render_graph(csv_path, png_path, title_font_size: 16.0, legend_font_size: 12
3234  g . legend_font_size  =  legend_font_size 
3335  g . marker_font_size  =  marker_font_size 
3436
35-   rubies  =  ruby_descriptions . map  {  |ruby ,  description | "#{ ruby }  : #{ description }  "  } 
36-   g . data  rubies . first ,  [ 1.0 ]  * bench_names . size 
37-   rubies . drop ( 1 ) . each_with_index  do  |ruby ,  index |
38-     speedup  =  table . drop ( 1 ) . map  do  |row |
39-       num_rests  =  rubies . size  - 1 
40-       row . last ( num_rests ) [ index ] 
41-     end 
42-     g . data  ruby ,  speedup . map ( &:to_f ) 
37+   ruby_descriptions . each  do  |ruby ,  description |
38+     speedups  =  bench_names . map  {  |bench |
39+       baseline_times  =  data . fetch ( baseline ) . fetch ( bench ) . fetch ( "bench" ) 
40+       times  =  data . fetch ( ruby ) . fetch ( bench ) . fetch ( "bench" ) 
41+       Stats . new ( baseline_times ) . mean  / Stats . new ( times ) . mean 
42+     } 
43+     g . data  "#{ ruby }  : #{ description }  " ,  speedups 
4344  end 
4445  g . write ( png_path ) 
4546end 
@@ -63,12 +64,17 @@ def render_graph(csv_path, png_path, title_font_size: 16.0, legend_font_size: 12
6364  end 
6465  parser . parse! 
6566
66-   csv_path  =  ARGV . first 
67-   abort  parser . help  if  csv_path . nil? 
67+   json_path  =  ARGV . first 
68+   abort  parser . help  if  json_path . nil? 
69+ 
70+   # For compatibility as this script used to take the .csv as input 
71+   if  json_path . end_with?  '.csv' 
72+     png_path  =  json_path . sub ( /\. csv\z / ,  '.json' ) 
73+   end 
6874
69-   png_path  =  csv_path . sub ( /\. csv \z / ,  '.png' ) 
70-   render_graph ( csv_path ,  png_path ,  **args ) 
75+   png_path  =  json_path . sub ( /\. json \z / ,  '.png' ) 
76+   render_graph ( json_path ,  png_path ,  **args ) 
7177
72-   open  =  %w[ open  xdg-open ] . find  {  |open | system ( "which #{ open }   >  /dev/null" )  } 
78+   open  =  %w[ open  xdg-open ] . find  {  |open | system ( "which #{ open }   >/dev/null 2> /dev/null" )  } 
7379  system ( open ,  png_path )  if  open 
7480end 
0 commit comments