benchmark-interface 0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (84) hide show
  1. checksums.yaml +7 -0
  2. data/.travis.yml +39 -0
  3. data/Gemfile +2 -0
  4. data/LICENCE +7 -0
  5. data/README.md +237 -0
  6. data/benchmark-interface.gemspec +20 -0
  7. data/bin/benchmark +15 -0
  8. data/examples/bench9000.rb +23 -0
  9. data/examples/bench9000micro.rb +31 -0
  10. data/examples/benchmark.rb +40 -0
  11. data/examples/bips.rb +34 -0
  12. data/examples/clamp.rb +17 -0
  13. data/examples/interface.rb +13 -0
  14. data/examples/long.rb +9 -0
  15. data/examples/mri.rb +15 -0
  16. data/examples/perfer.rb +25 -0
  17. data/examples/rbench.rb +46 -0
  18. data/examples/readme.rb +12 -0
  19. data/examples/script.rb +14 -0
  20. data/lib/benchmark-interface.rb +40 -0
  21. data/lib/benchmark-interface/backends/bench9000.rb +87 -0
  22. data/lib/benchmark-interface/backends/benchmark.rb +68 -0
  23. data/lib/benchmark-interface/backends/bips.rb +49 -0
  24. data/lib/benchmark-interface/backends/simple.rb +38 -0
  25. data/lib/benchmark-interface/benchmark-set.rb +85 -0
  26. data/lib/benchmark-interface/benchmark.rb +72 -0
  27. data/lib/benchmark-interface/frontends/bench9000.rb +21 -0
  28. data/lib/benchmark-interface/frontends/bench9000micro.rb +19 -0
  29. data/lib/benchmark-interface/frontends/benchmark.rb +50 -0
  30. data/lib/benchmark-interface/frontends/bips.rb +40 -0
  31. data/lib/benchmark-interface/frontends/mri.rb +84 -0
  32. data/lib/benchmark-interface/frontends/perfer.rb +27 -0
  33. data/lib/benchmark-interface/frontends/rbench.rb +64 -0
  34. data/lib/benchmark-interface/require.rb +38 -0
  35. data/lib/benchmark-interface/run.rb +111 -0
  36. data/lib/benchmark-interface/version.rb +13 -0
  37. data/tests/expected/bench9000-bips.txt +8 -0
  38. data/tests/expected/bench9000-bm.txt +2 -0
  39. data/tests/expected/bench9000-bmbm.txt +2 -0
  40. data/tests/expected/bench9000-simple.txt +2 -0
  41. data/tests/expected/bench9000micro-bips.txt +8 -0
  42. data/tests/expected/bench9000micro-bm.txt +2 -0
  43. data/tests/expected/bench9000micro-bmbm.txt +2 -0
  44. data/tests/expected/bench9000micro-simple.txt +2 -0
  45. data/tests/expected/benchmark-bips.txt +72 -0
  46. data/tests/expected/benchmark-bm.txt +12 -0
  47. data/tests/expected/benchmark-bmbm.txt +12 -0
  48. data/tests/expected/benchmark-simple.txt +22 -0
  49. data/tests/expected/bips-bips.txt +2 -0
  50. data/tests/expected/bips-bm.txt +7 -0
  51. data/tests/expected/bips-bmbm.txt +7 -0
  52. data/tests/expected/bips-simple.txt +11 -0
  53. data/tests/expected/interface-bips.txt +24 -0
  54. data/tests/expected/interface-bm.txt +4 -0
  55. data/tests/expected/interface-bmbm.txt +4 -0
  56. data/tests/expected/interface-simple.txt +6 -0
  57. data/tests/expected/long-bips.txt +9 -0
  58. data/tests/expected/long-bm.txt +2 -0
  59. data/tests/expected/long-bmbm.txt +2 -0
  60. data/tests/expected/long-simple.txt +2 -0
  61. data/tests/expected/mri-bips.txt +9 -0
  62. data/tests/expected/mri-bm.txt +2 -0
  63. data/tests/expected/mri-bmbm.txt +2 -0
  64. data/tests/expected/mri-simple.txt +2 -0
  65. data/tests/expected/perfer-bips.txt +20 -0
  66. data/tests/expected/perfer-bm.txt +4 -0
  67. data/tests/expected/perfer-bmbm.txt +4 -0
  68. data/tests/expected/perfer-simple.txt +5 -0
  69. data/tests/expected/rbench-bips.txt +42 -0
  70. data/tests/expected/rbench-bm.txt +7 -0
  71. data/tests/expected/rbench-bmbm.txt +7 -0
  72. data/tests/expected/rbench-simple.txt +12 -0
  73. data/tests/expected/readme-bips.txt +18 -0
  74. data/tests/expected/readme-bm.txt +3 -0
  75. data/tests/expected/readme-bmbm.txt +3 -0
  76. data/tests/expected/readme-simple.txt +4 -0
  77. data/tests/expected/script-bips.txt +18 -0
  78. data/tests/expected/script-bm.txt +3 -0
  79. data/tests/expected/script-bmbm.txt +3 -0
  80. data/tests/expected/script-simple.txt +4 -0
  81. data/tests/rewritten/mri.rb +15 -0
  82. data/tests/tests.rb +73 -0
  83. data/tests/tools/squash.rb +63 -0
  84. metadata +130 -0
@@ -0,0 +1,17 @@
1
+ # Copyright (c) 2016 Oracle and/or its affiliates. All rights reserved. This
2
+ # code is released under a tri EPL/GPL/LGPL license. You can use it,
3
+ # redistribute it and/or modify it under the terms of the:
4
+ #
5
+ # Eclipse Public License version 1.0
6
+ # GNU General Public License version 2
7
+ # GNU Lesser General Public License version 2.1
8
+
9
+ def clamp_a(min, value, max)
10
+ [min, value, max].sort[1]
11
+ end
12
+
13
+ def clamp_b(min, value, max)
14
+ return min if value < min
15
+ return max if value > max
16
+ value
17
+ end
@@ -0,0 +1,13 @@
1
+ # Copyright (c) 2016 Oracle and/or its affiliates. All rights reserved. This
2
+ # code is released under a tri EPL/GPL/LGPL license. You can use it,
3
+ # redistribute it and/or modify it under the terms of the:
4
+ #
5
+ # Eclipse Public License version 1.0
6
+ # GNU General Public License version 2
7
+ # GNU Lesser General Public License version 2.1
8
+
9
+ require File.expand_path('clamp', File.dirname(__FILE__))
10
+
11
+ benchmark { clamp_a(10, 40, 90) }
12
+ benchmark('clamp_a') { clamp_a(10, 40, 90) }
13
+ benchmark('clamp_b') { clamp_b(10, 40, 90) }
@@ -0,0 +1,9 @@
1
+ # Copyright (c) 2016 Oracle and/or its affiliates. All rights reserved. This
2
+ # code is released under a tri EPL/GPL/LGPL license. You can use it,
3
+ # redistribute it and/or modify it under the terms of the:
4
+ #
5
+ # Eclipse Public License version 1.0
6
+ # GNU General Public License version 2
7
+ # GNU Lesser General Public License version 2.1
8
+
9
+ benchmark('sleep') { sleep 3 }
@@ -0,0 +1,15 @@
1
+ # Copyright (c) 2016 Oracle and/or its affiliates. All rights reserved. This
2
+ # code is released under a tri EPL/GPL/LGPL license. You can use it,
3
+ # redistribute it and/or modify it under the terms of the:
4
+ #
5
+ # Eclipse Public License version 1.0
6
+ # GNU General Public License version 2
7
+ # GNU Lesser General Public License version 2.1
8
+
9
+ array = [1, 2, 3]
10
+
11
+ i = 0
12
+ while i < 30_000_000
13
+ i += 1
14
+ array.length
15
+ end
@@ -0,0 +1,25 @@
1
+ # Copyright (c) 2016 Oracle and/or its affiliates. All rights reserved. This
2
+ # code is released under a tri EPL/GPL/LGPL license. You can use it,
3
+ # redistribute it and/or modify it under the terms of the:
4
+ #
5
+ # Eclipse Public License version 1.0
6
+ # GNU General Public License version 2
7
+ # GNU Lesser General Public License version 2.1
8
+
9
+ require 'perfer'
10
+
11
+ require File.expand_path('clamp', File.dirname(__FILE__))
12
+
13
+ Perfer.session 'clamp' do |s|
14
+ s.iterate 'clamp_a' do |n|
15
+ n.times do
16
+ clamp_a(10, 40, 90)
17
+ end
18
+ end
19
+
20
+ s.iterate 'clamp_b' do |n|
21
+ n.times do
22
+ clamp_b(10, 40, 90)
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,46 @@
1
+ # Copyright (c) 2016 Oracle and/or its affiliates. All rights reserved. This
2
+ # code is released under a tri EPL/GPL/LGPL license. You can use it,
3
+ # redistribute it and/or modify it under the terms of the:
4
+ #
5
+ # Eclipse Public License version 1.0
6
+ # GNU General Public License version 2
7
+ # GNU Lesser General Public License version 2.1
8
+
9
+ require 'rbench'
10
+
11
+ require File.expand_path('clamp', File.dirname(__FILE__))
12
+
13
+ RBench.run(10_000) do
14
+ format :width => 65
15
+
16
+ group 'clamp1' do
17
+ report 'clamp_a1' do
18
+ clamp_a(10, 40, 90)
19
+ end
20
+ report 'clamp_b1' do
21
+ clamp_a(10, 40, 90)
22
+ end
23
+ end
24
+ end
25
+
26
+ RBench.run(10_000) do
27
+ format :width => 65
28
+
29
+ column :times
30
+ column :one, :title => "#1"
31
+ column :two, :title => "#2"
32
+ column :diff, :title => "#1/#2", :compare => [:one,:two]
33
+
34
+ group 'clamp2' do
35
+ report 'clamp_a2' do
36
+ one { clamp_a(10, 40, 90) }
37
+ two { clamp_a(10, 40, 90) }
38
+ end
39
+ report 'clamp_b2' do
40
+ one { clamp_a(10, 40, 90) }
41
+ two { clamp_a(10, 40, 90) }
42
+ end
43
+
44
+ summary 'all methods (totals)'
45
+ end
46
+ end
@@ -0,0 +1,12 @@
1
+ # Copyright (c) 2016 Oracle and/or its affiliates. All rights reserved. This
2
+ # code is released under a tri EPL/GPL/LGPL license. You can use it,
3
+ # redistribute it and/or modify it under the terms of the:
4
+ #
5
+ # Eclipse Public License version 1.0
6
+ # GNU General Public License version 2
7
+ # GNU Lesser General Public License version 2.1
8
+
9
+ require 'benchmark-interface'
10
+
11
+ benchmark('mul') { '14 * 14 * 14' }
12
+ benchmark('pow') { '14 ** 3' }
@@ -0,0 +1,14 @@
1
+ # Copyright (c) 2016 Oracle and/or its affiliates. All rights reserved. This
2
+ # code is released under a tri EPL/GPL/LGPL license. You can use it,
3
+ # redistribute it and/or modify it under the terms of the:
4
+ #
5
+ # Eclipse Public License version 1.0
6
+ # GNU General Public License version 2
7
+ # GNU Lesser General Public License version 2.1
8
+
9
+ require 'benchmark-interface'
10
+
11
+ require File.expand_path('clamp', File.dirname(__FILE__))
12
+
13
+ benchmark('clamp_a') { clamp_a(10, 40, 90) }
14
+ benchmark('clamp_b') { clamp_b(10, 40, 90) }
@@ -0,0 +1,40 @@
1
+ # Copyright (c) 2016 Oracle and/or its affiliates. All rights reserved. This
2
+ # code is released under a tri EPL/GPL/LGPL license. You can use it,
3
+ # redistribute it and/or modify it under the terms of the:
4
+ #
5
+ # Eclipse Public License version 1.0
6
+ # GNU General Public License version 2
7
+ # GNU Lesser General Public License version 2.1
8
+
9
+ require 'benchmark-interface/version'
10
+ require 'benchmark-interface/benchmark'
11
+ require 'benchmark-interface/benchmark-set'
12
+ require 'benchmark-interface/frontends/mri'
13
+ require 'benchmark-interface/backends/simple'
14
+ require 'benchmark-interface/backends/benchmark'
15
+ require 'benchmark-interface/backends/bips'
16
+ require 'benchmark-interface/backends/bench9000'
17
+ require 'benchmark-interface/require'
18
+ require 'benchmark-interface/run'
19
+
20
+ module BenchmarkInterface
21
+
22
+ def self.benchmark(name=nil, &block)
23
+ BenchmarkInterface::BenchmarkSet.current.register name, block
24
+ end
25
+
26
+ end
27
+
28
+ def benchmark(name=nil, &block)
29
+ BenchmarkInterface.benchmark name, &block
30
+ end
31
+
32
+ if $PROGRAM_NAME.split('/').last != 'benchmark'
33
+ set = BenchmarkInterface::BenchmarkSet.new
34
+ backend = BenchmarkInterface::Backends::Bips
35
+
36
+ at_exit do
37
+ set.prepare
38
+ backend.run set, set.benchmarks, {}
39
+ end
40
+ end
@@ -0,0 +1,87 @@
1
+ # Copyright (c) 2016 Oracle and/or its affiliates. All rights reserved. This
2
+ # code is released under a tri EPL/GPL/LGPL license. You can use it,
3
+ # redistribute it and/or modify it under the terms of the:
4
+ #
5
+ # Eclipse Public License version 1.0
6
+ # GNU General Public License version 2
7
+ # GNU Lesser General Public License version 2.1
8
+
9
+ module BenchmarkInterface
10
+ module Backends
11
+ module Bench9000
12
+
13
+ MIN_SAMPLING_TIME = 0.1 # seconds
14
+
15
+ def self.run(benchmark_set, names, options)
16
+ unless names.size == 1
17
+ abort 'The bench9000 backend only works when you run just one benchmark at a time - specify the name on the command line'
18
+ end
19
+
20
+ unless options['--no-scale']
21
+ min_time = benchmark_set.benchmarks.map(&:basic_iteration_time).min
22
+
23
+ if min_time < MIN_SAMPLING_TIME
24
+ short_iterations = true
25
+ samples = (MIN_SAMPLING_TIME / min_time / MIN_SAMPLING_TIME).to_i
26
+ puts "These are short benchmarks - we're running each #{samples} times so they take about a second and using the micro harness"
27
+ end
28
+ end
29
+
30
+ benchmark = benchmark_set.benchmark(names.first)
31
+ block = benchmark.block
32
+
33
+ Object.instance_eval do
34
+ if short_iterations
35
+ define_method(:micro_harness_input) do
36
+ nil
37
+ end
38
+
39
+ define_method(:micro_harness_iterations) do
40
+ samples
41
+ end
42
+
43
+ define_method(:micro_harness_sample) do |input|
44
+ block.call
45
+ end
46
+
47
+ define_method(:micro_harness_expected) do
48
+ raise 'not expecting this to be called, as we\'ve patched harness_verify'
49
+ end
50
+ else
51
+ define_method(:harness_input) do
52
+ nil
53
+ end
54
+
55
+ define_method(:harness_sample) do |input|
56
+ block.call
57
+ end
58
+
59
+ define_method(:harness_verify) do |output|
60
+ true
61
+ end
62
+ end
63
+
64
+ end
65
+
66
+ @loading_real = true
67
+
68
+ if short_iterations
69
+ benchmark_interface_original_require 'bench9000/micro-harness'
70
+
71
+ Object.instance_eval do
72
+ define_method(:harness_verify) do |output|
73
+ true
74
+ end
75
+ end
76
+ end
77
+
78
+ benchmark_interface_original_require 'bench9000/harness'
79
+ end
80
+
81
+ def self.loading_real?
82
+ @loading_real
83
+ end
84
+
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,68 @@
1
+ # Copyright (c) 2016 Oracle and/or its affiliates. All rights reserved. This
2
+ # code is released under a tri EPL/GPL/LGPL license. You can use it,
3
+ # redistribute it and/or modify it under the terms of the:
4
+ #
5
+ # Eclipse Public License version 1.0
6
+ # GNU General Public License version 2
7
+ # GNU Lesser General Public License version 2.1
8
+
9
+ module BenchmarkInterface
10
+ module Backends
11
+ module Bm
12
+
13
+ MIN_SAMPLING_TIME = 0.1 # seconds
14
+
15
+ def self.run(benchmark_set, names, options)
16
+ # If we don't remove these we'll get a warning when we load the real
17
+ # implementation.
18
+
19
+ ::Benchmark.send(:remove_const, :CAPTION) if defined?(::Benchmark::CAPTION)
20
+ ::Benchmark.send(:remove_const, :FORMAT) if defined?(::Benchmark::FORMAT)
21
+
22
+ benchmark_interface_original_require 'benchmark'
23
+
24
+ unless options['--no-scale']
25
+ min_time = benchmark_set.benchmarks.map(&:basic_iteration_time).min
26
+
27
+ if min_time < MIN_SAMPLING_TIME
28
+ short_iterations = true
29
+ samples = (MIN_SAMPLING_TIME / min_time / MIN_SAMPLING_TIME).to_i
30
+ puts "These are short benchmarks - we're running each #{samples} times so they take about a second"
31
+ end
32
+ end
33
+
34
+ label_width = benchmark_set.benchmarks(names).map(&:name).map(&:size).max
35
+
36
+ block = Proc.new do |x|
37
+ benchmark_set.benchmarks(names).each do |benchmark|
38
+ block = benchmark.block
39
+ if short_iterations
40
+ x.report(benchmark.name) do
41
+ samples.times do
42
+ block.call
43
+ end
44
+ end
45
+ else
46
+ x.report(benchmark.name, &benchmark.block)
47
+ end
48
+ end
49
+ end
50
+
51
+ if self == BmBm
52
+ ::Benchmark.bmbm label_width, &block
53
+ else
54
+ ::Benchmark.bm label_width, &block
55
+ end
56
+ end
57
+
58
+ end
59
+
60
+ module BmBm
61
+
62
+ def self.run(benchmark_set, names, options)
63
+ Bm.run benchmark_set, names, options
64
+ end
65
+
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,49 @@
1
+ # Copyright (c) 2016 Oracle and/or its affiliates. All rights reserved. This
2
+ # code is released under a tri EPL/GPL/LGPL license. You can use it,
3
+ # redistribute it and/or modify it under the terms of the:
4
+ #
5
+ # Eclipse Public License version 1.0
6
+ # GNU General Public License version 2
7
+ # GNU Lesser General Public License version 2.1
8
+
9
+ module BenchmarkInterface
10
+ module Backends
11
+ module Bips
12
+
13
+ LONG_ITERATION_THRESHOLD = 0.1 # seconds
14
+
15
+ def self.run(benchmark_set, names, options)
16
+ Kernel.instance_eval do
17
+ alias_method :require, :benchmark_interface_original_require
18
+ require 'rubygems'
19
+ alias_method :benchmark_interface_original_require, :require
20
+ end
21
+
22
+ benchmark_interface_original_require 'benchmark/ips'
23
+
24
+ unless options['--no-scale']
25
+ if benchmark_set.benchmarks.map(&:basic_iteration_time).max > LONG_ITERATION_THRESHOLD
26
+ long_iterations = true
27
+ puts "These are long benchmarks - we're increasing warmup and sample time"
28
+ end
29
+ end
30
+
31
+ ::Benchmark.ips do |x|
32
+ x.iterations = 3
33
+
34
+ if long_iterations
35
+ x.time = 10
36
+ x.warmup = 10
37
+ end
38
+
39
+ benchmark_set.benchmarks(names).each do |benchmark|
40
+ x.report benchmark.name, &benchmark.block
41
+ end
42
+
43
+ x.compare!
44
+ end
45
+ end
46
+
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,38 @@
1
+ # Copyright (c) 2016 Oracle and/or its affiliates. All rights reserved. This
2
+ # code is released under a tri EPL/GPL/LGPL license. You can use it,
3
+ # redistribute it and/or modify it under the terms of the:
4
+ #
5
+ # Eclipse Public License version 1.0
6
+ # GNU General Public License version 2
7
+ # GNU Lesser General Public License version 2.1
8
+
9
+ module BenchmarkInterface
10
+ module Backends
11
+ module Simple
12
+
13
+ def self.run(benchmark_set, names, options)
14
+ loop_time = options['--time']
15
+
16
+ benchmark_set.benchmarks(names).each do |benchmark|
17
+ puts benchmark.name
18
+ block = benchmark.block
19
+
20
+ start_time = Time.now
21
+
22
+ while Time.now - start_time < loop_time
23
+ start_iteration_time = Time.now
24
+ iterations = 0
25
+ while Time.now - start_iteration_time < 1
26
+ block.call
27
+ iterations += 1
28
+ end
29
+ iteration_time = Time.now - start_iteration_time
30
+ iterations *= benchmark_set.iterations
31
+ puts iterations / iteration_time
32
+ end
33
+ end
34
+ end
35
+
36
+ end
37
+ end
38
+ end