rubyperf 1.3.7 → 1.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/lib/perf/meter_factory.rb +29 -33
- data/rubyperf.gemspec +4 -5
- data/test/test_meter_factory.rb +26 -26
- metadata +35 -29
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.
|
1
|
+
1.4.0
|
data/lib/perf/meter_factory.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
require "rubyperf"
|
2
|
+
require "singleton"
|
2
3
|
|
3
4
|
module Perf
|
4
5
|
|
@@ -9,14 +10,10 @@ module Perf
|
|
9
10
|
# either creating a factory that fulfills your needs, or create Perf::Meter objects and pass them around or use
|
10
11
|
# this factory will well planned out key values so that you won't have conflicts and overrides.
|
11
12
|
#
|
12
|
-
# MeterFactory works keeping a global cache of Perf::Meter objects by key. Multiple independent parts
|
13
|
-
# of your code using this factory could get into trouble and trample on each other if they use the same key, or the
|
14
|
-
# default key.
|
15
|
-
#
|
16
13
|
# Example of usage where it would be inconvenient to pass around the Perf::Meter object from example to function2:
|
17
14
|
#
|
18
15
|
# def example
|
19
|
-
# Perf::MeterFactory.get.measure(:function1)
|
16
|
+
# Perf::MeterFactory.instance.get.measure(:function1)
|
20
17
|
# function1()
|
21
18
|
# end
|
22
19
|
# end
|
@@ -29,7 +26,7 @@ module Perf
|
|
29
26
|
#
|
30
27
|
# def function2()
|
31
28
|
# ..
|
32
|
-
# Perf::MeterFactory.get.measure(:some_hot_code_in_function2)
|
29
|
+
# Perf::MeterFactory.instance.get.measure(:some_hot_code_in_function2)
|
33
30
|
# ...
|
34
31
|
# end
|
35
32
|
# ..
|
@@ -37,87 +34,86 @@ module Perf
|
|
37
34
|
#
|
38
35
|
|
39
36
|
class MeterFactory
|
37
|
+
include Singleton
|
40
38
|
|
41
39
|
DEFAULT_METER = :default
|
42
40
|
|
43
|
-
|
44
|
-
|
45
|
-
|
41
|
+
def initialize
|
42
|
+
@perf_meters = {}
|
43
|
+
@new_meter_options = {}
|
44
|
+
@factory_options = {:noop=>false}
|
45
|
+
end
|
46
46
|
|
47
47
|
# Returns a Perf::Meter with a given key, and creates it lazily if it doesn't exist'.
|
48
48
|
# NOTE: The options are set ONLY the first time that get is called on a specific key.
|
49
49
|
# After that the options will be ignored!
|
50
50
|
|
51
|
-
def
|
52
|
-
|
53
|
-
if !@@factory_options[:noop]
|
51
|
+
def get(key=DEFAULT_METER,new_meter_options=nil)
|
52
|
+
if !@factory_options[:noop]
|
54
53
|
# Creates a real meter
|
55
|
-
|
54
|
+
@perf_meters[key] ||= Perf::Meter.new(new_meter_options || @new_meter_options)
|
56
55
|
else
|
57
56
|
# If noop is set, creates a no-nop version of the meter, unless a meter with this key has already been
|
58
57
|
# created.
|
59
|
-
|
58
|
+
@perf_meters[key] ||= Perf::NoOpMeter.new
|
60
59
|
end
|
61
60
|
end
|
62
61
|
|
63
62
|
# meter is like get, but if the meter doesn't already exists it returns a NoOpMeter. You can use this every time
|
64
63
|
# that you want "somebody else" make the decision of what meter to use.
|
65
64
|
|
66
|
-
def
|
67
|
-
|
68
|
-
@@perf_meters[key] ||= Perf::NoOpMeter.new
|
65
|
+
def meter(key=DEFAULT_METER)
|
66
|
+
@perf_meters[key] ||= Perf::NoOpMeter.new
|
69
67
|
end
|
70
68
|
|
71
69
|
# To set options for new meters created by get, when specific options are not passed, you can do so with this
|
72
70
|
# method.
|
73
71
|
|
74
|
-
def
|
75
|
-
|
72
|
+
def set_new_meters_options(options)
|
73
|
+
@new_meter_options.merge(options)
|
76
74
|
end
|
77
75
|
|
78
76
|
# Set options for the factory behaviour.
|
79
77
|
|
80
|
-
def
|
81
|
-
|
78
|
+
def set_factory_options(options)
|
79
|
+
@factory_options.merge!(options)
|
82
80
|
end
|
83
81
|
|
84
82
|
# If you use set_new_meters_options, or if you pass options to Perf::MeterFactory.get, you are setting options
|
85
83
|
# only for if the meter is created. For this reason you might need to find out if the meter already exist.
|
86
84
|
|
87
85
|
def exists?(key=DEFAULT_METER)
|
88
|
-
|
86
|
+
!@perf_meters[key].nil?
|
89
87
|
end
|
90
88
|
|
91
89
|
# Pushes a Perf::Meter into a key
|
92
90
|
|
93
|
-
def
|
94
|
-
|
95
|
-
@@perf_meters[key]=meter
|
91
|
+
def set_meter(key,meter)
|
92
|
+
@perf_meters[key]=meter
|
96
93
|
end
|
97
94
|
|
98
95
|
# Sets the default meter.
|
99
96
|
|
100
|
-
def
|
97
|
+
def set_default(meter)
|
101
98
|
set_meter(DEFAULT_METER,meter)
|
102
99
|
end
|
103
100
|
|
104
101
|
# Returns a hash of existing meters.
|
105
102
|
|
106
|
-
def
|
107
|
-
|
108
|
-
return @@perf_meters.clone
|
103
|
+
def all
|
104
|
+
@perf_meters.dup
|
109
105
|
end
|
110
106
|
|
111
107
|
# Removes an existing meter from the cache
|
112
108
|
|
113
|
-
def
|
114
|
-
|
109
|
+
def clear_meter(key=DEFAULT_METER)
|
110
|
+
@perf_meters.delete(key) if @perf_meters
|
115
111
|
end
|
116
112
|
|
117
113
|
# Clears the entire cache of meters.
|
118
114
|
|
119
|
-
def
|
120
|
-
|
115
|
+
def clear_all!
|
116
|
+
@perf_meters.clear
|
121
117
|
end
|
122
118
|
|
123
119
|
end
|
data/rubyperf.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{rubyperf}
|
8
|
-
s.version = "1.
|
8
|
+
s.version = "1.4.0"
|
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-
|
12
|
+
s.date = %q{2012-01-19}
|
13
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 = [
|
@@ -43,14 +43,13 @@ Gem::Specification.new do |s|
|
|
43
43
|
s.homepage = %q{http://github.com/lpasqualis/rubyperf}
|
44
44
|
s.licenses = ["MIT"]
|
45
45
|
s.require_paths = ["lib"]
|
46
|
-
s.rubygems_version = %q{1.
|
46
|
+
s.rubygems_version = %q{1.4.2}
|
47
47
|
s.summary = %q{rubyperf helps you measure ruby code performance}
|
48
48
|
|
49
49
|
if s.respond_to? :specification_version then
|
50
|
-
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
51
50
|
s.specification_version = 3
|
52
51
|
|
53
|
-
if Gem::Version.new(Gem::
|
52
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
54
53
|
s.add_development_dependency(%q<shoulda>, [">= 0"])
|
55
54
|
s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
|
56
55
|
s.add_development_dependency(%q<jeweler>, ["~> 1.6.4"])
|
data/test/test_meter_factory.rb
CHANGED
@@ -10,17 +10,17 @@ require 'perf_test_example'
|
|
10
10
|
class TestMeterFactory < Test::Unit::TestCase
|
11
11
|
|
12
12
|
def setup()
|
13
|
-
Perf::MeterFactory.clear_all!
|
13
|
+
Perf::MeterFactory.instance.clear_all!
|
14
14
|
end
|
15
15
|
|
16
16
|
def teardown()
|
17
|
-
Perf::MeterFactory.clear_all!
|
18
|
-
Perf::MeterFactory.set_factory_options(:noop=>false)
|
17
|
+
Perf::MeterFactory.instance.clear_all!
|
18
|
+
Perf::MeterFactory.instance.set_factory_options(:noop=>false)
|
19
19
|
end
|
20
20
|
|
21
21
|
def test_noop
|
22
|
-
Perf::MeterFactory.set_factory_options(:noop=>true)
|
23
|
-
m1=Perf::MeterFactory.get()
|
22
|
+
Perf::MeterFactory.instance.set_factory_options(:noop=>true)
|
23
|
+
m1=Perf::MeterFactory.instance.get()
|
24
24
|
assert m1.is_a? Perf::NoOpMeter
|
25
25
|
m1.measure(:something) do
|
26
26
|
# ...
|
@@ -29,18 +29,18 @@ class TestMeterFactory < Test::Unit::TestCase
|
|
29
29
|
end
|
30
30
|
|
31
31
|
def test_meter
|
32
|
-
m=Perf::MeterFactory.meter
|
32
|
+
m=Perf::MeterFactory.instance.meter
|
33
33
|
assert m.is_a? Perf::NoOpMeter
|
34
34
|
|
35
|
-
Perf::MeterFactory.clear_all!
|
36
|
-
m=Perf::MeterFactory.get
|
37
|
-
m=Perf::MeterFactory.meter
|
35
|
+
Perf::MeterFactory.instance.clear_all!
|
36
|
+
m=Perf::MeterFactory.instance.get
|
37
|
+
m=Perf::MeterFactory.instance.meter
|
38
38
|
assert m.is_a? Perf::Meter
|
39
39
|
end
|
40
40
|
|
41
41
|
def test_noop2
|
42
|
-
Perf::MeterFactory.set_factory_options(:noop=>true)
|
43
|
-
m=Perf::MeterFactory.get()
|
42
|
+
Perf::MeterFactory.instance.set_factory_options(:noop=>true)
|
43
|
+
m=Perf::MeterFactory.instance.get()
|
44
44
|
assert m.is_a? Perf::NoOpMeter
|
45
45
|
m.measure(:string_operations) do
|
46
46
|
m.measure(:ciao) do
|
@@ -88,9 +88,9 @@ class TestMeterFactory < Test::Unit::TestCase
|
|
88
88
|
end
|
89
89
|
|
90
90
|
def test_basic
|
91
|
-
Perf::MeterFactory.clear_all!
|
92
|
-
m1=Perf::MeterFactory.get()
|
93
|
-
m2=Perf::MeterFactory.get()
|
91
|
+
Perf::MeterFactory.instance.clear_all!
|
92
|
+
m1=Perf::MeterFactory.instance.get()
|
93
|
+
m2=Perf::MeterFactory.instance.get()
|
94
94
|
|
95
95
|
m1.measure(:a) {}
|
96
96
|
m1.measure(:b) {}
|
@@ -98,27 +98,27 @@ class TestMeterFactory < Test::Unit::TestCase
|
|
98
98
|
assert m1.eql?(m2)
|
99
99
|
assert_equal 3, m2.measurements.count
|
100
100
|
assert_equal 3, m1.measurements.count
|
101
|
-
assert_equal 1,Perf::MeterFactory.all.length
|
101
|
+
assert_equal 1,Perf::MeterFactory.instance.all.length
|
102
102
|
|
103
|
-
m3=Perf::MeterFactory.get(:some_meter)
|
104
|
-
m4=Perf::MeterFactory.get(:some_meter)
|
103
|
+
m3=Perf::MeterFactory.instance.get(:some_meter)
|
104
|
+
m4=Perf::MeterFactory.instance.get(:some_meter)
|
105
105
|
|
106
106
|
assert m3.eql? m4
|
107
107
|
assert !(m1.eql? m3)
|
108
|
-
assert_equal 2,Perf::MeterFactory.all.length
|
108
|
+
assert_equal 2,Perf::MeterFactory.instance.all.length
|
109
109
|
|
110
|
-
Perf::MeterFactory.clear_meter(:some_meter)
|
111
|
-
assert_equal 1,Perf::MeterFactory.all.length
|
110
|
+
Perf::MeterFactory.instance.clear_meter(:some_meter)
|
111
|
+
assert_equal 1,Perf::MeterFactory.instance.all.length
|
112
112
|
|
113
|
-
Perf::MeterFactory.clear_all!
|
114
|
-
assert_equal 0,Perf::MeterFactory.all.length
|
113
|
+
Perf::MeterFactory.instance.clear_all!
|
114
|
+
assert_equal 0,Perf::MeterFactory.instance.all.length
|
115
115
|
|
116
116
|
ameter = Perf::Meter.new
|
117
|
-
Perf::MeterFactory.set_meter(:ameter,ameter)
|
118
|
-
assert (ameter.eql? Perf::MeterFactory.get(:ameter))
|
117
|
+
Perf::MeterFactory.instance.set_meter(:ameter,ameter)
|
118
|
+
assert (ameter.eql? Perf::MeterFactory.instance.get(:ameter))
|
119
119
|
|
120
|
-
Perf::MeterFactory.set_default(ameter)
|
121
|
-
assert (ameter.eql? Perf::MeterFactory.get)
|
120
|
+
Perf::MeterFactory.instance.set_default(ameter)
|
121
|
+
assert (ameter.eql? Perf::MeterFactory.instance.get)
|
122
122
|
end
|
123
123
|
|
124
124
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rubyperf
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
4
|
+
hash: 7
|
5
|
+
prerelease:
|
5
6
|
segments:
|
6
7
|
- 1
|
7
|
-
-
|
8
|
-
-
|
9
|
-
|
10
|
-
version: 1.3.7
|
8
|
+
- 4
|
9
|
+
- 0
|
10
|
+
version: 1.4.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- lpasqualis
|
@@ -15,65 +15,69 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2012-01-
|
18
|
+
date: 2012-01-19 00:00:00 -08:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
22
22
|
prerelease: false
|
23
|
-
|
24
|
-
|
25
|
-
version_requirements: &id001 !ruby/object:Gem::Requirement
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
26
25
|
requirements:
|
27
26
|
- - ">="
|
28
27
|
- !ruby/object:Gem::Version
|
28
|
+
hash: 3
|
29
29
|
segments:
|
30
30
|
- 0
|
31
|
-
segments_generated: true
|
32
31
|
version: "0"
|
33
|
-
|
32
|
+
type: :development
|
33
|
+
name: shoulda
|
34
|
+
version_requirements: *id001
|
34
35
|
- !ruby/object:Gem::Dependency
|
35
36
|
prerelease: false
|
36
|
-
|
37
|
-
|
38
|
-
version_requirements: &id002 !ruby/object:Gem::Requirement
|
37
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
38
|
+
none: false
|
39
39
|
requirements:
|
40
40
|
- - ~>
|
41
41
|
- !ruby/object:Gem::Version
|
42
|
+
hash: 23
|
42
43
|
segments:
|
43
44
|
- 1
|
44
45
|
- 0
|
45
46
|
- 0
|
46
|
-
segments_generated: true
|
47
47
|
version: 1.0.0
|
48
|
-
|
48
|
+
type: :development
|
49
|
+
name: bundler
|
50
|
+
version_requirements: *id002
|
49
51
|
- !ruby/object:Gem::Dependency
|
50
52
|
prerelease: false
|
51
|
-
|
52
|
-
|
53
|
-
version_requirements: &id003 !ruby/object:Gem::Requirement
|
53
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
54
|
+
none: false
|
54
55
|
requirements:
|
55
56
|
- - ~>
|
56
57
|
- !ruby/object:Gem::Version
|
58
|
+
hash: 7
|
57
59
|
segments:
|
58
60
|
- 1
|
59
61
|
- 6
|
60
62
|
- 4
|
61
|
-
segments_generated: true
|
62
63
|
version: 1.6.4
|
63
|
-
|
64
|
+
type: :development
|
65
|
+
name: jeweler
|
66
|
+
version_requirements: *id003
|
64
67
|
- !ruby/object:Gem::Dependency
|
65
68
|
prerelease: false
|
66
|
-
|
67
|
-
|
68
|
-
version_requirements: &id004 !ruby/object:Gem::Requirement
|
69
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
70
|
+
none: false
|
69
71
|
requirements:
|
70
72
|
- - ">="
|
71
73
|
- !ruby/object:Gem::Version
|
74
|
+
hash: 3
|
72
75
|
segments:
|
73
76
|
- 0
|
74
|
-
segments_generated: true
|
75
77
|
version: "0"
|
76
|
-
|
78
|
+
type: :development
|
79
|
+
name: rcov
|
80
|
+
version_requirements: *id004
|
77
81
|
description: Used to easily measure the performance of blocks of Ruby code, expressions and methods; provides reporting in various formats
|
78
82
|
email: lpasqualis@gmail.com
|
79
83
|
executables: []
|
@@ -116,25 +120,27 @@ rdoc_options: []
|
|
116
120
|
require_paths:
|
117
121
|
- lib
|
118
122
|
required_ruby_version: !ruby/object:Gem::Requirement
|
123
|
+
none: false
|
119
124
|
requirements:
|
120
125
|
- - ">="
|
121
126
|
- !ruby/object:Gem::Version
|
127
|
+
hash: 3
|
122
128
|
segments:
|
123
129
|
- 0
|
124
|
-
segments_generated: true
|
125
130
|
version: "0"
|
126
131
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
132
|
+
none: false
|
127
133
|
requirements:
|
128
134
|
- - ">="
|
129
135
|
- !ruby/object:Gem::Version
|
136
|
+
hash: 3
|
130
137
|
segments:
|
131
138
|
- 0
|
132
|
-
segments_generated: true
|
133
139
|
version: "0"
|
134
140
|
requirements: []
|
135
141
|
|
136
142
|
rubyforge_project:
|
137
|
-
rubygems_version: 1.
|
143
|
+
rubygems_version: 1.4.2
|
138
144
|
signing_key:
|
139
145
|
specification_version: 3
|
140
146
|
summary: rubyperf helps you measure ruby code performance
|