ips 0.0.0 → 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 +4 -4
- data/CODE_OF_CONDUCT.md +10 -0
- data/LICENSE.txt +21 -0
- data/README.md +56 -0
- data/Rakefile +8 -0
- data/examples/basic.rb +12 -0
- data/examples/gc_pressure.rb +15 -0
- data/examples/hash_vs_struct.rb +16 -0
- data/examples/manual_loop.rb +20 -0
- data/examples/outlier.rb +35 -0
- data/examples/singleton_class.rb +14 -0
- data/examples/string_matching.rb +16 -0
- data/lib/ips/display.rb +72 -0
- data/lib/ips/job/entry.rb +40 -0
- data/lib/ips/job.rb +114 -0
- data/lib/ips/result.rb +91 -0
- data/lib/ips/timing.rb +20 -0
- data/lib/ips/version.rb +5 -0
- data/lib/ips.rb +16 -0
- metadata +28 -10
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 6b0cebe0c91bffd744baf3e8f52b8215799699ba4ca56d0856af1802dfc3df13
|
|
4
|
+
data.tar.gz: 43f27237a0abb534d951ac7da7c96fe0f8e3f053a1910a0534fcbfafaaf4dabf
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 5bbe4651a8879db139bdd2be7b551d4f5787a42cb22ab145056b92883e02acc7818144b61689f94e70f3218ea9f7566f8e337a97ef0a6ba9b280174a508cafb4
|
|
7
|
+
data.tar.gz: 3665d4bf59457dcfb246908fcc7f4d0eba583790d9171995f25dc02dad786fb6f5ab44ac4e32ee573b4775a87f4dfbbeb46e4389b1b3ad77ce2d994da5a51d3b
|
data/CODE_OF_CONDUCT.md
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
# Code of Conduct
|
|
2
|
+
|
|
3
|
+
"ips" follows [The Ruby Community Conduct Guideline](https://www.ruby-lang.org/en/conduct) in all "collaborative space", which is defined as community communications channels (such as mailing lists, submitted patches, commit comments, etc.):
|
|
4
|
+
|
|
5
|
+
* Participants will be tolerant of opposing views.
|
|
6
|
+
* Participants must ensure that their language and actions are free of personal attacks and disparaging personal remarks.
|
|
7
|
+
* When interpreting the words and actions of others, participants should always assume good intentions.
|
|
8
|
+
* Behaviour which can be reasonably considered harassment will not be tolerated.
|
|
9
|
+
|
|
10
|
+
If you have any concerns about behaviour within this project, please contact us at ["john@hawthorn.email"](mailto:"john@hawthorn.email").
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 John Hawthorn
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
# IPS
|
|
2
|
+
|
|
3
|
+
A Ruby benchmarking tool that measures iterations per second with statistical analysis, automatic warmup, and comparison summaries.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
Install the gem and add to the application's Gemfile by executing:
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
bundle add ips
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
gem install ips
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Usage
|
|
20
|
+
|
|
21
|
+
```ruby
|
|
22
|
+
require "ips"
|
|
23
|
+
|
|
24
|
+
IPS.run do |x|
|
|
25
|
+
x.report("addition") { 1 + 1 }
|
|
26
|
+
x.report("multiplication") { 2 * 3 }
|
|
27
|
+
x.report("wtf") { eval("#{1.to_s} + #{1.to_s}") }
|
|
28
|
+
end
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
Output:
|
|
32
|
+
|
|
33
|
+
```
|
|
34
|
+
addition: 45.759M i/s (± 1.2%)
|
|
35
|
+
multiplication: 45.593M i/s (± 0.2%)
|
|
36
|
+
wtf: 463.420k i/s (± 0.9%, GC 6.3%)
|
|
37
|
+
|
|
38
|
+
Summary
|
|
39
|
+
addition ran
|
|
40
|
+
1.00 ± 0.01 times faster than multiplication
|
|
41
|
+
98.74 ± 1.49 times faster than wtf
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
When two or more items are reported, a comparison summary is automatically displayed.
|
|
45
|
+
|
|
46
|
+
## Contributing
|
|
47
|
+
|
|
48
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/jhawthorn/ips. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/jhawthorn/ips/blob/main/CODE_OF_CONDUCT.md).
|
|
49
|
+
|
|
50
|
+
## License
|
|
51
|
+
|
|
52
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
|
53
|
+
|
|
54
|
+
## Code of Conduct
|
|
55
|
+
|
|
56
|
+
Everyone interacting in the IPS project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/jhawthorn/ips/blob/main/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/examples/basic.rb
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# Comparing allocation-heavy vs allocation-free approaches
|
|
2
|
+
#
|
|
3
|
+
# Usage:
|
|
4
|
+
# ruby -I lib examples/gc_pressure.rb
|
|
5
|
+
|
|
6
|
+
require "bundler/setup"
|
|
7
|
+
require "ips"
|
|
8
|
+
|
|
9
|
+
array = (1..1000).to_a
|
|
10
|
+
|
|
11
|
+
IPS.run do |x|
|
|
12
|
+
x.report("map + sum") { array.map { |i| i * 2 }.sum }
|
|
13
|
+
x.report("sum block") { array.sum { |i| i * 2 } }
|
|
14
|
+
x.report("inject") { array.inject(0) { |acc, i| acc + i * 2 } }
|
|
15
|
+
end
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# Hash vs Struct vs Data for simple records
|
|
2
|
+
#
|
|
3
|
+
# Usage:
|
|
4
|
+
# ruby -I lib examples/hash_vs_struct.rb
|
|
5
|
+
|
|
6
|
+
require "bundler/setup"
|
|
7
|
+
require "ips"
|
|
8
|
+
|
|
9
|
+
MyStruct = Struct.new(:name, :age)
|
|
10
|
+
MyData = Data.define(:name, :age)
|
|
11
|
+
|
|
12
|
+
IPS.run do |x|
|
|
13
|
+
x.report("Hash.new") { { name: "Alice", age: 30 } }
|
|
14
|
+
x.report("Struct.new") { MyStruct.new("Alice", 30) }
|
|
15
|
+
x.report("Data.new") { MyData.new(name: "Alice", age: 30) }
|
|
16
|
+
end
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# Using the manual loop style for ultra-fast operations
|
|
2
|
+
# where per-call overhead matters
|
|
3
|
+
#
|
|
4
|
+
# Usage:
|
|
5
|
+
# ruby -I lib examples/manual_loop.rb
|
|
6
|
+
|
|
7
|
+
require "bundler/setup"
|
|
8
|
+
require "ips"
|
|
9
|
+
|
|
10
|
+
IPS.run do |x|
|
|
11
|
+
x.report("block style") { 1 + 1 }
|
|
12
|
+
|
|
13
|
+
x.report("manual loop") { |n|
|
|
14
|
+
i = 0
|
|
15
|
+
while i < n
|
|
16
|
+
1 + 1
|
|
17
|
+
i += 1
|
|
18
|
+
end
|
|
19
|
+
}
|
|
20
|
+
end
|
data/examples/outlier.rb
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# Demonstrates how benchmark-ips hides rare stalls.
|
|
2
|
+
#
|
|
3
|
+
# Every 100th iteration sleeps for 1 second.
|
|
4
|
+
# Real throughput: ~100 i/s
|
|
5
|
+
# benchmark-ips reports: ~5.9M i/s (!!!)
|
|
6
|
+
#
|
|
7
|
+
# Usage:
|
|
8
|
+
# ruby -I lib examples/outlier.rb
|
|
9
|
+
|
|
10
|
+
require "benchmark/ips"
|
|
11
|
+
require "bundler/setup"
|
|
12
|
+
require "ips"
|
|
13
|
+
|
|
14
|
+
i = 0
|
|
15
|
+
j = 0
|
|
16
|
+
|
|
17
|
+
if false
|
|
18
|
+
puts "=== benchmark-ips ==="
|
|
19
|
+
Benchmark.ips do |x|
|
|
20
|
+
x.report("periodic stall") do
|
|
21
|
+
sleep 1 if i % 100 == 0
|
|
22
|
+
i += 1
|
|
23
|
+
end
|
|
24
|
+
x.compare!
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
puts
|
|
29
|
+
puts "=== IPS ==="
|
|
30
|
+
IPS.run do |x|
|
|
31
|
+
x.report("periodic stall") do
|
|
32
|
+
sleep 1 if j % 100 == 0
|
|
33
|
+
j += 1
|
|
34
|
+
end
|
|
35
|
+
end
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
# Singleton class creation benchmark
|
|
2
|
+
# From https://bugs.ruby-lang.org/issues/21856
|
|
3
|
+
#
|
|
4
|
+
# Usage:
|
|
5
|
+
# ruby -I lib examples/singleton_class.rb
|
|
6
|
+
|
|
7
|
+
require "bundler/setup"
|
|
8
|
+
require "ips"
|
|
9
|
+
|
|
10
|
+
IPS.run do |x|
|
|
11
|
+
x.report("singleton_class") do
|
|
12
|
+
Object.new.singleton_class
|
|
13
|
+
end
|
|
14
|
+
end
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# Comparing different ways to check string contents
|
|
2
|
+
#
|
|
3
|
+
# Usage:
|
|
4
|
+
# ruby -I lib examples/string_matching.rb
|
|
5
|
+
|
|
6
|
+
require "bundler/setup"
|
|
7
|
+
require "ips"
|
|
8
|
+
|
|
9
|
+
string = "the quick brown fox jumps over the lazy dog"
|
|
10
|
+
|
|
11
|
+
IPS.run do |x|
|
|
12
|
+
x.report("include?") { string.include?("fox") }
|
|
13
|
+
x.report("match?") { string.match?(/fox/) }
|
|
14
|
+
x.report("index") { string.index("fox") }
|
|
15
|
+
x.report("=~") { string =~ /fox/ }
|
|
16
|
+
end
|
data/lib/ips/display.rb
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module IPS
|
|
4
|
+
class Display
|
|
5
|
+
BAR_WIDTH = 30
|
|
6
|
+
|
|
7
|
+
def initialize(labels, out: $stdout)
|
|
8
|
+
@out = out
|
|
9
|
+
@max_label = labels.map(&:size).max
|
|
10
|
+
@max_label = 20 if @max_label < 20
|
|
11
|
+
@tty = @out.tty?
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def start_item(label, total_ns)
|
|
15
|
+
@item_label = label
|
|
16
|
+
@item_start = Timing.now
|
|
17
|
+
@item_total_ns = total_ns
|
|
18
|
+
progress
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def progress(estimate: nil)
|
|
22
|
+
return unless @tty
|
|
23
|
+
elapsed_ns = Timing.now - @item_start
|
|
24
|
+
fraction = (elapsed_ns.to_f / @item_total_ns).clamp(0.0, 1.0)
|
|
25
|
+
filled = (fraction * BAR_WIDTH).to_i
|
|
26
|
+
empty = BAR_WIDTH - filled
|
|
27
|
+
remaining_ns = @item_total_ns - elapsed_ns
|
|
28
|
+
remaining_s = remaining_ns > 0 ? (remaining_ns.to_f / Timing::NANOSECONDS_PER_SECOND).ceil : 0
|
|
29
|
+
bar = "█" * filled + "░" * empty
|
|
30
|
+
est = estimate ? "%10s i/s" % format_ips(estimate) : " "
|
|
31
|
+
@out.print "\r%#{@max_label}s: %s %s ETA %ds " % [@item_label, est, bar, remaining_s]
|
|
32
|
+
@out.flush
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def finish_item(result)
|
|
36
|
+
@out.print "\r\e[2K" if @tty
|
|
37
|
+
gc = result.gc_pct >= 1.0 ? ", GC %4.1f%%" % result.gc_pct : ""
|
|
38
|
+
error = result.error_pct > 100 ? ">100" : "%4.1f" % result.error_pct
|
|
39
|
+
@out.printf "%#{@max_label}s: %10s i/s (±%s%%%s)\n",
|
|
40
|
+
result.label, format_ips(result.ips), error, gc
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def summary(results)
|
|
44
|
+
return if results.size < 2
|
|
45
|
+
|
|
46
|
+
sorted = results.sort_by { |r| -r.ips }
|
|
47
|
+
best = sorted.first
|
|
48
|
+
|
|
49
|
+
@out.puts "\nSummary"
|
|
50
|
+
@out.puts " #{best.label} ran"
|
|
51
|
+
|
|
52
|
+
sorted[1..].each do |r|
|
|
53
|
+
ratio = best.ips / r.ips
|
|
54
|
+
ratio_error = ratio * Math.sqrt((best.stddev / best.ips)**2 + (r.stddev / r.ips)**2)
|
|
55
|
+
@out.printf " %.2f ± %.2f times faster than %s\n",
|
|
56
|
+
ratio, ratio_error, r.label
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def format_ips(ips)
|
|
61
|
+
if ips >= 1_000_000_000
|
|
62
|
+
"%.3fB" % (ips / 1_000_000_000.0)
|
|
63
|
+
elsif ips >= 1_000_000
|
|
64
|
+
"%.3fM" % (ips / 1_000_000.0)
|
|
65
|
+
elsif ips >= 1_000
|
|
66
|
+
"%.3fk" % (ips / 1_000.0)
|
|
67
|
+
else
|
|
68
|
+
"%.3f" % ips
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
end
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module IPS
|
|
4
|
+
class Job
|
|
5
|
+
class Entry
|
|
6
|
+
attr_reader :label
|
|
7
|
+
|
|
8
|
+
def initialize(label, action)
|
|
9
|
+
@label = label
|
|
10
|
+
@action = action
|
|
11
|
+
|
|
12
|
+
if action.arity > 0
|
|
13
|
+
define_call_times_manual_loop
|
|
14
|
+
else
|
|
15
|
+
define_call_times_block
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
private
|
|
20
|
+
|
|
21
|
+
def define_call_times_block
|
|
22
|
+
act = @action
|
|
23
|
+
define_singleton_method(:call_times) do |times|
|
|
24
|
+
i = 0
|
|
25
|
+
while i < times
|
|
26
|
+
act.call
|
|
27
|
+
i += 1
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def define_call_times_manual_loop
|
|
33
|
+
act = @action
|
|
34
|
+
define_singleton_method(:call_times) do |times|
|
|
35
|
+
act.call(times)
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
end
|
data/lib/ips/job.rb
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "ips/job/entry"
|
|
4
|
+
require "ips/result"
|
|
5
|
+
require "ips/display"
|
|
6
|
+
|
|
7
|
+
module IPS
|
|
8
|
+
class Job
|
|
9
|
+
MAX_ITERATIONS = 1 << 30
|
|
10
|
+
|
|
11
|
+
attr_accessor :warmup, :time
|
|
12
|
+
|
|
13
|
+
def initialize(time: 5, warmup: 2, out: $stdout)
|
|
14
|
+
@list = []
|
|
15
|
+
@time = time
|
|
16
|
+
@warmup = warmup
|
|
17
|
+
@timing = {}
|
|
18
|
+
@out = out
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def report(label, &block)
|
|
22
|
+
@list.push Entry.new(label, block)
|
|
23
|
+
self
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def run
|
|
27
|
+
display = Display.new(@list.map(&:label), out: @out)
|
|
28
|
+
total_ns = ((@warmup + @time) * Timing::NANOSECONDS_PER_SECOND).to_i
|
|
29
|
+
|
|
30
|
+
results = @list.map do |item|
|
|
31
|
+
display.start_item(item.label, total_ns)
|
|
32
|
+
warmup_item(item, display)
|
|
33
|
+
result = measure_item(item, display)
|
|
34
|
+
display.finish_item(result)
|
|
35
|
+
result
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
display.summary(results)
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
private
|
|
42
|
+
|
|
43
|
+
def cycles_per_100ms(time_ns, iters)
|
|
44
|
+
cycles = ((Timing::NANOSECONDS_PER_100MS.to_f / time_ns) * iters).to_i
|
|
45
|
+
cycles <= 0 ? 1 : cycles
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def warmup_item(item, display)
|
|
49
|
+
Timing.clean_env
|
|
50
|
+
|
|
51
|
+
before = Timing.now
|
|
52
|
+
target = Timing.add_second(before, @warmup / 2.0)
|
|
53
|
+
|
|
54
|
+
cycles = 1
|
|
55
|
+
begin
|
|
56
|
+
t0 = Timing.now
|
|
57
|
+
item.call_times(cycles)
|
|
58
|
+
t1 = Timing.now
|
|
59
|
+
warmup_iter = cycles
|
|
60
|
+
warmup_ns = t1 - t0
|
|
61
|
+
|
|
62
|
+
estimate = Timing::NANOSECONDS_PER_SECOND * (warmup_iter.to_f / warmup_ns)
|
|
63
|
+
display.progress(estimate: estimate)
|
|
64
|
+
|
|
65
|
+
break if cycles >= MAX_ITERATIONS
|
|
66
|
+
cycles *= 2
|
|
67
|
+
end while Timing.now + warmup_ns * 2 < target
|
|
68
|
+
|
|
69
|
+
per_100ms = cycles_per_100ms(warmup_ns, warmup_iter)
|
|
70
|
+
cycles = per_100ms > MAX_ITERATIONS ? MAX_ITERATIONS : per_100ms
|
|
71
|
+
@timing[item] = cycles
|
|
72
|
+
|
|
73
|
+
target = Timing.add_second(before, @warmup)
|
|
74
|
+
while Timing.now + Timing::NANOSECONDS_PER_100MS < target
|
|
75
|
+
t0 = Timing.now
|
|
76
|
+
item.call_times(cycles)
|
|
77
|
+
t1 = Timing.now
|
|
78
|
+
estimate = Timing::NANOSECONDS_PER_SECOND * (cycles.to_f / (t1 - t0))
|
|
79
|
+
display.progress(estimate: estimate)
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def measure_item(item, display)
|
|
84
|
+
Timing.clean_env
|
|
85
|
+
|
|
86
|
+
cycles = @timing[item]
|
|
87
|
+
times = [] # flat: [start0, end0, start1, end1, ...]
|
|
88
|
+
gc_times = [] # flat: [gc_before0, gc_after0, ...]
|
|
89
|
+
iter = 0
|
|
90
|
+
|
|
91
|
+
target = Timing.add_second(Timing.now, @time)
|
|
92
|
+
|
|
93
|
+
begin
|
|
94
|
+
gc0 = GC.total_time
|
|
95
|
+
t0 = Timing.now
|
|
96
|
+
item.call_times(cycles)
|
|
97
|
+
t1 = Timing.now
|
|
98
|
+
gc1 = GC.total_time
|
|
99
|
+
|
|
100
|
+
elapsed_ns = t1 - t0
|
|
101
|
+
next if elapsed_ns <= 0
|
|
102
|
+
|
|
103
|
+
iter += cycles
|
|
104
|
+
times << t0 << t1
|
|
105
|
+
gc_times << gc0 << gc1
|
|
106
|
+
|
|
107
|
+
total_ns = t1 - times[0]
|
|
108
|
+
display.progress(estimate: Timing::NANOSECONDS_PER_SECOND * (iter.to_f / total_ns))
|
|
109
|
+
end while Timing.now < target
|
|
110
|
+
|
|
111
|
+
Result.new(item.label, cycles, times, gc_times)
|
|
112
|
+
end
|
|
113
|
+
end
|
|
114
|
+
end
|
data/lib/ips/result.rb
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module IPS
|
|
4
|
+
class Result
|
|
5
|
+
attr_reader :label, :cycles, :times, :gc_times
|
|
6
|
+
|
|
7
|
+
# times: flat array [start0, end0, start1, end1, ...]
|
|
8
|
+
# gc_times: flat array [gc_before0, gc_after0, gc_before1, gc_after1, ...]
|
|
9
|
+
def initialize(label, cycles, times, gc_times)
|
|
10
|
+
@label = label
|
|
11
|
+
@cycles = cycles
|
|
12
|
+
@times = times
|
|
13
|
+
@gc_times = gc_times
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def sample_count
|
|
17
|
+
@times.size / 2
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def iterations
|
|
21
|
+
sample_count * @cycles
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
# Total wall time from first batch start to last batch end
|
|
25
|
+
def total_ns
|
|
26
|
+
@times[-1] - @times[0]
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
# Time spent actually running batches
|
|
30
|
+
def busy_ns
|
|
31
|
+
total = 0
|
|
32
|
+
i = 0
|
|
33
|
+
while i < @times.size
|
|
34
|
+
total += @times[i + 1] - @times[i]
|
|
35
|
+
i += 2
|
|
36
|
+
end
|
|
37
|
+
total
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
# Time between batches (measurement overhead, GC, scheduling)
|
|
41
|
+
def overhead_ns
|
|
42
|
+
total_ns - busy_ns
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
# Total GC time during measurement
|
|
46
|
+
def gc_ns
|
|
47
|
+
total = 0
|
|
48
|
+
i = 0
|
|
49
|
+
while i < @gc_times.size
|
|
50
|
+
total += @gc_times[i + 1] - @gc_times[i]
|
|
51
|
+
i += 2
|
|
52
|
+
end
|
|
53
|
+
total
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def ips
|
|
57
|
+
Timing::NANOSECONDS_PER_SECOND * (iterations.to_f / total_ns)
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
# Per-batch IPS samples for error estimation
|
|
61
|
+
def samples
|
|
62
|
+
@samples ||= begin
|
|
63
|
+
s = Array.new(sample_count)
|
|
64
|
+
i = 0
|
|
65
|
+
while i < @times.size
|
|
66
|
+
s[i / 2] = Timing::NANOSECONDS_PER_SECOND * (@cycles.to_f / (@times[i + 1] - @times[i]))
|
|
67
|
+
i += 2
|
|
68
|
+
end
|
|
69
|
+
s
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def sample_mean
|
|
74
|
+
@sample_mean ||= samples.sum / samples.size
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
def stddev
|
|
78
|
+
variance = samples.sum { |s| (s - sample_mean) ** 2 } / samples.size
|
|
79
|
+
Math.sqrt(variance)
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def error_pct
|
|
83
|
+
(stddev / ips) * 100.0
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
def gc_pct
|
|
87
|
+
total = total_ns
|
|
88
|
+
total > 0 ? (gc_ns.to_f / total) * 100.0 : 0.0
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
end
|
data/lib/ips/timing.rb
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module IPS
|
|
4
|
+
module Timing
|
|
5
|
+
NANOSECONDS_PER_SECOND = 1_000_000_000
|
|
6
|
+
NANOSECONDS_PER_100MS = 100_000_000
|
|
7
|
+
|
|
8
|
+
def self.now
|
|
9
|
+
Process.clock_gettime(Process::CLOCK_MONOTONIC, :nanosecond)
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def self.add_second(t, s)
|
|
13
|
+
t + (s * NANOSECONDS_PER_SECOND).to_i
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def self.clean_env
|
|
17
|
+
GC.start
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
data/lib/ips/version.rb
ADDED
data/lib/ips.rb
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "ips/version"
|
|
4
|
+
require "ips/timing"
|
|
5
|
+
require "ips/display"
|
|
6
|
+
require "ips/job"
|
|
7
|
+
|
|
8
|
+
module IPS
|
|
9
|
+
def self.run(time: 5, warmup: 2, out: $stdout)
|
|
10
|
+
job = Job.new(time: time, warmup: warmup, out: out)
|
|
11
|
+
yield job
|
|
12
|
+
job.run
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
Ips = IPS
|
metadata
CHANGED
|
@@ -1,28 +1,46 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: ips
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.1.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- John Hawthorn
|
|
8
|
-
bindir:
|
|
8
|
+
bindir: exe
|
|
9
9
|
cert_chain: []
|
|
10
10
|
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
11
|
dependencies: []
|
|
12
|
-
description:
|
|
12
|
+
description: A Ruby benchmarking tool that measures iterations per second with automatic
|
|
13
|
+
warmup, statistical error estimation, GC reporting, and comparison summaries.
|
|
13
14
|
email:
|
|
14
15
|
- john@hawthorn.email
|
|
15
16
|
executables: []
|
|
16
17
|
extensions: []
|
|
17
18
|
extra_rdoc_files: []
|
|
18
|
-
files:
|
|
19
|
-
|
|
19
|
+
files:
|
|
20
|
+
- CODE_OF_CONDUCT.md
|
|
21
|
+
- LICENSE.txt
|
|
22
|
+
- README.md
|
|
23
|
+
- Rakefile
|
|
24
|
+
- examples/basic.rb
|
|
25
|
+
- examples/gc_pressure.rb
|
|
26
|
+
- examples/hash_vs_struct.rb
|
|
27
|
+
- examples/manual_loop.rb
|
|
28
|
+
- examples/outlier.rb
|
|
29
|
+
- examples/singleton_class.rb
|
|
30
|
+
- examples/string_matching.rb
|
|
31
|
+
- lib/ips.rb
|
|
32
|
+
- lib/ips/display.rb
|
|
33
|
+
- lib/ips/job.rb
|
|
34
|
+
- lib/ips/job/entry.rb
|
|
35
|
+
- lib/ips/result.rb
|
|
36
|
+
- lib/ips/timing.rb
|
|
37
|
+
- lib/ips/version.rb
|
|
38
|
+
homepage: https://github.com/jhawthorn/ips
|
|
20
39
|
licenses:
|
|
21
40
|
- MIT
|
|
22
41
|
metadata:
|
|
23
|
-
homepage_uri: https://
|
|
24
|
-
source_code_uri: https://
|
|
25
|
-
changelog_uri: https://jhawthorn.com
|
|
42
|
+
homepage_uri: https://github.com/jhawthorn/ips
|
|
43
|
+
source_code_uri: https://github.com/jhawthorn/ips
|
|
26
44
|
rdoc_options: []
|
|
27
45
|
require_paths:
|
|
28
46
|
- lib
|
|
@@ -30,7 +48,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
30
48
|
requirements:
|
|
31
49
|
- - ">="
|
|
32
50
|
- !ruby/object:Gem::Version
|
|
33
|
-
version: 3.
|
|
51
|
+
version: 3.2.0
|
|
34
52
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
35
53
|
requirements:
|
|
36
54
|
- - ">="
|
|
@@ -39,5 +57,5 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
39
57
|
requirements: []
|
|
40
58
|
rubygems_version: 4.0.3
|
|
41
59
|
specification_version: 4
|
|
42
|
-
summary:
|
|
60
|
+
summary: Iterations per second benchmarking with statistical analysis
|
|
43
61
|
test_files: []
|