Skip to content

Commit 43e4e3c

Browse files
committed
wip
1 parent 7f52905 commit 43e4e3c

File tree

2 files changed

+54
-20
lines changed

2 files changed

+54
-20
lines changed

bin/bench

Lines changed: 47 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ require 'fileutils'
33
require 'benchmark'
44
require 'pathname'
55
require 'shellwords'
6+
require 'English'
67

78
class Benchmarking
89
ROOT = Pathname File.expand_path(['..', '..'].join(File::Separator), __FILE__)
@@ -13,6 +14,7 @@ class Benchmarking
1314
def initialize(prepend: '', append: '')
1415
@prepend = prepend
1516
@append = append
17+
refresh_temp_dir if temp_dir_empty?
1618
end
1719

1820
def temp_dir_empty?
@@ -39,7 +41,6 @@ class Benchmarking
3941
end
4042

4143
def benchmark_tests
42-
refresh_temp_dir if temp_dir_empty?
4344
tmp_dir = Shellwords.shellescape(TMP_DIR)
4445
system("#{prepend} bundle exec ruby -Ilib:test #{tmp_dir}/*_benchmark.rb #{append}")
4546
end
@@ -50,11 +51,10 @@ class Benchmarking
5051

5152
def checkout_ref(ref)
5253
puts `git checkout #{ref}`.chomp
53-
abort "Checkout failed: #{ref}, #{$?.exitstatus}" unless $?.success?
54+
abort "Checkout failed: #{ref}, #{$CHILD_STATUS.exitstatus}" unless $CHILD_STATUS.success?
5455
end
5556

5657
def benchmark_refs(ref1: nil, ref2: nil)
57-
refresh_temp_dir
5858
ref0 = current_branch
5959
ref1 ||= current_branch
6060
ref2 ||= 'master'
@@ -64,22 +64,29 @@ class Benchmarking
6464

6565
checkout_ref(ref0)
6666

67-
puts "\n\nResults ============================\n"
68-
puts "------------------------------------~> (Branch) #{ref1.upcase}"
69-
puts master
70-
puts "------------------------------------\n\n"
67+
<<-REPORT
68+
69+
70+
Results ============================
71+
------------------------------------~> (Branch) #{ref2.upcase}
72+
#{master} (seconds)
73+
------------------------------------
7174
72-
puts "------------------------------------~> (Actual Branch) #{ref2.upcase}"
73-
puts actual
74-
puts "------------------------------------"
75-
rescue Exception
75+
76+
77+
------------------------------------~> (Actual Branch) #{ref1.upcase}
78+
#{actual} (seconds)
79+
------------------------------------
80+
REPORT
81+
rescue Exception # rubocop:disable Lint/RescueException
7682
checkout_ref(ref0)
7783
raise
7884
end
7985

8086
def run_benchmark
87+
bundle
8188
benchmark_tests # warmup
82-
Benchmark.realtime {
89+
parse_measurement Benchmark.measure {
8390
benchmark_tests
8491
}
8592
end
@@ -88,14 +95,35 @@ class Benchmarking
8895
checkout_ref(ref)
8996
run_benchmark
9097
end
98+
99+
def bundle
100+
system('rm -f Gemfile.lock; bundle check || bundle --local --quiet || bundle --quiet')
101+
end
102+
103+
def parse_measurement(measurement)
104+
user = measurement.utime
105+
system = measurement.stime
106+
total = measurement.total
107+
real = measurement.real
108+
{
109+
:real => real,
110+
:total => total,
111+
:user => user,
112+
:system => system
113+
}
114+
end
91115
end
92116

93-
if $0 == __FILE__
117+
if $PROGRAM_NAME == __FILE__
94118
benchmarking = Benchmarking.new(append: '> /dev/null')
95-
# Current only
96-
# puts "Ran in #{benchmarking.run_benchmark} seconds."
97-
98-
# Compare ref to master
99-
# Optionally pass in two refs as args to `bin/bench`
100-
benchmarking.benchmark_refs(ref1: ARGV[0], ref2: ARGV[1])
119+
test_type = ARGV[0]
120+
case test_type
121+
when 'current'
122+
puts "Ran in #{benchmarking.run_benchmark} seconds."
123+
else
124+
# Default: Compare current_branch to master
125+
# Optionally: pass in two refs as args to `bin/bench`
126+
# TODO: Consider checking across more revisions, to automatically find problems.
127+
puts benchmarking.benchmark_refs(ref1: ARGV[0], ref2: ARGV[1])
128+
end
101129
end

test/benchmark/benchmark_helper.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,11 @@ def setup
4848
end
4949
end
5050

51+
# Needs to initialize app before any serializes are defined, for sanity's sake.
52+
# Otherwise, you have to manually set perform caching.
53+
Rails.application.initialize!
54+
5155
require_relative 'fixtures'
52-
BenchmarkApp.initialize!
56+
57+
# Uncomment the below to test that cache is in use.
58+
# ActiveSupport::Cache::Store.logger = Logger.new(STDERR)

0 commit comments

Comments
 (0)