File: test_benchmark_ips.rb

package info (click to toggle)
ruby-benchmark-ips 2.14.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 224 kB
  • sloc: ruby: 1,257; makefile: 11
file content (291 lines) | stat: -rw-r--r-- 7,203 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
require "minitest/autorun"
require "benchmark/ips"
require "stringio"
require "tmpdir"

class TestBenchmarkIPS < Minitest::Test
  def setup
    @old_stdout = $stdout
    $stdout = StringIO.new
  end

  def teardown
    $stdout = @old_stdout
  end

  def test_kwargs
    Benchmark.ips(:time => 0.001, :warmup => 0.001, :quiet => false) do |x|
      x.report("sleep 0.25") { sleep(0.25) }
    end

    assert $stdout.string.size > 0
  end

  def test_warmup0
    $stdout = @old_stdout

    out, err = capture_io do
      Benchmark.ips(:time => 1, :warmup => 0, :quiet => false) do |x|
        x.report("sleep 0.25") { sleep(0.25) }
      end
    end

    refute_match(/Warming up -+/, out)
    assert_empty err
  end

  def test_output
    Benchmark.ips(1) do |x|
      x.warmup = 0
      x.time = 0.1
      x.report("operation") { 100 * 100 }
    end

    assert $stdout.string.size > 0
  end

  def test_quiet
    Benchmark.ips(nil, nil, true) do |x|
      x.config(:warmup => 0.001, :time => 0.001)
      x.report("operation") { 100 * 100 }
    end

    assert $stdout.string.size.zero?

    Benchmark.ips(:quiet => true) do |x|
      x.config(:warmup => 0.001, :time => 0.001)
      x.report("operation") { 100 * 100 }
    end

    assert $stdout.string.size.zero?

    Benchmark.ips do |x|
      x.config(:warmup => 0.001, :time => 0.001)
      x.quiet = true
      x.report("operation") { 100 * 100 }
    end

    assert $stdout.string.size.zero?
  end

  def test_quiet_option_override
    Benchmark.ips(quiet: true) do |x|
      x.config(:warmup => 0.001, :time => 0.001)
      x.quiet = false
      x.report("operation") { 100 * 100 }
    end

    assert $stdout.string.size > 0
    $stdout.truncate(0)

    Benchmark.ips(quiet: true) do |x|
      x.config(quiet: false, warmup: 0.001, time: 0.001)
      x.report("operation") { 100 * 100 }
    end

    assert $stdout.string.size > 0
    $stdout.truncate(0)

    Benchmark.ips(quiet: true, warmup: 0.001, time: 0.001) do |x|
      # Calling config should not make quiet option overridden when no specified
      x.config({})
      x.report("operation") { 100 * 100 }
    end

    assert $stdout.string.size.zero?
  end

  def test_ips
    report = Benchmark.ips do |x|
      x.config(:time => 1, :warmup => 1)
      x.report("sleep 0.25") { sleep(0.25) }
      x.report("sleep 0.05") { sleep(0.05) }
      x.compare!
    end

    rep1 = report.entries[0]
    rep2 = report.entries[1]

    assert_equal "sleep 0.25", rep1.label
    assert_equal 4, rep1.iterations
    assert_in_delta 4.0, rep1.ips, 0.2

    assert_equal "sleep 0.05", rep2.label
    assert_in_delta 20.0, rep2.iterations.to_f, 1.0
    assert_in_delta 20.0, rep2.ips, 2.0
  end

  def test_ips_alternate_config
    report = Benchmark.ips do |x|
      x.time = 1
      x.warmup = 1
      x.report("sleep 0.25") { sleep(0.25) }
    end

    rep = report.entries.first

    assert_equal "sleep 0.25", rep.label
    assert_equal 4, rep.iterations
    assert_in_delta 4.0, rep.ips, 0.4
  end

  def test_ips_old_config
    Benchmark.ips(1, 1) do |x|
      assert_equal 1, x.time
      assert_equal 1, x.warmup
      return
    end
  end

  def test_ips_defaults
    report = Benchmark.ips do |x|
      x.report("sleep 0.25") { sleep(0.25) }
    end

    rep = report.entries.first

    assert_equal "sleep 0.25", rep.label
    assert_equal 4*5, rep.iterations
    assert_in_delta 4.0, rep.ips, 0.2
  end

  def test_ips_report_using_symbol
    report = Benchmark.ips do |x|
      x.warmup = 0
      x.time = 0.1
      x.report(:sleep_a_quarter_second) { 1 + 1 }
    end

    rep = report.entries.first

    assert_equal :sleep_a_quarter_second, rep.label
  end

  def test_ips_default_data
    report = Benchmark.ips do |x|
      x.config(:warmup => 0.001, :time => 0.001)
      x.report("sleep 0.25") { sleep(0.25) }
    end

    all_data = report.data

    assert all_data
    assert_equal "sleep 0.25", all_data[0][:name]
    assert all_data[0][:ips]
    assert all_data[0][:stddev]
  end

  def test_ips_empty
    report = Benchmark.ips do |x|
      x.config(:warmup => 0.001, :time => 0.001)
    end

    all_data = report.data

    assert all_data
    assert_equal [], all_data
  end

  def test_json_output
    json_file = Tempfile.new("data.json")

    Benchmark.ips do |x|
      x.config(:warmup => 0.001, :time => 0.001)
      x.report("sleep 0.25") { sleep(0.25) }
      x.json! json_file.path
    end

    json_data = json_file.read
    assert json_data

    data = JSON.parse json_data
    assert data
    assert_equal 1, data.size
    assert_equal "sleep 0.25", data[0]["name"]
    assert data[0]["ips"]
    assert data[0]["stddev"]
  end

  def test_json_output_to_stdout
    Benchmark.ips do |x|
      x.config(:warmup => 0.001, :time => 0.001)
      x.report("sleep 0.25") { sleep(0.25) }
      x.quiet = true
      x.json! $stdout
    end

    assert $stdout.string.size > 0

    data = JSON.parse $stdout.string
    assert data
    assert_equal 1, data.size
    assert_equal "sleep 0.25", data[0]["name"]
    assert data[0]["ips"]
    assert data[0]["stddev"]
  end

  def test_hold!
    temp_file_name = Dir::Tmpname.create(["benchmark-ips", ".tmp"]) { }

    Benchmark.ips(:time => 0.001, :warmup => 0.001) do |x|
      x.report("operation") { 100 * 100 }
      x.report("operation2") { 100 * 100 }
      x.hold! temp_file_name
    end

    assert File.exist?(temp_file_name)
    File.unlink(temp_file_name)
  end

  def test_small_warmup_and_time
    report = Benchmark.ips do |x|
      x.config(:warmup => 0.0000000001, :time => 0.001)
      x.report("addition") { 1 + 2 }
    end
    assert_operator report.entries[0].iterations, :>=, 1

    report = Benchmark.ips do |x|
      x.config(:warmup => 0, :time => 0.0000000001)
      x.report("addition") { 1 + 2 }
    end
    assert_equal 1, report.entries[0].iterations

    report = Benchmark.ips do |x|
      x.config(:warmup => 0.001, :time => 0.0000000001)
      x.report("addition") { 1 + 2 }
    end
    assert_operator report.entries[0].iterations, :>=, 1

    report = Benchmark.ips do |x|
      x.config(:warmup => 0.0000000001, :time => 0.0000000001)
      x.report("addition") { 1 + 2 }
    end
    assert_operator report.entries[0].iterations, :>=, 1

    report = Benchmark.ips do |x|
      x.config(:warmup => 0, :time => 0)
      x.report("addition") { 1 + 2 }
    end
    assert_equal 1, report.entries[0].iterations
  end

  def test_humanize_duration
    assert_equal Benchmark::IPS::Helpers.humanize_duration(0.000000001), "0.00 ns"
    assert_equal Benchmark::IPS::Helpers.humanize_duration(123.456789), "123.46 ns"
    assert_equal Benchmark::IPS::Helpers.humanize_duration(12345.67890123), "12.35 μs"
    assert_equal Benchmark::IPS::Helpers.humanize_duration(123456789.0123456789), "123.46 ms"
    assert_equal Benchmark::IPS::Helpers.humanize_duration(123456789012.3456789012), "123.46 s"
  end

  def test_quick
    Benchmark.ips_quick(:upcase, :downcase, on: "Hello World!", warmup: 0.001, time: 0.001)

    assert $stdout.string.size > 0
  end

  def test_quick_on_kernel
    Benchmark.ips_quick(:srand, :rand, warmup: 0.001, time: 0.001)

    assert $stdout.string.size > 0
  end
end