threadsafe_benchmark 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,2 @@
1
+ 2007-12-12 Sean Soper <sean.soper@revolutionhealth.com>
2
+ * First release
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2007 Revolution Health Group LLC. All rights reserved.
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,39 @@
1
+ == Summary
2
+ When testing products such as services which need to be stress tested as much as possible, it's necessary to use multi-threading to get as close to real world usage as possible. To reduce the duplication of code, this plugin utilizes Ruby's built-in Benchmark for the base functionality while preventing the output from clobbering through the use of thread-specific IO buffers.
3
+
4
+ == Install
5
+ Download the gem or source from http://rubyforge.org/projects/revhealth/
6
+
7
+ == Dependencies
8
+ None save for Ruby 1.8.x.
9
+
10
+ == Usage
11
+ Anywhere you need to do multi-threaded testing, just use 'threadsafe_bm' instead of the standard 'bm' call. The method has also been aliased as 'ts_bm'.
12
+
13
+ == Sample
14
+ require 'rubygems'
15
+ require_gem 'threadsafe_benchmark'
16
+ include ThreadsafeBenchmark
17
+
18
+ threads = []
19
+ max_num = 5000
20
+
21
+ 5.to_i.times { |i|
22
+ threads << Thread.new(max_num) { |n|
23
+ threadsafe_bm(6) { |x|
24
+ x.report("for:") { for i in 1..n; a = "1"; end }
25
+ x.report("times:") { n.times do ; a = "1"; end }
26
+ x.report("upto:") { 1.upto(n) do ; a = "1"; end }
27
+ }
28
+ }
29
+ }
30
+
31
+ threads.each { |t| t.join }
32
+
33
+ == License
34
+ This code is released under the MIT license.
35
+
36
+ == Support
37
+ The RubyForge page for all RHG-related plugins is http://rubyforge.org/projects/revhealth
38
+
39
+ You can email the RHG Rails team at rails-trunk@revolutionhealth.com
@@ -0,0 +1,43 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/gempackagetask'
4
+ require 'rake/rdoctask'
5
+
6
+ dist_dirs = [ "config", "lib", "samples" ]
7
+
8
+ manifest = YAML.load_file(File.join(File.dirname(__FILE__),"config", "manifest.yml"))
9
+
10
+ gem_spec = Gem::Specification.new do |s|
11
+ tiny_ver = 0
12
+ s.platform = Gem::Platform::RUBY
13
+ s.name = manifest[:name]
14
+ s.version = manifest[:version].join(".") + ".#{tiny_ver}"
15
+ s.author = "RHG Team"
16
+ s.email = "rails-trunk@revolution.com"
17
+ s.summary = manifest[:description]
18
+ s.files = [ "Changelog", "MIT-LICENSE", "Rakefile", "README" ]
19
+ dist_dirs.each do |dir|
20
+ s.files = s.files + Dir.glob( "#{dir}/**/*" ).delete_if { |item| /(\.[svn|log|db])|(schema\.rb)/ === item }
21
+ end
22
+ s.autorequire = s.name
23
+ s.require_path = "lib"
24
+ s.has_rdoc = false
25
+ s.extra_rdoc_files = ["README", "Changelog"]
26
+ manifest[:dependencies].each do|pkg,version|
27
+ s.add_dependency(pkg, version)
28
+ end if manifest[:dependencies]
29
+ end
30
+
31
+ gem = Rake::GemPackageTask.new(gem_spec) do |pkg|
32
+ pkg.need_tar = true
33
+ pkg.need_zip = true
34
+ end
35
+
36
+ desc 'Generate documentation for the plugin.'
37
+ Rake::RDocTask.new(:rdoc) do |rdoc|
38
+ rdoc.rdoc_dir = 'rdoc'
39
+ rdoc.title = manifest[:name].gsub(/_/, ' ').capitalize
40
+ rdoc.options << '--line-numbers' << '--inline-source'
41
+ rdoc.rdoc_files.include('README')
42
+ rdoc.rdoc_files.include('lib/*.rb')
43
+ end
@@ -0,0 +1,6 @@
1
+ ---
2
+ :version: [1, 0]
3
+ :name: "threadsafe_benchmark"
4
+ :title: "Threadsafe Benchmark"
5
+ :description: "Allows for thread-safe benchmarking"
6
+ :dependencies:
@@ -0,0 +1,50 @@
1
+ require 'stringio'
2
+ require 'benchmark'
3
+
4
+ # == Overview
5
+ #
6
+ # The ThreadsafeBenchmark module provides methods for benchmarking Ruby
7
+ # code in a thread-safe manner. Use it as you would the standard Benchmark
8
+ # module.
9
+
10
+ module ThreadsafeBenchmark
11
+ include Benchmark
12
+
13
+ class Report
14
+ def initialize(width = 0, fmtstr = nil)
15
+ @width, @fmtstr = width, fmtstr
16
+ end
17
+
18
+ def item(label = "", *fmt, &blk) # :yield:
19
+ Thread.current["io"] << label.ljust(@width)
20
+ res = Benchmark::measure(&blk)
21
+ Thread.current["io"] << res.format(@fmtstr, *fmt)
22
+ res
23
+ end
24
+
25
+ alias :report :item
26
+ end
27
+
28
+ def threadsafe_benchmark(caption = "", label_width = nil, fmtstr = nil, *labels)
29
+ Thread.current["io"] = StringIO.new
30
+ label_width ||= 0
31
+ fmtstr ||= Benchmark::FMTSTR
32
+ raise ArgumentError, "no block" unless iterator?
33
+ unless Thread.main["caption"]
34
+ puts caption
35
+ Thread.main["caption"] = true
36
+ end
37
+ results = yield(ThreadsafeBenchmark::Report.new(label_width, fmtstr))
38
+ Array === results and results.grep(Tms).each {|t|
39
+ Thread.current["io"] << (labels.shift || t.label || "").ljust(label_width)
40
+ Thread.current["io"] << t.format(fmtstr)
41
+ }
42
+ puts Thread.current["io"].string
43
+ end
44
+ alias :ts_benchmark :threadsafe_benchmark
45
+
46
+ def threadsafe_bm(label_width = 0, *labels, &blk)
47
+ threadsafe_benchmark(" "*label_width + Benchmark::CAPTION, label_width, Benchmark::FMTSTR, *labels, &blk)
48
+ end
49
+ alias :ts_bm :threadsafe_bm
50
+ end
@@ -0,0 +1,19 @@
1
+ #!/usr/bin/ruby
2
+
3
+ require File.join(File.dirname(__FILE__), '..', 'lib', 'threadsafe_benchmark')
4
+ include ThreadsafeBenchmark
5
+
6
+ threads = []
7
+ max_num = 5000
8
+
9
+ 5.to_i.times { |i|
10
+ threads << Thread.new(max_num) { |n|
11
+ threadsafe_bm(6) { |x|
12
+ x.report("for:") { for i in 1..n; a = "1"; end }
13
+ x.report("times:") { n.times do ; a = "1"; end }
14
+ x.report("upto:") { 1.upto(n) do ; a = "1"; end }
15
+ }
16
+ }
17
+ }
18
+
19
+ threads.each { |t| t.join }
metadata ADDED
@@ -0,0 +1,53 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.9.4
3
+ specification_version: 1
4
+ name: threadsafe_benchmark
5
+ version: !ruby/object:Gem::Version
6
+ version: 1.0.0
7
+ date: 2007-12-13 00:00:00 -05:00
8
+ summary: Allows for thread-safe benchmarking
9
+ require_paths:
10
+ - lib
11
+ email: rails-trunk@revolution.com
12
+ homepage:
13
+ rubyforge_project:
14
+ description:
15
+ autorequire: threadsafe_benchmark
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: false
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ - - ">"
22
+ - !ruby/object:Gem::Version
23
+ version: 0.0.0
24
+ version:
25
+ platform: ruby
26
+ signing_key:
27
+ cert_chain:
28
+ post_install_message:
29
+ authors:
30
+ - RHG Team
31
+ files:
32
+ - Changelog
33
+ - MIT-LICENSE
34
+ - Rakefile
35
+ - README
36
+ - config/manifest.yml
37
+ - lib/threadsafe_benchmark.rb
38
+ - samples/iterations.rb
39
+ test_files: []
40
+
41
+ rdoc_options: []
42
+
43
+ extra_rdoc_files:
44
+ - README
45
+ - Changelog
46
+ executables: []
47
+
48
+ extensions: []
49
+
50
+ requirements: []
51
+
52
+ dependencies: []
53
+