benches 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 289a188d0ee7e80411be4e9d4812c49e98496ea0
4
- data.tar.gz: d8798a409099b2a5a9e5bb0ef177ed93142957ba
3
+ metadata.gz: c5283633dfb5c13e49a963c2ac09b027387441c9
4
+ data.tar.gz: b28b6003fd2a48648b364a9142e004f8a91404cf
5
5
  SHA512:
6
- metadata.gz: 798b63c764e81844813d278b3c74692fcba823cc1443cd6dd3b70af6292ec1e09e3c85fa7d7ebbf13f92c548819dc9a2d0eaf23634058afacc7c099e65746cc4
7
- data.tar.gz: 5e22ea4b6deb9d7350103f12f8f76432089d2428f12de1664b465571855692510ea25b454d7a3cf93fbafdc56508189470bb306b469c701f7130958132fa62aa
6
+ metadata.gz: c6643716c20ebb0178d298fe53fae8415f6fd68918fc139ead3342a23f8a72676bf70802197f8dcc030687629fe33bc1aa2742f4c4821dbab2c3c8405e5cbfb0
7
+ data.tar.gz: 021dfda72bcf425c84d68d0771e19afa58ef1eeab6af95083812c3b6b69a3b6ed0617765804d4165d9953a2d4fc32f5871a190983b6533b30ce52aea0c074556
data/README.md CHANGED
@@ -1,35 +1,52 @@
1
1
  # Benches
2
2
 
3
- Benches defines a simple benchmarking DSL that allows you to create benchmark routines for your Ruby code.
3
+ Benches defines a simple rspec matcher that allows you to create benchmarking specs for your Ruby code.
4
4
 
5
- ## Defining a Routine
5
+ ## Usage
6
6
 
7
- Create a folder called regimens. For each class that you want to benchmark,
8
- create a file like integer_regimen.rb
7
+ To include the matchers in your specs, set up your `spec_helper.rb` like so:
8
+ ```ruby
9
+ require 'benches'
10
+ # other requirements
11
+
12
+ RSpec.configure do |config|
13
+ config.include Benches::Matchers
14
+ #other configuration code
15
+ end
16
+ ```
17
+
18
+ The run_in_less_than matcher expects a block and checks if the block runs in less than a certain amount of time.
9
19
 
10
- integer_regimen.rb
11
20
  ```ruby
12
- subject 5 do
13
- benches '500 reps of to_s in less than 5 seconds'
14
- benches '400 reps of + in less than 2 seconds', 3
15
- benches '300 reps of * in less than 3 seconds', 400
16
- end
21
+ describe Integer
22
+ describe 'to_s' do
23
+ it 'meets the performance metrics when running 1 time' do
24
+ expect{5.to_s}.to run_in_less_than(1.second)
25
+ end
26
+ end
27
+ end
28
+ ```
17
29
 
18
- subject 20 do
19
- benches '500 reps of to_s in less than 5 seconds'
20
- benches '400 reps of + in less than 2 seconds', 3
21
- benches '300 reps of * in less than 3 seconds', 400
30
+ You can also specify that a block should run a certain number of times in less than a certain duration.
31
+
32
+ ```ruby
33
+ describe Integer do
34
+ describe 'to_s' do
35
+ it 'meets the performance metrics' do
36
+ expect{5.to_s}.to run(500).times_in_less_than(5.seconds)
37
+ end
38
+ end
22
39
  end
23
40
  ```
24
41
 
25
- If the benchmarks runs within the allotted time, it will
26
- be printed green, otherwise it will be printed red.
42
+ The duration specified should be an ActiveSupport::Duration object, like `5.seconds` or `1.hour`.
27
43
 
28
- The command `benchpress` runs all of the regimen files
44
+ If the benchmark runs within the allotted time, the test will pass.
45
+ Otherwise it will fail.
29
46
 
30
47
  ## Testing
31
48
 
32
- Simply run `rspec` to run the test suite.
49
+ Simply run `rspec` to run the sample spec.
33
50
 
34
51
  ## Credits
35
52
 
data/benches.gemspec CHANGED
@@ -9,13 +9,16 @@ Gem::Specification.new do |s|
9
9
  s.authors = ["Evan Hemsley"]
10
10
  s.email = ["evan.hemsley@gmail.com"]
11
11
  s.homepage = 'http://github.com/ehemsley/benches'
12
- s.summary = 'A simple DSL for benchmarking your Ruby code'
13
- s.description = 'Benches defines a simple benchmarking DSL that allows you to create benchmark routines for your Ruby code.'
12
+ s.summary = 'A simple rspec matcher for testing performance metrics'
13
+ s.description = 'Benches defines a simple rspec matcher that allows you to create benchmarking specs for your Ruby code.'
14
14
 
15
15
  s.files = `git ls-files`.split("\n")
16
16
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
17
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
18
18
  s.require_paths = ["lib"]
19
19
 
20
+ s.add_dependency 'rspec', '~> 2.14', '>= 2.14.1'
21
+ s.add_dependency 'activesupport', '~> 4.0', '>= 4.0.3'
22
+
20
23
  s.license = 'MIT'
21
24
  end
@@ -0,0 +1,37 @@
1
+ module Benches
2
+ module Matchers
3
+
4
+ RSpec::Matchers.define :run do |repetitions|
5
+
6
+ chain :time_in_less_than do |duration|
7
+ @duration = duration
8
+ end
9
+
10
+ chain :times_in_less_than do |duration|
11
+ @duration = duration
12
+ end
13
+
14
+ match do |actual|
15
+ Benchmark.measure do
16
+ repetitions.times do
17
+ actual.call
18
+ end
19
+ end.utime.seconds < @duration
20
+ end
21
+
22
+ failure_message_for_should do |actual|
23
+ "expected code to run in less than #{@duration.inspect}"
24
+ end
25
+
26
+ failure_message_for_should_not do |actual|
27
+ "expected code not to run in less than #{@duration.inspect} seconds"
28
+ end
29
+
30
+ description do
31
+ "run in less than #{@duration.inspect}"
32
+ end
33
+ end
34
+
35
+ end
36
+ end
37
+
@@ -0,0 +1,27 @@
1
+ module Benches
2
+ module Matchers
3
+
4
+ RSpec::Matchers.define :run_in_less_than do |duration|
5
+
6
+ match do |actual|
7
+ Benchmark.measure do
8
+ actual.call
9
+ end.utime.seconds < duration
10
+ end
11
+
12
+ failure_message_for_should do |actual|
13
+ "expected code to run in less than #{duration.inspect}"
14
+ end
15
+
16
+ failure_message_for_should_not do |actual|
17
+ "expected code not to run in less than #{duration.inspect} seconds"
18
+ end
19
+
20
+ description do
21
+ "run in less than #{duration.inspect}"
22
+ end
23
+
24
+ end
25
+
26
+ end
27
+ end
@@ -1,3 +1,3 @@
1
1
  module Benches
2
- VERSION = '0.2.0'
2
+ VERSION = '0.3.0'
3
3
  end
data/lib/benches.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require 'benchmark'
2
- require 'benches/routine'
3
- require 'benches/regimen'
4
- require 'benches/dsl'
2
+ require 'active_support/core_ext/numeric/time'
3
+
4
+ require 'benches/matchers/run'
5
+ require 'benches/matchers/run_in_less_than'
@@ -0,0 +1,13 @@
1
+ require 'spec_helper'
2
+
3
+ describe Integer do
4
+ describe 'to_s' do
5
+ it 'meets the performance metrics when running 1 time' do
6
+ expect{5.to_s}.to run_in_less_than(1.second)
7
+ end
8
+
9
+ it 'meets the performance metrics when running 500 times' do
10
+ expect{5.to_s}.to run(500).times_in_less_than(5.seconds)
11
+ end
12
+ end
13
+ end
metadata CHANGED
@@ -1,21 +1,60 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: benches
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Evan Hemsley
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-03-09 00:00:00.000000000 Z
12
- dependencies: []
13
- description: Benches defines a simple benchmarking DSL that allows you to create benchmark
14
- routines for your Ruby code.
11
+ date: 2014-03-11 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: '2.14'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 2.14.1
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '2.14'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 2.14.1
33
+ - !ruby/object:Gem::Dependency
34
+ name: activesupport
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '4.0'
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: 4.0.3
43
+ type: :runtime
44
+ prerelease: false
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - "~>"
48
+ - !ruby/object:Gem::Version
49
+ version: '4.0'
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: 4.0.3
53
+ description: Benches defines a simple rspec matcher that allows you to create benchmarking
54
+ specs for your Ruby code.
15
55
  email:
16
56
  - evan.hemsley@gmail.com
17
- executables:
18
- - benchpress
57
+ executables: []
19
58
  extensions: []
20
59
  extra_rdoc_files: []
21
60
  files:
@@ -23,15 +62,12 @@ files:
23
62
  - Gemfile.lock
24
63
  - README.md
25
64
  - benches.gemspec
26
- - bin/benchpress
27
65
  - lib/benches.rb
28
- - lib/benches/dsl.rb
29
- - lib/benches/regimen.rb
30
- - lib/benches/routine.rb
66
+ - lib/benches/matchers/run.rb
67
+ - lib/benches/matchers/run_in_less_than.rb
31
68
  - lib/benches/version.rb
32
69
  - license.md
33
- - spec/dsl_spec.rb
34
- - spec/routine_spec.rb
70
+ - spec/matchers/run_spec.rb
35
71
  - spec/spec_helper.rb
36
72
  homepage: http://github.com/ehemsley/benches
37
73
  licenses:
@@ -56,8 +92,7 @@ rubyforge_project:
56
92
  rubygems_version: 2.2.2
57
93
  signing_key:
58
94
  specification_version: 4
59
- summary: A simple DSL for benchmarking your Ruby code
95
+ summary: A simple rspec matcher for testing performance metrics
60
96
  test_files:
61
- - spec/dsl_spec.rb
62
- - spec/routine_spec.rb
97
+ - spec/matchers/run_spec.rb
63
98
  - spec/spec_helper.rb
data/bin/benchpress DELETED
@@ -1,10 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- require 'benches'
4
- load 'regimens/regimen_helper.rb'
5
-
6
- Dir["regimens/*_regimen.rb"].each do |file|
7
- puts "Performing #{File.basename(file).gsub("_regimen.rb", "")} regimen: "
8
- load file
9
- puts "\n"
10
- end
data/lib/benches/dsl.rb DELETED
@@ -1,9 +0,0 @@
1
- def subject(instance, &block)
2
- @regimen = Benches::Regimen.new(instance)
3
- block.call
4
- @regimen.call
5
- end
6
-
7
- def benches(input, *args)
8
- @regimen.add_input(input, args)
9
- end
@@ -1,33 +0,0 @@
1
- require 'colorize'
2
-
3
- module Benches
4
- class Regimen
5
- def initialize(subject)
6
- @subject = subject
7
- @inputs = []
8
- end
9
-
10
- def add_input(input, args)
11
- @inputs << [input, args]
12
- end
13
-
14
- def call
15
- puts @subject.to_s.blue
16
- @inputs.each do |input|
17
- input_string = input.first
18
- args = input.last
19
- routine(@subject, input_string, args) ? puts(" #{input_string}".green) : puts(" #{input_string}".red)
20
- end
21
- end
22
-
23
- private
24
-
25
- def routine(instance, input, args)
26
- params = input.split(" ")
27
-
28
- result = Benches::Routine.new(instance, params[3], params[0].to_i, *args).call
29
- result.utime < params[7].to_f
30
- end
31
-
32
- end
33
- end
@@ -1,18 +0,0 @@
1
- module Benches
2
- class Routine
3
- def initialize(instance, method, repetitions, *args)
4
- @instance = instance
5
- @method = method
6
- @repetitions = repetitions
7
- @args = args
8
- end
9
-
10
- def call
11
- Benchmark.measure do
12
- @repetitions.times do
13
- @instance.send(@method, *@args)
14
- end
15
- end
16
- end
17
- end
18
- end
data/spec/dsl_spec.rb DELETED
@@ -1,5 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe 'subject' do
4
-
5
- end
data/spec/routine_spec.rb DELETED
@@ -1,63 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Benches::Routine do
4
- describe 'initialize' do
5
- context 'no method arguments' do
6
- let(:bench) { Benches::Routine.new("test_string", 'split', 50000) }
7
-
8
- it 'assigns instance correctly' do
9
- expect(bench.instance_variable_get('@instance')).to eql 'test_string'
10
- end
11
-
12
- it 'assigns method correctly' do
13
- expect(bench.instance_variable_get('@method')).to eql 'split'
14
- end
15
-
16
- it 'assigns repetitions correctly' do
17
- expect(bench.instance_variable_get('@repetitions')).to eql 50000
18
- end
19
-
20
- it 'sets args to empty array' do
21
- expect(bench.instance_variable_get('@args')).to eql []
22
- end
23
- end
24
-
25
- context 'method has arguments' do
26
- let(:bench) { Benches::Routine.new("test_string", 'insert', 50000, 1, 'a') }
27
-
28
- it 'assigns instance correctly' do
29
- expect(bench.instance_variable_get('@instance')).to eql 'test_string'
30
- end
31
-
32
- it 'assigns method correctly' do
33
- expect(bench.instance_variable_get('@method')).to eql 'insert'
34
- end
35
-
36
- it 'assigns repetitions correctly' do
37
- expect(bench.instance_variable_get('@repetitions')).to eql 50000
38
- end
39
-
40
- it' assigns args correctly' do
41
- expect(bench.instance_variable_get('@args')).to eql [1, 'a']
42
- end
43
- end
44
- end
45
-
46
- describe 'call' do
47
- context 'no method arguments' do
48
- let(:bench) { Benches::Routine.new("test_string", 'split', 20) }
49
-
50
- it 'performs benchmark' do
51
- expect(bench.call).to be_a Benchmark::Tms
52
- end
53
- end
54
-
55
- context 'method has arguments' do
56
- let(:bench) { Benches::Routine.new("test_string", 'insert', 20, 1, 'a') }
57
-
58
- it 'performs benchmark' do
59
- expect(bench.call).to be_a Benchmark::Tms
60
- end
61
- end
62
- end
63
- end