@@ -4,78 +4,98 @@ require 'benchmark'
44require 'pathname'
55require 'shellwords'
66
7- ROOT = Pathname File . expand_path ( [ '..' , '..' ] . join ( File ::Separator ) , __FILE__ )
8- TMP_DIR = File . join ( ROOT , 'tmp/bench' )
7+ class Benchmarking
8+ ROOT = Pathname File . expand_path ( [ '..' , '..' ] . join ( File ::Separator ) , __FILE__ )
9+ TMP_DIR = File . join ( ROOT , 'tmp/bench' )
910
10- def temp_dir_empty?
11- Dir [ File . join ( TMP_DIR , '*' ) ] . none?
12- end
11+ attr_reader :prepend , :append
1312
14- def empty_temp_dir
15- FileUtils . mkdir_p ( TMP_DIR )
16- Dir [ File . join ( TMP_DIR , '*' ) ] . each do |file |
17- FileUtils . rm ( file )
13+ def initialize ( prepend : '' , append : '' )
14+ @prepend = prepend
15+ @append = append
1816 end
19- end
2017
21- def fill_temp_dir
22- Dir [ File . join ( ROOT , 'test' , 'benchmark' , '*.rb' ) ] . each do |file |
23- FileUtils . cp ( file , File . join ( TMP_DIR , File . basename ( file ) ) )
18+ def temp_dir_empty?
19+ Dir [ File . join ( TMP_DIR , '*' ) ] . none?
2420 end
25- at_exit { empty_temp_dir }
26- end
2721
28- def refresh_temp_dir
29- empty_temp_dir
30- fill_temp_dir
31- end
22+ def empty_temp_dir
23+ FileUtils . mkdir_p ( TMP_DIR )
24+ Dir [ File . join ( TMP_DIR , '*' ) ] . each do |file |
25+ FileUtils . rm ( file )
26+ end
27+ end
3228
33- def benchmark_tests
34- refresh_temp_dir if temp_dir_empty?
35- system ( "bundle exec ruby -Ilib:test #{ Shellwords . shellescape ( TMP_DIR ) } /*_benchmark.rb" )
36- end
29+ def fill_temp_dir
30+ Dir [ File . join ( ROOT , 'test' , 'benchmark' , '*.rb' ) ] . each do |file |
31+ FileUtils . cp ( file , File . join ( TMP_DIR , File . basename ( file ) ) )
32+ end
33+ at_exit { empty_temp_dir }
34+ end
3735
38- def current_branch
39- @current_branch ||= `cat .git/HEAD | cut -d/ -f3,4,5` . chomp
40- end
36+ def refresh_temp_dir
37+ empty_temp_dir
38+ fill_temp_dir
39+ end
4140
42- def checkout_ref ( ref )
43- puts `git checkout #{ ref } ` . chomp
44- abort "Checkout failed: #{ ref } , #{ $?. exitstatus } " unless $?. success?
45- end
41+ def benchmark_tests
42+ refresh_temp_dir if temp_dir_empty?
43+ tmp_dir = Shellwords . shellescape ( TMP_DIR )
44+ system ( "#{ prepend } bundle exec ruby -Ilib:test #{ tmp_dir } /*_benchmark.rb #{ append } " )
45+ end
4646
47- def benchmark
48- refresh_temp_dir
49- ref = current_branch
47+ def current_branch
48+ @current_branch ||= `cat .git/HEAD | cut -d/ -f3,4,5` . chomp
49+ end
5050
51- actual = run_benchmark_at_ref ref
52- master = run_benchmark_at_ref 'master'
51+ def checkout_ref ( ref )
52+ puts `git checkout #{ ref } ` . chomp
53+ abort "Checkout failed: #{ ref } , #{ $?. exitstatus } " unless $?. success?
54+ end
5355
54- checkout_ref ( ref )
56+ def benchmark_refs ( ref1 : nil , ref2 : nil )
57+ refresh_temp_dir
58+ ref0 = current_branch
59+ ref1 ||= current_branch
60+ ref2 ||= 'master'
5561
56- puts "\n \n Results ============================\n "
57- puts "------------------------------------~> (Branch) MASTER"
58- puts master
59- puts "------------------------------------\n \n "
62+ actual = run_benchmark_at_ref ( ref1 )
63+ master = run_benchmark_at_ref ( ref2 )
6064
61- puts "------------------------------------~> (Actual Branch) #{ ref } "
62- puts actual
63- puts "------------------------------------"
64- end
65+ checkout_ref ( ref0 )
6566
66- def run_benchmark
67- response = Benchmark . realtime {
68- benchmark_tests
69- }
70- benchmark_tests
71- response
72- end
67+ puts "\n \n Results ============================\n "
68+ puts "------------------------------------~> (Branch) #{ ref1 . upcase } "
69+ puts master
70+ puts "------------------------------------\n \n "
71+
72+ puts "------------------------------------~> (Actual Branch) #{ ref2 . upcase } "
73+ puts actual
74+ puts "------------------------------------"
75+ rescue Exception
76+ checkout_ref ( ref0 )
77+ raise
78+ end
79+
80+ def run_benchmark
81+ benchmark_tests # warmup
82+ Benchmark . realtime {
83+ benchmark_tests
84+ }
85+ end
7386
74- def run_benchmark_at_ref ( ref )
75- checkout_ref ( ref )
76- run_benchmark
87+ def run_benchmark_at_ref ( ref )
88+ checkout_ref ( ref )
89+ run_benchmark
90+ end
7791end
7892
7993if $0 == __FILE__
80- benchmark
94+ 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 ] )
81101end
0 commit comments