rubyperf 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -17,8 +17,8 @@ Jeweler::Tasks.new do |gem|
17
17
  gem.name = "rubyperf"
18
18
  gem.homepage = "http://github.com/lpasqualis/rubyperf"
19
19
  gem.license = "MIT"
20
- gem.summary = %Q{A gem to measure ruby code performance}
21
- gem.description = %Q{Measures the performance of blocks of code, and provide reporting in various formats}
20
+ gem.summary = %Q{rubyperf helps you measure ruby code performance}
21
+ gem.description = %Q{Used to easily measure the performance of blocks of Ruby code, expressions and methods; provides reporting in various formats}
22
22
  gem.email = "lpasqualis@gmail.com"
23
23
  gem.authors = ["lpasqualis"]
24
24
  # dependencies defined in Gemfile
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.0
1
+ 1.0.1
data/lib/perf/meter.rb CHANGED
@@ -36,7 +36,7 @@ module Perf
36
36
  end
37
37
 
38
38
  # Takes a description and a code block and measures the performance of the block.
39
- # It returns the value retuend by the block
39
+ # It returns the value returned by the block
40
40
  #
41
41
  # ==== Attributes
42
42
  #
@@ -134,9 +134,9 @@ module Perf
134
134
  end
135
135
 
136
136
  # Puts a wrapper around a set of methods of a specific class to measure their performance.
137
- # The set is provided with an array of iinstance methods, and one of class methods.
137
+ # The set is provided with an array of instance methods, and one of class methods.
138
138
  #
139
- # The method defines the wrapper, yelds to the block, and then restores the instrumented class.
139
+ # The method defines the wrapper, yields to the block, and then restores the instrumented class.
140
140
  # This ensures that the instrumented class is restored, and that the instrumentation occurs only in the context
141
141
  # of the block
142
142
  #
@@ -360,10 +360,14 @@ protected
360
360
 
361
361
  def method_missing(method_sym, *arguments, &block)
362
362
  if method_sym.to_s =~ /^report_(.*)$/
363
- klass=Object.const_get("Perf").const_get("ReportFormat#{$1.capitalize}")
363
+ klass=Object.const_get("Perf").const_get("ReportFormat#{camelize($1)}")
364
364
  return klass.new.format(self) if klass
365
365
  end
366
366
  super
367
367
  end
368
+
369
+ def camelize(from)
370
+ from.to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
371
+ end
368
372
  end
369
373
  end
@@ -0,0 +1,38 @@
1
+ #
2
+ # Copyright (c) 2012 Lorenzo Pasqualis - DreamBox Learning, Inc
3
+ # https://github.com/lpasqualis/rubyperf
4
+ #
5
+
6
+
7
+ require 'rubyperf'
8
+ require 'cgi'
9
+
10
+ module Perf
11
+
12
+ # ReportFormatListOfMeasures is use for unit tests to list the measures taken and their count. It has no other
13
+ # real life purpose.
14
+
15
+ class ReportFormatListOfMeasures < ReportFormat
16
+
17
+ def format(perf,options={})
18
+ super.select{|x| x.length>0 }
19
+ end
20
+
21
+ def format_header(v)
22
+ ""
23
+ end
24
+
25
+ def format_measure(v)
26
+ "#{v[:title]},#{v[:count]}"
27
+ end
28
+
29
+ def format_footer(v)
30
+ ""
31
+ end
32
+
33
+ def format_title(what,options)
34
+ what
35
+ end
36
+
37
+ end
38
+ end
data/lib/rubyperf.rb CHANGED
@@ -153,3 +153,4 @@ require 'perf/no_op_meter'
153
153
  require 'perf/report_format'
154
154
  require 'perf/report_format_simple'
155
155
  require 'perf/report_format_html'
156
+ require 'perf/report_format_list_of_measures'
data/rubyperf.gemspec CHANGED
@@ -5,12 +5,12 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{rubyperf}
8
- s.version = "1.0.0"
8
+ s.version = "1.0.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["lpasqualis"]
12
- s.date = %q{2012-01-13}
13
- s.description = %q{Measures the performance of blocks of code, and provide reporting in various formats}
12
+ s.date = %q{2012-01-15}
13
+ s.description = %q{Used to easily measure the performance of blocks of Ruby code, expressions and methods; provides reporting in various formats}
14
14
  s.email = %q{lpasqualis@gmail.com}
15
15
  s.extra_rdoc_files = [
16
16
  "LICENSE.txt",
@@ -30,6 +30,7 @@ Gem::Specification.new do |s|
30
30
  "lib/perf/no_op_meter.rb",
31
31
  "lib/perf/report_format.rb",
32
32
  "lib/perf/report_format_html.rb",
33
+ "lib/perf/report_format_list_of_measures.rb",
33
34
  "lib/perf/report_format_simple.rb",
34
35
  "lib/rubyperf.rb",
35
36
  "rubyperf.gemspec",
@@ -43,7 +44,7 @@ Gem::Specification.new do |s|
43
44
  s.licenses = ["MIT"]
44
45
  s.require_paths = ["lib"]
45
46
  s.rubygems_version = %q{1.3.6}
46
- s.summary = %q{A gem to measure ruby code performance}
47
+ s.summary = %q{rubyperf helps you measure ruby code performance}
47
48
 
48
49
  if s.respond_to? :specification_version then
49
50
  current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
@@ -15,5 +15,19 @@ class PerfTestExample
15
15
  def self.static_method
16
16
  (0..300000).to_a.reverse.reverse.reverse # Do something heavy
17
17
  end
18
+
19
+ def test_with_measure
20
+ Perf::MeterFactory.get.measure(:test_with_measure_block) do
21
+ test(1,2,3)
22
+ PerfTestExample.static_method
23
+ end
24
+ end
25
+
26
+ def test_with_measure_static
27
+ Perf::MeterFactory.get.measure(:test_with_measure_block) do
28
+ static_method
29
+ end
30
+ end
31
+
18
32
  end
19
33
 
@@ -12,9 +12,10 @@ require 'rubyperf'
12
12
  require 'rubyperf_test_helpers'
13
13
  require 'perf_test_example'
14
14
 
15
- class TestPerfMeter < Test::Unit::TestCase
15
+ class TestMeterFactory < Test::Unit::TestCase
16
16
 
17
17
  def test_basic
18
+ Perf::MeterFactory.clear_all!
18
19
  m1=Perf::MeterFactory.get()
19
20
  m2=Perf::MeterFactory.get()
20
21
 
@@ -11,17 +11,87 @@ require 'perf_test_example'
11
11
 
12
12
  class TestPerfMeter < Test::Unit::TestCase
13
13
 
14
- ALLOW_OUTPUT = false
15
-
16
- def test_overhead
17
- runs=3000
14
+ def test_basic
18
15
  m=Perf::Meter.new
19
- b1=Benchmark.measure { runs.times { m.measure(:a) { } }}
20
- b2=Benchmark.measure {runs.times {} }
16
+ m.measure(:string_operations) do
17
+ m.measure(:ciao) do
18
+ 1000.times do; "CIAO"*100; end
19
+ end
20
+ end
21
+ m.measure(:string_operations) do
22
+ m.measure(:help) do
23
+ 1000.times do; "HELP"*100; end
24
+ end
25
+ end
26
+ m.measure(:emtpy_loop) do
27
+ 50000.times do; end;
28
+ end
29
+ m.measure(:rough_overhead_x10000) do
30
+ 1000.times do
31
+ m.measure(:block_1) do
32
+ m.measure(:block_1_1) do
33
+ end
34
+ m.measure(:block_1_2) do
35
+ m.measure(:block_1_2_1) do
36
+ end
37
+ m.measure(:block_1_2_2) do
38
+ end
39
+ m.measure(:block_1_2_3) do
40
+ m.measure_result(:bool_exp_1_2_3) { false }
41
+ m.measure_result(:bool_exp_1_2_3) { true }
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
21
47
 
22
- puts b1 if ALLOW_OUTPUT
23
- puts b2 if ALLOW_OUTPUT
24
- puts b1-b2 if ALLOW_OUTPUT
48
+ m.measure(:sleep1) do
49
+ m.measure(:sleep1_1) do
50
+ sleep(0.01)
51
+ end
52
+ m.measure(:sleep_1_2) do
53
+ sleep(0.02)
54
+ end
55
+ m.measure(:sleep_1_3) do
56
+ sleep(0.03)
57
+ end
58
+ end
59
+ m.measure(:empty) do
60
+ end
61
+ m.measure_result("test") { sleep(1) }
62
+ m.measure_result("test") { false }
63
+ m.measure_result("test") { false }
64
+
65
+ m.method_meters(Array,[:sort,:reverse],[:new]) do
66
+ Array.new(1000000,"abc").reverse.sort
67
+ end
68
+
69
+ assert_equal ['\blocks,0',
70
+ '\blocks\empty,1',
71
+ '\blocks\emtpy_loop,1',
72
+ '\blocks\rough_overhead_x10000,1',
73
+ '\blocks\rough_overhead_x10000\block_1,1000',
74
+ '\blocks\rough_overhead_x10000\block_1\block_1_1,1000',
75
+ '\blocks\rough_overhead_x10000\block_1\block_1_2,1000',
76
+ '\blocks\rough_overhead_x10000\block_1\block_1_2\block_1_2_1,1000',
77
+ '\blocks\rough_overhead_x10000\block_1\block_1_2\block_1_2_2,1000',
78
+ '\blocks\rough_overhead_x10000\block_1\block_1_2\block_1_2_3,1000',
79
+ '\blocks\rough_overhead_x10000\block_1\block_1_2\block_1_2_3\bool_exp_1_2_3 = "false",1000',
80
+ '\blocks\rough_overhead_x10000\block_1\block_1_2\block_1_2_3\bool_exp_1_2_3 = "true",1000',
81
+ '\blocks\sleep1,1',
82
+ '\blocks\sleep1\sleep1_1,1',
83
+ '\blocks\sleep1\sleep_1_2,1',
84
+ '\blocks\sleep1\sleep_1_3,1',
85
+ '\blocks\string_operations,2',
86
+ '\blocks\string_operations\ciao,1',
87
+ '\blocks\string_operations\help,1',
88
+ '\blocks\test = "1",1',
89
+ '\blocks\test = "false",2',
90
+ '\methods,0',
91
+ '\methods\#<Class:Array>.new,1',
92
+ '\methods\Array.reverse,1',
93
+ '\methods\Array.sort,1'],
94
+ m.report_list_of_measures
25
95
  end
26
96
 
27
97
  def test_method_not_corrupted_after_restoring
@@ -36,7 +106,15 @@ class TestPerfMeter < Test::Unit::TestCase
36
106
  end
37
107
  assert PerfTestExample.methods.sort == cmethods
38
108
  assert PerfTestExample.new.methods.sort == imethods
39
- puts m.report_simple if ALLOW_OUTPUT
109
+
110
+ assert_equal ['\methods,0',
111
+ '\methods\#<Class:PerfTestExample>.static_method,1',
112
+ '\methods\PerfTestExample.test,1',
113
+ '\methods\PerfTestExample.test_np,1'],
114
+ m.report_list_of_measures
115
+
116
+ assert_equal 6,m.report_simple.length
117
+ assert_equal 6,m.report_html.length
40
118
  end
41
119
 
42
120
  def test_method_metering
@@ -47,7 +125,26 @@ class TestPerfMeter < Test::Unit::TestCase
47
125
  a.test_np
48
126
  PerfTestExample.static_method
49
127
  end
50
- puts m.report_simple if ALLOW_OUTPUT
128
+
129
+ assert_equal ['\methods,0',
130
+ '\methods\#<Class:PerfTestExample>.static_method,1',
131
+ '\methods\PerfTestExample.test,1',
132
+ '\methods\PerfTestExample.test_np,1'],
133
+ m.report_list_of_measures
134
+ end
135
+
136
+ def test_methods_with_measure
137
+ Perf::MeterFactory.clear_all!
138
+ m=Perf::MeterFactory.get
139
+ m.method_meters(PerfTestExample,[:test,:test_np,:test_with_measure],[:static_method]) do
140
+ a=PerfTestExample.new
141
+ a.test(1,2,3)
142
+ a.test_np
143
+ a.test_with_measure
144
+ PerfTestExample.static_method
145
+ end
146
+ assert_equal 10,m.report_simple.length
147
+ assert_equal 10,m.report_html.length
51
148
  end
52
149
 
53
150
  def test_base_report
@@ -70,17 +167,10 @@ class TestPerfMeter < Test::Unit::TestCase
70
167
  "#{Perf::Meter::PATH_MEASURES}\\d",
71
168
  "#{Perf::Meter::PATH_MEASURES}\\b",
72
169
  "#{Perf::Meter::PATH_MEASURES}"])
73
- puts m.report_simple if ALLOW_OUTPUT
74
- end
75
-
76
- def test_output_html
77
- m=RubyperfTestHelpers.get_measure
78
- htmlcode=m.report_html
79
- puts htmlcode if ALLOW_OUTPUT
80
170
  end
81
171
 
82
172
  def test_exception_handling
83
- # An exeption thwon in a block that we are measuring needs to leave the Perf::Meter in a good state.
173
+ # An exception thrown in a block that we are measuring needs to leave the Perf::Meter in a good state.
84
174
  # This test checks the state after an exception.
85
175
  exception_raised = false
86
176
  measuring_correct = false
@@ -105,7 +195,6 @@ class TestPerfMeter < Test::Unit::TestCase
105
195
  assert exception_raised
106
196
  assert measuring_correct
107
197
  assert stack_correct
108
- puts m.report_simple if ALLOW_OUTPUT
109
198
  end
110
199
 
111
200
  def test_return_values
@@ -121,8 +210,6 @@ class TestPerfMeter < Test::Unit::TestCase
121
210
  assert_equal 5,m.measure_result(:five) {5}
122
211
  assert_equal "byebye",m.measure_result(:byebye) {"bye"+"bye"}
123
212
  end
124
-
125
- puts m.report_simple if ALLOW_OUTPUT
126
213
  end
127
214
 
128
215
  def test_nesting_measure
@@ -130,14 +217,14 @@ class TestPerfMeter < Test::Unit::TestCase
130
217
  m.measure(:a) { }
131
218
  m.measure(:b) { }
132
219
  m.measure(:d) { m.measure(:c) { m.measure(:d) {} }}
133
- assert_not_nil m.measurements["#{Perf::Meter::PATH_MEASURES}\\d\\c\\d"]
134
- assert_not_nil m.measurements["#{Perf::Meter::PATH_MEASURES}\\d\\c"]
135
- assert_not_nil m.measurements["#{Perf::Meter::PATH_MEASURES}\\d"]
136
- assert_not_nil m.measurements["#{Perf::Meter::PATH_MEASURES}\\b"]
137
- assert_not_nil m.measurements["#{Perf::Meter::PATH_MEASURES}\\a"]
138
- assert_not_nil m.measurements["#{Perf::Meter::PATH_MEASURES}"]
139
- assert_nil m.measurements["#{Perf::Meter::PATH_MEASURES}\\huh"]
140
- puts m.report_simple if ALLOW_OUTPUT
220
+
221
+ assert_equal ['\blocks,0',
222
+ '\blocks\a,1',
223
+ '\blocks\b,1',
224
+ '\blocks\d,1',
225
+ '\blocks\d\c,1',
226
+ '\blocks\d\c\d,1'],
227
+ m.report_list_of_measures
141
228
  end
142
229
 
143
230
  def test_measure_instance_method
@@ -180,65 +267,31 @@ class TestPerfMeter < Test::Unit::TestCase
180
267
  a.test_np
181
268
  #puts puts m.report_simple
182
269
  m.restore_all_methods(PerfTestExample)
183
- puts m.report_simple if ALLOW_OUTPUT
270
+ assert_equal ['\blocks,0',
271
+ '\blocks\measure_test,1',
272
+ '\blocks\measure_test_np,1',
273
+ '\blocks\some_expressions,1',
274
+ '\blocks\some_expressions\expression1 = "1111",1',
275
+ '\blocks\some_expressions\expression1 = "13579",1',
276
+ '\blocks\some_expressions\expression2 = "string",1',
277
+ '\methods,0',
278
+ '\methods\#<Class:PerfTestExample>.static_method,1',
279
+ '\methods\PerfTestExample.test,1',
280
+ '\methods\PerfTestExample.test_np,2'],
281
+ m.report_list_of_measures
184
282
  end
185
283
 
186
- def test_basic
187
- m=Perf::Meter.new
188
- m.measure(:string_operations) do
189
- m.measure(:ciao) do
190
- 1000.times do; "CIAO"*100; end
191
- end
192
- end
193
- m.measure(:string_operations) do
194
- m.measure(:help) do
195
- 1000.times do; "HELP"*100; end
196
- end
197
- end
198
- m.measure(:emtpy_loop) do
199
- 50000.times do; end;
200
- end
201
- m.measure(:rough_overhead_x10000) do
202
- 1000.times do
203
- m.measure(:block_1) do
204
- m.measure(:block_1_1) do
205
- end
206
- m.measure(:block_1_2) do
207
- m.measure(:block_1_2_1) do
208
- end
209
- m.measure(:block_1_2_2) do
210
- end
211
- m.measure(:block_1_2_3) do
212
- m.measure_result(:bool_exp_1_2_3) { false }
213
- m.measure_result(:bool_exp_1_2_3) { true }
214
- end
215
- end
216
- end
217
- end
218
- end
219
-
220
- m.measure(:sleep1) do
221
- m.measure(:sleep1_1) do
222
- sleep(0.01)
223
- end
224
- m.measure(:sleep_1_2) do
225
- sleep(0.02)
226
- end
227
- m.measure(:sleep_1_3) do
228
- sleep(0.03)
229
- end
230
- end
231
- m.measure(:empty) do
232
- end
233
- m.measure_result("test") { sleep(1) }
234
- m.measure_result("test") { false }
235
- m.measure_result("test") { false }
236
-
237
- m.method_meters(Array,[:sort,:reverse],[:new]) do
238
- Array.new(1000000,"abc").reverse.sort
239
- end
240
-
241
- puts m.report_simple if ALLOW_OUTPUT
242
284
 
285
+ def test_overhead
286
+ runs=1_000
287
+ a=(1..100_000).to_a
288
+ m=Perf::Meter.new
289
+ b1=Benchmark.measure { runs.times { m.measure(:a) { a.reverse! } } }
290
+ b2=Benchmark.measure { runs.times { a.reverse! } }
291
+ assert_equal ['\blocks,0',
292
+ '\blocks\a,1000'],
293
+ m.report_list_of_measures
294
+ # Here we should assert if the overhead is > a certain percentage.
295
+ # puts "Measurement overhead x 1000 = #{b1-b2}"
243
296
  end
244
297
  end
metadata CHANGED
@@ -5,9 +5,9 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 1
7
7
  - 0
8
- - 0
8
+ - 1
9
9
  segments_generated: true
10
- version: 1.0.0
10
+ version: 1.0.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - lpasqualis
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2012-01-13 00:00:00 -08:00
18
+ date: 2012-01-15 00:00:00 -08:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -74,7 +74,7 @@ dependencies:
74
74
  segments_generated: true
75
75
  version: "0"
76
76
  requirement: *id004
77
- description: Measures the performance of blocks of code, and provide reporting in various formats
77
+ description: Used to easily measure the performance of blocks of Ruby code, expressions and methods; provides reporting in various formats
78
78
  email: lpasqualis@gmail.com
79
79
  executables: []
80
80
 
@@ -97,6 +97,7 @@ files:
97
97
  - lib/perf/no_op_meter.rb
98
98
  - lib/perf/report_format.rb
99
99
  - lib/perf/report_format_html.rb
100
+ - lib/perf/report_format_list_of_measures.rb
100
101
  - lib/perf/report_format_simple.rb
101
102
  - lib/rubyperf.rb
102
103
  - rubyperf.gemspec
@@ -136,6 +137,6 @@ rubyforge_project:
136
137
  rubygems_version: 1.3.6
137
138
  signing_key:
138
139
  specification_version: 3
139
- summary: A gem to measure ruby code performance
140
+ summary: rubyperf helps you measure ruby code performance
140
141
  test_files: []
141
142