Class: LogStash::PerformanceMeter::Stats

Inherits:
Object
  • Object
show all
Defined in:
lib/lsperfm/core/stats.rb

Constant Summary collapse

REFRESH_COUNT =
100

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeStats

Returns a new instance of Stats.



14
15
16
# File 'lib/lsperfm/core/stats.rb', line 14

def initialize
  @stats = []
end

Instance Attribute Details

#statsObject

Returns the value of attribute stats.



12
13
14
# File 'lib/lsperfm/core/stats.rb', line 12

def stats
  @stats
end

Instance Method Details

#detach_output_reader(io, regex) ⇒ Object

detach_output_reader spawns a thread that will fill in the @stats instance var with tps samples for every seconds once the output reader thread is completed.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/lsperfm/core/stats.rb', line 34

def detach_output_reader(io, regex)
  Thread.new(io, regex) do |io, regex|
    i = 0
    @stats = []
    @stats_count = 0
    @stats_lock = Mutex.new
    t = detach_stats_counter

    expect_output(io, regex) do
      i += 1
      # avoid mutex synchronize on every loop cycle, using REFRESH_COUNT = 100 results in
      # much lower mutex overhead and still provides a good resolution since we are typically
      # have 2000..100000 tps
      @stats_lock.synchronize{@stats_count = i} if (i % REFRESH_COUNT) == 0
    end

    @stats_lock.synchronize{t.kill}
  end
end

#detach_stats_counterObject

below stats counter and output reader threads are sharing state using the @stats_lock mutex, @stats_count and @stats. this is a bit messy and should be refactored into a proper class eventually



21
22
23
24
25
26
27
28
29
# File 'lib/lsperfm/core/stats.rb', line 21

def detach_stats_counter
  Thread.new do
    loop do
      start = @stats_lock.synchronize{@stats_count}
      sleep(1)
      @stats_lock.synchronize{@stats << (@stats_count - start)}
    end
  end
end

#expect_output(io, regex) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/lsperfm/core/stats.rb', line 54

def expect_output(io, regex)
  io.each_line do |line|
    puts("received: #{line}") if @debug
    yield if block_given?
    break if line =~ regex
  end
end