benchmark_time 1.0.0

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.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 ebeland
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,48 @@
1
+ # BenchmarkTime
2
+
3
+ Benchmark the execution time of an arbitrary block of code. Specify concurrent
4
+ threads and number of execution loops for more robust sampling.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ gem 'benchmark_time'
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install benchmark_time
19
+
20
+ ## Usage
21
+ require 'benchmark_time'
22
+
23
+ benchmark_time(threads: 10, loops: 2) do
24
+ conn = Bunny.new
25
+ conn.start
26
+ ch = conn.create_channel
27
+ q = ch.queue("test1")
28
+ q.publish(@data)
29
+ conn.stop
30
+ end
31
+
32
+ ->
33
+
34
+ "----------------------------------------"
35
+ "Samples: 20"
36
+ "Min time: 0.059449195861816406"
37
+ "Max time: 0.15325689315795898"
38
+ "Average time: 0.10354292392730713"
39
+ "Standard Deviation: 0.027390028761805192"
40
+ "----------------------------------------"
41
+
42
+ ## Contributing
43
+
44
+ 1. Fork it
45
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
46
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
47
+ 4. Push to the branch (`git push origin my-new-feature`)
48
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'benchmark_time/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "benchmark_time"
8
+ spec.version = BenchmarkTime::VERSION
9
+ spec.authors = ["ebeland"]
10
+ spec.email = ["ebeland@gmail.com"]
11
+ spec.description = %q{Quickly benchmark functionality from the command line}
12
+ spec.summary = %q{Run and benchmark a ruby block with min/max/avg}
13
+ spec.homepage = "http://github.com/ericbeland"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ end
@@ -0,0 +1,75 @@
1
+ require_relative "enumerable_statistics"
2
+
3
+ module BenchmarkTime
4
+ # A simple time-based benchmarking script for the command line.
5
+ class BenchmarkTime
6
+
7
+ # announce if we have an exception
8
+ Thread.abort_on_exception = true
9
+
10
+ # ==== Arguments
11
+ #
12
+ # +&work_block+ The actual work to be performed
13
+ #
14
+ # Create and run a new benchmark with output to the command line
15
+ # ==== Options
16
+ # +:work_warmup_proc:+ A proc to call for each thread. this can pre-warm connections,
17
+ # or perform oother non-timed work.
18
+ # +:threads+ Number of concurrent threads to execute the work
19
+ # +:loops+ Number of times to call the block in each execution thread.
20
+ def initialize(options = {}, &work_block)
21
+ default_options = {num_threads: 10, num_loops: 10, print_samples: true, work_warmup_proc: nil}
22
+ options = default_options.merge(options)
23
+ options.to_instance_variables(binding, define: :attr_reader)
24
+ @threads = []
25
+ @results = []
26
+ @results.extend(EnumerableStatistics)
27
+ @work_block = work_block
28
+ run
29
+ end
30
+
31
+ # Returns a time for an arbitrary block's execution
32
+ def time_operation(&block)
33
+ start = Time.now.to_ms
34
+ yield
35
+ Time.now.to_ms - start
36
+ end
37
+
38
+ # Prints benchmark results to stdout.
39
+ def display_results(result_array)
40
+ puts result_array.inspect if @print_samples
41
+ puts "--------------------------------------------------------------"
42
+ puts "Samples: #{result_array.length}"
43
+ puts "Min time (ms): #{result_array.min}"
44
+ puts "Max time (ms): #{result_array.max}"
45
+ puts "Average time (ms): #{result_array.mean}"
46
+ puts "Standard Deviation (ms): #{result_array.standard_deviation}"
47
+ puts "--------------------------------------------------------------"
48
+ end
49
+
50
+ # Performs a multi-threaded execution of the block as specified in the initializer
51
+ def run
52
+ puts "Starting run with #{@num_threads} threads looping #{@num_loops} times"
53
+ @num_threads.times do
54
+ @threads << Thread.new do |th|
55
+ @num_loops.times do
56
+ @results << time_operation do
57
+ @work_warmup_proc.call if @work_warmup_proc
58
+ @work_block.call
59
+ end
60
+ end
61
+ end
62
+ end
63
+ @threads.each {|th| th.join }
64
+ puts "Completed run"
65
+ display_results(@results)
66
+ end
67
+
68
+ end
69
+
70
+ end
71
+
72
+ # an easy wrapper to do the bem
73
+ def benchmark_time(*args, &block)
74
+ ::BenchmarkTime::BenchmarkTime.new(*args, &block)
75
+ end
@@ -0,0 +1,26 @@
1
+ # Extend enumerable with basic statistical methods
2
+
3
+ # arr = []
4
+ # arr.extend(EnumerableStatistics)
5
+
6
+ module EnumerableStatistics
7
+
8
+ def sum
9
+ self.inject(0){|accum, i| accum + i }
10
+ end
11
+
12
+ def mean
13
+ self.sum/self.length.to_f
14
+ end
15
+
16
+ def sample_variance
17
+ m = self.mean
18
+ sum = self.inject(0){|accum, i| accum +(i-m)**2 }
19
+ sum/(self.length - 1).to_f
20
+ end
21
+
22
+ def standard_deviation
23
+ Math.sqrt(self.sample_variance)
24
+ end
25
+
26
+ end
@@ -0,0 +1,13 @@
1
+ module HashExtensions
2
+ def to_instance_variables(bind, opts={})
3
+ each do |key, val|
4
+ bind.eval("@#{key}=#{val.inspect}")
5
+ # we can build attr_accessor, attr_reader, attr_writers off these options
6
+ # bind.eval "self.class.class_eval 'attr_reader :foo'"if opts[:define]
7
+ end
8
+ end
9
+ end
10
+
11
+ class Hash
12
+ include HashExtensions
13
+ end
@@ -0,0 +1,5 @@
1
+ class Time
2
+ def to_ms
3
+ (self.to_f * 1000.0).to_i
4
+ end
5
+ end
@@ -0,0 +1,3 @@
1
+ module BenchmarkTime
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,6 @@
1
+ require_relative "benchmark_time/version"
2
+
3
+ require_relative "benchmark_time/hash_extensions"
4
+ require_relative "benchmark_time/enumerable_statistics"
5
+ require_relative "benchmark_time/benchmark_time"
6
+ require_relative "benchmark_time/time"
@@ -0,0 +1,5 @@
1
+ require_relative "../lib/benchmark_time"
2
+
3
+ benchmark_time(threads: 10, loops: 2, print_samples: true) do
4
+ sleep 1.00
5
+ end
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: benchmark_time
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - ebeland
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-08-03 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.3'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.3'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: Quickly benchmark functionality from the command line
47
+ email:
48
+ - ebeland@gmail.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - .gitignore
54
+ - Gemfile
55
+ - LICENSE.txt
56
+ - README.md
57
+ - Rakefile
58
+ - benchmark_time.gemspec
59
+ - lib/benchmark_time.rb
60
+ - lib/benchmark_time/benchmark_time.rb
61
+ - lib/benchmark_time/enumerable_statistics.rb
62
+ - lib/benchmark_time/hash_extensions.rb
63
+ - lib/benchmark_time/time.rb
64
+ - lib/benchmark_time/version.rb
65
+ - sample/sample_benchmark.rb
66
+ homepage: http://github.com/ericbeland
67
+ licenses:
68
+ - MIT
69
+ post_install_message:
70
+ rdoc_options: []
71
+ require_paths:
72
+ - lib
73
+ required_ruby_version: !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ! '>='
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ required_rubygems_version: !ruby/object:Gem::Requirement
80
+ none: false
81
+ requirements:
82
+ - - ! '>='
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ requirements: []
86
+ rubyforge_project:
87
+ rubygems_version: 1.8.25
88
+ signing_key:
89
+ specification_version: 3
90
+ summary: Run and benchmark a ruby block with min/max/avg
91
+ test_files: []
92
+ has_rdoc: