Changeset 2704 for lang/ruby/ssb

Show
Ignore:
Timestamp:
12/07/07 08:53:56 (13 months ago)
Author:
coji
Message:

lang/ruby/ssb: refactored TimeStamp?

Location:
lang/ruby/ssb/trunk
Files:
4 modified

Legend:

Unmodified
Added
Removed
  • lang/ruby/ssb/trunk/libs/ssb.rb

    r2695 r2704  
    2020  class Application 
    2121    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                                         }) 
    3827    end 
    3928 
  • lang/ruby/ssb/trunk/libs/ssb/time_stamper.rb

    r1870 r2704  
    88module SSB 
    99  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? 
    1116      @log = Hash.new 
    1217    end 
    1318 
    14     def stamp(label) 
    15       @log[label] = Time.now 
    16       @log[label] 
     19    def span_labels 
     20      @span_defs.keys 
    1721    end 
    1822 
    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 
    2133    end 
    2234 
     
    2840      @log[label] 
    2941    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 
    3050  end 
    3151end 
  • lang/ruby/ssb/trunk/templates/ssb.rhtml

    r2526 r2704  
    5252              <% 
    5353  time_class = 
    54   case request_time 
     54  case @time_stamp.request_time 
    5555  when 0..500 
    5656    "time_safe" 
     
    6060    "time_warning" 
    6161  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> 
    6363              <td class="time_unit">ms</td> 
    6464            </tr> 
    6565            <tr> 
    6666              <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> 
    6868              <td class="time_unit">ms</td> 
    6969            </tr> 
    7070            <tr> 
    7171              <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> 
    7373              <td class="time_unit">ms</td> 
    7474            </tr> 
  • lang/ruby/ssb/trunk/test/time_stamper_test.rb

    r2212 r2704  
    33 
    44unit_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) 
    712  end 
    813 
    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 
    1032    stamp = SSB::TimeStamper.new 
    1133    now = Time.now 
    1234 
    13     assert_equal(stamp.count, 0) 
    1435    stamped = stamp.stamp(:test) 
     36 
    1537    assert_equal(stamped.class, Time) 
    1638    assert(stamped >= now) 
     
    1941    assert_equal(stamped, stamp[:test]) 
    2042  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 
    2172end