File: benchmark_spec.cr

package info (click to toggle)
crystal 1.14.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 24,384 kB
  • sloc: javascript: 6,400; sh: 695; makefile: 269; ansic: 121; python: 105; cpp: 77; xml: 32
file content (116 lines) | stat: -rw-r--r-- 3,557 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
require "spec"
require "benchmark"

# Make sure this compiles (#2578)
typeof(begin
  Benchmark.bm do |b|
    b.report("Here") { puts "Yes" }
  end
end)

describe Benchmark::IPS::Job do
  it "works in general / integration test" do
    # test several things to avoid running a benchmark over and over again in
    # the specs
    j = Benchmark::IPS::Job.new(1.millisecond, 1.millisecond, interactive: false)
    a = j.report("a") { sleep 1.milliseconds }
    b = j.report("b") { sleep 2.milliseconds }

    j.execute

    # the mean should be calculated
    a.mean.should be > 10

    # one of the reports should be normalized to the fastest but do to the
    # timer precision sleep 0.001 may not always be faster than 0.002 so we
    # don't care which
    first, second = [a.slower, b.slower].sort
    first.should eq(1)
    second.should be > 1
  end
end

private def create_entry
  Benchmark::IPS::Entry.new("label", ->{ 1 + 1 })
end

private def h_mean(mean)
  create_entry.tap { |e| e.mean = mean }.human_mean
end

private def h_ips(seconds)
  mean = 1.0 / seconds
  create_entry.tap { |e| e.mean = mean }.human_iteration_time
end

describe Benchmark::IPS::Entry do
  describe "#set_cycles" do
    it "sets the number of cycles needed to make 100ms" do
      e = create_entry
      e.set_cycles(2.seconds, 100)
      e.cycles.should eq(5)

      e.set_cycles(100.milliseconds, 1)
      e.cycles.should eq(1)
    end

    it "sets the cycles to 1 no matter what" do
      e = create_entry
      e.set_cycles(2.seconds, 1)
      e.cycles.should eq(1)
    end
  end

  describe "#calculate_stats" do
    it "correctly calculates basic stats" do
      e = create_entry
      e.calculate_stats([2, 4, 4, 4, 5, 5, 7, 9])

      e.size.should eq(8)
      e.mean.should eq(5.0)
      e.variance.should eq(4.0)
      e.stddev.should eq(2.0)
    end
  end

  describe "#human_mean" do
    it { h_mean(0.01234567890123).should eq(" 12.35m") }
    it { h_mean(0.12345678901234).should eq("123.46m") }

    it { h_mean(1.23456789012345).should eq("  1.23 ") }
    it { h_mean(12.3456789012345).should eq(" 12.35 ") }
    it { h_mean(123.456789012345).should eq("123.46 ") }

    it { h_mean(1234.56789012345).should eq("  1.23k") }
    it { h_mean(12345.6789012345).should eq(" 12.35k") }
    it { h_mean(123456.789012345).should eq("123.46k") }

    it { h_mean(1234567.89012345).should eq("  1.23M") }
    it { h_mean(12345678.9012345).should eq(" 12.35M") }
    it { h_mean(123456789.012345).should eq("123.46M") }

    it { h_mean(1234567890.12345).should eq("  1.23G") }
    it { h_mean(12345678901.2345).should eq(" 12.35G") }
    it { h_mean(123456789012.345).should eq("123.46G") }
  end

  describe "#human_iteration_time" do
    it { h_ips(1234.567_890_123).should eq("1,234.57s ") }
    it { h_ips(123.456_789_012_3).should eq("123.46s ") }
    it { h_ips(12.345_678_901_23).should eq(" 12.35s ") }
    it { h_ips(1.234_567_890_123).should eq("  1.23s ") }

    it { h_ips(0.123_456_789_012).should eq("123.46ms") }
    it { h_ips(0.012_345_678_901).should eq(" 12.35ms") }
    it { h_ips(0.001_234_567_890).should eq("  1.23ms") }

    it { h_ips(0.000_123_456_789).should eq("123.46µs") }
    it { h_ips(0.000_012_345_678).should eq(" 12.35µs") }
    it { h_ips(0.000_001_234_567).should eq("  1.23µs") }

    it { h_ips(0.000_000_123_456).should eq("123.46ns") }
    it { h_ips(0.000_000_012_345).should eq(" 12.34ns") }
    it { h_ips(0.000_000_001_234).should eq("  1.23ns") }
    it { h_ips(0.000_000_000_123).should eq("  0.12ns") }
  end
end