Skip to content

Commit 166d5b7

Browse files
committed
Convert misc/graph.rb to take JSON as input instead of CSV
* JSON is much easier to merge than CSV, so this makes it possible to graph from multiple results files. (`jq '. * input'` can be used to merge JSON files) * The JSON is also more cleanly structured. * If the input is a file ending in .csv automatically use the neighbor .json file for compatibility.
1 parent 2ce877c commit 166d5b7

File tree

1 file changed

+25
-19
lines changed

1 file changed

+25
-19
lines changed

misc/graph.rb

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env ruby
22

3-
require 'csv'
3+
require_relative 'stats'
4+
require 'json'
45
begin
56
require 'gruff'
67
rescue LoadError
@@ -9,11 +10,12 @@
910
require 'gruff'
1011
end
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)
4546
end
@@ -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
7480
end

0 commit comments

Comments
 (0)