easybench 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,5 @@
1
+ *.sw?
2
+ .DS_Store
3
+ coverage
4
+ rdoc
5
+ pkg
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 jameswilding
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,64 @@
1
+ # bme: benchmark everything
2
+
3
+ BME is a simple wrapper around ruby's Benchmark library which helps you write benchmark code that is clearer, simpler, and more meaningful.
4
+
5
+ A normal benchmark with the 'benchmark' library can quickly get untidy:
6
+
7
+ require 'benchmark'
8
+
9
+ iterations = 100,000
10
+
11
+ Benchmark.bm do |bm|
12
+ bm.report 'some_method' do
13
+ iterations.times { Library.some_method(args) }
14
+ end
15
+ end
16
+
17
+ As you add more benchmarks, those iterations will start to get in the way of the important code. A benchmark with BME is cleaner -- the code being tested is more apparent:
18
+
19
+ require 'bme'
20
+
21
+ BME.benchmark do |bm|
22
+ bm.report 'some_method' do
23
+ Library.some_method(args)
24
+ end
25
+ end
26
+
27
+ By default, BME iterates over each piece of code 100,000 times. Use the :iterations option to override the default behaviour for all benchmarks:
28
+
29
+ BME.benchmark(:iterations => 500) do |bm|
30
+ # ...
31
+ end
32
+
33
+ Or on a per-benchmark basis:
34
+
35
+ BME.benchmark do |bm|
36
+ bm.report 'some_method' do
37
+ Library.some_method(args)
38
+ end
39
+
40
+ bm.report 'other_method', :iterations => 500 do
41
+ Library.other_method(args)
42
+ end
43
+ end
44
+
45
+ That's it!
46
+
47
+ ## Installation
48
+
49
+ $ gem install bme --source http://gemcutter.org
50
+
51
+ ## Note on Patches/Pull Requests
52
+
53
+ * Fork the project.
54
+ * Make your feature addition or bug fix.
55
+ * Add tests for it. This is important so I don't break it in a
56
+ future version unintentionally.
57
+ * Commit, do not mess with rakefile, version, or history.
58
+ (if you want to have your own version, that is fine but
59
+ bump version in a commit by itself I can ignore when I pull)
60
+ * Send me a pull request. Bonus points for topic branches.
61
+
62
+ ## Copyright
63
+
64
+ Copyright (c) 2009 James Wilding. See LICENSE for details.
@@ -0,0 +1,18 @@
1
+ = easybench
2
+
3
+ Description goes here.
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
13
+ bump version in a commit by itself I can ignore when I pull)
14
+ * Send me a pull request. Bonus points for topic branches.
15
+
16
+ == Copyright
17
+
18
+ Copyright (c) 2009 jameswilding. See LICENSE for details.
@@ -0,0 +1,58 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "easybench"
8
+ gem.summary = %Q{Cleaner, simpler ruby benchmarks}
9
+ gem.description = %Q{A simple wrapper around ruby's Benchmark library for cleaner, simpler benchmark tests}
10
+ gem.email = "james@jameswilding.net"
11
+ gem.homepage = "http://github.com/jameswilding/easybench/"
12
+ gem.authors = ["jameswilding"]
13
+ gem.add_development_dependency "thoughtbot-shoulda"
14
+ gem.add_development_dependency "redgreen"
15
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
16
+ end
17
+ Jeweler::GemcutterTasks.new
18
+ rescue LoadError
19
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
20
+ end
21
+
22
+ require 'rake/testtask'
23
+ Rake::TestTask.new(:test) do |test|
24
+ test.libs << 'lib' << 'test'
25
+ test.pattern = 'test/**/*_test.rb'
26
+ test.verbose = true
27
+ end
28
+
29
+ begin
30
+ require 'rcov/rcovtask'
31
+ Rcov::RcovTask.new do |test|
32
+ test.libs << 'test'
33
+ test.pattern = 'test/**/*_test.rb'
34
+ test.verbose = true
35
+ end
36
+ rescue LoadError
37
+ task :rcov do
38
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
39
+ end
40
+ end
41
+
42
+ task :test #=> :check_dependencies
43
+
44
+ task :default => :test
45
+
46
+ require 'rake/rdoctask'
47
+ Rake::RDocTask.new do |rdoc|
48
+ if File.exist?('VERSION')
49
+ version = File.read('VERSION')
50
+ else
51
+ version = ""
52
+ end
53
+
54
+ rdoc.rdoc_dir = 'rdoc'
55
+ rdoc.title = "easybench #{version}"
56
+ rdoc.rdoc_files.include('README*')
57
+ rdoc.rdoc_files.include('lib/**/*.rb')
58
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,61 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{easybench}
5
+ s.version = "0.1.0"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["jameswilding"]
9
+ s.date = %q{2009-10-24}
10
+ s.description = %q{A simple wrapper around ruby's Benchmark library for cleaner, simpler benchmark tests}
11
+ s.email = %q{james@jameswilding.net}
12
+ s.extra_rdoc_files = [
13
+ "LICENSE",
14
+ "README.markdown",
15
+ "README.rdoc"
16
+ ]
17
+ s.files = [
18
+ ".document",
19
+ ".gitignore",
20
+ "LICENSE",
21
+ "README.markdown",
22
+ "README.rdoc",
23
+ "Rakefile",
24
+ "VERSION",
25
+ "easybench.gemspec",
26
+ "examples/benchmark.rb",
27
+ "lib/easybench.rb",
28
+ "test/benchmark_test.rb",
29
+ "test/easybench_test.rb",
30
+ "test/suite_test.rb",
31
+ "test/test_helper.rb"
32
+ ]
33
+ s.homepage = %q{http://github.com/jameswilding/easybench/}
34
+ s.rdoc_options = ["--charset=UTF-8"]
35
+ s.require_paths = ["lib"]
36
+ s.rubygems_version = %q{1.3.5}
37
+ s.summary = %q{Cleaner, simpler ruby benchmarks}
38
+ s.test_files = [
39
+ "test/benchmark_test.rb",
40
+ "test/easybench_test.rb",
41
+ "test/suite_test.rb",
42
+ "test/test_helper.rb",
43
+ "examples/benchmark.rb"
44
+ ]
45
+
46
+ if s.respond_to? :specification_version then
47
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
48
+ s.specification_version = 3
49
+
50
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
51
+ s.add_development_dependency(%q<thoughtbot-shoulda>, [">= 0"])
52
+ s.add_development_dependency(%q<redgreen>, [">= 0"])
53
+ else
54
+ s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
55
+ s.add_dependency(%q<redgreen>, [">= 0"])
56
+ end
57
+ else
58
+ s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
59
+ s.add_dependency(%q<redgreen>, [">= 0"])
60
+ end
61
+ end
@@ -0,0 +1,16 @@
1
+ require File.dirname(__FILE__) + '/../lib/bme'
2
+
3
+ Easybench.bm do |bm|
4
+ bm.report 'Adding' do
5
+ 1 + 1
6
+ end
7
+
8
+ bm.report 'Subtracting' do
9
+ 1 - 1
10
+ end
11
+
12
+ bm.report 'Division', :iterations => 200000 do
13
+ 1 / 1
14
+ end
15
+
16
+ end
@@ -0,0 +1,86 @@
1
+ require 'benchmark'
2
+
3
+ module Easybench # BenchMark Everything
4
+
5
+ DEFAULT_ITERATIONS = 100000
6
+
7
+ class << self
8
+ # Use :iterations => n to set n number of iterations (defaults to 100,000)
9
+ def bm(options = {}, &block)
10
+ suite = Suite.new(options[:iterations] || 100000, &block)
11
+
12
+ if options[:run] == false
13
+ suite
14
+ else
15
+ suite.run
16
+ end
17
+ end
18
+ end
19
+
20
+ class Benchmark
21
+
22
+ attr_reader :options
23
+ attr_reader :tests
24
+
25
+ def initialize(name, options = {}, &tests)
26
+ @name = name
27
+ @options = options
28
+ @tests = tests
29
+ end
30
+
31
+ def name(indent = nil)
32
+ indent ? @name.rjust(indent) : @name
33
+ end
34
+
35
+ def iterations
36
+ @options[:iterations]
37
+ end
38
+
39
+ end
40
+
41
+ class Suite
42
+ # Raised when two benchmark tests with the same name are defined
43
+ class DuplicateBenchmark < StandardError; end
44
+
45
+ attr_reader :tests
46
+ attr_reader :benchmarks
47
+
48
+ def initialize(iterations, &tests)
49
+ @iterations = iterations
50
+ @tests = tests
51
+ @benchmarks = []
52
+ end
53
+
54
+ def add_benchmark(name, options = {}, &block)
55
+ if benchmark_defined?(name)
56
+ raise DuplicateBenchmark, "A benchmark test called '#{name}' has already been defined"
57
+ else
58
+ @benchmarks << Easybench::Benchmark.new(name, options, &block)
59
+ end
60
+ end
61
+
62
+ alias :report :add_benchmark
63
+
64
+ def run(runner = ::Benchmark)
65
+ @tests.call(self)
66
+
67
+ runner.bm do |interface|
68
+ @benchmarks.each do |benchmark|
69
+ interface.report(benchmark.name(indent)) do
70
+ (benchmark.iterations || @iterations).times(&benchmark.tests)
71
+ end
72
+ end
73
+ end
74
+ end
75
+
76
+ private
77
+ def benchmark_defined?(name)
78
+ @benchmarks.any? { |benchmark| benchmark.name == name }
79
+ end
80
+
81
+ def indent
82
+ @indent ||= @benchmarks.map { |b| b.name }.sort.last.length
83
+ end
84
+
85
+ end
86
+ end
@@ -0,0 +1,49 @@
1
+ require 'test_helper'
2
+
3
+ class BenchmarkTest < Test::Unit::TestCase
4
+
5
+ context 'a BME::Benchmark instance' do
6
+ should 'initialize with name' do
7
+ name = 'test'
8
+ bm = create_benchmark(name)
9
+
10
+ assert_equal name, bm.name
11
+ end
12
+
13
+ should 'initialize with options' do
14
+ name = 'test'
15
+ options = {:iterations => 10}
16
+ bm = create_benchmark(name, options)
17
+
18
+ assert_equal options, bm.options
19
+ end
20
+
21
+ should 'know how many times to iterate' do
22
+ bm = create_benchmark('test', :iterations => 10)
23
+
24
+ assert_equal 10, bm.iterations
25
+ end
26
+
27
+ should 'initialize with a block' do
28
+ name = 'test'
29
+ tests = lambda { 1 + 1 }
30
+ bm = create_benchmark(name, &tests)
31
+
32
+ assert_equal tests, bm.tests
33
+ end
34
+
35
+ should 'indent its name if requested to' do
36
+ name = 'test'
37
+ bm = create_benchmark(name)
38
+
39
+ assert_equal name, bm.name
40
+ assert_equal name.rjust(10), bm.name(10)
41
+ end
42
+
43
+ end
44
+
45
+ def create_benchmark(name, options = {}, &block)
46
+ Easybench::Benchmark.new(name, options, &block)
47
+ end
48
+
49
+ end
@@ -0,0 +1,19 @@
1
+ require 'test_helper'
2
+
3
+ class BmeTest < Test::Unit::TestCase
4
+
5
+ context 'Easybench' do
6
+ should 'initiate a benchmark suite without a block' do
7
+ assert_nothing_raised { suite = Easybench.bm(:run => false) }
8
+ end
9
+
10
+ should 'initialize a benchmark suite with a block' do
11
+ tests = lambda { |bm| bm.report('adding') { 1 + 1 } }
12
+ suite = Easybench.bm({:run => false}, &tests)
13
+
14
+ assert_kind_of Easybench::Suite, suite
15
+ assert_not_nil suite.tests
16
+ assert_equal tests, suite.tests
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,42 @@
1
+ require 'test_helper'
2
+
3
+ class SuiteTest < Test::Unit::TestCase
4
+
5
+ context 'a BME::Suite instance' do
6
+
7
+ setup { @suite = Easybench.bm(:run => false) }
8
+
9
+ context 'initialized without a block' do
10
+ should 'have no benchmarks' do
11
+ assert @suite.benchmarks.empty?
12
+ end
13
+ end
14
+
15
+ context 'initializing with a block' do
16
+ should 'add benchmarks from the block' do
17
+ @suite.add_benchmark('adding') { 1 + 1 }
18
+ assert_equal 1, @suite.benchmarks.length
19
+ end
20
+
21
+ should 'add benchmarks as BME::Becnhmark objects' do
22
+ @suite.add_benchmark('adding') { 1 + 1 }
23
+ assert_kind_of Easybench::Benchmark, @suite.benchmarks.last
24
+ end
25
+
26
+ should 'set individual benchmark options' do
27
+ options = {:iterations => 10}
28
+ @suite.add_benchmark('subtracting', options) { 1 - 1 }
29
+ assert_equal options, @suite.benchmarks.last.options
30
+ end
31
+
32
+ should 'raise an exception when a duplicate benchmark is added' do
33
+ assert_raises(Easybench::Suite::DuplicateBenchmark) do
34
+ @suite.add_benchmark('adding') { 1 + 1 }
35
+ @suite.add_benchmark('adding') { 1 + 1 }
36
+ end
37
+ end
38
+
39
+ end
40
+ end
41
+
42
+ end
@@ -0,0 +1,11 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+ require 'redgreen'
5
+
6
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
7
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
8
+ require 'easybench'
9
+
10
+ class Test::Unit::TestCase
11
+ end
metadata ADDED
@@ -0,0 +1,93 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: easybench
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - jameswilding
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-10-24 00:00:00 +01:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: thoughtbot-shoulda
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: redgreen
27
+ type: :development
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: "0"
34
+ version:
35
+ description: A simple wrapper around ruby's Benchmark library for cleaner, simpler benchmark tests
36
+ email: james@jameswilding.net
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files:
42
+ - LICENSE
43
+ - README.markdown
44
+ - README.rdoc
45
+ files:
46
+ - .document
47
+ - .gitignore
48
+ - LICENSE
49
+ - README.markdown
50
+ - README.rdoc
51
+ - Rakefile
52
+ - VERSION
53
+ - easybench.gemspec
54
+ - examples/benchmark.rb
55
+ - lib/easybench.rb
56
+ - test/benchmark_test.rb
57
+ - test/easybench_test.rb
58
+ - test/suite_test.rb
59
+ - test/test_helper.rb
60
+ has_rdoc: true
61
+ homepage: http://github.com/jameswilding/easybench/
62
+ licenses: []
63
+
64
+ post_install_message:
65
+ rdoc_options:
66
+ - --charset=UTF-8
67
+ require_paths:
68
+ - lib
69
+ required_ruby_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: "0"
74
+ version:
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: "0"
80
+ version:
81
+ requirements: []
82
+
83
+ rubyforge_project:
84
+ rubygems_version: 1.3.5
85
+ signing_key:
86
+ specification_version: 3
87
+ summary: Cleaner, simpler ruby benchmarks
88
+ test_files:
89
+ - test/benchmark_test.rb
90
+ - test/easybench_test.rb
91
+ - test/suite_test.rb
92
+ - test/test_helper.rb
93
+ - examples/benchmark.rb