benchmark-perf 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +14 -0
- data/.rspec +2 -0
- data/.travis.yml +28 -0
- data/CHANGELOG.md +7 -0
- data/Gemfile +14 -0
- data/LICENSE.txt +22 -0
- data/README.md +73 -0
- data/Rakefile +11 -0
- data/benchmark-perf.gemspec +23 -0
- data/lib/benchmark-perf.rb +3 -0
- data/lib/benchmark/perf.rb +67 -0
- data/lib/benchmark/perf/execution_time.rb +108 -0
- data/lib/benchmark/perf/iteration.rb +97 -0
- data/lib/benchmark/perf/version.rb +7 -0
- data/spec/spec_helper.rb +45 -0
- data/spec/unit/assertions_spec.rb +15 -0
- data/spec/unit/average_spec.rb +11 -0
- data/spec/unit/execution_time_spec.rb +32 -0
- data/spec/unit/iteration_spec.rb +18 -0
- data/tasks/coverage.rake +11 -0
- data/tasks/spec.rake +34 -0
- metadata +105 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: b05020c28d534d0348e045bb0778e18ecabd5236
|
4
|
+
data.tar.gz: bcf5fdf5efbca164cce07a5718c01000d32f4671
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: f8392ae93fa4f2e48de3c41fb987a188b781410b690f8bcf8c47df9ee019ffdced40de4f424c587c6ada25237909ca018593523321da2077c03e619c49db2d5f
|
7
|
+
data.tar.gz: 5fa5b6298532e8936b85e615a2e0b1ce748e1cedb209e22621ec7313611f017b80627aa522977f65736668889caeb3380eca65cdb45a814fe011d7528e1121ad
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
---
|
2
|
+
language: ruby
|
3
|
+
sudo: false
|
4
|
+
cache: bundler
|
5
|
+
bundler_args: --without yard benchmarks
|
6
|
+
script: "bundle exec rake ci"
|
7
|
+
rvm:
|
8
|
+
- 1.9.3
|
9
|
+
- 2.0
|
10
|
+
- 2.1
|
11
|
+
- 2.2
|
12
|
+
- ruby-head
|
13
|
+
- jruby-19mode
|
14
|
+
- jruby
|
15
|
+
- jruby-head
|
16
|
+
- rbx-2
|
17
|
+
env:
|
18
|
+
global:
|
19
|
+
- JRUBY_OPTS="-Xcli.debug=true --debug"
|
20
|
+
matrix:
|
21
|
+
allow_failures:
|
22
|
+
- rvm: ruby-head
|
23
|
+
- rvm: jruby-head
|
24
|
+
fast_finish: true
|
25
|
+
branches:
|
26
|
+
only: master
|
27
|
+
notifications:
|
28
|
+
email: false
|
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2016 Piotr Murach
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
# Benchmark::Perf
|
2
|
+
|
3
|
+
[][gem]
|
4
|
+
[][travis]
|
5
|
+
[][codeclimate]
|
6
|
+
[][coverage]
|
7
|
+
[][inchpages]
|
8
|
+
|
9
|
+
[gem]: http://badge.fury.io/rb/benchmark-perf
|
10
|
+
[travis]: http://travis-ci.org/peter-murach/benchmark-perf
|
11
|
+
[codeclimate]: https://codeclimate.com/github/peter-murach/benchmark-perf
|
12
|
+
[coverage]: https://coveralls.io/github/peter-murach/benchmark-perf?branch=master
|
13
|
+
[inchpages]: http://inch-ci.org/github/peter-murach/benchmark-perf
|
14
|
+
|
15
|
+
> Measure execution time and iterations per second.
|
16
|
+
|
17
|
+
The **Benchmark::Perf** is used by [rspec-benchmark](https://github.com/peter-murach/rspec-benchmark)
|
18
|
+
|
19
|
+
## Installation
|
20
|
+
|
21
|
+
Add this line to your application's Gemfile:
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
gem 'benchmark-perf'
|
25
|
+
```
|
26
|
+
|
27
|
+
And then execute:
|
28
|
+
|
29
|
+
$ bundle
|
30
|
+
|
31
|
+
Or install it yourself as:
|
32
|
+
|
33
|
+
$ gem install benchmark-perf
|
34
|
+
|
35
|
+
## Usage
|
36
|
+
|
37
|
+
To see how long it takes to execute code do:
|
38
|
+
|
39
|
+
```ruby
|
40
|
+
bench = Benchmark::Perf::ExecutionTime.new
|
41
|
+
mean, stddev = bench.run { ... }
|
42
|
+
```
|
43
|
+
|
44
|
+
By default `30` samples are taken, and `1` sample for the warmup phase. If you need to change number of measurement samples do:
|
45
|
+
|
46
|
+
```ruby
|
47
|
+
bench = Benchmark::Perf::ExecutionTime.new samples: 10
|
48
|
+
```
|
49
|
+
|
50
|
+
And to change number of warmup cycles do:
|
51
|
+
|
52
|
+
```ruby
|
53
|
+
bench = Benchmark::Perf::ExecutionTime.new warmup: 2
|
54
|
+
```
|
55
|
+
|
56
|
+
In order to check how many iterations per second a given code takes do:
|
57
|
+
|
58
|
+
```ruby
|
59
|
+
bench = Benchmark::Perf::Iteration.new
|
60
|
+
mean, stddev, iter, elapsed_time = bench.run { ... }
|
61
|
+
```
|
62
|
+
|
63
|
+
## Contributing
|
64
|
+
|
65
|
+
1. Fork it ( https://github.com/[my-github-username]/benchmark-perf/fork )
|
66
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
67
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
68
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
69
|
+
5. Create a new Pull Request
|
70
|
+
|
71
|
+
## Copyright
|
72
|
+
|
73
|
+
Copyright (c) 2016 Piotr Murach. See LICENSE for further details.
|
data/Rakefile
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'benchmark/perf/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "benchmark-perf"
|
8
|
+
spec.version = Benchmark::Perf::VERSION
|
9
|
+
spec.authors = ["Piotr Murach"]
|
10
|
+
spec.email = [""]
|
11
|
+
spec.summary = %q{Execution time and iteration performance benchmarking}
|
12
|
+
spec.description = %q{Execution time and iteration performance benchmarking}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^spec/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency 'bundler', '>= 1.5.0', '< 2.0'
|
22
|
+
spec.add_development_dependency 'rake'
|
23
|
+
end
|
@@ -0,0 +1,67 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'benchmark'
|
4
|
+
|
5
|
+
require 'benchmark/perf/execution_time'
|
6
|
+
require 'benchmark/perf/iteration'
|
7
|
+
require 'benchmark/perf/version'
|
8
|
+
|
9
|
+
module Benchmark
|
10
|
+
module Perf
|
11
|
+
# Calculate arithemtic average of measurements
|
12
|
+
#
|
13
|
+
# @param [Array[Float]] measurements
|
14
|
+
#
|
15
|
+
# @return [Float]
|
16
|
+
# the average of given measurements
|
17
|
+
#
|
18
|
+
# @api public
|
19
|
+
def average(measurements)
|
20
|
+
return 0 if measurements.empty?
|
21
|
+
measurements.reduce(&:+).to_f / measurements.size
|
22
|
+
end
|
23
|
+
|
24
|
+
# Calculate standard deviation
|
25
|
+
#
|
26
|
+
# @param [Array[Float]] measurements
|
27
|
+
#
|
28
|
+
# @api public
|
29
|
+
def std_dev(measurements)
|
30
|
+
return 0 if measurements.empty?
|
31
|
+
average = average(measurements)
|
32
|
+
Math.sqrt(
|
33
|
+
measurements.reduce(0) do |sum, x|
|
34
|
+
sum + (x - average)**2
|
35
|
+
end.to_f / (measurements.size - 1)
|
36
|
+
)
|
37
|
+
end
|
38
|
+
|
39
|
+
# Run given work and gather time statistics
|
40
|
+
#
|
41
|
+
# @param [Float] threshold
|
42
|
+
#
|
43
|
+
# @return [Boolean]
|
44
|
+
#
|
45
|
+
# @api public
|
46
|
+
def assert_perform_under(threshold, options = {}, &work)
|
47
|
+
bench = ExecutionTime.new(options)
|
48
|
+
actual, _ = bench.run(&work)
|
49
|
+
actual <= threshold
|
50
|
+
end
|
51
|
+
|
52
|
+
# Assert work is performed within expected iterations per second
|
53
|
+
#
|
54
|
+
# @param [Integer] iterations
|
55
|
+
#
|
56
|
+
# @return [Boolean]
|
57
|
+
#
|
58
|
+
# @api public
|
59
|
+
def assert_perform_ips(iterations, options = {}, &work)
|
60
|
+
bench = Iteration.new(options)
|
61
|
+
mean, stddev, _ = bench.run(&work)
|
62
|
+
iterations <= (mean + 3 * stddev)
|
63
|
+
end
|
64
|
+
|
65
|
+
extend Benchmark::Perf
|
66
|
+
end # Perf
|
67
|
+
end # Benchmark
|
@@ -0,0 +1,108 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Benchmark
|
4
|
+
module Perf
|
5
|
+
# Measure length of time the work could take on average
|
6
|
+
#
|
7
|
+
# @api public
|
8
|
+
class ExecutionTime
|
9
|
+
attr_reader :io
|
10
|
+
|
11
|
+
# Initialize execution time
|
12
|
+
#
|
13
|
+
# @param [Hash] options
|
14
|
+
#
|
15
|
+
# @param options :warmup
|
16
|
+
# the number of cycles for warmup, default 1
|
17
|
+
#
|
18
|
+
# @api public
|
19
|
+
def initialize(options = {})
|
20
|
+
@io = options.fetch(:io) { nil }
|
21
|
+
@samples = options.fetch(:samples) { 30 }
|
22
|
+
@warmup = options.fetch(:warmup) { 1 }
|
23
|
+
end
|
24
|
+
|
25
|
+
# Set of ranges in linear progression
|
26
|
+
#
|
27
|
+
# @api private
|
28
|
+
def linear_range(min, max, step = 1)
|
29
|
+
(min..max).step(step).to_a
|
30
|
+
end
|
31
|
+
|
32
|
+
def bench_range
|
33
|
+
linear_range(1, @samples)
|
34
|
+
end
|
35
|
+
|
36
|
+
# Isolate run in subprocess
|
37
|
+
#
|
38
|
+
# @example
|
39
|
+
# iteration.run_in_subproces { ... }
|
40
|
+
#
|
41
|
+
# @return [Float]
|
42
|
+
# the elapsed time of the measurement
|
43
|
+
#
|
44
|
+
# @api private
|
45
|
+
def run_in_subprocess
|
46
|
+
return yield unless Process.respond_to?(:fork)
|
47
|
+
|
48
|
+
reader, writer = IO.pipe
|
49
|
+
pid = Process.fork do
|
50
|
+
GC.start
|
51
|
+
GC.disable if ENV['BENCH_DISABLE_GC']
|
52
|
+
reader.close
|
53
|
+
|
54
|
+
time = yield
|
55
|
+
|
56
|
+
io.print "%9.6f" % time if io
|
57
|
+
writer.write(Marshal.dump(time))
|
58
|
+
GC.enable if ENV['BENCH_DISABLE_GC']
|
59
|
+
exit!(0) # run without hooks
|
60
|
+
end
|
61
|
+
|
62
|
+
writer.close
|
63
|
+
Process.waitpid(pid)
|
64
|
+
Marshal.load(reader.read)
|
65
|
+
end
|
66
|
+
|
67
|
+
# Run warmup measurement
|
68
|
+
#
|
69
|
+
# @api private
|
70
|
+
def run_warmup(&work)
|
71
|
+
GC.start
|
72
|
+
@warmup.times do
|
73
|
+
run_in_subprocess { ::Benchmark.realtime(&work) }
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
# Perform work x times
|
78
|
+
#
|
79
|
+
# @param [Integer] times
|
80
|
+
# how many times sample the code measuremenets
|
81
|
+
#
|
82
|
+
# @example
|
83
|
+
# iteration = Iteration.new
|
84
|
+
# iteration.run(10) { ... }
|
85
|
+
#
|
86
|
+
# @return [Array[Float, Float]]
|
87
|
+
# average and standard deviation
|
88
|
+
#
|
89
|
+
# @api public
|
90
|
+
def run(times = (not_set = true), &work)
|
91
|
+
range = not_set ? bench_range : (0..times)
|
92
|
+
measurements = []
|
93
|
+
run_warmup(&work)
|
94
|
+
|
95
|
+
range.each do
|
96
|
+
GC.start
|
97
|
+
|
98
|
+
measurements << run_in_subprocess do
|
99
|
+
::Benchmark.realtime(&work)
|
100
|
+
end
|
101
|
+
end
|
102
|
+
io.puts if io
|
103
|
+
|
104
|
+
[Perf.average(measurements), Perf.std_dev(measurements)]
|
105
|
+
end
|
106
|
+
end # ExecutionTime
|
107
|
+
end # Perf
|
108
|
+
end # Benchmark
|
@@ -0,0 +1,97 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Benchmark
|
4
|
+
module Perf
|
5
|
+
# Measure number of iterations a work could take in a second
|
6
|
+
#
|
7
|
+
# @api private
|
8
|
+
class Iteration
|
9
|
+
MICROSECONDS_PER_SECOND = 1_000_000
|
10
|
+
MICROSECONDS_PER_100MS = 100_000
|
11
|
+
|
12
|
+
def initialize(options = {})
|
13
|
+
@time = options.fetch(:time) { 2 } # Default 2 seconds for measurement
|
14
|
+
@warmup = options.fetch(:warmup) { 1 }
|
15
|
+
end
|
16
|
+
|
17
|
+
# Call work by given times
|
18
|
+
#
|
19
|
+
# @param [Integer] times
|
20
|
+
# the times to call
|
21
|
+
#
|
22
|
+
# @return [Integer]
|
23
|
+
# the number of times worke has been called
|
24
|
+
#
|
25
|
+
# @api private
|
26
|
+
def call_times(times)
|
27
|
+
i = 0
|
28
|
+
while i < times
|
29
|
+
yield
|
30
|
+
i += 1
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
# Calcualte the number of cycles needed for 100ms
|
35
|
+
#
|
36
|
+
# @param [Integer] iterations
|
37
|
+
# @param [Float] elapsed_time
|
38
|
+
# the total time for all iterations
|
39
|
+
#
|
40
|
+
# @return [Integer]
|
41
|
+
# the cycles per 100ms
|
42
|
+
#
|
43
|
+
# @api private
|
44
|
+
def cycles_per_100ms(iterations, elapsed_time)
|
45
|
+
cycles = (iterations * (MICROSECONDS_PER_100MS / elapsed_time)).to_i
|
46
|
+
cycles <= 0 ? 1 : cycles
|
47
|
+
end
|
48
|
+
|
49
|
+
# Warmup run
|
50
|
+
#
|
51
|
+
# @api private
|
52
|
+
def run_warmup(&work)
|
53
|
+
GC.start
|
54
|
+
target = Time.now + @warmup
|
55
|
+
iter = 0
|
56
|
+
|
57
|
+
elapsed_time = ::Benchmark.realtime do
|
58
|
+
while Time.now < target
|
59
|
+
call_times(1, &work)
|
60
|
+
iter += 1
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
elapsed_time *= MICROSECONDS_PER_SECOND
|
65
|
+
cycles_per_100ms(iter, elapsed_time)
|
66
|
+
end
|
67
|
+
|
68
|
+
# Run measurements
|
69
|
+
#
|
70
|
+
# @api public
|
71
|
+
def run(&work)
|
72
|
+
target = Time.now + @time
|
73
|
+
iter = 0
|
74
|
+
measurements = []
|
75
|
+
cycles = run_warmup(&work)
|
76
|
+
|
77
|
+
GC.start
|
78
|
+
|
79
|
+
while Time.now < target
|
80
|
+
time = ::Benchmark.realtime { call_times(cycles, &work) }
|
81
|
+
next if time <= 0.0 # Iteration took no time
|
82
|
+
iter += cycles
|
83
|
+
measurements << time * MICROSECONDS_PER_SECOND
|
84
|
+
end
|
85
|
+
|
86
|
+
ips = measurements.map do |time_ms|
|
87
|
+
(cycles / time_ms) * MICROSECONDS_PER_SECOND
|
88
|
+
end
|
89
|
+
|
90
|
+
final_time = Time.now
|
91
|
+
elapsed_time = (final_time - target).abs
|
92
|
+
|
93
|
+
[Perf.average(ips).round, Perf.std_dev(ips).round, iter, elapsed_time]
|
94
|
+
end
|
95
|
+
end # Iteration
|
96
|
+
end # Perf
|
97
|
+
end # Benchmark
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
if RUBY_VERSION > '1.9' and (ENV['COVERAGE'] || ENV['TRAVIS'])
|
4
|
+
require 'simplecov'
|
5
|
+
require 'coveralls'
|
6
|
+
|
7
|
+
SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
|
8
|
+
SimpleCov::Formatter::HTMLFormatter,
|
9
|
+
Coveralls::SimpleCov::Formatter
|
10
|
+
]
|
11
|
+
|
12
|
+
SimpleCov.start do
|
13
|
+
command_name 'spec'
|
14
|
+
add_filter 'spec'
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
require 'benchmark-perf'
|
19
|
+
|
20
|
+
RSpec.configure do |config|
|
21
|
+
config.expect_with :rspec do |expectations|
|
22
|
+
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
|
23
|
+
end
|
24
|
+
|
25
|
+
config.mock_with :rspec do |mocks|
|
26
|
+
mocks.verify_partial_doubles = true
|
27
|
+
end
|
28
|
+
|
29
|
+
config.filter_run :focus
|
30
|
+
config.run_all_when_everything_filtered = true
|
31
|
+
|
32
|
+
config.disable_monkey_patching!
|
33
|
+
|
34
|
+
config.warnings = true
|
35
|
+
|
36
|
+
if config.files_to_run.one?
|
37
|
+
config.default_formatter = 'doc'
|
38
|
+
end
|
39
|
+
|
40
|
+
config.profile_examples = 2
|
41
|
+
|
42
|
+
config.order = :random
|
43
|
+
|
44
|
+
Kernel.srand config.seed
|
45
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
RSpec.describe Benchmark::Perf, '#assert_perform' do
|
4
|
+
it "passes asertion by performing under threshold" do
|
5
|
+
bench = Benchmark::Perf
|
6
|
+
assertion = bench.assert_perform_under(0.01, samples: 2) { 'x' * 1_024 }
|
7
|
+
expect(assertion).to eq(true)
|
8
|
+
end
|
9
|
+
|
10
|
+
it "passes asertion by performing 10K ips" do
|
11
|
+
bench = Benchmark::Perf
|
12
|
+
assertion = bench.assert_perform_ips(10_000) { 'x' * 1_024 }
|
13
|
+
expect(assertion).to eq(true)
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
RSpec.describe Benchmark::Perf do
|
4
|
+
it "calculates average without measurements" do
|
5
|
+
expect(Benchmark::Perf.average([])).to eq(0)
|
6
|
+
end
|
7
|
+
|
8
|
+
it "calculates average with measurements" do
|
9
|
+
expect(described_class.average([1,2,3])).to eq(2.0)
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
RSpec.describe Benchmark::Perf::ExecutionTime do
|
4
|
+
it "provides linear range" do
|
5
|
+
bench = described_class.new
|
6
|
+
expect(bench.linear_range(0, 3)).to eq([0,1,2,3])
|
7
|
+
end
|
8
|
+
|
9
|
+
it "provides default benchmark range" do
|
10
|
+
bench = described_class.new
|
11
|
+
expect(bench.bench_range.size).to eq(30)
|
12
|
+
end
|
13
|
+
|
14
|
+
it "provides measurements for 30 samples by default" do
|
15
|
+
bench = described_class.new
|
16
|
+
sample = bench.run { 'x' * 1024 }
|
17
|
+
expect(sample).to all(be < 0.01)
|
18
|
+
end
|
19
|
+
|
20
|
+
it "executes code to warmup ruby vm" do
|
21
|
+
bench = described_class.new
|
22
|
+
sample = bench.run_warmup { 'x' * 1_000_000 }
|
23
|
+
expect(sample).to eq(1)
|
24
|
+
end
|
25
|
+
|
26
|
+
it "measures work performance for 10 samples" do
|
27
|
+
bench = described_class.new
|
28
|
+
sample = bench.run(10) { 'x' * 1_000_000 }
|
29
|
+
expect(sample.size).to eq(2)
|
30
|
+
expect(sample).to all(be < 0.01)
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
RSpec.describe Benchmark::Perf::Iteration do
|
4
|
+
it "defines cycles per 100 microseconds" do
|
5
|
+
bench = described_class.new
|
6
|
+
sample = bench.run_warmup { 'x' * 1_000_000 }
|
7
|
+
expect(sample).to be > 25
|
8
|
+
end
|
9
|
+
|
10
|
+
it "measures 10K iterations per second" do
|
11
|
+
bench = described_class.new
|
12
|
+
sample = bench.run { 'x' * 1_000_000 }
|
13
|
+
expect(sample.size).to eq(4)
|
14
|
+
expect(sample[0]).to be > 250
|
15
|
+
expect(sample[1]).to be > 5
|
16
|
+
expect(sample[2]).to be > 250
|
17
|
+
end
|
18
|
+
end
|
data/tasks/coverage.rake
ADDED
data/tasks/spec.rake
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
begin
|
4
|
+
require 'rspec/core/rake_task'
|
5
|
+
|
6
|
+
desc 'Run all specs'
|
7
|
+
RSpec::Core::RakeTask.new(:spec) do |task|
|
8
|
+
task.pattern = 'spec/{unit,integration}{,/*/**}/*_spec.rb'
|
9
|
+
end
|
10
|
+
|
11
|
+
namespace :spec do
|
12
|
+
desc 'Run unit specs'
|
13
|
+
RSpec::Core::RakeTask.new(:unit) do |task|
|
14
|
+
task.pattern = 'spec/unit{,/*/**}/*_spec.rb'
|
15
|
+
end
|
16
|
+
|
17
|
+
desc 'Run integration specs'
|
18
|
+
RSpec::Core::RakeTask.new(:integration) do |task|
|
19
|
+
task.pattern = 'spec/integration{,/*/**}/*_spec.rb'
|
20
|
+
end
|
21
|
+
|
22
|
+
desc 'Run performance specs'
|
23
|
+
RSpec::Core::RakeTask.new(:perf) do |task|
|
24
|
+
task.pattern = 'spec/performance{,/*/**}/*_spec.rb'
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
rescue LoadError
|
29
|
+
%w[spec spec:unit spec:integration spec:perf].each do |name|
|
30
|
+
task name do
|
31
|
+
$stderr.puts "In order to run #{name}, do `gem install rspec`"
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
metadata
ADDED
@@ -0,0 +1,105 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: benchmark-perf
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Piotr Murach
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-01-25 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 1.5.0
|
20
|
+
- - <
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '2.0'
|
23
|
+
type: :development
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 1.5.0
|
30
|
+
- - <
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '2.0'
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: rake
|
35
|
+
requirement: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - '>='
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '0'
|
40
|
+
type: :development
|
41
|
+
prerelease: false
|
42
|
+
version_requirements: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - '>='
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '0'
|
47
|
+
description: Execution time and iteration performance benchmarking
|
48
|
+
email:
|
49
|
+
- ''
|
50
|
+
executables: []
|
51
|
+
extensions: []
|
52
|
+
extra_rdoc_files: []
|
53
|
+
files:
|
54
|
+
- .gitignore
|
55
|
+
- .rspec
|
56
|
+
- .travis.yml
|
57
|
+
- CHANGELOG.md
|
58
|
+
- Gemfile
|
59
|
+
- LICENSE.txt
|
60
|
+
- README.md
|
61
|
+
- Rakefile
|
62
|
+
- benchmark-perf.gemspec
|
63
|
+
- lib/benchmark-perf.rb
|
64
|
+
- lib/benchmark/perf.rb
|
65
|
+
- lib/benchmark/perf/execution_time.rb
|
66
|
+
- lib/benchmark/perf/iteration.rb
|
67
|
+
- lib/benchmark/perf/version.rb
|
68
|
+
- spec/spec_helper.rb
|
69
|
+
- spec/unit/assertions_spec.rb
|
70
|
+
- spec/unit/average_spec.rb
|
71
|
+
- spec/unit/execution_time_spec.rb
|
72
|
+
- spec/unit/iteration_spec.rb
|
73
|
+
- tasks/coverage.rake
|
74
|
+
- tasks/spec.rake
|
75
|
+
homepage: ''
|
76
|
+
licenses:
|
77
|
+
- MIT
|
78
|
+
metadata: {}
|
79
|
+
post_install_message:
|
80
|
+
rdoc_options: []
|
81
|
+
require_paths:
|
82
|
+
- lib
|
83
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
84
|
+
requirements:
|
85
|
+
- - '>='
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: '0'
|
88
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
89
|
+
requirements:
|
90
|
+
- - '>='
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
version: '0'
|
93
|
+
requirements: []
|
94
|
+
rubyforge_project:
|
95
|
+
rubygems_version: 2.0.3
|
96
|
+
signing_key:
|
97
|
+
specification_version: 4
|
98
|
+
summary: Execution time and iteration performance benchmarking
|
99
|
+
test_files:
|
100
|
+
- spec/spec_helper.rb
|
101
|
+
- spec/unit/assertions_spec.rb
|
102
|
+
- spec/unit/average_spec.rb
|
103
|
+
- spec/unit/execution_time_spec.rb
|
104
|
+
- spec/unit/iteration_spec.rb
|
105
|
+
has_rdoc:
|