Changeset 2704
- Timestamp:
- 12/07/07 08:53:56 (5 years ago)
- Location:
- lang/ruby/ssb/trunk
- Files:
-
- 4 modified
-
libs/ssb.rb (modified) (1 diff)
-
libs/ssb/time_stamper.rb (modified) (2 diffs)
-
templates/ssb.rhtml (modified) (2 diffs)
-
test/time_stamper_test.rb (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
lang/ruby/ssb/trunk/libs/ssb.rb
r2695 r2704 20 20 class Application 21 21 def initialize 22 @time_stamp = SSB::TimeStamper.new 23 end 24 25 # リクエスト開始 〜 終了 26 def request_time 27 @time_stamp.diff(:request_start, :request_finish) 28 end 29 30 # 処理開始 〜 処理完了 31 def proc_time 32 @time_stamp.diff(:request_finish, :proc_finish) 33 end 34 35 # リクエスト終了 〜 処理完了 36 def total_time 37 proc_time + request_time 22 @time_stamp = SSB::TimeStamper.new({ 23 :request_time => [:request_start, :request_finish], 24 :proc_time => [:request_finish, :proc_finish], 25 :total_time => [:request_start, :request_proc_finish], 26 }) 38 27 end 39 28 -
lang/ruby/ssb/trunk/libs/ssb/time_stamper.rb
r1870 r2704 8 8 module SSB 9 9 class TimeStamper 10 def initialize 10 attr_reader :log 11 12 # { span_label => [stamp_from, stamp_to], ... } 13 def initialize(span_defs = nil) 14 @span_defs = Hash.new 15 @span_defs.update(span_defs) unless span_defs.nil? 11 16 @log = Hash.new 12 17 end 13 18 14 def stamp(label) 15 @log[label] = Time.now 16 @log[label] 19 def span_labels 20 @span_defs.keys 17 21 end 18 22 19 def diff(from, to) 20 ((self[to].to_f - self[from].to_f)*1000).to_i 23 def span(label) 24 diff(@span_defs[label][0], @span_defs[label][1]) 25 end 26 27 def stamp(stamp, time = Time.now) 28 @log[stamp] = time 29 end 30 31 def diff(stamp_from, stamp_to) 32 ((@log[stamp_to].to_f - @log[stamp_from].to_f)*1000).to_i 21 33 end 22 34 … … 28 40 @log[label] 29 41 end 42 43 def method_missing(msg, *args) 44 if span_labels.include?(msg.to_sym) 45 span(msg.to_sym) 46 else 47 #super 48 end 49 end 30 50 end 31 51 end -
lang/ruby/ssb/trunk/templates/ssb.rhtml
r2526 r2704 52 52 <% 53 53 time_class = 54 case request_time54 case @time_stamp.request_time 55 55 when 0..500 56 56 "time_safe" … … 60 60 "time_warning" 61 61 end %> 62 <td class="<%= time_class %>"><%= SSB::Misc.numeric( request_time) %></td>62 <td class="<%= time_class %>"><%= SSB::Misc.numeric(@time_stamp.request_time) %></td> 63 63 <td class="time_unit">ms</td> 64 64 </tr> 65 65 <tr> 66 66 <td class="time_label">変換処理時間</td> 67 <td class="time"><%= SSB::Misc.numeric( proc_time) %></td>67 <td class="time"><%= SSB::Misc.numeric(@time_stamp.proc_time) %></td> 68 68 <td class="time_unit">ms</td> 69 69 </tr> 70 70 <tr> 71 71 <td class="time_label">合計</td> 72 <td class="time"><%= SSB::Misc.numeric( total_time) %></td>72 <td class="time"><%= SSB::Misc.numeric(@time_stamp.total_time) %></td> 73 73 <td class="time_unit">ms</td> 74 74 </tr> -
lang/ruby/ssb/trunk/test/time_stamper_test.rb
r2212 r2704 3 3 4 4 unit_tests do 5 test 'instance' do 6 assert SSB::TimeStamper.new 5 test 'instance default' do 6 stamp = SSB::TimeStamper.new 7 assert stamp 8 assert_instance_of(Hash, stamp.log) 9 assert_equal(stamp.log.size, 0) 10 assert_instance_of(Array, stamp.span_labels) 11 assert_equal(stamp.span_labels.size, 0) 7 12 end 8 13 9 test 'stampit' do 14 test 'instance with span definision' do 15 stamp = SSB::TimeStamper.new({ 16 :test1 => [:test1_start, :test1_done], 17 :test2 => [:test2_start, :test2_done], 18 :test3 => [:test3_start, :test3_done], 19 :total => [:test1_start, :test3_done],}) 20 assert stamp 21 assert_instance_of(Hash, stamp.log) 22 assert_equal(stamp.log.size, 0) 23 assert_instance_of(Array, stamp.span_labels) 24 assert_equal(stamp.span_labels.size, 4) 25 assert(stamp.span_labels.include?(:test1)) 26 assert(stamp.span_labels.include?(:test2)) 27 assert(stamp.span_labels.include?(:test3)) 28 assert(stamp.span_labels.include?(:total)) 29 end 30 31 test 'simple stamp collect' do 10 32 stamp = SSB::TimeStamper.new 11 33 now = Time.now 12 34 13 assert_equal(stamp.count, 0)14 35 stamped = stamp.stamp(:test) 36 15 37 assert_equal(stamped.class, Time) 16 38 assert(stamped >= now) … … 19 41 assert_equal(stamped, stamp[:test]) 20 42 end 43 44 test 'stamp span' do 45 stamp = SSB::TimeStamper.new({ 46 :test1 => [:test1_start, :test1_done], 47 :test2 => [:test2_start, :test2_done], 48 :test3 => [:test3_start, :test3_done], 49 :total => [:test1_start, :test3_done],}) 50 51 stamp.stamp :test1_start, Time.at(1) 52 stamp.stamp :test1_done, Time.at(2) 53 assert_equal(stamp.count, 2) 54 assert_equal(stamp.span(:test1), 1000) 55 assert_equal(stamp.test1, 1000) 56 57 stamp.stamp :test2_start, Time.at(3) 58 stamp.stamp :test2_done, Time.at(5) 59 assert_equal(stamp.count, 4) 60 assert_equal(stamp.span(:test2), 2000) 61 assert_equal(stamp.test2, 2000) 62 63 stamp.stamp :test3_start, Time.at(8) 64 stamp.stamp :test3_done, Time.at(13) 65 assert_equal(stamp.count, 6) 66 assert_equal(stamp.span(:test3), 5000) 67 assert_equal(stamp.test3, 5000) 68 69 assert_equal(stamp.span(:total), 12000) 70 assert_equal(stamp.total, 12000) 71 end 21 72 end
![(please configure the [header_logo] section in trac.ini)](/share/chrome/site/your_project_logo.png)