nested-benchmark 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
data/HISTORY ADDED
@@ -0,0 +1,7 @@
1
+ = History
2
+
3
+ == 0.8.0, 2007-12-04
4
+
5
+ Initial public release with basic features and a little testing.
6
+
7
+ * TODO: Build out tests, move output to ruport
data/README ADDED
@@ -0,0 +1,72 @@
1
+ = Nested Benchmark
2
+
3
+ == Author
4
+
5
+ Bruce Williams <bruce@codefluency.com>
6
+ http://codefluency.com
7
+
8
+ == Synopsis
9
+
10
+ The purpose of the nested-benchmark library is to add simple support for:
11
+ * Named, nested benchmarks that you don't have to keep track of to output later
12
+ * Blocks of code you want ignored by the benchmarks
13
+
14
+ == Usage
15
+
16
+ Requiring the library is intentionally similar to the Ruby Standard Library 'benchmark'
17
+
18
+ require 'benchmark/nested'
19
+
20
+ Note that it's 'benchmark/nested' and NOT 'nested/benchmark'
21
+
22
+ This adds two methods to Object; +benchmark+ and +ignore+. Here's an example of usage:
23
+
24
+ benchmark "Insert 30 records" do
25
+ ignore do
26
+ # Some database setup you don't want benchmarked
27
+ end
28
+ 1.upto(30) do |number|
29
+ benchmark "Prepare record ##{number} for insertion" do
30
+ sleep rand(0.3)
31
+ # something that takes some time
32
+ end
33
+ sleep rand(0.1)
34
+ # insert into database
35
+ end
36
+ end
37
+
38
+ Afterwards, you can get the results by:
39
+
40
+ puts Benchmark::CAPTION # Just for some headers
41
+ NestedBenchmark.each do |benchmark|
42
+ # Note these are the toplevel benchmarks
43
+ puts benchmark
44
+ end
45
+
46
+ and get something like:
47
+
48
+ user system total real
49
+ 0.010000 0.000000 0.010000 ( 25.789001) Insert 30 records
50
+ 0.000000 0.000000 0.000000 ( 0.409687) > Prepare record #1 for insertion
51
+ 0.000000 0.000000 0.000000 ( 0.332127) > Prepare record #2 for insertion
52
+ 0.000000 0.000000 0.000000 ( 0.774727) > Prepare record #3 for insertion
53
+ 0.000000 0.000000 0.000000 ( 0.522462) > Prepare record #4 for insertion
54
+ 0.000000 0.000000 0.000000 ( 0.099711) > Prepare record #5 for insertion
55
+
56
+ You could also crawl the benchmarks manually if you want:
57
+
58
+ NestedBenchmark.each do |benchmark|
59
+ benchmark.each do |child|
60
+ child.each do |grandchild|
61
+ # ...
62
+ end
63
+ end
64
+ end
65
+
66
+ == Caveats
67
+
68
+ Don't put a +benchmark+ inside an +ignore+.
69
+
70
+ It doesn't really make any sense, and neither will the results.
71
+
72
+
data/Rakefile ADDED
@@ -0,0 +1,42 @@
1
+ require 'rake/testtask'
2
+ require 'rubygems/specification'
3
+ require 'rake/gempackagetask'
4
+ require 'rake/rdoctask'
5
+
6
+ task :default => :test
7
+
8
+ Rake::TestTask.new do |t|
9
+ t.libs = %w(lib)
10
+ t.test_files = FileList["test/**/*.rb"]
11
+ end
12
+
13
+ Rake::RDocTask.new do |d|
14
+ d.main = "README"
15
+ d.rdoc_files.include("README", "HISTORY", "lib/**/*.rb")
16
+ end
17
+
18
+ PKG_VERSION = '0.8.0'
19
+ PKG_FILES = FileList['**/*'].exclude(/CVS|\.svn|pkg|coverage|Makefile|doc|rdoc|html/)
20
+ GEMSPEC = Gem::Specification.new do |s|
21
+ s.name = 'nested-benchmark'
22
+ s.version = PKG_VERSION
23
+ s.summary = "Support for nested bechmarking and ignored blocks"
24
+ s.description = "Add support for simple named benchmarking blocks, blocks to ignore during benchmarking, and output of benchmarks"
25
+ s.files = PKG_FILES
26
+ s.require_path = 'lib'
27
+
28
+ s.has_rdoc = true
29
+ s.rdoc_options <<
30
+ '--main' << 'NestedBenchmark' << '--line-numbers'
31
+
32
+ s.author = "Bruce Williams"
33
+ s.email = "bruce@codefluency.com"
34
+ s.homepage = "http://codefluency.rubyforge.org/nested-benchmark"
35
+ s.rubyforge_project = "codefluency"
36
+ end
37
+
38
+ Rake::GemPackageTask.new(GEMSPEC) do |pkg|
39
+ pkg.need_tar = true
40
+ pkg.package_files += PKG_FILES
41
+ end
42
+
@@ -0,0 +1,5 @@
1
+ require File.dirname(__FILE__) << "/nested/benchmark"
2
+
3
+ class Object
4
+ include NestedBenchmark
5
+ end
@@ -0,0 +1,81 @@
1
+ require File.dirname(__FILE__) << '/tms_ext'
2
+
3
+ module NestedBenchmark
4
+
5
+ class << self
6
+
7
+ include Enumerable
8
+
9
+ def each(&block)
10
+ benchmarks.each(&block)
11
+ end
12
+
13
+ def benchmarks
14
+ @benchmarks ||= []
15
+ end
16
+
17
+ def children_stack
18
+ @children_stack ||= []
19
+ end
20
+
21
+ def ignores
22
+ @ignores ||= []
23
+ end
24
+
25
+ def included(base)
26
+ base.__send__(:include, InstanceMethods)
27
+ end
28
+
29
+ def ignore_in_parent(offset)
30
+ NestedBenchmark.ignores.last << offset unless NestedBenchmark.ignores.empty?
31
+ end
32
+
33
+ def ignore
34
+ offset = sum(NestedBenchmark.ignores.pop)
35
+ ignore_in_parent offset
36
+ offset
37
+ end
38
+
39
+ def calculate(&block)
40
+ NestedBenchmark.ignores << []
41
+ yield - ignore
42
+ end
43
+
44
+ def reset!
45
+ benchmarks.clear
46
+ end
47
+
48
+ def sum(benchmarks)
49
+ benchmarks.inject(Benchmark::Tms.new) do |result, benchmark|
50
+ result + benchmark
51
+ end
52
+ end
53
+
54
+ def add(benchmark)
55
+ if NestedBenchmark.children_stack.empty?
56
+ NestedBenchmark.benchmarks << benchmark
57
+ else
58
+ NestedBenchmark.children_stack.last << benchmark
59
+ end
60
+ benchmark
61
+ end
62
+
63
+ end
64
+
65
+ module InstanceMethods
66
+
67
+ def benchmark(name=nil, &block)
68
+ NestedBenchmark.children_stack << []
69
+ result = NestedBenchmark.calculate { Benchmark.measure(&block) }
70
+ result.label = name
71
+ result.children = NestedBenchmark.children_stack.pop
72
+ NestedBenchmark.add result
73
+ end
74
+
75
+ def ignore(&block)
76
+ NestedBenchmark.ignores.last << Benchmark.measure(&block)
77
+ end
78
+
79
+ end
80
+
81
+ end
@@ -0,0 +1,32 @@
1
+ require 'benchmark'
2
+
3
+ module Benchmark
4
+
5
+ class Tms
6
+
7
+ include Enumerable
8
+
9
+ attr_accessor :label
10
+ attr_writer :children
11
+ def children
12
+ @children ||= []
13
+ end
14
+
15
+ def each(&block)
16
+ children.each(&block)
17
+ end
18
+
19
+ def to_s(indent=0)
20
+ lines = []
21
+ lines << format(FMTSTR).rstrip
22
+ indenter = ('>' * indent) + ' ' if indent > 0
23
+ lines.last << " #{indenter}#{@label}"
24
+ children.each do |child|
25
+ lines << child.to_s(indent + 1)
26
+ end
27
+ lines.join("\n")
28
+ end
29
+
30
+ end
31
+
32
+ end
@@ -0,0 +1,44 @@
1
+ require 'rubygems'
2
+ require 'test/spec'
3
+
4
+ require File.dirname(__FILE__) << "/test_helper"
5
+
6
+ context "Nested Benchmark" do
7
+
8
+ specify "can use in arbitrary objects" do
9
+ assert_supports_benchmarking_in Object.new
10
+ assert_supports_benchmarking_in self
11
+ assert_supports_benchmarking_in Class.new
12
+ assert_supports_benchmarking_in Class.new.new
13
+ end
14
+
15
+ specify "benchmark returns a benchmark with children" do
16
+ b = benchmark { benchmark { } }
17
+ assert_kind_of Benchmark::Tms, b
18
+ assert_kind_of Array, b.children
19
+ assert_respond_to b, :each
20
+ b.each do |c|
21
+ assert_kind_of Benchmark::Tms, c
22
+ end
23
+ end
24
+
25
+ specify "can reset toplevel benchmarks" do
26
+ benchmark { benchmark { } }
27
+ assert 1, benchmarks.size
28
+ NestedBenchmark.reset!
29
+ assert 0, benchmarks.size
30
+ end
31
+
32
+ #######
33
+ private
34
+ #######
35
+
36
+ def benchmarks
37
+ NestedBenchmark.benchmarks
38
+ end
39
+
40
+ def assert_supports_benchmarking_in(object)
41
+ %w(benchmark ignore).all? { |m| object.respond_to?(m) }
42
+ end
43
+
44
+ end
@@ -0,0 +1,3 @@
1
+ $:.unshift File.dirname(__FILE__) + "/../lib"
2
+
3
+ require 'benchmark/nested'
metadata ADDED
@@ -0,0 +1,59 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.9.4
3
+ specification_version: 1
4
+ name: nested-benchmark
5
+ version: !ruby/object:Gem::Version
6
+ version: 0.8.0
7
+ date: 2007-12-04 00:00:00 -06:00
8
+ summary: Support for nested bechmarking and ignored blocks
9
+ require_paths:
10
+ - lib
11
+ email: bruce@codefluency.com
12
+ homepage: http://codefluency.rubyforge.org/nested-benchmark
13
+ rubyforge_project: codefluency
14
+ description: Add support for simple named benchmarking blocks, blocks to ignore during benchmarking, and output of benchmarks
15
+ autorequire:
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: true
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
+ - Bruce Williams
31
+ files:
32
+ - HISTORY
33
+ - lib
34
+ - lib/benchmark
35
+ - lib/benchmark/nested
36
+ - lib/benchmark/nested/benchmark.rb
37
+ - lib/benchmark/nested/tms_ext.rb
38
+ - lib/benchmark/nested.rb
39
+ - Rakefile
40
+ - README
41
+ - test
42
+ - test/nested_benchmark_test.rb
43
+ - test/test_helper.rb
44
+ test_files: []
45
+
46
+ rdoc_options:
47
+ - --main
48
+ - NestedBenchmark
49
+ - --line-numbers
50
+ extra_rdoc_files: []
51
+
52
+ executables: []
53
+
54
+ extensions: []
55
+
56
+ requirements: []
57
+
58
+ dependencies: []
59
+