benches 0.1.0 → 0.2.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: d0a31536816bbaf808af25fe99966c2d8968f6c6
4
- data.tar.gz: d826ff23eebf397bf7c853ab12b226e67163a52e
3
+ metadata.gz: 289a188d0ee7e80411be4e9d4812c49e98496ea0
4
+ data.tar.gz: d8798a409099b2a5a9e5bb0ef177ed93142957ba
5
5
  SHA512:
6
- metadata.gz: b12382125632d996603f544e0f3ce547c5db085eaf28d5282b060ce6168271c3bbb7f131a657b5d37f9d29d6fc716416b4d8f0f48d39419e53b55bc094b91999
7
- data.tar.gz: 7c9f41a9839e4a08081414f26b5b909f70bbbbdfa57c7a6114a16d1410db7ecbcebf1151066f88791daa84ec6f2984d6a269489ce9f6d721046eb660cd793341
6
+ metadata.gz: 798b63c764e81844813d278b3c74692fcba823cc1443cd6dd3b70af6292ec1e09e3c85fa7d7ebbf13f92c548819dc9a2d0eaf23634058afacc7c099e65746cc4
7
+ data.tar.gz: 5e22ea4b6deb9d7350103f12f8f76432089d2428f12de1664b465571855692510ea25b454d7a3cf93fbafdc56508189470bb306b469c701f7130958132fa62aa
data/README.md CHANGED
@@ -5,28 +5,32 @@ Benches defines a simple benchmarking DSL that allows you to create benchmark ro
5
5
  ## Defining a Routine
6
6
 
7
7
  Create a folder called regimens. For each class that you want to benchmark,
8
- create a file like integer.rb
8
+ create a file like integer_regimen.rb
9
9
 
10
- integer_routine.rb
10
+ integer_regimen.rb
11
11
  ```ruby
12
- routine 4, 'benches 400 reps of to_s' # no arguments
13
- routine 30, 'benches 300 reps of * with (400)' # one argument
14
- routine 'test_string', "benches 500 reps of gsub with (s, '')" # two arguments
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
17
+
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
22
+ end
15
23
  ```
16
24
 
17
- This will return the benchmark results of running `4.to_s` 400 times,
18
- `30 * 400` 300 times, and `'test_string'.gsub('s', '')` 500 times.
25
+ If the benchmarks runs within the allotted time, it will
26
+ be printed green, otherwise it will be printed red.
19
27
 
20
- The command `benchpress` will run all of the regimens.
28
+ The command `benchpress` runs all of the regimen files
21
29
 
22
30
  ## Testing
23
31
 
24
32
  Simply run `rspec` to run the test suite.
25
33
 
26
- ## Future Work
27
-
28
- - Allow benchmark results to be tested in block passed to routine
29
-
30
34
  ## Credits
31
35
 
32
36
  All code (c) Evan Hemsley 2014
data/bin/benchpress CHANGED
@@ -1,8 +1,10 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  require 'benches'
4
+ load 'regimens/regimen_helper.rb'
4
5
 
5
- Dir["regimens/*_routine.rb"].each do |file|
6
- puts "Performing #{File.basename(file).gsub("_routine.rb", "")} regimen: "
6
+ Dir["regimens/*_regimen.rb"].each do |file|
7
+ puts "Performing #{File.basename(file).gsub("_regimen.rb", "")} regimen: "
7
8
  load file
9
+ puts "\n"
8
10
  end
data/lib/benches/dsl.rb CHANGED
@@ -1,32 +1,9 @@
1
- #input: instance_name benches n reps of method_name (with *args)
2
- #example: graph benches 50000 reps of dfs
3
- def routine(instance, input)
4
- params = input.split(" ")
5
- if params.last == params[4]
6
- args = nil
7
- else
8
- args = coerce_args(input[/\(.*?\)/].gsub(/[()]/, "").split(","))
9
- end
10
-
11
- Benches::Routine.new(instance, params[4], params[1].to_i, *args).call
12
- end
13
-
14
- def coerce_args(args)
15
- args.map do |arg|
16
- if integer?(arg)
17
- arg.to_i
18
- elsif float?(arg)
19
- arg.to_f
20
- else
21
- arg
22
- end
23
- end
24
- end
25
-
26
- def integer?(input)
27
- !!(/^\d+$/ =~ input)
1
+ def subject(instance, &block)
2
+ @regimen = Benches::Regimen.new(instance)
3
+ block.call
4
+ @regimen.call
28
5
  end
29
6
 
30
- def float?(input)
31
- !!(/^\d+(.\d+)$/ =~ input)
7
+ def benches(input, *args)
8
+ @regimen.add_input(input, args)
32
9
  end
@@ -0,0 +1,33 @@
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
@@ -8,11 +8,9 @@ module Benches
8
8
  end
9
9
 
10
10
  def call
11
- Benchmark.bm(@method.length) do |x|
12
- x.report("#{@method}:") do
13
- @repetitions.times do
14
- @instance.send(@method, *@args)
15
- end
11
+ Benchmark.measure do
12
+ @repetitions.times do
13
+ @instance.send(@method, *@args)
16
14
  end
17
15
  end
18
16
  end
@@ -1,3 +1,3 @@
1
1
  module Benches
2
- VERSION = '0.1.0'
2
+ VERSION = '0.2.0'
3
3
  end
data/lib/benches.rb CHANGED
@@ -1,3 +1,4 @@
1
1
  require 'benchmark'
2
2
  require 'benches/routine'
3
+ require 'benches/regimen'
3
4
  require 'benches/dsl'
data/spec/dsl_spec.rb CHANGED
@@ -1,73 +1,5 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe 'routine' do
4
- context 'no method arguments' do
5
- it 'returns benchmark result' do
6
- expect((routine 'test_string', 'benches 20 reps of split').first).to be_a Benchmark::Tms
7
- end
8
- end
9
-
10
- context 'method has integer argument' do
11
- it 'returns benchmark result' do
12
- expect((routine 'test_string', "benches 20 reps of insert with (0,a)").first).to be_a Benchmark::Tms
13
- end
14
- end
15
-
16
- context 'method has float argument' do
17
- it 'returns benchmark result' do
18
- expect((routine 32.444, 'benches 20 reps of + with (4.43)').first).to be_a Benchmark::Tms
19
- end
20
- end
21
- end
22
-
23
- describe 'float?' do
24
- context 'input string contains only a float' do
25
- it 'returns true' do
26
- expect(float?('32.555')).to eql true
27
- end
28
- end
29
-
30
- context 'input string contains letters and numbers' do
31
- it 'returns false' do
32
- expect(float?('ab321')).to eql false
33
- end
34
- end
35
-
36
- context 'input string contains an integer' do
37
- it 'returns false' do
38
- expect(float?('32')).to eql false
39
- end
40
- end
41
-
42
- context 'input string does not contain any numbers' do
43
- it 'returns false' do
44
- expect(float?('stuff')).to eql false
45
- end
46
- end
47
- end
48
-
49
- describe 'integer?' do
50
- context 'input string contains only an integer' do
51
- it 'returns true' do
52
- expect(integer?('32')).to eql true
53
- end
54
- end
55
-
56
- context 'input string contains a float' do
57
- it 'returns false' do
58
- expect(integer?('32.423')).to eql false
59
- end
60
- end
61
-
62
- context 'input string contains letters and numbers' do
63
- it 'returns false' do
64
- expect(integer?('abc123')).to eql false
65
- end
66
- end
67
-
68
- context 'input string does not contain any numbers' do
69
- it 'returns false' do
70
- expect(integer?('abc')).to eql false
71
- end
72
- end
3
+ describe 'subject' do
4
+
73
5
  end
data/spec/routine_spec.rb CHANGED
@@ -48,7 +48,7 @@ describe Benches::Routine do
48
48
  let(:bench) { Benches::Routine.new("test_string", 'split', 20) }
49
49
 
50
50
  it 'performs benchmark' do
51
- expect(bench.call.first).to be_a Benchmark::Tms
51
+ expect(bench.call).to be_a Benchmark::Tms
52
52
  end
53
53
  end
54
54
 
@@ -56,7 +56,7 @@ describe Benches::Routine do
56
56
  let(:bench) { Benches::Routine.new("test_string", 'insert', 20, 1, 'a') }
57
57
 
58
58
  it 'performs benchmark' do
59
- expect(bench.call.first).to be_a Benchmark::Tms
59
+ expect(bench.call).to be_a Benchmark::Tms
60
60
  end
61
61
  end
62
62
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: benches
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Evan Hemsley
@@ -26,6 +26,7 @@ files:
26
26
  - bin/benchpress
27
27
  - lib/benches.rb
28
28
  - lib/benches/dsl.rb
29
+ - lib/benches/regimen.rb
29
30
  - lib/benches/routine.rb
30
31
  - lib/benches/version.rb
31
32
  - license.md