benchmark_time 1.0.1 → 1.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6e0dd882b3f3c6affd29552750cde584f613743a
4
+ data.tar.gz: 787352d18319099f0cc43bcab25ba6bda005f621
5
+ SHA512:
6
+ metadata.gz: 4f7cf60d047bfbba6794e3e1c390354d0abea16a20fb8d0d7ee5cce762624b5124d614c1b16314f186e07bc1c85bcc065b78d5ef55cee426dd75b51a31a6dca5
7
+ data.tar.gz: 899d34d1995fe566cfd8716ea289ced015b8b699690b0e8485cb46562483b5819cf529b911377a6269bd0c5327008f035480e8a3e777b0841d3d25f8fa0eb34d
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/README.md CHANGED
@@ -1,7 +1,12 @@
1
1
  # BenchmarkTime
2
2
 
3
3
  Benchmark the execution time of an arbitrary block of code. Specify concurrent
4
- threads and number of execution loops.
4
+ <<<<<<< HEAD
5
+ threads and number of execution loops for more robust sampling.
6
+
7
+ BenchmarkTime is especially built to benchmark things like external services,
8
+ rather than ruby code itself. It can do something like a small scale
9
+ load test of external dependencies and give you a benchmark with concurrency.
5
10
 
6
11
  ## Installation
7
12
 
@@ -18,9 +23,11 @@ Or install it yourself as:
18
23
  $ gem install benchmark_time
19
24
 
20
25
  ## Usage
21
-
22
- require 'benchmark_time'
23
- require 'bunny'
26
+
27
+ # Benchmarking rabbitmq with bunny:
28
+
29
+ require 'benchmark_time'
30
+ require 'bunny'
24
31
 
25
32
  # Benchmark connecting and filing an item into rabbitmq with bunny
26
33
  benchmark_time(num_threads: 10, num_loops: 5) do
@@ -33,20 +40,36 @@ Or install it yourself as:
33
40
  end
34
41
 
35
42
  ->
36
- --------------------------------------------------------------
43
+ "----------------------------------------"
44
+
45
+ "Samples: 20"
37
46
 
38
- Samples: 50
47
+ "Min time: 0.059449195861816406"
39
48
 
40
- Min time (ms): 6
49
+ "Max time: 0.15325689315795898"
41
50
 
42
- Max time (ms): 157
51
+ "Average time: 0.10354292392730713"
43
52
 
44
- Average time (ms): 32.6
53
+ "Standard Deviation: 0.027390028761805192"
45
54
 
46
- Standard Deviation (ms): 25.33046886777315
55
+ "----------------------------------------"
47
56
 
48
- --------------------------------------------------------------
57
+ # Options for benchmark_time:
49
58
 
59
+ :work_warmup_proc A proc to call for each thread. this can pre-warm connections,
60
+ or perform oother non-timed work.
61
+
62
+ :num_threads Number of concurrent threads to execute the work
63
+
64
+ :num_loops Number of times to call the block in each execution thread.
65
+
66
+ :loop_delay Delay after each loop. (defaults to 0)
67
+
68
+ :show_values If printing output show actual variable values (good at small scale)
69
+
70
+ :print_results If true, puts the result to sdtout at the end.
71
+
72
+ :garbage_collection If false (default) garbage colleciton will be off during the loop to benchmark.
50
73
 
51
74
 
52
75
  ## Contributing
@@ -17,7 +17,8 @@ Gem::Specification.new do |spec|
17
17
  spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
19
  spec.require_paths = ["lib"]
20
-
20
+ spec.add_development_dependency "mocha"
21
+ spec.add_development_dependency "rspec"
21
22
  spec.add_development_dependency "bundler", "~> 1.3"
22
23
  spec.add_development_dependency "rake"
23
24
  end
@@ -14,19 +14,32 @@ module BenchmarkTime
14
14
  # Create and run a new benchmark with output to the command line
15
15
  # ==== Options
16
16
  # +:work_warmup_proc:+ A proc to call for each thread. this can pre-warm connections,
17
- # or perform other non-timed work.
18
- # +: num_threads+ Number of concurrent threads to execute the work
19
- # +: num_loops+ Number of times to call the block in each execution thread.
17
+ # or perform oother non-timed work.
18
+ # +:num_threads+ Number of concurrent threads to execute the work
19
+ # +:num_loops+ Number of times to call the block in each execution thread.
20
+ # +:loop_delay Delay after each loop. (defaults to 0)
21
+ #
22
+ # +:show_values+ If output is printed, display array of actual variable values (good at small scale)
23
+ # +:print_results+ If true, puts the result to sdtout at the end.
24
+ # +:garbage_collection+ If false (default) garbage colleciton will be off during the loop to benchmark.
25
+
26
+
20
27
  def initialize(options = {}, &work_block)
21
- default_options = {num_threads: 10, num_loops: 10, print_samples: true, work_warmup_proc: nil}
28
+ default_options = { num_threads: 10,
29
+ num_loops: 10,
30
+ loop_delay: 0,
31
+ show_values: true,
32
+ print_results: true,
33
+ garbage_collection: false,
34
+ work_warmup_proc: nil,
35
+ }
36
+
22
37
  options = default_options.merge(options)
23
- # experiment: auto-create instance variables from hash...
24
- options.to_instance_variables(binding, define: :attr_reader)
38
+ options.to_instance_variables(binding, define: :attr_reader)
25
39
  @threads = []
26
40
  @results = []
27
- @results.extend(EnumerableStatistics)
28
41
  @work_block = work_block
29
- run
42
+ @results.extend(EnumerableStatistics)
30
43
  end
31
44
 
32
45
  # Returns a time for an arbitrary block's execution
@@ -38,32 +51,36 @@ module BenchmarkTime
38
51
 
39
52
  # Prints benchmark results to stdout.
40
53
  def display_results(result_array)
41
- puts result_array.inspect if @print_samples
42
- puts "--------------------------------------------------------------"
43
- puts "Samples: #{result_array.length}"
44
- puts "Min time (ms): #{result_array.min}"
45
- puts "Max time (ms): #{result_array.max}"
46
- puts "Average time (ms): #{result_array.mean}"
47
- puts "Standard Deviation (ms): #{result_array.standard_deviation}"
54
+ puts "------------------- Completed run ----------------------------"
55
+ puts "Samples: #{result_array.length}"
56
+ puts "Min time: #{result_array.min}"
57
+ puts "Max time: #{result_array.max}"
58
+ puts "Average time: #{result_array.mean}"
59
+ puts "Standard Deviation: #{result_array.standard_deviation}"
60
+ puts "Values: #{result_array.inspect}" if @show_values
48
61
  puts "--------------------------------------------------------------"
49
62
  end
50
63
 
51
64
  # Performs a multi-threaded execution of the block as specified in the initializer
52
65
  def run
53
- puts "Starting run with #{@num_threads} threads looping #{@num_loops} times"
66
+ puts "Starting run with #{@num_threads} threads looping #{@num_loops} times" if @print_results
54
67
  @num_threads.times do
55
68
  @threads << Thread.new do |th|
56
69
  @num_loops.times do
57
70
  @results << time_operation do
58
71
  @work_warmup_proc.call if @work_warmup_proc
59
- @work_block.call
72
+ result = @work_block.call
73
+ sleep @loop_delay
74
+ result
60
75
  end
61
76
  end
62
77
  end
63
78
  end
79
+ GC.disable unless @garbage_collection
64
80
  @threads.each {|th| th.join }
65
- puts "Completed run"
66
- display_results(@results)
81
+ GC.enable
82
+ display_results(@results) if @print_results
83
+ @results
67
84
  end
68
85
 
69
86
  end
@@ -72,5 +89,6 @@ end
72
89
 
73
90
  # an easy wrapper to do the bem
74
91
  def benchmark_time(*args, &block)
75
- ::BenchmarkTime::BenchmarkTime.new(*args, &block)
92
+ b = BenchmarkTime::BenchmarkTime.new(*args, &block)
93
+ b.run
76
94
  end
@@ -6,17 +6,17 @@
6
6
  module EnumerableStatistics
7
7
 
8
8
  def sum
9
- self.inject(0){|accum, i| accum + i }
9
+ self.inject(0) {|accum, i| accum + i }
10
10
  end
11
11
 
12
12
  def mean
13
- self.sum/self.length.to_f
13
+ self.sum / self.length.to_f
14
14
  end
15
15
 
16
16
  def sample_variance
17
- m = self.mean
17
+ m = self.mean
18
18
  sum = self.inject(0){|accum, i| accum +(i-m)**2 }
19
- sum/(self.length - 1).to_f
19
+ sum / (self.length - 1).to_f
20
20
  end
21
21
 
22
22
  def standard_deviation
@@ -1,3 +1,3 @@
1
1
  module BenchmarkTime
2
- VERSION = "1.0.1"
2
+ VERSION = "1.2.1"
3
3
  end
@@ -0,0 +1,9 @@
1
+ require "bunny"
2
+ require_relative "../lib/benchmark_time"
3
+
4
+
5
+ benchmark_time(threads: 10, loops: 2) do
6
+ # thing to benchmark goes here...
7
+
8
+
9
+ end
@@ -0,0 +1,54 @@
1
+ require_relative "spec_helper"
2
+
3
+ describe 'benchmark_time' do
4
+
5
+ it "should return an array with results" do
6
+ results = benchmark_time(num_threads: 1, num_loops: 2) { }
7
+ results.length.should == 2
8
+ results = benchmark_time(num_threads: 4, num_loops: 4) { }
9
+ results.length.should == 16
10
+ end
11
+
12
+ it "should return min/max/average" do
13
+ results = benchmark_time(num_threads: 1, num_loops: 2) { }
14
+ (results.min >= 0).should be_true
15
+ (results.max >= 0).should be_true
16
+ (results.mean >= 0).should be_true
17
+ end
18
+
19
+ it "should print values if true" do
20
+ output = capture_stdout do
21
+ benchmark_time(num_threads: 1, num_loops: 1) {}
22
+ end
23
+ (output =~ /Values/).should be_true
24
+ end
25
+
26
+ it "should print min/max/average if printing output" do
27
+ output = capture_stdout do
28
+ benchmark_time(num_threads: 1, num_loops: 2) {}
29
+ end
30
+ (output =~ /min time/i).should be_true
31
+ (output =~ /max time/i).should be_true
32
+ (output =~ /average time/i).should be_true
33
+ end
34
+
35
+ it "should not print anything if printing output is disabled" do
36
+ output = capture_stdout do
37
+ benchmark_time(num_threads: 1, num_loops: 2, print_results: false) {}
38
+ end
39
+ output.empty?.should be_true
40
+ end
41
+
42
+ it "should disable garbage collection if off" do
43
+ GC.expects(:disable).once.returns(true)
44
+ results = benchmark_time(num_threads: 2, num_loops: 1, garbage_collection: false) { }
45
+ GC.unstub(:disable)
46
+ end
47
+
48
+ it "should not disable garbage collection if off" do
49
+ GC.expects(:disable).never.returns(true)
50
+ results = benchmark_time(num_threads: 2, num_loops: 1, garbage_collection: true) { }
51
+ GC.unstub(:disable)
52
+ end
53
+
54
+ end
@@ -0,0 +1,17 @@
1
+ require 'bundler/setup'
2
+ require "benchmark_time"
3
+ require 'mocha/api'
4
+
5
+ RSpec.configure do |config|
6
+
7
+ end
8
+
9
+ def capture_stdout &block
10
+ old_stdout = $stdout
11
+ fake_stdout = StringIO.new
12
+ $stdout = fake_stdout
13
+ block.call
14
+ fake_stdout.string
15
+ ensure
16
+ $stdout = old_stdout
17
+ end
metadata CHANGED
@@ -1,20 +1,46 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: benchmark_time
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
5
- prerelease:
4
+ version: 1.2.1
6
5
  platform: ruby
7
6
  authors:
8
7
  - ebeland
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-08-03 00:00:00.000000000 Z
11
+ date: 2014-03-16 00:00:00.000000000 Z
13
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: mocha
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
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'
14
41
  - !ruby/object:Gem::Dependency
15
42
  name: bundler
16
43
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
44
  requirements:
19
45
  - - ~>
20
46
  - !ruby/object:Gem::Version
@@ -22,7 +48,6 @@ dependencies:
22
48
  type: :development
23
49
  prerelease: false
24
50
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
51
  requirements:
27
52
  - - ~>
28
53
  - !ruby/object:Gem::Version
@@ -30,17 +55,15 @@ dependencies:
30
55
  - !ruby/object:Gem::Dependency
31
56
  name: rake
32
57
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
58
  requirements:
35
- - - ! '>='
59
+ - - '>='
36
60
  - !ruby/object:Gem::Version
37
61
  version: '0'
38
62
  type: :development
39
63
  prerelease: false
40
64
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
65
  requirements:
43
- - - ! '>='
66
+ - - '>='
44
67
  - !ruby/object:Gem::Version
45
68
  version: '0'
46
69
  description: Quickly benchmark functionality from the command line
@@ -51,6 +74,7 @@ extensions: []
51
74
  extra_rdoc_files: []
52
75
  files:
53
76
  - .gitignore
77
+ - .rspec
54
78
  - Gemfile
55
79
  - LICENSE.txt
56
80
  - README.md
@@ -62,31 +86,34 @@ files:
62
86
  - lib/benchmark_time/hash_extensions.rb
63
87
  - lib/benchmark_time/time.rb
64
88
  - lib/benchmark_time/version.rb
89
+ - sample/media_library_benchmark.rb
65
90
  - sample/sample_benchmark.rb
91
+ - spec/benchmark_spec.rb
92
+ - spec/spec_helper.rb
66
93
  homepage: http://github.com/ericbeland
67
94
  licenses:
68
95
  - MIT
96
+ metadata: {}
69
97
  post_install_message:
70
98
  rdoc_options: []
71
99
  require_paths:
72
100
  - lib
73
101
  required_ruby_version: !ruby/object:Gem::Requirement
74
- none: false
75
102
  requirements:
76
- - - ! '>='
103
+ - - '>='
77
104
  - !ruby/object:Gem::Version
78
105
  version: '0'
79
106
  required_rubygems_version: !ruby/object:Gem::Requirement
80
- none: false
81
107
  requirements:
82
- - - ! '>='
108
+ - - '>='
83
109
  - !ruby/object:Gem::Version
84
110
  version: '0'
85
111
  requirements: []
86
112
  rubyforge_project:
87
- rubygems_version: 1.8.25
113
+ rubygems_version: 2.2.1
88
114
  signing_key:
89
- specification_version: 3
115
+ specification_version: 4
90
116
  summary: Run and benchmark a ruby block with min/max/avg
91
- test_files: []
92
- has_rdoc:
117
+ test_files:
118
+ - spec/benchmark_spec.rb
119
+ - spec/spec_helper.rb