benchmark_harness 0.0.0 → 0.0.1

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.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.0
1
+ 0.0.1
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{benchmark_harness}
8
- s.version = "0.0.0"
8
+ s.version = "0.0.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Daniel Higginbotham"]
@@ -30,6 +30,7 @@ Gem::Specification.new do |s|
30
30
  "lib/benchmark_harness.rb",
31
31
  "lib/benchmark_harness/collection.rb",
32
32
  "spec/benchmark_harness_spec.rb",
33
+ "spec/fixtures/nested_example.rb",
33
34
  "spec/fixtures/string_example.rb",
34
35
  "spec/spec_helper.rb"
35
36
  ]
@@ -40,6 +41,7 @@ Gem::Specification.new do |s|
40
41
  s.summary = %q{harness for running benchmark multiple times and running stats on the results}
41
42
  s.test_files = [
42
43
  "spec/benchmark_harness_spec.rb",
44
+ "spec/fixtures/nested_example.rb",
43
45
  "spec/fixtures/string_example.rb",
44
46
  "spec/spec_helper.rb"
45
47
  ]
@@ -10,7 +10,7 @@ class BenchmarkHarness
10
10
 
11
11
  class << self
12
12
  extend Forwardable
13
- def_delegators :instance, :collections, :measure, :flush!
13
+ def_delegators :instance, :collections, :measure, :flush!, :wrap_method
14
14
  end
15
15
 
16
16
  def collections
@@ -22,9 +22,56 @@ class BenchmarkHarness
22
22
  end
23
23
 
24
24
  def measure(collection_name)
25
+ result = nil
25
26
  m = Benchmark.measure{
26
- yield
27
+ result = yield
27
28
  }
28
29
  collections[collection_name] << m.to_a[1..-1]
30
+ result
31
+ end
32
+
33
+ # @param [String] signature
34
+ # @example "BenchmarkHarness#measure" instance method
35
+ # @example "BenchmarkHarness.measure" class method
36
+ def wrap_method(signature, collection_name)
37
+ if signature =~ /#/
38
+ split_on = "#"
39
+ class_method = false
40
+ elsif signature =~ /\./
41
+ split_on = "."
42
+ class_method = true
43
+ else
44
+ raise ArgumentError, "your signature must contain a method"
45
+ end
46
+
47
+ constant_name, method_name = signature.split(split_on)
48
+ constant = get_constant(constant_name)
49
+
50
+ method_definition = <<-END
51
+ alias :#{method_name}_pre_benchmark_harness :#{method_name}
52
+ def #{method_name}(*args, &block)
53
+ BenchmarkHarness.measure(:#{collection_name}){result = #{method_name}_pre_benchmark_harness(*args, &block)}
54
+ end
55
+ END
56
+
57
+ if class_method
58
+ constant = class << constant; self; end
59
+ end
60
+
61
+ constant.class_eval method_definition
62
+ end
63
+
64
+ # @param [String] name
65
+ def get_constant(name)
66
+ pieces = name.split("::")
67
+ parent_constant = Object
68
+ pieces.each do |piece|
69
+ if parent_constant.const_defined?(piece)
70
+ parent_constant = parent_constant.const_get(piece)
71
+ else
72
+ raise NameError, "#{name} contains an undefined constant"
73
+ end
74
+ end
75
+ parent_constant
29
76
  end
30
77
  end
@@ -83,6 +83,20 @@ describe "BenchmarkHarness" do
83
83
  end
84
84
  end
85
85
 
86
+ describe "#wrap" do
87
+ it "should wrap a class method with benchmark harness measure" do
88
+ BenchmarkHarness.wrap_method("NestedExample::OneLevelDown.add", :add)
89
+ NestedExample::OneLevelDown.add
90
+ BenchmarkHarness.collections[:add].should_not be_empty
91
+ end
92
+
93
+ it "should wrap an instance method with benchmark harness measure" do
94
+ BenchmarkHarness.wrap_method("NestedExample::OneLevelDown#subtract", :subtract)
95
+ NestedExample::OneLevelDown.new.subtract
96
+ BenchmarkHarness.collections[:subtract].should_not be_empty
97
+ end
98
+ end
99
+
86
100
  describe "#flush" do
87
101
  it "should remove all previous samples" do
88
102
  2.times{
@@ -0,0 +1,13 @@
1
+ module NestedExample
2
+ class OneLevelDown
3
+ class << self
4
+ def add
5
+ 1 + 1
6
+ end
7
+ end
8
+
9
+ def subtract
10
+ 1 - 1
11
+ end
12
+ end
13
+ end
data/spec/spec_helper.rb CHANGED
@@ -12,5 +12,5 @@ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
12
12
  Dir["#{File.dirname(__FILE__)}/fixtures/**/*.rb"].each {|f| require f}
13
13
 
14
14
  RSpec.configure do |config|
15
-
15
+ config.mock_with :mocha
16
16
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: benchmark_harness
3
3
  version: !ruby/object:Gem::Version
4
- hash: 31
4
+ hash: 29
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 0
10
- version: 0.0.0
9
+ - 1
10
+ version: 0.0.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - Daniel Higginbotham
@@ -145,6 +145,7 @@ files:
145
145
  - lib/benchmark_harness.rb
146
146
  - lib/benchmark_harness/collection.rb
147
147
  - spec/benchmark_harness_spec.rb
148
+ - spec/fixtures/nested_example.rb
148
149
  - spec/fixtures/string_example.rb
149
150
  - spec/spec_helper.rb
150
151
  has_rdoc: true
@@ -183,5 +184,6 @@ specification_version: 3
183
184
  summary: harness for running benchmark multiple times and running stats on the results
184
185
  test_files:
185
186
  - spec/benchmark_harness_spec.rb
187
+ - spec/fixtures/nested_example.rb
186
188
  - spec/fixtures/string_example.rb
187
189
  - spec/spec_helper.rb