bench_press 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
@@ -0,0 +1,7 @@
1
+ *.sw?
2
+ .DS_Store
3
+ coverage
4
+ rdoc
5
+ pkg
6
+ /mockups
7
+ /TODO
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Sandro Turriate
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.
@@ -0,0 +1,17 @@
1
+ = Bench Press
2
+
3
+ Sharable benchmarks
4
+
5
+ == Note on Patches/Pull Requests
6
+
7
+ * Fork the project.
8
+ * Make your feature addition or bug fix.
9
+ * Add tests for it. This is important so I don't break it in a
10
+ future version unintentionally.
11
+ * Commit, do not mess with rakefile, version, or history.
12
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
13
+ * Send me a pull request. Bonus points for topic branches.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2009 Sandro Turriate. See LICENSE for details.
@@ -0,0 +1,51 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "bench_press"
8
+ gem.summary = %Q{Sharable benchmarks}
9
+ gem.description = %Q{Sharable benchmarks}
10
+ gem.email = "sandro.turriate@gmail.com"
11
+ gem.homepage = "http://github.com/sandro/bench_press"
12
+ gem.authors = ["Sandro Turriate"]
13
+ gem.add_dependency "facter", "1.5.7"
14
+ gem.add_dependency "activesupport", "2.3.5"
15
+ gem.add_development_dependency "rspec", "1.2.9"
16
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
17
+ end
18
+ Jeweler::GemcutterTasks.new
19
+ rescue LoadError
20
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
21
+ end
22
+
23
+ require 'spec/rake/spectask'
24
+ Spec::Rake::SpecTask.new(:spec) do |spec|
25
+ spec.libs << 'lib' << 'spec'
26
+ spec.spec_files = FileList['spec/**/*_spec.rb']
27
+ end
28
+
29
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
30
+ spec.libs << 'lib' << 'spec'
31
+ spec.pattern = 'spec/**/*_spec.rb'
32
+ spec.rcov = true
33
+ end
34
+
35
+
36
+ task :default => :spec
37
+
38
+ require 'rake/rdoctask'
39
+ Rake::RDocTask.new do |rdoc|
40
+ if File.exist?('VERSION.yml')
41
+ config = YAML.load(File.read('VERSION.yml'))
42
+ version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
43
+ else
44
+ version = ""
45
+ end
46
+
47
+ rdoc.rdoc_dir = 'rdoc'
48
+ rdoc.title = "bench_press #{version}"
49
+ rdoc.rdoc_files.include('README*')
50
+ rdoc.rdoc_files.include('lib/**/*.rb')
51
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,81 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{bench_press}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Sandro Turriate"]
12
+ s.date = %q{2010-01-20}
13
+ s.default_executable = %q{bench_press}
14
+ s.description = %q{Sharable benchmarks}
15
+ s.email = %q{sandro.turriate@gmail.com}
16
+ s.executables = ["bench_press"]
17
+ s.extra_rdoc_files = [
18
+ "README.rdoc",
19
+ "TODO"
20
+ ]
21
+ s.files = [
22
+ ".document",
23
+ ".gitignore",
24
+ "MIT-LICENSE",
25
+ "README.rdoc",
26
+ "Rakefile",
27
+ "VERSION",
28
+ "bench_press.gemspec",
29
+ "bin/bench_press",
30
+ "examples/compare_rr_to_rspec.rb",
31
+ "examples/existence_of_method.rb",
32
+ "examples/hash_merge.rb",
33
+ "examples/implicit_versus_explicit_return.rb",
34
+ "lib/bench_press.rb",
35
+ "lib/bench_press/report.rb",
36
+ "lib/bench_press/result.rb",
37
+ "lib/bench_press/runnable.rb",
38
+ "lib/bench_press/system_information.rb",
39
+ "spec/bench_press/report_spec.rb",
40
+ "spec/bench_press/result_spec.rb",
41
+ "spec/bench_press/system_information_spec.rb",
42
+ "spec/bench_press_spec.rb",
43
+ "spec/spec_helper.rb"
44
+ ]
45
+ s.homepage = %q{http://github.com/sandro/bench_press}
46
+ s.rdoc_options = ["--charset=UTF-8"]
47
+ s.require_paths = ["lib"]
48
+ s.rubygems_version = %q{1.3.5}
49
+ s.summary = %q{Sharable benchmarks}
50
+ s.test_files = [
51
+ "spec/bench_press/report_spec.rb",
52
+ "spec/bench_press/result_spec.rb",
53
+ "spec/bench_press/system_information_spec.rb",
54
+ "spec/bench_press_spec.rb",
55
+ "spec/spec_helper.rb",
56
+ "examples/compare_rr_to_rspec.rb",
57
+ "examples/existence_of_method.rb",
58
+ "examples/hash_merge.rb",
59
+ "examples/implicit_versus_explicit_return.rb"
60
+ ]
61
+
62
+ if s.respond_to? :specification_version then
63
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
64
+ s.specification_version = 3
65
+
66
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
67
+ s.add_runtime_dependency(%q<facter>, ["= 1.5.7"])
68
+ s.add_runtime_dependency(%q<activesupport>, ["= 2.3.5"])
69
+ s.add_development_dependency(%q<rspec>, ["= 1.2.9"])
70
+ else
71
+ s.add_dependency(%q<facter>, ["= 1.5.7"])
72
+ s.add_dependency(%q<activesupport>, ["= 2.3.5"])
73
+ s.add_dependency(%q<rspec>, ["= 1.2.9"])
74
+ end
75
+ else
76
+ s.add_dependency(%q<facter>, ["= 1.5.7"])
77
+ s.add_dependency(%q<activesupport>, ["= 2.3.5"])
78
+ s.add_dependency(%q<rspec>, ["= 1.2.9"])
79
+ end
80
+ end
81
+
@@ -0,0 +1,24 @@
1
+ #!/usr/bin/env ruby
2
+ require 'optparse'
3
+
4
+ options = {}
5
+
6
+ optparse = OptionParser.new do |opts|
7
+ opts.banner = "Usage: bench_press [options] file_to_benchmark.rb"
8
+
9
+ opts.on( '-p', '--publish', 'Publish the benchmark to http://rubybenchmark.com' ) do
10
+ options[:publish] = true
11
+ end
12
+ end
13
+
14
+ optparse.parse!
15
+
16
+ filename = ARGV.first
17
+ abort "You must supply a filename that you want to benchmark." if filename.nil?
18
+
19
+ file = File.join(Dir.pwd, ARGV.first)
20
+ if File.exists?(file)
21
+ load file
22
+ else
23
+ abort "The file #{file} does not exist."
24
+ end
@@ -0,0 +1,24 @@
1
+ $:.unshift(File.dirname(__FILE__) + "/../lib")
2
+ require "rubygems"
3
+ require "spec/mocks"
4
+ require 'rr'
5
+ require 'bench_press'
6
+
7
+ module CompareRRToRspec
8
+ extend BenchPress
9
+
10
+ rspec_object = Object.new
11
+ rr_object = Object.new
12
+
13
+ measure('rspec should_receive') do
14
+ rspec_object.should_receive(:foobar).and_return("baz")
15
+ rspec_object.foobar
16
+ end
17
+ measure("rr mock") do
18
+ RR.mock(rr_object).foobar.returns("baz")
19
+ rr_object.foobar
20
+ RR.reset
21
+ end
22
+ end
23
+
24
+ CompareRRToRspec.bench_press
@@ -0,0 +1,28 @@
1
+ $:.unshift(File.dirname(__FILE__) + "/../lib")
2
+ require 'bench_press'
3
+
4
+ module ExistenceOfMethod
5
+ extend BenchPress
6
+
7
+ class A
8
+ def c
9
+ end
10
+ end
11
+
12
+ a = A.new
13
+
14
+ measure('method_defined?') do
15
+ A.method_defined? :c
16
+ end
17
+ measure('respond_to?') do
18
+ a.respond_to? :c
19
+ end
20
+ measure('instance_methods include') do
21
+ A.instance_methods.include? :c
22
+ end
23
+ measure('instance_methods(false) include') do
24
+ A.instance_methods(false).include? :c
25
+ end
26
+ end
27
+
28
+ ExistenceOfMethod.bench_press
@@ -0,0 +1,22 @@
1
+ $:.unshift(File.dirname(__FILE__) + "/../lib")
2
+ require 'bench_press'
3
+
4
+ extend BenchPress
5
+
6
+ reps 30_000
7
+
8
+ measure "Hash#merge" do
9
+ {}.merge(:key => :value)
10
+ end
11
+
12
+ measure "Hash#merge!" do
13
+ {}.merge!(:key => :value)
14
+ end
15
+
16
+ measure "Hash#store" do
17
+ {}.store(:key, :value)
18
+ end
19
+
20
+ measure "Hash#[]=" do
21
+ {}.[]=(:key, :value)
22
+ end
@@ -0,0 +1,20 @@
1
+ $:.unshift(File.dirname(__FILE__) + "/../lib")
2
+ require 'bench_press'
3
+
4
+ extend BenchPress
5
+
6
+ def implicit
7
+ 1
8
+ end
9
+
10
+ def explicit
11
+ return 1
12
+ end
13
+
14
+ measure("implicit return") do
15
+ implicit
16
+ end
17
+ measure("explicit return") do
18
+ explicit
19
+ end
20
+
@@ -0,0 +1,71 @@
1
+ require 'active_support/inflector'
2
+ require 'benchmark'
3
+ require 'facter'
4
+ require 'bench_press/runnable'
5
+ require 'bench_press/result'
6
+ require 'bench_press/report'
7
+ require 'bench_press/system_information'
8
+
9
+ module BenchPress
10
+
11
+ def self.extended(base)
12
+ base.instance_variable_set(:@module_name, base.name) if base.respond_to?(:name)
13
+ end
14
+
15
+ def module_name
16
+ @module_name
17
+ end
18
+
19
+ def runnables
20
+ @runnables ||= []
21
+ end
22
+
23
+ def report
24
+ @report ||= Report.new report_name
25
+ end
26
+
27
+ def name(label = nil)
28
+ if label.nil?
29
+ begin
30
+ Module.instance_method(:name).bind(self).call
31
+ rescue TypeError; end
32
+ else
33
+ report.name = label
34
+ end
35
+ end
36
+
37
+ def summary(summary)
38
+ report.summary = summary
39
+ end
40
+
41
+ def author(author)
42
+ report.author = author
43
+ end
44
+
45
+ def reps(times)
46
+ Runnable.repetitions = times
47
+ end
48
+
49
+ def measure(name, &block)
50
+ runnables << Runnable.new(name, block)
51
+ end
52
+
53
+ def bench_press
54
+ report.runnables = runnables
55
+ puts report
56
+ end
57
+
58
+ protected
59
+
60
+ def default_report_name
61
+ module_name || ActiveSupport::Inflector.titleize(File.basename(__FILE__, ".rb"))
62
+ end
63
+
64
+ def report_name
65
+ name || default_report_name
66
+ end
67
+ end
68
+
69
+ at_exit do
70
+ bench_press if respond_to?(:bench_press)
71
+ end
@@ -0,0 +1,141 @@
1
+ module BenchPress
2
+ class Report
3
+ SPACING = 4
4
+
5
+ attr_accessor :runnables, :name, :summary, :author
6
+
7
+ def initialize(name = "")
8
+ @name = name
9
+ end
10
+
11
+ def date
12
+ Date.today
13
+ end
14
+
15
+ def to_s
16
+ [
17
+ cover_page,
18
+ system_information,
19
+ runnable_heading,
20
+ runnable_table,
21
+ ].join("\n\n")
22
+ end
23
+
24
+ def cover_page
25
+ [
26
+ header(name),
27
+ announce_author,
28
+ announce_date,
29
+ announce_summary
30
+ ].compact.join("\n")
31
+ end
32
+
33
+ def runnable_heading
34
+ header(
35
+ %("#{result.fastest.name}" is up to #{result.slowest.percent_slower}% faster over #{repetitions} repetitions),
36
+ "-"
37
+ )
38
+ end
39
+
40
+ def runnable_table
41
+ result.runnables.map do |r|
42
+ row(run_name(r.name), run_time(r.run_time), run_summary(r))
43
+ end.join("\n")
44
+ end
45
+
46
+ def system_information
47
+ [
48
+ header("System Information", '-'),
49
+ prefix(SystemInformation.summary)
50
+ ].join("\n")
51
+ end
52
+
53
+ protected
54
+
55
+ def announce_author
56
+ line("Author: #{author}") unless author.nil?
57
+ end
58
+
59
+ def announce_date
60
+ line("Date: #{date}") unless date.nil?
61
+ end
62
+
63
+ def announce_summary
64
+ line("Summary: #{summary}") unless summary.nil?
65
+ end
66
+
67
+ def header(content, decorator = "=")
68
+ [content, decorator * content.size].join("\n")
69
+ end
70
+
71
+ def prefix(content)
72
+ content.gsub(/^|(\\n)/, "#{$1} ")
73
+ end
74
+
75
+ def line(content)
76
+ content << " "
77
+ end
78
+
79
+ def repetitions
80
+ Runnable.repetitions
81
+ end
82
+
83
+ def row(*columns)
84
+ row = spacer
85
+ columns.each do |column|
86
+ row << column
87
+ end
88
+ row
89
+ end
90
+
91
+ def spacer
92
+ ' ' * SPACING
93
+ end
94
+
95
+ def run_name(content)
96
+ content.to_s.ljust(result.longest_name.size + SPACING)
97
+ end
98
+
99
+ def run_summary(r)
100
+ if r == result.fastest
101
+ "Fastest"
102
+ else
103
+ "#{r.percent_slower}% Slower"
104
+ end
105
+ end
106
+
107
+ def run_time(secs)
108
+ secs.to_s.ljust(result.longest_run_time.size) + " secs" + spacer
109
+ end
110
+
111
+ def result
112
+ @result ||= Result.new(runnables).evaluate
113
+ end
114
+ end
115
+ end
116
+
117
+ __END__
118
+
119
+ HASH MERGE
120
+ ===========
121
+ Author: Sandro Turriate
122
+ Date: 12-12-09
123
+ Summary: Fastest way to merge or append a hash to another hash
124
+
125
+ System Information
126
+ ------------------
127
+ Operating System: Mac OS X 10.6.2 (10C540)
128
+ CPU: Intel Core 2 Duo 2.4 GHz
129
+ Processor Count: 2
130
+ Memory: 4 GB
131
+ Ruby version: 1.8.7 patchlevel 174
132
+
133
+
134
+ "Implicit return" is up to 17% faster over 1000 repetitions
135
+ -----------------------------------------------------
136
+ Implicit Return 0.00029 secs Fastest
137
+ Explicit 0.00035 secs 17% Slower
138
+
139
+ 50% faster is 25 secs rather than 50, 50x = (50-25)
140
+ 35x = (35-29) = 17%
141
+ .00035x = .00006
@@ -0,0 +1,48 @@
1
+ module BenchPress
2
+ class Result
3
+ attr_reader :runnables
4
+
5
+ def initialize(runnables)
6
+ @runnables = runnables
7
+ end
8
+
9
+ def evaluate
10
+ sort
11
+ grade
12
+ self
13
+ end
14
+
15
+ def sort
16
+ runnables.each {|r| r.run}
17
+ @runnables = runnables.sort_by {|r| r.run_time}
18
+ end
19
+
20
+ def fastest
21
+ runnables.first
22
+ end
23
+
24
+ def slowest
25
+ runnables.last
26
+ end
27
+
28
+ def longest_name
29
+ runnables.sort_by {|r| r.name.size}.last.name
30
+ end
31
+
32
+ def longest_run_time
33
+ runnables.sort_by {|r| r.run_time.to_s.size}.last.run_time.to_s
34
+ end
35
+
36
+ def grade
37
+ runnables.each do |r|
38
+ r.percent_slower = percentage_slower(r.run_time)
39
+ end
40
+ end
41
+
42
+ protected
43
+
44
+ def percentage_slower(time)
45
+ (((time - fastest.run_time) / time) * 100).to_i
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,27 @@
1
+ module BenchPress
2
+ class Runnable
3
+ attr_reader :name, :code_block, :run_time
4
+ attr_accessor :percent_slower
5
+
6
+ class << self
7
+ def repetitions
8
+ @repetitions ||= 1000
9
+ end
10
+
11
+ def repetitions=(times)
12
+ @repetitions = times
13
+ end
14
+ end
15
+
16
+ def initialize(name, block)
17
+ @name = name
18
+ @code_block = block
19
+ end
20
+
21
+ def run
22
+ @run_time = Benchmark.realtime do
23
+ self.class.repetitions.times &code_block
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,80 @@
1
+ module BenchPress
2
+ module SystemInformation
3
+ require 'digest/sha1'
4
+ extend self
5
+
6
+ def announce_cpu
7
+ [
8
+ announce("CPU:", cpu),
9
+ announce("Processor Count:", processor_count)
10
+ ].join("\n")
11
+ end
12
+
13
+ def announce_memory
14
+ announce "Memory:", memory
15
+ end
16
+
17
+ def announce_os
18
+ announce "Operating System:", os
19
+ end
20
+
21
+ def announce_ruby_version
22
+ announce "Ruby version:", ruby_version
23
+ end
24
+
25
+ def cpu
26
+ if mac?
27
+ "#{facts['sp_cpu_type']} #{facts['sp_current_processor_speed']}"
28
+ else
29
+ facts['processor0']
30
+ end
31
+ end
32
+
33
+ def crypted_identifier
34
+ Digest::SHA1.hexdigest(identifier)
35
+ end
36
+
37
+ def memory
38
+ facts['sp_physical_memory'] || facts['memorysize']
39
+ end
40
+
41
+ def os
42
+ facts['sp_os_version'] || facts['lsbdistdescription'] || facts['operatingsystem']
43
+ end
44
+
45
+ def processor_count
46
+ facts['sp_number_processors'] || facts['processorcount'] || facts['physicalprocessorcount']
47
+ end
48
+
49
+ def ruby_version
50
+ "#{RUBY_VERSION} patchlevel #{RUBY_PATCHLEVEL}"
51
+ end
52
+
53
+ def summary
54
+ [
55
+ announce_os,
56
+ announce_cpu,
57
+ announce_memory,
58
+ announce_ruby_version
59
+ ].join("\n")
60
+ end
61
+
62
+ protected
63
+
64
+ def announce(key, value)
65
+ key.ljust(21) << value
66
+ end
67
+
68
+ def facts
69
+ @facts ||= Facter.to_hash
70
+ end
71
+
72
+ def identifier
73
+ facts['sp_serial_number'] || facts['uniqueid']
74
+ end
75
+
76
+ def mac?
77
+ Facter.kernel =~ /Darwin/
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,117 @@
1
+ require 'spec_helper'
2
+
3
+ describe BenchPress::Report do
4
+ describe "#heading" do
5
+ it "creates a markdown h1 heading" do
6
+ subject.send(:header, "Hi").should == "Hi\n=="
7
+ end
8
+
9
+ it "creates a markdown h2 heading" do
10
+ subject.send(:header, "Hi There", '-').should == "Hi There\n--------"
11
+ end
12
+ end
13
+
14
+ describe "#prefix" do
15
+ it "prepends 4 spaces to the content" do
16
+ subject.send(:prefix, "Hi There").should == " Hi There"
17
+ end
18
+
19
+ it "appends 4 spaces after new-line characters" do
20
+ subject.send(:prefix, "Hi\nThere\nSam").should == " Hi\n There\n Sam"
21
+ end
22
+ end
23
+
24
+ describe "#line" do
25
+ it "appends two spaces to the end of the line" do
26
+ subject.send(:line, "Hi There").should == "Hi There "
27
+ end
28
+ end
29
+
30
+ describe "#runnable_results" do
31
+ let(:report) do
32
+ r = BenchPress::Report.new
33
+ implicit = BenchPress::Runnable.new('Implicit return', lambda { })
34
+ explicit = BenchPress::Runnable.new('Explicit', lambda { })
35
+ implicit.stub(:run => nil, :run_time => 0.00029)
36
+ explicit.stub(:run => nil, :run_time => 0.00035)
37
+ r.runnables = [implicit, explicit]
38
+ r
39
+ end
40
+
41
+ it "displays a heading" do
42
+ heading = <<-EOS
43
+ "Implicit return" is up to 17% faster over 1000 repetitions
44
+ -----------------------------------------------------------
45
+ EOS
46
+
47
+ report.runnable_heading.should == heading.strip
48
+ end
49
+
50
+ it "displays the table of results" do
51
+ table = <<-EOS
52
+ Implicit return 0.00029 secs Fastest
53
+ Explicit 0.00035 secs 17% Slower
54
+ EOS
55
+ report.runnable_table.should == table.chop
56
+ end
57
+ end
58
+
59
+ it "displays the System Information" do
60
+ BenchPress::SystemInformation.stub(:summary => "Operating System: Mac OS X 10.6.2 (10C540)\nCPU: Intel Core 2 Duo 2.4 GHz\nProcessor Count: 2\nMemory: 4 GB\nRuby version: 1.8.7 patchlevel 174")
61
+ info = <<-EOS
62
+ System Information
63
+ ------------------
64
+ Operating System: Mac OS X 10.6.2 (10C540)
65
+ CPU: Intel Core 2 Duo 2.4 GHz
66
+ Processor Count: 2
67
+ Memory: 4 GB
68
+ Ruby version: 1.8.7 patchlevel 174
69
+ EOS
70
+ subject.system_information.should == info.chop
71
+ end
72
+
73
+ describe "#cover_page" do
74
+ let(:report) do
75
+ report = BenchPress::Report.new("Hash Merge")
76
+ report.author = "Sandro Turriate"
77
+ report.summary = "Various methods for appending to a hash"
78
+ report
79
+ end
80
+ let (:date) { Date.new(2009,1,1) }
81
+
82
+ before do
83
+ Date.stub(:today => date)
84
+ end
85
+
86
+ it "displays the report name" do
87
+ report.cover_page.should include("Hash Merge\n==========")
88
+ end
89
+
90
+ it "displays the report name and author name" do
91
+ report.cover_page.should include("Hash Merge\n==========\nAuthor: Sandro Turriate ")
92
+ end
93
+
94
+ it "displays the report name, author name, and date" do
95
+ report.cover_page.should include("Hash Merge\n==========\nAuthor: Sandro Turriate \nDate: #{date.to_s} ")
96
+ end
97
+
98
+ it "displays the report name, author name, date, and summary" do
99
+ report.cover_page.should include("Hash Merge\n==========\nAuthor: Sandro Turriate \nDate: #{date.to_s} \nSummary: Various methods for appending to a hash ")
100
+ end
101
+
102
+ it "does not display the author when there is no author" do
103
+ report.stub(:author)
104
+ report.cover_page.should == "Hash Merge\n==========\nDate: #{date.to_s} \nSummary: Various methods for appending to a hash "
105
+ end
106
+
107
+ it "does not display the summary when there is no summary" do
108
+ report.stub(:summary)
109
+ report.cover_page.should == "Hash Merge\n==========\nAuthor: Sandro Turriate \nDate: #{date.to_s} "
110
+ end
111
+
112
+ it "only displays the date when author and summary are nil" do
113
+ report.stub(:summary => nil, :author => nil)
114
+ report.cover_page.should == "Hash Merge\n==========\nDate: #{date.to_s} "
115
+ end
116
+ end
117
+ end
@@ -0,0 +1,4 @@
1
+ require 'spec_helper'
2
+
3
+ describe BenchPress::Result do
4
+ end
@@ -0,0 +1,66 @@
1
+ require 'spec_helper'
2
+
3
+ describe BenchPress::SystemInformation do
4
+ before do
5
+ BenchPress::SystemInformation.stub(:ruby_version => "1.8.7 patchlevel 174")
6
+ end
7
+
8
+ context "for Mac OS X" do
9
+ before do
10
+ @facts = {
11
+ 'sp_physical_memory' => '4 GB',
12
+ 'sp_os_version' => 'Mac OS X 10.6.2 (10C540)',
13
+ 'sp_cpu_type' => 'Intel Core 2 Duo',
14
+ 'sp_current_processor_speed' => '2.4 GHz',
15
+ 'sp_number_processors' => '2',
16
+ 'sp_serial_number' => '123ABC'
17
+ }
18
+ BenchPress::SystemInformation.stub(:mac? => true)
19
+ BenchPress::SystemInformation.stub(:facts => @facts)
20
+ end
21
+
22
+ it "summarizes system information" do
23
+ summary = <<-EOS
24
+ Operating System: Mac OS X 10.6.2 (10C540)
25
+ CPU: Intel Core 2 Duo 2.4 GHz
26
+ Processor Count: 2
27
+ Memory: 4 GB
28
+ Ruby version: 1.8.7 patchlevel 174
29
+ EOS
30
+ BenchPress::SystemInformation.summary.should == summary.strip
31
+ end
32
+
33
+ it "encrypts the serial number" do
34
+ BenchPress::SystemInformation.crypted_identifier.should == Digest::SHA1.hexdigest("123ABC")
35
+ end
36
+ end
37
+
38
+ context "for Linux" do
39
+ before do
40
+ @facts = {
41
+ 'memorysize' => '254.75 MB',
42
+ 'lsbdistdescription' => 'Ubuntu 8.10',
43
+ 'processor0' => 'Dual Core AMD Opteron(tm) Processor 270',
44
+ 'processorcount' => '4',
45
+ 'uniqueid' => '123ABC'
46
+ }
47
+ BenchPress::SystemInformation.stub(:mac? => false)
48
+ BenchPress::SystemInformation.stub(:facts => @facts)
49
+ end
50
+
51
+ it "summarizes system information" do
52
+ summary = <<-EOS
53
+ Operating System: Ubuntu 8.10
54
+ CPU: Dual Core AMD Opteron(tm) Processor 270
55
+ Processor Count: 4
56
+ Memory: 254.75 MB
57
+ Ruby version: 1.8.7 patchlevel 174
58
+ EOS
59
+ BenchPress::SystemInformation.summary.should == summary.strip
60
+ end
61
+
62
+ it "encrypts the unique id" do
63
+ BenchPress::SystemInformation.crypted_identifier.should == Digest::SHA1.hexdigest("123ABC")
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,49 @@
1
+ require 'spec_helper'
2
+
3
+ module TestModule
4
+ extend BenchPress
5
+ end
6
+
7
+ describe BenchPress do
8
+ describe "#default_report_name" do
9
+ it "is the name of the enclosing module" do
10
+ mod = Module.new do
11
+ def self.name
12
+ "ModuleName"
13
+ end
14
+ extend BenchPress
15
+ end
16
+ mod.send(:default_report_name).should == "ModuleName"
17
+ end
18
+
19
+ it "is the name of the ruby script when the extending class has no name" do
20
+ mod = Module.new do
21
+ class << self
22
+ undef name
23
+ end
24
+ extend BenchPress
25
+ end
26
+ mod.send(:default_report_name).should == "Bench Press"
27
+ end
28
+ end
29
+
30
+ describe "#name" do
31
+ it "sets the name of the report" do
32
+ TestModule.module_eval do
33
+ name "Foo versus Bar"
34
+ end
35
+ TestModule.report.name.should == "Foo versus Bar"
36
+ end
37
+
38
+ context "when no argument is provided" do
39
+ it "returns the module name when no argument is provided" do
40
+ TestModule.name.should == "TestModule"
41
+ end
42
+
43
+ it "rescues TypeError and returns nil" do
44
+ Module.stub(:instance_method).and_raise(TypeError)
45
+ TestModule.name.should be_nil
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,8 @@
1
+ begin; require 'rubygems'; rescue LoadError; end
2
+ require 'bench_press'
3
+ require 'spec'
4
+ require 'spec/autorun'
5
+
6
+
7
+ Spec::Runner.configure do |config|
8
+ end
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bench_press
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Sandro Turriate
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-01-20 00:00:00 -05:00
13
+ default_executable: bench_press
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: facter
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - "="
22
+ - !ruby/object:Gem::Version
23
+ version: 1.5.7
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: activesupport
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "="
32
+ - !ruby/object:Gem::Version
33
+ version: 2.3.5
34
+ version:
35
+ - !ruby/object:Gem::Dependency
36
+ name: rspec
37
+ type: :development
38
+ version_requirement:
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - "="
42
+ - !ruby/object:Gem::Version
43
+ version: 1.2.9
44
+ version:
45
+ description: Sharable benchmarks
46
+ email: sandro.turriate@gmail.com
47
+ executables:
48
+ - bench_press
49
+ extensions: []
50
+
51
+ extra_rdoc_files:
52
+ - README.rdoc
53
+ - TODO
54
+ files:
55
+ - .document
56
+ - .gitignore
57
+ - MIT-LICENSE
58
+ - README.rdoc
59
+ - Rakefile
60
+ - VERSION
61
+ - bench_press.gemspec
62
+ - bin/bench_press
63
+ - examples/compare_rr_to_rspec.rb
64
+ - examples/existence_of_method.rb
65
+ - examples/hash_merge.rb
66
+ - examples/implicit_versus_explicit_return.rb
67
+ - lib/bench_press.rb
68
+ - lib/bench_press/report.rb
69
+ - lib/bench_press/result.rb
70
+ - lib/bench_press/runnable.rb
71
+ - lib/bench_press/system_information.rb
72
+ - spec/bench_press/report_spec.rb
73
+ - spec/bench_press/result_spec.rb
74
+ - spec/bench_press/system_information_spec.rb
75
+ - spec/bench_press_spec.rb
76
+ - spec/spec_helper.rb
77
+ - TODO
78
+ has_rdoc: true
79
+ homepage: http://github.com/sandro/bench_press
80
+ licenses: []
81
+
82
+ post_install_message:
83
+ rdoc_options:
84
+ - --charset=UTF-8
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: "0"
92
+ version:
93
+ required_rubygems_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: "0"
98
+ version:
99
+ requirements: []
100
+
101
+ rubyforge_project:
102
+ rubygems_version: 1.3.5
103
+ signing_key:
104
+ specification_version: 3
105
+ summary: Sharable benchmarks
106
+ test_files:
107
+ - spec/bench_press/report_spec.rb
108
+ - spec/bench_press/result_spec.rb
109
+ - spec/bench_press/system_information_spec.rb
110
+ - spec/bench_press_spec.rb
111
+ - spec/spec_helper.rb
112
+ - examples/compare_rr_to_rspec.rb
113
+ - examples/existence_of_method.rb
114
+ - examples/hash_merge.rb
115
+ - examples/implicit_versus_explicit_return.rb