Class: EventHub::Statistics

Inherits:
Object
  • Object
show all
Defined in:
lib/eventhub/statistics.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeStatistics

Returns a new instance of Statistics.



4
5
6
7
8
9
10
11
# File 'lib/eventhub/statistics.rb', line 4

def initialize
  @messages_successful = 0
  @messages_unsuccessful = 0
  @messages_average_size = 0
  @messages_average_process_time = 0
  @messages_total_process_time = 0
  @mutex = Mutex.new
end

Instance Attribute Details

#messages_average_process_timeObject (readonly)

Returns the value of attribute messages_average_process_time.



2
3
4
# File 'lib/eventhub/statistics.rb', line 2

def messages_average_process_time
  @messages_average_process_time
end

#messages_average_sizeObject (readonly)

Returns the value of attribute messages_average_size.



2
3
4
# File 'lib/eventhub/statistics.rb', line 2

def messages_average_size
  @messages_average_size
end

#messages_successfulObject (readonly)

Returns the value of attribute messages_successful.



2
3
4
# File 'lib/eventhub/statistics.rb', line 2

def messages_successful
  @messages_successful
end

#messages_unsuccessfulObject (readonly)

Returns the value of attribute messages_unsuccessful.



2
3
4
# File 'lib/eventhub/statistics.rb', line 2

def messages_unsuccessful
  @messages_unsuccessful
end

Instance Method Details

#failureObject



35
36
37
38
39
40
# File 'lib/eventhub/statistics.rb', line 35

def failure
  @mutex.lock
  @messages_unsuccessful += 1
ensure
  @mutex.unlock
end

#measure(size, &block) ⇒ Object



14
15
16
17
18
19
20
21
22
23
# File 'lib/eventhub/statistics.rb', line 14

def measure(size, &block)
  begin
    start = Time.now
    yield
    success(Time.now - start, size)
  rescue
    failure
    raise
  end
end

#messages_totalObject



42
43
44
# File 'lib/eventhub/statistics.rb', line 42

def messages_total
  messages_unsuccessful + messages_successful
end

#messages_total_process_timeObject



46
47
48
# File 'lib/eventhub/statistics.rb', line 46

def messages_total_process_time
  messages_average_process_time * messages_successful
end

#messages_total_sizeObject



50
51
52
# File 'lib/eventhub/statistics.rb', line 50

def messages_total_size
  messages_average_size * messages_successful
end

#success(process_time, size) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/eventhub/statistics.rb', line 25

def success(process_time, size)
  @mutex.lock
  @messages_total_process_time += process_time
  @messages_average_process_time = (messages_total_process_time + process_time) / (messages_successful + 1).to_f
  @messages_average_size = (messages_total_size + size) / (messages_successful + 1).to_f
  @messages_successful += 1
ensure
  @mutex.unlock
end