hitimes 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/HISTORY CHANGED
@@ -1,4 +1,9 @@
1
1
  = Changelog
2
+ == Version 1.0.1 2009-06-12
3
+
4
+ * Fix examples
5
+ * performance tuning, new Metric classes are faster than old Timer class
6
+
2
7
  == Version 1.0.0 2009-06-12
3
8
 
4
9
  * Major version bump with complete refactor of the metric collection API
@@ -44,16 +44,39 @@ end
44
44
  # Use a new timer each time
45
45
  #
46
46
  def hitimes_duration_t1
47
- Hitimes::Timer.now.stop
47
+ Hitimes::TimedMetric.now('duration_t1').stop
48
48
  end
49
49
 
50
50
  #
51
51
  # reuse the same timer over and over
52
52
  #
53
- HT = Hitimes::Timer.new
53
+ HT2= Hitimes::TimedMetric.new( 'duration_t2' )
54
54
  def hitimes_duration_t2
55
- HT.start
56
- HT.stop
55
+ HT2.start
56
+ HT2.stop
57
+ end
58
+
59
+ HT3 = Hitimes::TimedMetric.new( 'duration_t3' )
60
+ def hitimes_duration_t3
61
+ HT3.measure { nil }
62
+ end
63
+
64
+ #
65
+ # Check out the speed of the TimedValueMetric too
66
+ #
67
+ def hitimes_duration_tv1
68
+ Hitimes::TimedValueMetric.now( 'duration_tv1' ).stop( 42 )
69
+ end
70
+
71
+ HTV2 = Hitimes::TimedValueMetric.new( 'duration_tv2' )
72
+ def hitimes_duration_tv2
73
+ HTV2.start
74
+ HTV2.stop( 42 )
75
+ end
76
+
77
+ HTV3 = Hitimes::TimedValueMetric.new( 'duration_tv3' )
78
+ def hitimes_duration_tv3
79
+ HTV3.measure( 42 ) { nil }
57
80
  end
58
81
 
59
82
  #
@@ -76,11 +99,15 @@ end
76
99
 
77
100
  puts "Testing time sampling 100,000 times"
78
101
 
79
- bm(20) do |x|
80
- x.report("Process") { 100_000.times { process_duration } }
81
- x.report("Time") { 100_000.times { time_duration } }
82
- x.report("Hitimes::Timer 1") { 100_000.times { hitimes_duration_t1 } }
83
- x.report("Hitimes::Timer 2") { 100_000.times { hitimes_duration_t2 } }
84
- x.report("Hitimes::Interval 1") { 100_000.times { hitimes_duration_i1 } }
85
- x.report("Hitimes::Interval 2") { 100_000.times { hitimes_duration_i2 } }
102
+ bm(30) do |x|
103
+ x.report("Process") { 100_000.times { process_duration } }
104
+ x.report("Time") { 100_000.times { time_duration } }
105
+ x.report("Hitimes::TimedMetric 1") { 100_000.times { hitimes_duration_t1 } }
106
+ x.report("Hitimes::TimedMetric 2") { 100_000.times { hitimes_duration_t2 } }
107
+ x.report("Hitimes::TimedMetric 3") { 100_000.times { hitimes_duration_t3 } }
108
+ x.report("Hitimes::Interval 1") { 100_000.times { hitimes_duration_i1 } }
109
+ x.report("Hitimes::Interval 2") { 100_000.times { hitimes_duration_i2 } }
110
+ x.report("Hitimes::TimedValueMetric 1") { 100_000.times { hitimes_duration_tv1 } }
111
+ x.report("Hitimes::TimedValueMetric 2") { 100_000.times { hitimes_duration_tv2 } }
112
+ x.report("Hitimes::TimedValueMetric 3") { 100_000.times { hitimes_duration_tv3 } }
86
113
  end
data/examples/stats.rb CHANGED
@@ -23,7 +23,9 @@ Dir.entries( dir ).each do |entry|
23
23
  end
24
24
  end
25
25
 
26
- %w[ count min max mean sum stddev ].each do |m|
26
+ Hitimes::Stats::STATS.each do |m|
27
27
  puts "#{m.rjust(6)} : #{s.send( m ) }"
28
28
  end
29
29
 
30
+ puts s.to_hash.inspect
31
+
@@ -164,6 +164,34 @@ VALUE hitimes_interval_stop( VALUE self )
164
164
  return rc;
165
165
  }
166
166
 
167
+ /**
168
+ * call-seq:
169
+ * interval.duration_so_far -> Float or false
170
+ *
171
+ * return how the duration so far. This will return the duration from the time
172
+ * the Interval was started if the interval is running, otherwise it will return
173
+ * false.
174
+ */
175
+ VALUE hitimes_interval_duration_so_far( VALUE self )
176
+ {
177
+ hitimes_interval_t *i;
178
+ VALUE rc = Qfalse;
179
+ double d;
180
+
181
+ Data_Get_Struct( self, hitimes_interval_t, i );
182
+ if ( 0L == i->start_instant ) {
183
+ return rc;
184
+ }
185
+
186
+ if ( 0L == i->stop_instant ) {
187
+ hitimes_instant_t now = hitimes_get_current_instant( );
188
+ d = ( now - i->start_instant ) / HITIMES_INSTANT_CONVERSION_FACTOR;
189
+ rc = rb_float_new( d );
190
+ }
191
+ return rc;
192
+ }
193
+
194
+
167
195
  /**
168
196
  * call-seq:
169
197
  * interval.started? -> boolean
@@ -328,6 +356,8 @@ void Init_hitimes_interval()
328
356
  rb_define_method( cH_Interval, "length", hitimes_interval_duration, 0 );
329
357
  rb_define_method( cH_Interval, "to_f", hitimes_interval_duration, 0 );
330
358
  rb_define_method( cH_Interval, "to_seconds", hitimes_interval_duration, 0 );
359
+
360
+ rb_define_method( cH_Interval, "duration_so_far", hitimes_interval_duration_so_far, 0); /* in hitimes_interval.c */
331
361
 
332
362
  rb_define_method( cH_Interval, "started?", hitimes_interval_started, 0 ); /* in hitimes_interval.c */
333
363
  rb_define_method( cH_Interval, "running?", hitimes_interval_running, 0 ); /* in hitimes_interval.c */
@@ -16,13 +16,8 @@ module Hitimes
16
16
  #
17
17
  class Metric
18
18
 
19
- # the time at which the first sample was taken
20
- # This is the number of microseconds since UNIX epoch UTC as a Float
21
- attr_accessor :sampling_start_time
22
-
23
- # the time at which the last sample was taken.
24
- # This is the number of microseconds since UNIX epoch UTC as a Float
25
- attr_accessor :sampling_stop_time
19
+ # the number of seconds as a float since the sampling_start_time
20
+ attr_reader :sampling_delta
26
21
 
27
22
  # An additional hash of data to associate with the metric
28
23
  attr_reader :additional_data
@@ -41,12 +36,55 @@ module Hitimes
41
36
  # +name+ may be anything that follows the +to_s+ protocol.
42
37
  #
43
38
  def initialize( name, additional_data = {} )
44
- @sampling_start_time = nil
45
- @sampling_stop_time = nil
39
+ @sampling_start_time = nil
40
+ @sampling_start_interval = nil
41
+ @sampling_delta = 0
42
+
46
43
  @name = name.to_s
47
44
  @additional_data = additional_data.to_hash
48
45
  end
49
46
 
47
+ #
48
+ # :call-seq:
49
+ # metric.sampling_start_time -> Float or nil
50
+ #
51
+ # The time at which the first sample was taken.
52
+ # This is the number of microseconds since UNIX epoch UTC as a Float
53
+ #
54
+ # If the metric has not started measuring then the start time is nil.
55
+ #
56
+ def sampling_start_time
57
+ if @sampling_start_interval then
58
+ @sampling_start_time ||= self.utc_microseconds()
59
+ else
60
+ nil
61
+ end
62
+ end
63
+
64
+ #
65
+ # :call-seq:
66
+ # metric.sampling_stop_time -> Float or nil
67
+ #
68
+ # The time at which the last sample was taken
69
+ # This is the number of microseconds since UNIX epoch UTC as a Float
70
+ #
71
+ # If the metric has not completely measured at least one thing then
72
+ # stop time is nil.
73
+ #
74
+ # Because accessing the actual 'time of day' is an expesive operation, we
75
+ # only get the time of day at the beginning of the first measurement and we
76
+ # keep track of the offset from that point in @sampling_delta.
77
+ #
78
+ # When sampling_stop_time is called, the actual time of day is caculated.
79
+ #
80
+ def sampling_stop_time
81
+ if @sampling_delta > 0 then
82
+ (self.sampling_start_time + (@sampling_delta * 1_000_000))
83
+ else
84
+ nil
85
+ end
86
+ end
87
+
50
88
  #
51
89
  # :call-seq:
52
90
  # metric.to_hash -> Hash
@@ -55,10 +93,10 @@ module Hitimes
55
93
  # Convert the metric to a Hash.
56
94
  #
57
95
  def to_hash
58
- { 'sampling_start_time' => @sampling_start_time,
59
- 'sampling_stop_time' => @sampling_stop_time,
60
- 'additional_data' => @additional_data,
61
- 'name' => @name }
96
+ { 'sampling_start_time' => self.sampling_start_time,
97
+ 'sampling_stop_time' => self.sampling_stop_time,
98
+ 'additional_data' => self.additional_data,
99
+ 'name' => self.name }
62
100
  end
63
101
 
64
102
  #
data/lib/hitimes/stats.rb CHANGED
@@ -1,4 +1,4 @@
1
- require 'hitimes_ext'
1
+ require 'hitimes'
2
2
  require 'stringio'
3
3
  module Hitimes
4
4
  class Stats
@@ -60,17 +60,7 @@ module Hitimes
60
60
  def initialize( name, additional_data = {} )
61
61
  super( name, additional_data )
62
62
  @stats = Stats.new
63
- @current_interval = nil
64
- end
65
-
66
- #
67
- # :call-seq:
68
- # timed_metric.current_interval -> Interval
69
- #
70
- # Return the current interval, if one doesn't exist create one.
71
- #
72
- def current_interval
73
- @current_interval ||= Interval.new
63
+ @current_interval = Interval.new
74
64
  end
75
65
 
76
66
  #
@@ -80,7 +70,7 @@ module Hitimes
80
70
  # return whether or not the timer is currently running.
81
71
  #
82
72
  def running?
83
- current_interval.running?
73
+ @current_interval.running?
84
74
  end
85
75
 
86
76
  #
@@ -91,8 +81,11 @@ module Hitimes
91
81
  # this is a noop.
92
82
  #
93
83
  def start
94
- current_interval.start unless running?
95
- @sampling_start_time ||= self.utc_microseconds()
84
+ if not @current_interval.running? then
85
+ @current_interval.start
86
+ @sampling_start_time ||= self.utc_microseconds()
87
+ @sampling_start_interval ||= Interval.now
88
+ end
96
89
  nil
97
90
  end
98
91
 
@@ -106,11 +99,14 @@ module Hitimes
106
99
  # no stats are updated.
107
100
  #
108
101
  def stop
109
- if running? then
110
- d = current_interval.stop
111
- @current_interval = nil
112
- @sampling_stop_time = self.utc_microseconds()
102
+ if @current_interval.running? then
103
+ d = @current_interval.stop
113
104
  @stats.update( d )
105
+ @current_interval = Interval.new
106
+
107
+ # update the length of time we have been sampling
108
+ @sampling_delta = @sampling_start_interval.duration_so_far
109
+
114
110
  return d
115
111
  end
116
112
  return false
@@ -148,9 +144,9 @@ module Hitimes
148
144
  # happens and false is returned.
149
145
  #
150
146
  def split
151
- if running? then
152
- next_interval = current_interval.split
153
- d = current_interval.duration
147
+ if @current_interval.running? then
148
+ next_interval = @current_interval.split
149
+ d = @current_interval.duration
154
150
  @stats.update( d )
155
151
  @current_interval = next_interval
156
152
  return d
@@ -59,22 +59,11 @@ module Hitimes
59
59
  #
60
60
  def initialize( name, additional_data = {} )
61
61
  super( name, additional_data )
62
- @current_interval = nil
63
62
  @timed_stats = Stats.new
64
63
  @value_stats = Stats.new
64
+ @current_interval = Interval.new
65
65
  end
66
66
 
67
- #
68
- # :call-seq:
69
- # timed_value_metric.current_interval -> Interval
70
- #
71
- # Return the current interval, if one doesn't exist create one.
72
- #
73
- def current_interval
74
- @current_interval ||= Interval.new
75
- end
76
-
77
-
78
67
  #
79
68
  # :call-seq:
80
69
  # timed_value_metric.running? -> true or false
@@ -82,7 +71,7 @@ module Hitimes
82
71
  # return whether or not the metric is currently timing something.
83
72
  #
84
73
  def running?
85
- current_interval.running?
74
+ @current_interval.running?
86
75
  end
87
76
 
88
77
  #
@@ -93,8 +82,11 @@ module Hitimes
93
82
  # this is a noop.
94
83
  #
95
84
  def start
96
- current_interval.start unless running?
97
- @sampling_start_time ||= self.utc_microseconds()
85
+ if not @current_interval.running? then
86
+ @current_interval.start
87
+ @sampling_start_time ||= self.utc_microseconds()
88
+ @sampling_start_interval ||= Interval.now
89
+ end
98
90
  nil
99
91
  end
100
92
 
@@ -114,12 +106,15 @@ module Hitimes
114
106
  #
115
107
  #
116
108
  def stop( value )
117
- if running? then
118
- d = current_interval.stop
119
- @current_interval = nil
120
- @sampling_stop_time = self.utc_microseconds()
109
+ if @current_interval.running? then
110
+ d = @current_interval.stop
121
111
  @timed_stats.update( d )
112
+ @current_interval = Interval.new
122
113
  @value_stats.update( value )
114
+
115
+ # update the lenght of time we have been sampling
116
+ @sampling_delta = @sampling_start_interval.duration_so_far
117
+
123
118
  return d
124
119
  end
125
120
  return false
@@ -159,9 +154,9 @@ module Hitimes
159
154
  #
160
155
  #
161
156
  def split( value )
162
- if running? then
163
- next_interval = current_interval.split
164
- d = current_interval.duration
157
+ if @current_interval.running? then
158
+ next_interval = @current_interval.split
159
+ d = @current_interval.duration
165
160
  @timed_stats.update( d )
166
161
  @value_stats.update( value )
167
162
  @current_interval = next_interval
@@ -42,10 +42,13 @@ module Hitimes
42
42
  # Give the +value+ as the measurement to the metric. The value is returned
43
43
  #
44
44
  def measure( value )
45
- now = self.utc_microseconds()
46
- @sampling_start_time ||= now
47
- @sampling_stop_time = now
45
+ @sampling_start_time ||= self.utc_microseconds()
46
+ @sampling_start_interval ||= Interval.now
47
+
48
48
  @stats.update( value )
49
+
50
+ # update the length of time we have been sampling
51
+ @sampling_delta = @sampling_start_interval.duration_so_far
49
52
  end
50
53
 
51
54
  #
@@ -16,7 +16,7 @@ module Hitimes
16
16
  MINOR = 0
17
17
 
18
18
  # Build number
19
- BUILD = 0
19
+ BUILD = 1
20
20
 
21
21
  #
22
22
  # :call-seq:
data/lib/hitimes.rb CHANGED
@@ -18,8 +18,8 @@ module Hitimes
18
18
  end
19
19
  require 'hitimes/paths'
20
20
  require 'hitimes/version'
21
- require 'hitimes/stats'
22
21
  require 'hitimes_ext'
22
+ require 'hitimes/stats'
23
23
  require 'hitimes/mutexed_stats'
24
24
  require 'hitimes/metric'
25
25
  require 'hitimes/value_metric'
@@ -94,6 +94,18 @@ describe Hitimes::Interval do
94
94
  x.object_id.should == y.object_id
95
95
  end
96
96
 
97
+ it "can return how much time has elapsed from the start without stopping the interval" do
98
+ i = Hitimes::Interval.new
99
+ i.start
100
+ x = i.duration_so_far
101
+ i.should be_running
102
+ y = i.duration_so_far
103
+ i.stop
104
+ x.should < y
105
+ x.should < i.duration
106
+ y.should < i.duration
107
+ end
108
+
97
109
  describe "#split" do
98
110
 
99
111
  it "creates a new Interval object" do
@@ -95,9 +95,9 @@ describe Hitimes::TimedMetric do
95
95
  end
96
96
 
97
97
  it "keeps track of the last stop time of all the intervals" do
98
- f1 = Time.now.gmtime.to_f * 1000000
98
+ f1 = Time.now.gmtime.to_f * 1_000_000
99
99
  5.times { @tm.start ; sleep 0.05 ; @tm.stop }
100
- f2 = Time.now.gmtime.to_f * 1000000
100
+ f2 = Time.now.gmtime.to_f * 1_000_000
101
101
  @tm.sampling_stop_time.should > f1
102
102
  @tm.sampling_stop_time.should < f2
103
103
  # distance from now to max stop time time should be less than the distance
@@ -131,7 +131,7 @@ describe Hitimes::TimedMetric do
131
131
  h['name'].should == "test-timed-metric"
132
132
  end
133
133
 
134
- it "has an empty has for additional_data" do
134
+ it "has an empty hash for additional_data" do
135
135
  h = @tm.to_hash
136
136
  h['additional_data'].should == Hash.new
137
137
  h['additional_data'].size.should == 0
data/tasks/config.rb CHANGED
@@ -83,7 +83,7 @@ Configuration.for('rdoc') {
83
83
  files Configuration.for('packaging').files.rdoc
84
84
  main_page files.first
85
85
  title Configuration.for('project').name
86
- options %w[ --line-numbers --inline-source ]
86
+ options %w[ --line-numbers ]
87
87
  output_dir "doc"
88
88
  }
89
89
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hitimes
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeremy Hinegardner
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-06-12 00:00:00 -06:00
12
+ date: 2009-06-13 00:00:00 -06:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -118,7 +118,6 @@ homepage: http://copiousfreetime.rubyforge.org/hitimes/
118
118
  post_install_message:
119
119
  rdoc_options:
120
120
  - --line-numbers
121
- - --inline-source
122
121
  - --main
123
122
  - README
124
123
  require_paths: