This repository was archived by the owner on Apr 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathbench.rb
More file actions
executable file
·89 lines (74 loc) · 2.12 KB
/
Copy pathbench.rb
File metadata and controls
executable file
·89 lines (74 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/usr/bin/env ruby
# Benchmark solutions to New Relic's metaprogramming counter challenge.
#
# ./tmp/program.rb is auto generated by the test cases.
require 'benchmark'
require 'fileutils'
SOLUTION_FILE = ARGV[0]
unless SOLUTION_FILE
warn "usage #$0 <SOLUTION_FILE>"
exit 1
end
module Bencher
extend self
def workspace
File.expand_path(File.dirname(__FILE__) + '/tmp')
end
def program_path
workspace + '/program.rb'
end
def solution_path
SOLUTION_FILE
end
def create_program(source)
FileUtils.mkdir_p(workspace)
File.open(program_path, 'w') do |f|
f.print source
end
end
def execute_program(count_calls_to, source)
create_program(source)
`COUNT_CALLS_TO='#{count_calls_to}' ruby -r #{solution_path} #{program_path} 2>&1`
end
def execute_program_without_instrumentation(source)
create_program(source)
`#{program_path} 2>&1`
end
def benchmark(name, count_calls_to, source)
Benchmark.bm(50) do |x|
x.report("#{name} baseline") do
execute_program_without_instrumentation(source)
end
x.report(name) do
execute_program(count_calls_to, source)
end
end
end
end
Bencher.benchmark('standard lib class and method', 'String#to_s', <<-RB)
100_000.times{ "foo".to_s }
RB
Bencher.benchmark('method defined in class', 'A#b', <<-RB)
class A; def b; end; end
100_000.times{ A.new.b }
RB
# some solutions (such as asm) have significant startup overhead but less
# runtime overhead. A longer benchmark shows the benefit of this approach.
Bencher.benchmark('method defined in class more times', 'A#b', <<-RB)
class A; def b; end; end
1_000_000.times{ A.new.b }
RB
Bencher.benchmark('two methods called alternately', 'A#b', <<-RB)
class A; def b; end; def c; end; end
100_000.times{ A.new.b; A.new.c }
RB
Bencher.benchmark('method included through module', 'B#b', <<-RB)
module A; def b; end; end
class B; include A; end
100_000.times{ B.new.b }
RB
Bencher.benchmark('two method included called alternately', 'B#b', <<-RB)
module A; def b; end; def c; end; end
class B; include A; end
100_000.times{ B.new.b; B.new.c }
RB