benchmark-lab 0.0.1

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.
@@ -0,0 +1,5 @@
1
+ module Benchmark
2
+ module Experiment
3
+ VERSION = '0.0.1'
4
+ end
5
+ end
@@ -0,0 +1,7 @@
1
+ # encoding: UTF-8
2
+ gem 'minitest' # demand gem version
3
+ require 'minitest/autorun'
4
+ require 'turn/autorun'
5
+
6
+ $LOAD_PATH << File.expand_path('../lib', File.dirname(__FILE__))
7
+ require_relative '../lib/benchmark/lab'
@@ -0,0 +1,45 @@
1
+ require_relative '../spec_helper'
2
+
3
+ describe Benchmark::Experiment::DescriptiveStatistics do
4
+ let(:sample) {
5
+ [49, 7, 6, 15, 43, 39, 47, 41, 42, 36, 40]
6
+ }
7
+
8
+ let(:data_name) { 'Test' }
9
+
10
+ subject {
11
+ Benchmark::Experiment::DescriptiveStatistics.new(sample, data_name)
12
+ }
13
+
14
+ it 'returns the name of the data collected' do
15
+ subject.name.must_equal data_name
16
+ end
17
+
18
+ it 'returns the sample size' do
19
+ subject.sample_size.must_equal 11
20
+ end
21
+
22
+ it 'returns the minimum of the sample' do
23
+ subject.minimum.must_equal 6
24
+ end
25
+
26
+ it 'returns the maximum of the sample' do
27
+ subject.maximum.must_equal 49
28
+ end
29
+
30
+ it 'returns the median of the sample' do
31
+ subject.median.must_equal 40
32
+ end
33
+
34
+ it 'returns the first quartile of the sample' do
35
+ subject.first_quartile.must_equal 25.5
36
+ end
37
+
38
+ it 'returns the first quartile of the sample' do
39
+ subject.third_quartile.must_equal 42.5
40
+ end
41
+
42
+ it 'returns the interquartile range of the sample' do
43
+ subject.interquartile_range.must_equal 17
44
+ end
45
+ end
@@ -0,0 +1,101 @@
1
+ require_relative '../spec_helper'
2
+
3
+ describe Benchmark::Experiment do
4
+ let(:cases) do
5
+ n = 5_000_000
6
+ {
7
+ 'for:' => proc { for i in 1..n; a = "1"; end },
8
+ 'times:' => proc { n.times do ; a = "1"; end },
9
+ 'upto:' => proc { 1.upto(n) do ; a = "1"; end }
10
+ }
11
+ end
12
+
13
+ it 'prints a report for each run case.' do
14
+ output_arr = []
15
+ output_arr << %r{\s+user\s+system\s+total\s+real\s+\n}
16
+
17
+ cases.keys.each do |label|
18
+ output_arr << %r{#{label}}
19
+ 4.times.each do
20
+ output_arr << %r{\s+\[\d+\.\d+,\d+\.\d+,\d+\.\d+\]}
21
+ end
22
+ output_arr << %r{\n}
23
+ end
24
+
25
+ output_regexp = output_arr.inject(//) { |o, r| Regexp.new(o.source + r.source) }
26
+
27
+ times = 2
28
+ proc do
29
+ Benchmark.experiment(times) do |x|
30
+ cases.each { |label, blk| x.report(label, &blk) }
31
+ end
32
+ end.must_output output_regexp
33
+ end
34
+
35
+ it 'returns an array with stats for each run case.' do
36
+ times = 20
37
+ results = Benchmark.experiment(times) do |x|
38
+ cases.each { |label, blk| x.report(label, &blk) }
39
+ end
40
+
41
+ results.size.must_equal 3
42
+ results.map{ |label, _| label}.must_equal cases.keys
43
+ end
44
+
45
+ it 'collects and stores the descriptive statistics into a JSON' do
46
+ times = 2
47
+ result = Benchmark.observe_and_summarize(times) do |x|
48
+ cases.each { |label, blk| x.report(label, &blk) }
49
+ end
50
+
51
+ items = JSON.parse(result)
52
+ items.size.must_equal 3
53
+ items['for:'].first['name'].must_equal 'utime'
54
+ end
55
+
56
+ it 'aggregates several benchmark results and ranks them' do
57
+ stat_one = {
58
+ 'first' => [
59
+ {
60
+ 'name' => 'total',
61
+ 'median' => 10,
62
+ 'sample' => [10] * 20
63
+ }
64
+ ]
65
+ }
66
+ stat_two = {
67
+ 'second' => [
68
+ {
69
+ 'name' => 'total',
70
+ 'median' => 20,
71
+ 'sample' => [20] * 20
72
+ }
73
+ ]
74
+ }
75
+ best, is_h0_rejected = Benchmark.aggregate_and_rank([stat_one, stat_two])
76
+ assert is_h0_rejected
77
+ best['label'].must_equal 'first'
78
+ end
79
+
80
+ it 'ranks given stats' do
81
+ stats = {
82
+ 'first' => [
83
+ {
84
+ 'name' => 'total',
85
+ 'median' => 10,
86
+ 'sample' => [10] * 20
87
+ }
88
+ ],
89
+ 'second' => [
90
+ {
91
+ 'name' => 'total',
92
+ 'median' => 20,
93
+ 'sample' => [20] * 20
94
+ }
95
+ ]
96
+ }
97
+ best, is_h0_rejected = Benchmark.rank(stats)
98
+ assert is_h0_rejected
99
+ best['label'].must_equal 'first'
100
+ end
101
+ end
@@ -0,0 +1,32 @@
1
+ require_relative '../spec_helper'
2
+
3
+ describe Benchmark::Experiment::MannWhitneyUTest do
4
+ let(:x) { [19, 22, 16, 29, 24] }
5
+ let(:y_no_ties) { [20, 11, 17, 12] }
6
+ let(:y_ties) { [19, 20, 11, 17, 12] }
7
+
8
+ it 'calculates U' do
9
+ Benchmark::Experiment::MannWhitneyUTest::calculate_U(x, y_no_ties).must_equal [3.0, 17.0]
10
+ end
11
+
12
+ it 'calculates U with ties' do
13
+ Benchmark::Experiment::MannWhitneyUTest::calculate_U(x, y_ties).must_equal [4.5, 20.5]
14
+ end
15
+
16
+ it 'calculates the standardized value z' do
17
+ Benchmark::Experiment::MannWhitneyUTest::calculate_z(x, y_no_ties).must_be_close_to -1.715
18
+ end
19
+
20
+ it 'calculates the standardized value z with ties' do
21
+ Benchmark::Experiment::MannWhitneyUTest::calculate_z(x, y_ties).must_be_close_to -1.853
22
+ end
23
+
24
+ it 'calculates p-value' do
25
+ z = Benchmark::Experiment::MannWhitneyUTest::calculate_z(x, y_no_ties)
26
+ Benchmark::Experiment::MannWhitneyUTest::calculate_probability_z(z).must_be_close_to 0.086
27
+ end
28
+
29
+ it 'rejects null hypothesis' do
30
+ refute Benchmark::Experiment::MannWhitneyUTest::is_null_hypothesis_rejected?(0.9, 0.05)
31
+ end
32
+ end
metadata ADDED
@@ -0,0 +1,134 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: benchmark-lab
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Christophe Philemotte
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-06-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '='
46
+ - !ruby/object:Gem::Version
47
+ version: 4.5.0
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '='
53
+ - !ruby/object:Gem::Version
54
+ version: 4.5.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: turn
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.9'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.9'
69
+ - !ruby/object:Gem::Dependency
70
+ name: distribution
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Run Real Experiment and Calculate Non-Parametric Statistics.
84
+ email:
85
+ - christophe.philemotte@8thcolor.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".pullreview.yml"
92
+ - Gemfile
93
+ - LICENSE.txt
94
+ - README.md
95
+ - Rakefile
96
+ - benchmark-lab.gemspec
97
+ - lib/benchmark/lab.rb
98
+ - lib/benchmark/lab/descriptive_statistics.rb
99
+ - lib/benchmark/lab/mann_whitney_u_test.rb
100
+ - lib/benchmark/lab/version.rb
101
+ - spec/spec_helper.rb
102
+ - spec/unit/descriptive_statistics_spec.rb
103
+ - spec/unit/experiment_spec.rb
104
+ - spec/unit/mann_whitney_u_test_spec.rb
105
+ homepage: https://github.com/toch/benchmark-lab
106
+ licenses:
107
+ - GPLv3
108
+ metadata: {}
109
+ post_install_message:
110
+ rdoc_options: []
111
+ require_paths:
112
+ - lib
113
+ required_ruby_version: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ required_rubygems_version: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - ">="
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ requirements: []
124
+ rubyforge_project:
125
+ rubygems_version: 2.4.3
126
+ signing_key:
127
+ specification_version: 4
128
+ summary: Run Real Experiment and Calculate Non-Parametric Statistics.
129
+ test_files:
130
+ - spec/spec_helper.rb
131
+ - spec/unit/descriptive_statistics_spec.rb
132
+ - spec/unit/experiment_spec.rb
133
+ - spec/unit/mann_whitney_u_test_spec.rb
134
+ has_rdoc: