|
1 | 1 | #!/usr/bin/env ruby |
2 | 2 |
|
3 | 3 | require "benchmark" |
| 4 | +require "grm" |
4 | 5 | require "pg" |
5 | 6 | require_relative "../lib/activerecord-adbc-adapter" |
6 | 7 |
|
@@ -101,28 +102,88 @@ class AdbcLog < ActiveRecord::Base |
101 | 102 | end |
102 | 103 | arrow_table = Arrow::Table.new(arrow_columns) |
103 | 104 |
|
104 | | -Benchmark.bm do |benchmark| |
105 | | - benchmark.report("SQL: Load") do |
| 105 | +puts("Load:") |
| 106 | +load_results = Benchmark.bm do |benchmark| |
| 107 | + benchmark.report("SQL") do |
106 | 108 | SqlLog.connection.execute(sql_load) |
107 | 109 | end |
108 | 110 |
|
109 | | - benchmark.report("SQL: Dump") do |
110 | | - SqlLog.connection.execute(sql_dump) |
| 111 | + benchmark.report("Active Record") do |
| 112 | + ActiveRecordLog.insert_all(raw_records) |
111 | 113 | end |
112 | 114 |
|
113 | | - benchmark.report("Active Record: Load") do |
114 | | - ActiveRecordLog.insert_all(raw_records) |
| 115 | + benchmark.report("ADBC") do |
| 116 | + AdbcLog.ingest(arrow_table) |
115 | 117 | end |
| 118 | +end |
116 | 119 |
|
117 | | - benchmark.report("Active Record: Dump") do |
118 | | - ActiveRecordLog.pluck |
| 120 | +File.open("load.csv", "w") do |csv| |
| 121 | + csv.puts("approach,elapsed_time") |
| 122 | + load_results.each do |result| |
| 123 | + csv.puts("#{result.label},#{result.real}") |
119 | 124 | end |
| 125 | +end |
120 | 126 |
|
121 | | - benchmark.report("ADBC: Load") do |
122 | | - AdbcLog.ingest(arrow_table) |
| 127 | +puts |
| 128 | +puts("Dump:") |
| 129 | +dump_results = Benchmark.bm do |benchmark| |
| 130 | + benchmark.report("SQL") do |
| 131 | + SqlLog.connection.execute(sql_dump) |
| 132 | + end |
| 133 | + |
| 134 | + benchmark.report("Active Record") do |
| 135 | + ActiveRecordLog.pluck |
123 | 136 | end |
124 | 137 |
|
125 | | - benchmark.report("ADBC: Dump") do |
| 138 | + benchmark.report("ADBC") do |
126 | 139 | AdbcLog.all.to_arrow |
127 | 140 | end |
128 | 141 | end |
| 142 | + |
| 143 | +File.open("dump.csv", "w") do |csv| |
| 144 | + csv.puts("approach,elapsed_time") |
| 145 | + dump_results.each do |result| |
| 146 | + csv.puts("#{result.label},#{result.real}") |
| 147 | + end |
| 148 | +end |
| 149 | + |
| 150 | + |
| 151 | +results = [ |
| 152 | + {title: "Load", results: load_results}, |
| 153 | + {title: "Dump", results: dump_results}, |
| 154 | +] |
| 155 | +n_results = results.size |
| 156 | +x_margin = 0.0 |
| 157 | +x_width = (1 / n_results.to_f) - (x_margin * 2) |
| 158 | +y_min = 0.05 |
| 159 | +y_max = 0.95 |
| 160 | +all_reals = results.collect do |results| |
| 161 | + results[:results].collect do |result| |
| 162 | + result[:real] |
| 163 | + end |
| 164 | +end |
| 165 | +y_range = [0.0, all_reals.flatten.max] |
| 166 | +subplots = results.each_with_index.collect do |data, i| |
| 167 | + x_index = (i - 1) % n_results |
| 168 | + x_margin_right = x_margin + (2 * x_margin * i) |
| 169 | + position = [ |
| 170 | + x_width * i + x_margin_right, |
| 171 | + x_width * (i + 1) + x_margin_right, |
| 172 | + y_min, |
| 173 | + y_max, |
| 174 | + ] |
| 175 | + { |
| 176 | + kind: "barplot", |
| 177 | + title: data[:title], |
| 178 | + c: n_results.times.collect {|j| j + 2}, |
| 179 | + x_label: "Approach", |
| 180 | + y: data[:results].collect {|result| result[:real]}, |
| 181 | + y_label: "Elapsed time (s)", |
| 182 | + y_labels: data[:results].collect {|result| result[:label]}, |
| 183 | + y_range: y_range, |
| 184 | + subplot: position, |
| 185 | + } |
| 186 | +end |
| 187 | +GRM.merge(subplots: subplots, size: [1200, 600]) |
| 188 | +GRM.export("load-dump.png") |
| 189 | +GRM.export("load-dump.svg") |
0 commit comments