rspec-stopwatch 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/lib/rspec-stopwatch.rb +2 -0
- data/lib/rspec/stopwatch/matchers/run_for_matcher.rb +83 -0
- data/lib/rspec/stopwatch/version.rb +6 -0
- data/spec/matchers/run_for_spec.rb +64 -0
- metadata +77 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 0e0a606b083a128209f5db137f74421518a7111c
|
4
|
+
data.tar.gz: 31a6dd12c74dbb4c430a7082e9bc8a72a4e635f8
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 7cf4e151d64cd3e0cfa51d98c3762f6011415e4afb605c041bff17d7d4c097295eb95794c284614dae6a9c009e8ed7af15dd49b8110a2feaed3842dd4665d2b9
|
7
|
+
data.tar.gz: 117c606c609d00b87a7de6d5430b9d0fca1c80e99509cc798279b52a6d07728bbf91fbde7246ebf286d543bbf93aad25a9f33c824b06c9a028ee3b960dd925f8
|
@@ -0,0 +1,83 @@
|
|
1
|
+
require 'rspec'
|
2
|
+
|
3
|
+
require 'active_support/core_ext/numeric/time'
|
4
|
+
require 'benchmark'
|
5
|
+
|
6
|
+
RSpec::Matchers.define :run_for do |time|
|
7
|
+
[:<, :<=, :>, :>=].each do |operator|
|
8
|
+
define_method(operator) do |expected|
|
9
|
+
RunForComparedTo.new(operator, expected)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
class RunForComparedTo < RSpec::Matchers::BuiltIn::BaseMatcher
|
14
|
+
attr_reader :operator, :expected, :actual, :config
|
15
|
+
|
16
|
+
def initialize(operator, expected)
|
17
|
+
@operator, @expected = operator, expected.to_f
|
18
|
+
|
19
|
+
@config = {
|
20
|
+
:average_over => 10.times,
|
21
|
+
:drop_first => 1,
|
22
|
+
}
|
23
|
+
end
|
24
|
+
|
25
|
+
def matches?(block)
|
26
|
+
raise ArgumentError, 'Expecting a block to `expect`' if !block.is_a?(Proc)
|
27
|
+
|
28
|
+
begin
|
29
|
+
warmup_runs.each { block.call }
|
30
|
+
benchmark = Benchmark.measure { runs.each { block.call } }
|
31
|
+
|
32
|
+
@actual = benchmark.total / runs.count
|
33
|
+
@actual.in_milliseconds.send(operator, expected.in_milliseconds)
|
34
|
+
rescue ArgumentError
|
35
|
+
false
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def supports_block_expectations?
|
40
|
+
true
|
41
|
+
end
|
42
|
+
|
43
|
+
def failure_message
|
44
|
+
failure_message_for_operator(operator)
|
45
|
+
end
|
46
|
+
|
47
|
+
def failure_message_when_negated
|
48
|
+
failure_message_for_operator(opposite_operator)
|
49
|
+
end
|
50
|
+
|
51
|
+
def description
|
52
|
+
"run for #{operator} #{expected_str}"
|
53
|
+
end
|
54
|
+
|
55
|
+
private
|
56
|
+
|
57
|
+
def runs
|
58
|
+
config[:average_over]
|
59
|
+
end
|
60
|
+
|
61
|
+
def warmup_runs
|
62
|
+
config[:drop_first].times
|
63
|
+
end
|
64
|
+
|
65
|
+
def failure_message_for_operator(operator)
|
66
|
+
"expected: #{operator} #{expected_str}\n" \
|
67
|
+
" got: #{operator.to_s.gsub(/./, ' ')} #{actual_str}"
|
68
|
+
end
|
69
|
+
|
70
|
+
def opposite_operator
|
71
|
+
{ :< => :>=, :<= => :>, :> => :<=, :>= => :< }[operator]
|
72
|
+
end
|
73
|
+
|
74
|
+
def expected_str
|
75
|
+
"#{expected.in_milliseconds} ms"
|
76
|
+
end
|
77
|
+
|
78
|
+
def actual_str
|
79
|
+
"#{actual.in_milliseconds} ms"
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
@@ -0,0 +1,64 @@
|
|
1
|
+
require 'rspec-stopwatch'
|
2
|
+
|
3
|
+
describe :run_for, :matcher => true do
|
4
|
+
describe 'timing' do
|
5
|
+
it 'reports comparisons correctly' do
|
6
|
+
expect { 1000000.times { } }.to run_for > 0.001.seconds
|
7
|
+
end
|
8
|
+
|
9
|
+
it 'ignores real time' do
|
10
|
+
expect { sleep(0.2) }.to run_for < 0.1.second
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
describe 'success messages' do
|
15
|
+
subject { RunForComparedTo.new(:<, 1.second) }
|
16
|
+
|
17
|
+
before do
|
18
|
+
subject.matches?(lambda { })
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'includes the operator' do
|
22
|
+
expect(subject.description).to include '<'
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'includes the expectation time' do
|
26
|
+
expect(subject.description).to include '1000.0 ms'
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
describe 'failure messages' do
|
31
|
+
subject { RunForComparedTo.new(:>, 1.second) }
|
32
|
+
|
33
|
+
before do
|
34
|
+
subject.matches?(lambda { })
|
35
|
+
end
|
36
|
+
|
37
|
+
it 'includes the operator' do
|
38
|
+
expect(subject.failure_message).to include '>'
|
39
|
+
end
|
40
|
+
|
41
|
+
it 'includes the expected time' do
|
42
|
+
expect(subject.failure_message).to include '1000.0 ms'
|
43
|
+
end
|
44
|
+
|
45
|
+
it 'includes the actual time' do
|
46
|
+
expect(subject.failure_message).to include '0.0 ms'
|
47
|
+
end
|
48
|
+
|
49
|
+
context 'negated' do
|
50
|
+
it 'includes the opposite operator' do
|
51
|
+
expect(subject.failure_message_when_negated).to include '<='
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
describe 'error messages' do
|
57
|
+
it "raises when a block isn't passed" do
|
58
|
+
expect do
|
59
|
+
expect(:pikachu).to run_for < 1.second
|
60
|
+
end.to raise_error(ArgumentError).with_message 'Expecting a block to `expect`'
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
metadata
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rspec-stopwatch
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Henry J. Wylde
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-07-13 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rspec
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '3'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: activesupport
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '4'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '4'
|
41
|
+
description: Provides a matcher that checks the run time of a block
|
42
|
+
email:
|
43
|
+
- public@hjwylde.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- lib/rspec-stopwatch.rb
|
49
|
+
- lib/rspec/stopwatch/matchers/run_for_matcher.rb
|
50
|
+
- lib/rspec/stopwatch/version.rb
|
51
|
+
- spec/matchers/run_for_spec.rb
|
52
|
+
homepage: https://git.powershop.co.nz/hjwylde/rspec-stopwatch-gem
|
53
|
+
licenses:
|
54
|
+
- BSD-3-Clause
|
55
|
+
metadata: {}
|
56
|
+
post_install_message:
|
57
|
+
rdoc_options: []
|
58
|
+
require_paths:
|
59
|
+
- lib
|
60
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
61
|
+
requirements:
|
62
|
+
- - ">="
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: '0'
|
65
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - ">="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
requirements: []
|
71
|
+
rubyforge_project:
|
72
|
+
rubygems_version: 2.4.6
|
73
|
+
signing_key:
|
74
|
+
specification_version: 4
|
75
|
+
summary: Ruby gem for writing performance specs
|
76
|
+
test_files:
|
77
|
+
- spec/matchers/run_for_spec.rb
|