benchmark-ips 2.10.0 → 2.12.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
  SHA256:
3
- metadata.gz: 0516f3e1e35f43a8fe32a6f32357aa124478ba54228ffe57d928cfc1231851bc
4
- data.tar.gz: d62da6a6c1d2696b7667bd125ffe6bb2c33b3d61da5c393e729d7ca8a3d70ee7
3
+ metadata.gz: f5c89b6607d6960de72e63e6e14ee0283f2dbe3f2418f33d90b57e947f555d70
4
+ data.tar.gz: 5ee11f5ae33b1c426a90deba5799a739cd13d785d5ab7c4e4ffaa942d5ddc8db
5
5
  SHA512:
6
- metadata.gz: b644d293980f1ac9e3158abbe4f2c9d44d23185878edb9cb3861f3ca4f6837d557f89f8eb19bfcc42a45f53f3d112f2fe3ab22fc85a9633ede7e678b5fb90336
7
- data.tar.gz: afe9972cdecdb2f42d4946afbac3d02cb3ead3a3d47a804960026ea77487cf885f6ddd7368edb13c522ee11e251599bf953d3d9a8172411c10e7259f7e9352b2
6
+ metadata.gz: 52e1d308de07129514ff4c6c9848a0e68f074937ab2cd05b75060ac055655bc581f382041ddd21780439f682540fdb3ee5d041214a677dc87cd8a527b587bfe8
7
+ data.tar.gz: fbb5b4045f318f31772eb3e27cc6f49c1589d857d13c17d7e16c52d82be5253aef30b7c327f8f76f9e14cc3c81bd496423f1f043e8cd7287074a31466bd49b07
data/History.md CHANGED
@@ -1,3 +1,13 @@
1
+ ### 2.12.0 / 2023-03-08
2
+
3
+ * Feature
4
+ * Adds MultiReport and ability report to a stream rather than a string.
5
+
6
+ ### 2.11.0 / 2023-02-15
7
+
8
+ * Feature
9
+ * Adds .json! method to the ips block argument, allowing you to print the output as JSON to a file or STDOUT.
10
+
1
11
  ### 2.10.0 / 2022-02-17
2
12
 
3
13
  * Feature
data/README.md CHANGED
@@ -237,6 +237,29 @@ Benchmark.ips do |x|
237
237
  end
238
238
  ```
239
239
 
240
+ ### Output as JSON
241
+
242
+ You can generate output in JSON. If you want to write JSON to a file, pass filename to `json!` method:
243
+
244
+ ```ruby
245
+ Benchmark.ips do |x|
246
+ x.report("some report") { }
247
+ x.json! 'filename.json'
248
+ end
249
+ ```
250
+
251
+ If you want to write JSON to STDOUT, pass `STDOUT` to `json!` method and set `quiet = true` before `json!`:
252
+
253
+ ```ruby
254
+ Benchmark.ips do |x|
255
+ x.report("some report") { }
256
+ x.quiet = true
257
+ x.json! STDOUT
258
+ end
259
+ ```
260
+
261
+ This is useful when the output from `benchmark-ips` becomes an input of other tools via stdin.
262
+
240
263
  ## REQUIREMENTS:
241
264
 
242
265
  * None!
@@ -0,0 +1,67 @@
1
+ module Benchmark
2
+ module IPS
3
+ class Job
4
+ class MultiReport
5
+ # @returns out [Array<StreamReport>] list of reports to send output
6
+ attr_accessor :out
7
+
8
+ def empty?
9
+ @out.empty?
10
+ end
11
+
12
+ def quiet?
13
+ @out.none? { |rpt| rpt.kind_of?(StreamReport) }
14
+ end
15
+
16
+ def quiet!
17
+ @out.delete_if { |rpt| rpt.kind_of?(StreamReport) }
18
+ end
19
+
20
+ # @param report [StreamReport] report to accept input?
21
+ def <<(report)
22
+ if report.kind_of?(MultiReport)
23
+ self << report.out
24
+ elsif report.kind_of?(Enumerable)
25
+ @out += report
26
+ elsif report
27
+ @out << report
28
+ end
29
+ end
30
+
31
+ # @param out [Array<StreamReport>] list of reports to send output
32
+ def initialize(out = nil)
33
+ @out = []
34
+ self << out
35
+ end
36
+
37
+ def start_warming
38
+ @out.each { |o| o.start_warming if o.respond_to?(:start_warming) }
39
+ end
40
+
41
+ def warming(label, warmup)
42
+ @out.each { |o| o.warming(label, warmup) }
43
+ end
44
+
45
+ def warmup_stats(warmup_time_us, timing)
46
+ @out.each { |o| o.warmup_stats(warmup_time_us, timing) }
47
+ end
48
+
49
+ def start_running
50
+ @out.each { |o| o.start_running if o.respond_to?(:start_running) }
51
+ end
52
+
53
+ def running(label, warmup)
54
+ @out.each { |o| o.running(label, warmup) }
55
+ end
56
+
57
+ def add_report(item, caller)
58
+ @out.each { |o| o.add_report(item, caller) }
59
+ end
60
+
61
+ def footer
62
+ @out.each { |o| o.footer if o.respond_to?(:footer) }
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end
@@ -1,43 +1,44 @@
1
1
  module Benchmark
2
2
  module IPS
3
3
  class Job
4
- class StdoutReport
5
- def initialize
4
+ class StreamReport
5
+ def initialize(stream = $stdout)
6
6
  @last_item = nil
7
+ @out = stream
7
8
  end
8
9
 
9
10
  def start_warming
10
- $stdout.puts "Warming up --------------------------------------"
11
+ @out.puts "Warming up --------------------------------------"
11
12
  end
12
13
 
13
14
  def start_running
14
- $stdout.puts "Calculating -------------------------------------"
15
+ @out.puts "Calculating -------------------------------------"
15
16
  end
16
17
 
17
18
  def warming(label, _warmup)
18
- $stdout.print rjust(label)
19
+ @out.print rjust(label)
19
20
  end
20
21
 
21
22
  def warmup_stats(_warmup_time_us, timing)
22
23
  case format
23
24
  when :human
24
- $stdout.printf "%s i/100ms\n", Helpers.scale(timing)
25
+ @out.printf "%s i/100ms\n", Helpers.scale(timing)
25
26
  else
26
- $stdout.printf "%10d i/100ms\n", timing
27
+ @out.printf "%10d i/100ms\n", timing
27
28
  end
28
29
  end
29
30
 
30
31
  alias_method :running, :warming
31
32
 
32
33
  def add_report(item, caller)
33
- $stdout.puts " #{item.body}"
34
+ @out.puts " #{item.body}"
34
35
  @last_item = item
35
36
  end
36
37
 
37
38
  def footer
38
39
  return unless @last_item
39
40
  footer = @last_item.stats.footer
40
- $stdout.puts footer.rjust(40) if footer
41
+ @out.puts footer.rjust(40) if footer
41
42
  end
42
43
 
43
44
  private
@@ -53,11 +53,15 @@ module Benchmark
53
53
 
54
54
  # Silence output
55
55
  # @return [Boolean]
56
- attr_reader :quiet
56
+ def quiet
57
+ @out.quiet?
58
+ end
57
59
 
58
60
  # Suite
59
- # @return [Benchmark::IPS::NoopSuite]
60
- attr_reader :suite
61
+ # @return [Benchmark::IPS::MultiReport]
62
+ def suite
63
+ @out
64
+ end
61
65
 
62
66
  # Instantiate the Benchmark::IPS::Job.
63
67
  def initialize opts={}
@@ -81,7 +85,7 @@ module Benchmark
81
85
  @stats = :sd
82
86
  @confidence = 95
83
87
 
84
- self.quiet = false
88
+ @out = MultiReport.new(StreamReport.new)
85
89
  end
86
90
 
87
91
  # Job configuration options, set +@warmup+ and +@time+.
@@ -91,24 +95,23 @@ module Benchmark
91
95
  def config opts
92
96
  @warmup = opts[:warmup] if opts[:warmup]
93
97
  @time = opts[:time] if opts[:time]
94
- @suite = opts[:suite] if opts[:suite]
95
98
  @iterations = opts[:iterations] if opts[:iterations]
96
99
  @stats = opts[:stats] if opts[:stats]
97
100
  @confidence = opts[:confidence] if opts[:confidence]
98
101
  self.quiet = opts[:quiet] if opts.key?(:quiet)
99
- self.suite = opts[:suite]
102
+ self.suite = opts[:suite] if opts[:suite]
100
103
  end
101
104
 
102
105
  def quiet=(val)
103
- @stdout = reporter(quiet: val)
106
+ if val # remove instances of StreamReport
107
+ @out.quiet!
108
+ else # ensure there is an instance of StreamReport
109
+ @out << StreamReport.new if @out.quiet?
110
+ end
104
111
  end
105
112
 
106
113
  def suite=(suite)
107
- @suite = suite || Benchmark::IPS::NoopSuite.new
108
- end
109
-
110
- def reporter(quiet:)
111
- quiet ? NoopReport.new : StdoutReport.new
114
+ @out << suite
112
115
  end
113
116
 
114
117
  # Return true if job needs to be compared.
@@ -245,19 +248,19 @@ module Benchmark
245
248
 
246
249
  def run
247
250
  if @warmup && @warmup != 0 then
248
- @stdout.start_warming
251
+ @out.start_warming
249
252
  @iterations.times do
250
253
  run_warmup
251
254
  end
252
255
  end
253
256
 
254
- @stdout.start_running
257
+ @out.start_running
255
258
 
256
259
  @iterations.times do |n|
257
260
  run_benchmark
258
261
  end
259
262
 
260
- @stdout.footer
263
+ @out.footer
261
264
  end
262
265
 
263
266
  # Run warmup.
@@ -265,8 +268,7 @@ module Benchmark
265
268
  @list.each do |item|
266
269
  next if run_single? && @held_results && @held_results.key?(item.label)
267
270
 
268
- @suite.warming item.label, @warmup
269
- @stdout.warming item.label, @warmup
271
+ @out.warming item.label, @warmup
270
272
 
271
273
  Timing.clean_env
272
274
 
@@ -300,8 +302,7 @@ module Benchmark
300
302
  item.call_times cycles
301
303
  end
302
304
 
303
- @stdout.warmup_stats warmup_time_us, @timing[item]
304
- @suite.warmup_stats warmup_time_us, @timing[item]
305
+ @out.warmup_stats warmup_time_us, @timing[item]
305
306
 
306
307
  break if run_single?
307
308
  end
@@ -312,8 +313,7 @@ module Benchmark
312
313
  @list.each do |item|
313
314
  next if run_single? && @held_results && @held_results.key?(item.label)
314
315
 
315
- @suite.running item.label, @time
316
- @stdout.running item.label, @time
316
+ @out.running item.label, @time
317
317
 
318
318
  Timing.clean_env
319
319
 
@@ -355,8 +355,7 @@ module Benchmark
355
355
  rep.show_total_time!
356
356
  end
357
357
 
358
- @stdout.add_report rep, caller(1).first
359
- @suite.add_report rep, caller(1).first
358
+ @out.add_report rep, caller(1).first
360
359
 
361
360
  break if run_single?
362
361
  end
@@ -183,9 +183,13 @@ module Benchmark
183
183
  # Generate json from Report#data to given path.
184
184
  # @param path [String] path to generate json.
185
185
  def generate_json(path)
186
- File.open path, "w" do |f|
187
- require "json"
188
- f.write JSON.pretty_generate(data)
186
+ require "json"
187
+ if path.respond_to?(:write) # STDOUT
188
+ path.write JSON.pretty_generate(data)
189
+ else
190
+ File.open path, "w" do |f|
191
+ f.write JSON.pretty_generate(data)
192
+ end
189
193
  end
190
194
  end
191
195
  end
@@ -24,9 +24,9 @@ module Benchmark
24
24
  # @returns [Array<Float, nil>] the slowdown and the error (not calculated for standard deviation)
25
25
  def slowdown(baseline)
26
26
  if baseline.central_tendency > central_tendency
27
- [baseline.central_tendency.to_f / central_tendency, 0]
27
+ [baseline.central_tendency.to_f / central_tendency, nil]
28
28
  else
29
- [central_tendency.to_f / baseline.central_tendency, 0]
29
+ [central_tendency.to_f / baseline.central_tendency, nil]
30
30
  end
31
31
  end
32
32
 
data/lib/benchmark/ips.rb CHANGED
@@ -5,10 +5,9 @@ require 'benchmark/ips/stats/stats_metric'
5
5
  require 'benchmark/ips/stats/sd'
6
6
  require 'benchmark/ips/stats/bootstrap'
7
7
  require 'benchmark/ips/report'
8
- require 'benchmark/ips/noop_suite'
9
8
  require 'benchmark/ips/job/entry'
10
- require 'benchmark/ips/job/stdout_report'
11
- require 'benchmark/ips/job/noop_report'
9
+ require 'benchmark/ips/job/stream_report'
10
+ require 'benchmark/ips/job/multi_report'
12
11
  require 'benchmark/ips/job'
13
12
 
14
13
  # Performance benchmarking library
@@ -18,10 +17,10 @@ module Benchmark
18
17
  module IPS
19
18
 
20
19
  # Benchmark-ips Gem version.
21
- VERSION = "2.10.0"
20
+ VERSION = "2.12.0"
22
21
 
23
22
  # CODENAME of current version.
24
- CODENAME = "Watashi Wa Genki"
23
+ CODENAME = "Long Awaited"
25
24
 
26
25
  # Measure code in block, each code's benchmarked result will display in
27
26
  # iteration per second with standard deviation in given time.
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: benchmark-ips
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.10.0
4
+ version: 2.12.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Evan Phoenix
@@ -59,9 +59,8 @@ files:
59
59
  - lib/benchmark/ips.rb
60
60
  - lib/benchmark/ips/job.rb
61
61
  - lib/benchmark/ips/job/entry.rb
62
- - lib/benchmark/ips/job/noop_report.rb
63
- - lib/benchmark/ips/job/stdout_report.rb
64
- - lib/benchmark/ips/noop_suite.rb
62
+ - lib/benchmark/ips/job/multi_report.rb
63
+ - lib/benchmark/ips/job/stream_report.rb
65
64
  - lib/benchmark/ips/report.rb
66
65
  - lib/benchmark/ips/share.rb
67
66
  - lib/benchmark/ips/stats/bootstrap.rb
@@ -89,7 +88,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
89
88
  - !ruby/object:Gem::Version
90
89
  version: '0'
91
90
  requirements: []
92
- rubygems_version: 3.2.26
91
+ rubygems_version: 3.3.20
93
92
  signing_key:
94
93
  specification_version: 4
95
94
  summary: A iterations per second enhancement to Benchmark.
@@ -1,27 +0,0 @@
1
- module Benchmark
2
- module IPS
3
- class Job
4
- class NoopReport
5
- def start_warming
6
- end
7
-
8
- def start_running
9
- end
10
-
11
- def footer
12
- end
13
-
14
- def warming(a, b)
15
- end
16
-
17
- def warmup_stats(a, b)
18
- end
19
-
20
- def add_report(a, b)
21
- end
22
-
23
- alias_method :running, :warming
24
- end
25
- end
26
- end
27
- end
@@ -1,25 +0,0 @@
1
- module Benchmark
2
- module IPS
3
- class NoopSuite
4
- def start_warming
5
- end
6
-
7
- def start_running
8
- end
9
-
10
- def footer
11
- end
12
-
13
- def warming(a, b)
14
- end
15
-
16
- def warmup_stats(a, b)
17
- end
18
-
19
- def add_report(a, b)
20
- end
21
-
22
- alias_method :running, :warming
23
- end
24
- end
25
- end