Class: TingYun::Metrics::Stats

Inherits:
Object
  • Object
show all
Defined in:
lib/ting_yun/metrics/stats.rb

Direct Known Subclasses

Agent::Collector::SqlTrace

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeStats

Returns a new instance of Stats.



27
28
29
# File 'lib/ting_yun/metrics/stats.rb', line 27

def initialize
  reset
end

Instance Attribute Details

#call_countObject Also known as: apdex_s

Returns the value of attribute call_count.



7
8
9
# File 'lib/ting_yun/metrics/stats.rb', line 7

def call_count
  @call_count
end

#max_call_timeObject

Returns the value of attribute max_call_time.



9
10
11
# File 'lib/ting_yun/metrics/stats.rb', line 9

def max_call_time
  @max_call_time
end

#min_call_timeObject

Returns the value of attribute min_call_time.



8
9
10
# File 'lib/ting_yun/metrics/stats.rb', line 8

def min_call_time
  @min_call_time
end

#sum_of_squaresObject

Returns the value of attribute sum_of_squares.



12
13
14
# File 'lib/ting_yun/metrics/stats.rb', line 12

def sum_of_squares
  @sum_of_squares
end

#total_call_timeObject Also known as: apdex_t

Returns the value of attribute total_call_time.



10
11
12
# File 'lib/ting_yun/metrics/stats.rb', line 10

def total_call_time
  @total_call_time
end

#total_exclusive_timeObject Also known as: apdex_f

Returns the value of attribute total_exclusive_time.



11
12
13
# File 'lib/ting_yun/metrics/stats.rb', line 11

def total_exclusive_time
  @total_exclusive_time
end

Class Method Details

.create_from_hash(hash_value) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/ting_yun/metrics/stats.rb', line 15

def self.create_from_hash(hash_value)
  stats = Stats.new
  stats.call_count           = hash_value[:count] if hash_value[:count]
  stats.total_call_time      = hash_value[:total] if hash_value[:total]
  stats.total_exclusive_time = hash_value[:total] if hash_value[:total]
  stats.min_call_time        = hash_value[:min] if hash_value[:min]
  stats.max_call_time        = hash_value[:max] if hash_value[:max]
  stats.sum_of_squares       = hash_value[:sum_of_squares] if hash_value[:sum_of_squares]
  stats
end

Instance Method Details

#==(other) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
# File 'lib/ting_yun/metrics/stats.rb', line 133

def ==(other)
  other.class == self.class &&
      (
      @min_call_time == other.min_call_time &&
          @max_call_time == other.max_call_time &&
          @total_call_time == other.total_call_time &&
          @total_exclusive_time == other.total_exclusive_time &&
          @sum_of_squares == other.sum_of_squares &&
          @call_count == other.call_count
      )
end

#increment_count(value = 1) ⇒ Object

increments the call_count by one



129
130
131
# File 'lib/ting_yun/metrics/stats.rb', line 129

def increment_count(value = 1)
  @call_count += value
end

#is_reset?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/ting_yun/metrics/stats.rb', line 45

def is_reset?
  call_count == 0 && total_call_time == 0.0 && total_exclusive_time == 0.0
end

#merge(other_stats) ⇒ Object

self不变



50
51
52
53
# File 'lib/ting_yun/metrics/stats.rb', line 50

def merge(other_stats)
  stats = self.clone
  stats.merge!(other_stats)
end

#merge!(other) ⇒ Object

self变化



56
57
58
59
60
61
62
63
64
# File 'lib/ting_yun/metrics/stats.rb', line 56

def merge!(other)
  @min_call_time = other.min_call_time if min_time_less?(other)
  @max_call_time = other.max_call_time if max_time?(other)
  @total_call_time += other.total_call_time
  @total_exclusive_time += other.total_exclusive_time
  @sum_of_squares += other.sum_of_squares
  @call_count += other.call_count
  self
end

#record(value = nil, aux = nil, &blk) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/ting_yun/metrics/stats.rb', line 81

def record(value=nil, aux=nil, &blk)
  if blk
    yield self
  else
    case value
      when Numeric
        aux ||= value
        self.record_data_point(value, aux)
      when :apdex_s, :apdex_t, :apdex_f
        self.record_apdex(value, aux)
      when TingYun::Metrics::Stats
        self.merge!(value)
    end
  end
end

#record_apdex(bucket, apdex_t) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/ting_yun/metrics/stats.rb', line 97

def record_apdex(bucket, apdex_t)
  case bucket
    when :apdex_s then @call_count += 1
    when :apdex_t then @total_call_time += 1
    when :apdex_f then @total_exclusive_time += 1
  end
  if apdex_t
    @max_call_time = apdex_t
  else
    ::TingYun::Agent.logger.warn("Attempted to set apdex_t to #{apdex_t.inspect}, backtrace = #{caller.join("\n")}")
  end
end

#record_data_point(value, exclusive_time = value) ⇒ Object Also known as: trace_call

record a single data point into the statistical gatherer. The gatherer will aggregate all data points collected over a specified period and upload its data to the TingYun server



115
116
117
118
119
120
121
122
123
124
# File 'lib/ting_yun/metrics/stats.rb', line 115

def record_data_point(value, exclusive_time = value)
  @call_count += 1
  @total_call_time += value
  @min_call_time = value if value < @min_call_time || @call_count == 1
  @max_call_time = value if value > @max_call_time
  @total_exclusive_time += exclusive_time

  @sum_of_squares += (value * value)
  self
end

#resetObject



31
32
33
34
35
36
37
38
# File 'lib/ting_yun/metrics/stats.rb', line 31

def reset
  @call_count = 0
  @total_call_time = 0.0
  @total_exclusive_time = 0.0
  @min_call_time = 0.0
  @max_call_time = 0.0
  @sum_of_squares = 0.0
end

#to_json(*_) ⇒ Object



70
71
72
73
74
75
76
77
78
79
# File 'lib/ting_yun/metrics/stats.rb', line 70

def to_json(*_)
  {
      'call_count' => call_count.to_i,
      'min_call_time' => min_call_time.to_f,
      'max_call_time' => max_call_time.to_f,
      'total_call_time' => total_call_time.to_f,
      'total_exclusive_time' => total_exclusive_time.to_f,
      'sum_of_squares' => sum_of_squares.to_f
  }.to_json(*_)
end

#to_sObject



66
67
68
# File 'lib/ting_yun/metrics/stats.rb', line 66

def to_s
  "[#{'%2i' % call_count.to_i} calls #{'%.4f' % total_call_time.to_f}s / #{'%.4f' % total_exclusive_time.to_f}s ex]"
end