benchmarkx 001

Sign up to get free protection for your applications and to get access to all the features.
data/.gemified ADDED
@@ -0,0 +1,11 @@
1
+ ---
2
+ :summary: BenchmarkX is a extension of standard benchmark.rb for rendering sexy graph using gruff.
3
+ :email: keita.yamaguchi@gmail.com
4
+ :has_rdoc: true
5
+ :name: benchmarkx
6
+ :homepage: http://rubyforge.org/projects/benchmarkx/
7
+ :version: "001"
8
+ :dependencies:
9
+ - gruff
10
+ :rubyforge_project: benchmarkx
11
+ :author: Keita Yamaguchi
data/History.txt ADDED
@@ -0,0 +1,5 @@
1
+ = 001: 2008-04-12
2
+
3
+ * Keita Yamaguchi:
4
+ * Initial release
5
+
data/License.txt ADDED
@@ -0,0 +1,56 @@
1
+ BenchmarkX is copyrighted free software by Keita Yamaguchi <keita.yamaguchi@gmail.com>.
2
+ You can redistribute it and/or modify it under either the terms of the GPL
3
+ (see the file GPL), or the conditions below:
4
+
5
+ 1. You may make and give away verbatim copies of the source form of the
6
+ software without restriction, provided that you duplicate all of the
7
+ original copyright notices and associated disclaimers.
8
+
9
+ 2. You may modify your copy of the software in any way, provided that
10
+ you do at least ONE of the following:
11
+
12
+ a) place your modifications in the Public Domain or otherwise
13
+ make them Freely Available, such as by posting said
14
+ modifications to Usenet or an equivalent medium, or by allowing
15
+ the author to include your modifications in the software.
16
+
17
+ b) use the modified software only within your corporation or
18
+ organization.
19
+
20
+ c) give non-standard binaries non-standard names, with
21
+ instructions on where to get the original software distribution.
22
+
23
+ d) make other distribution arrangements with the author.
24
+
25
+ 3. You may distribute the software in object code or binary form,
26
+ provided that you do at least ONE of the following:
27
+
28
+ a) distribute the binaries and library files of the software,
29
+ together with instructions (in the manual page or equivalent)
30
+ on where to get the original distribution.
31
+
32
+ b) accompany the distribution with the machine-readable source of
33
+ the software.
34
+
35
+ c) give non-standard binaries non-standard names, with
36
+ instructions on where to get the original software distribution.
37
+
38
+ d) make other distribution arrangements with the author.
39
+
40
+ 4. You may modify and include the part of the software into any other
41
+ software (possibly commercial). But some files in the distribution
42
+ are not written by the author, so that they are not under these terms.
43
+
44
+ For the list of those files and their copying conditions, see the
45
+ file LEGAL.
46
+
47
+ 5. The scripts and library files supplied as input to or produced as
48
+ output from the software do not automatically fall under the
49
+ copyright of the software, but belong to whomever generated them,
50
+ and may be sold commercially, and may be aggregated with this
51
+ software.
52
+
53
+ 6. THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
54
+ IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
55
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
56
+ PURPOSE.
data/README.txt ADDED
@@ -0,0 +1,19 @@
1
+ = BenchmarkX
2
+
3
+ Author:: Keita Yamaguchi(山口 慶太) <keita.yamaguchi@gmail.com>
4
+ Copyright:: © Keita Yamaguchi, 2008. All rights reserved.
5
+ License:: Ruby License
6
+
7
+ BenchmarkX is a Ruby library for extending standard benchmark with sexy graph!
8
+
9
+ == Usage
10
+
11
+ See {the documentation}[http://github.com/keita/benchmarkx/wikis].
12
+
13
+ == Links
14
+
15
+ * BenchmarkX
16
+ * {RubyForge}[http://rubyforge.org/projects/weatherhacks/]
17
+ * {GitHub}[http://github.com/keita/ruby-weatherhacks/tree/master]
18
+ * author's blog(written in Japanese)
19
+ * {¬¬日常日記}[http://d.hatena.ne.jp/keita_yamaguchi/]
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require "rubygems"
2
+ require "rtask"
3
+
4
+ RTask.new
5
+
6
+ desc "Run the specs"
7
+ task "spec" do
8
+ sh "bacon spec/benchmarkx_spec.rb"
9
+ end
data/lib/benchmarkx.rb ADDED
@@ -0,0 +1,164 @@
1
+ require "benchmark"
2
+ require "gruff"
3
+
4
+ module BenchmarkX
5
+ VERSION = "001"
6
+
7
+ include Benchmark
8
+
9
+ alias :benchmark_orig :benchmark
10
+
11
+ # Same as Benchmark.benchmark.
12
+ def benchmark(caption = "", label_width = nil, fmtstr = nil, *labels)
13
+ # copied from benchmark.rb
14
+ sync = STDOUT.sync
15
+ STDOUT.sync = true
16
+ label_width ||= 0
17
+ fmtstr ||= FMTSTR
18
+ raise ArgumentError, "no block" unless iterator?
19
+ print caption
20
+
21
+ # modified
22
+ report = Report.new(label_width, fmtstr)
23
+ results = yield(report)
24
+
25
+ # copied from benchmark.rb and modified
26
+ Array === results and results.grep(Tms).each {|t|
27
+ label = labels.shift
28
+ print((label || t.label || "").ljust(label_width), t.format(fmtstr))
29
+ report.record(label, t)
30
+ }
31
+ STDOUT.sync = sync
32
+
33
+ # new
34
+ report.render
35
+ end
36
+
37
+ alias :bmbm_orig :bmbm
38
+
39
+ # Same as Benchmark.bmbm
40
+ def bmbm(width = 0, &blk)
41
+ # copied from benchmark.rb
42
+ job = Job.new(width)
43
+ yield(job)
44
+ width = job.width
45
+ sync = STDOUT.sync
46
+ STDOUT.sync = true
47
+
48
+ # copied from benchmark.rb
49
+ # rehearsal
50
+ print "Rehearsal "
51
+ puts '-'*(width+CAPTION.length - "Rehearsal ".length)
52
+ list = []
53
+ rehearsal = Report.new # appended
54
+ job.list.each{|label,item|
55
+ print(label.ljust(width))
56
+ res = Benchmark::measure(&item)
57
+ print res.format()
58
+ list.push res
59
+
60
+ # new
61
+ rehearsal.record(label, res)
62
+ }
63
+ sum = Tms.new; list.each{|i| sum += i}
64
+ ets = sum.format("total: %tsec")
65
+ printf("%s %s\n\n",
66
+ "-"*(width+CAPTION.length-ets.length-1), ets)
67
+
68
+ # new
69
+ rehearsal.filename = "rehearsal-" + rehearsal.filename
70
+ rehearsal.gruff.title = rehearsal.gruff.title.to_s + "(Rehearsal)"
71
+ rehearsal.render
72
+
73
+ # copied from benchmark.rb
74
+ # take
75
+ print ' '*width, CAPTION
76
+ list = []
77
+ ary = []
78
+ report = Report.new # appended
79
+ job.list.each{|label,item|
80
+ GC::start
81
+ print label.ljust(width)
82
+ res = Benchmark::measure(&item)
83
+ print res.format()
84
+ ary.push res
85
+ list.push [label, res]
86
+
87
+ # new
88
+ report.record(label, res)
89
+ }
90
+
91
+ # new
92
+ report.render
93
+
94
+ # copied from benchmark.rb
95
+ STDOUT.sync = sync
96
+ ary
97
+ end
98
+
99
+ module_function :benchmark, :measure, :realtime, :bm, :bmbm
100
+
101
+ class Report < Benchmark::Report
102
+ attr_accessor :filename
103
+ attr_reader :gruff, :labels, :utimes, :stimes, :totals, :reals
104
+
105
+ alias :initialize_orig :initialize
106
+
107
+ def initialize(width = 0, fmtstr = nil)
108
+ initialize_orig(width, fmtstr)
109
+ @gruff = Gruff::SideBar.new
110
+ @gruff.x_axis_label = "sec"
111
+ @gruff.sort = false
112
+ @labels, @utimes, @stimes, @totals, @reals = [], [], [], [], []
113
+ @filename = "graph.png"
114
+ end
115
+
116
+ def item(label = "", *fmt, &blk)
117
+ # copied from benchmark.rb
118
+ print label.ljust(@width)
119
+ res = Benchmark::measure(&blk)
120
+ print res.format(@fmtstr, *fmt)
121
+
122
+ # new
123
+ record(label, res)
124
+
125
+ # copied from benchmark.rb
126
+ res
127
+ end
128
+
129
+ alias :report :item
130
+
131
+ def record(label,time)
132
+ @labels << label
133
+ @utimes << time.utime
134
+ @stimes << time.stime
135
+ @totals << time.total
136
+ @reals << time.real
137
+ end
138
+
139
+ def render
140
+ @labels.each_with_index{|label, idx| @gruff.labels[idx] = label }
141
+ @gruff.data("user", @utimes)
142
+ @gruff.data("system", @stimes)
143
+ @gruff.data("total", @totals)
144
+ @gruff.data("real", @reals)
145
+ @gruff.write(@filename)
146
+ return self
147
+ end
148
+ end
149
+ end
150
+
151
+ # copied from benchmark.rb and modified a little
152
+ if __FILE__ == $0
153
+ include BenchmarkX
154
+
155
+ n = ARGV[0].to_i.nonzero? || 50000
156
+ puts "#{n} times iterations of `a = \"1\"'"
157
+ benchmark(" " + CAPTION, 7, FMTSTR) do |x|
158
+ x.filename = "graph1.png"
159
+ x.gruff.title = "#{n} times iterations of `a = \"1\"'"
160
+ x.report("for:") {for i in 1..n; a = "1"; end} # Benchmark::measure
161
+ x.report("times:") {n.times do ; a = "1"; end}
162
+ x.report("upto:") {1.upto(n) do ; a = "1"; end}
163
+ end
164
+ end
@@ -0,0 +1,103 @@
1
+ $LOAD_PATH.unshift File.join(File.dirname(__FILE__), "..", "lib")
2
+ require "benchmarkx"
3
+
4
+ require "rubygems"
5
+ require "stringio"
6
+ require "bacon"
7
+
8
+ describe "BenchmarkX" do
9
+ before do
10
+ FileUtils.rm_f "graph.png"
11
+ FileUtils.rm_f "rehearsal-graph.png"
12
+ end
13
+
14
+ after do
15
+ FileUtils.rm_f "graph.png"
16
+ FileUtils.rm_f "rehearsal-graph.png"
17
+ end
18
+
19
+ # Records the reporting within the block
20
+ def report(&block)
21
+ begin
22
+ @result = StringIO.new
23
+ $stdout = @result
24
+ yield
25
+ $stdout = STDOUT
26
+ rescue Object => ex
27
+ $stdout = STDOUT
28
+ raise ex
29
+ end
30
+ end
31
+
32
+ it 'should create a graph image' do
33
+ report do
34
+ BenchmarkX.bm do |x|
35
+ x.report("A") { 1 }
36
+ x.report("B") { 2 }
37
+ x.report("C") { 3 }
38
+ end
39
+ end
40
+ File.exist?("graph.png").should.be.true
41
+ end
42
+
43
+ it 'should handle "bm" method' do
44
+ report do
45
+ Proc.new {
46
+ BenchmarkX.bm do |x|
47
+ x.report("A") { 1 }
48
+ x.report("B") { 2 }
49
+ x.report("C") { 3 }
50
+ end
51
+ }.should.not.raise
52
+ end
53
+ end
54
+
55
+ it 'should handle "bmbm" method' do
56
+ report do
57
+ Proc.new {
58
+ BenchmarkX.bmbm do |x|
59
+ x.report("A") { 1 }
60
+ x.report("B") { 2 }
61
+ x.report("C") { 3 }
62
+ end
63
+ }.should.not.raise
64
+ end
65
+ File.exist?("rehearsal-graph.png").should.be.true
66
+ File.exist?("graph.png").should.be.true
67
+ end
68
+
69
+ it 'should report the result to STDOUT by the normal benchmark format' do
70
+ report do
71
+ BenchmarkX.bm do |x|
72
+ x.report("A") { 1 }
73
+ x.report("B") { 2 }
74
+ x.report("C") { 3 }
75
+ end
76
+ end
77
+ @result.string.should =~ /user\s+system\s+total\s+real/
78
+ end
79
+
80
+ it 'should setup gruff object' do
81
+ report do
82
+ BenchmarkX.bm do |x|
83
+ x.gruff.should.be.kind_of(Gruff::SideBar)
84
+ x.gruff.title = "BenchmarkX"
85
+ x.gruff.title.should == "BenchmarkX"
86
+ x.report("A") { 1 }
87
+ x.report("B") { 2 }
88
+ x.report("C") { 3 }
89
+ end
90
+ end
91
+ end
92
+
93
+ it 'should keep label order' do
94
+ report do
95
+ BenchmarkX.bm do |x|
96
+ x.report("A") { 1 }
97
+ x.report("B") { 2 }
98
+ x.report("C") { 3 }
99
+ x.labels.should == ["A", "B", "C"]
100
+ end
101
+ end
102
+ end
103
+ end
metadata ADDED
@@ -0,0 +1,67 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: benchmarkx
3
+ version: !ruby/object:Gem::Version
4
+ version: "001"
5
+ platform: ruby
6
+ authors:
7
+ - Keita Yamaguchi
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-04-12 00:00:00 +09:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: gruff
17
+ version_requirement:
18
+ version_requirements: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: "0"
23
+ version:
24
+ description:
25
+ email: keita.yamaguchi@gmail.com
26
+ executables: []
27
+
28
+ extensions: []
29
+
30
+ extra_rdoc_files: []
31
+
32
+ files:
33
+ - .gemified
34
+ - History.txt
35
+ - License.txt
36
+ - README.txt
37
+ - Rakefile
38
+ - lib/benchmarkx.rb
39
+ - spec/benchmarkx_spec.rb
40
+ has_rdoc: true
41
+ homepage: http://rubyforge.org/projects/benchmarkx/
42
+ post_install_message:
43
+ rdoc_options: []
44
+
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: "0"
52
+ version:
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: "0"
58
+ version:
59
+ requirements: []
60
+
61
+ rubyforge_project: benchmarkx
62
+ rubygems_version: 1.1.1
63
+ signing_key:
64
+ specification_version: 2
65
+ summary: BenchmarkX is a extension of standard benchmark.rb for rendering sexy graph using gruff.
66
+ test_files: []
67
+