micro_bench 0.1.1 → 0.1.2

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: 4b772118be3419e0878ddf0f0cec08a83c67eefb
4
- data.tar.gz: 1e08246f863d68db6434eeeb60ffd6bd8e04bc61
3
+ metadata.gz: a8c91db76af4392989e7cef3b27ab9bd4b421c77
4
+ data.tar.gz: 609656197b6121fab78832159497579b63c0b877
5
5
  SHA512:
6
- metadata.gz: cc8cc8b61b6324d3fbb5a0e6d430b1dfbe057f7e23d802b4bf1094cb74795e49cf59461a9ed9431c96a17001d22383b8789079e2238c687e46437058f0914615
7
- data.tar.gz: 1f6c2876766fec38271ef0245a9e9af48ee5fe5585e73435b2259dd70a0a8b752dfe439e7b28fcc6d560326bbe9337281f57825b89b547288f85b07dd5a6f9ca
6
+ metadata.gz: 295ec1527e81d7c5954dd949cb084b65c3c634bccacaed32a2ca16b8e86e5d4d1bb1ec6f759a523b201a4023d0b0e1d54d69e09c44f9a999240fece436b90306
7
+ data.tar.gz: d7d06b06eb69cc307900a9a3e6833815d61d7fa694b7830456d9d079560b3f13ec8395c30e0f041debfebd939417aad28adbbf330504e462f333dfd13ac4c570
data/README.md CHANGED
@@ -81,13 +81,36 @@ MicroBench.duration("timer1")
81
81
  # 1.628093000501394
82
82
  ```
83
83
 
84
+ ### Multiple starts
85
+
86
+ Calling `.start` multiple times with the same `bench_id` (or none) will cause a "restart" of the given benchmark.
87
+
84
88
  ### Thread safety
85
89
 
86
90
  A benchmark is tied to a thread, ensuring that `MicroBench` is thread-safe. At the same time, it doesn't allow to share a benchmark between multiple threads.
87
91
 
88
- ### Multiple starts
92
+ ### Methods isolation
93
+
94
+ A benchmark is tied to its calling method so the following code will output 2 separated durations for `method_1` and `method_2`. This prevent collisions when using `MicroBench` in a large codebase.
89
95
 
90
- Calling `.start` multiple times with the same `bench_id` will cause a "restart" of the given benchmark.
96
+ ```ruby
97
+ def method_1
98
+ MicroBench.start
99
+ method_2
100
+ # Do something
101
+ MicroBench.stop
102
+ puts "method_1 duration : #{MicroBench.duration}"
103
+ end
104
+
105
+ def method_2
106
+ MicroBench.start
107
+ # Do something
108
+ MicroBench.stop
109
+ puts "method_2 duration : #{MicroBench.duration}"
110
+ end
111
+
112
+ method_1
113
+ ```
91
114
 
92
115
  ## Versioning
93
116
 
@@ -1,3 +1,3 @@
1
1
  module MicroBench
2
- VERSION = "0.1.1".freeze
2
+ VERSION = "0.1.2".freeze
3
3
  end
data/lib/micro_bench.rb CHANGED
@@ -62,7 +62,18 @@ module MicroBench
62
62
  end
63
63
 
64
64
  def benchmark_key(bench_id = nil)
65
- "#{Thread.current.object_id}||#{bench_id}"
65
+ "#{thread_key}||#{caller_key}||#{bench_id}"
66
+ end
67
+
68
+ def thread_key
69
+ Thread.current.object_id
70
+ end
71
+
72
+ def caller_key
73
+ caller_location = caller_locations(2..4).detect do |loc|
74
+ !loc.absolute_path.include?(__FILE__)
75
+ end
76
+ "#{caller_location.absolute_path}:#{caller_location.label}"
66
77
  end
67
78
  end
68
79
  end
@@ -59,9 +59,35 @@ describe MicroBench do
59
59
  Thread.current[:expected] = 0.01 * 2
60
60
  Thread.current[:duration] = described_class.duration
61
61
  end
62
+
62
63
  [t1, t2].each do |t|
63
64
  t.join
64
65
  expect(t[:duration].round(2)).to eq(t[:expected])
65
66
  end
66
67
  end
68
+
69
+ it "prevents cross-method collisions" do
70
+ test_klass = Class.new do
71
+ attr_accessor :method_1_duration
72
+ attr_accessor :method_2_duration
73
+ def method_1
74
+ MicroBench.start
75
+ method_2
76
+ sleep(0.01)
77
+ MicroBench.stop
78
+ @method_1_duration = MicroBench.duration
79
+ end
80
+ def method_2
81
+ MicroBench.start
82
+ sleep(0.01)
83
+ MicroBench.stop
84
+ @method_2_duration = MicroBench.duration
85
+ end
86
+ end
87
+
88
+ subject = test_klass.new
89
+ subject.method_1
90
+ expect(subject.method_1_duration).to_not eq(subject.method_2_duration)
91
+ end
92
+
67
93
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: micro_bench
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Cyrille Courtière
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-11-29 00:00:00.000000000 Z
11
+ date: 2018-12-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: byebug