Adding tests for the heap calculation, also changed the visibility

In retrospect, it just easier to expose the methods in the jvm spec
monitoring to be able to test them in isolation.

Fixes #6827
This commit is contained in:
Pier-Hugues Pellerin 2017-03-23 11:09:21 -04:00 committed by Suyog Rao
parent 68151c894a
commit cf57bcb99e
2 changed files with 44 additions and 13 deletions

View file

@ -34,6 +34,13 @@ module LogStash module Instrument module PeriodicPoller
end end
end end
MEMORY_TRANSPOSE_MAP = {
"usage.used" => :used_in_bytes,
"usage.committed" => :committed_in_bytes,
"usage.max" => :max_in_bytes,
"peak.max" => :peak_max_in_bytes,
"peak.used" => :peak_used_in_bytes
}
attr_reader :metric attr_reader :metric
@ -52,8 +59,6 @@ module LogStash module Instrument module PeriodicPoller
collect_load_average collect_load_average
end end
private
def collect_gc_stats def collect_gc_stats
garbage_collectors = ManagementFactory.getGarbageCollectorMXBeans() garbage_collectors = ManagementFactory.getGarbageCollectorMXBeans()
@ -141,7 +146,6 @@ module LogStash module Instrument module PeriodicPoller
end end
end end
def build_pools_metrics(data) def build_pools_metrics(data)
heap = data["heap"] heap = data["heap"]
old = {} old = {}
@ -161,19 +165,11 @@ module LogStash module Instrument module PeriodicPoller
end end
def aggregate_information_for(collection) def aggregate_information_for(collection)
transpose_map = {
"usage.used" => :used_in_bytes,
"usage.committed" => :committed_in_bytes,
"usage.max" => :max_in_bytes,
"peak.max" => :peak_max_in_bytes,
"peak.used" => :peak_used_in_bytes
}
collection.reduce(default_information_accumulator) do |m,e| collection.reduce(default_information_accumulator) do |m,e|
e = { e[0] => e[1] } if e.is_a?(Array) e = { e[0] => e[1] } if e.is_a?(Array)
e.each_pair do |k,v| e.each_pair do |k,v|
if transpose_map.include?(k) if MEMORY_TRANSPOSE_MAP.include?(k)
transpose_key = transpose_map[k] transpose_key = MEMORY_TRANSPOSE_MAP[k]
m[transpose_key] += v m[transpose_key] += v
end end
end end

View file

@ -54,6 +54,41 @@ describe LogStash::Instrument::PeriodicPoller::JVM do
end end
end end
describe "aggregate heap information" do
shared_examples "heap_information" do
let(:data_set) do
{
"usage.used" => 5,
"usage.committed" => 11,
"usage.max" => 21,
"peak.max" => 51,
"peak.used" => 61
}
end
let(:collection) { [data_set] }
it "return the right values" do
expect(subject.aggregate_information_for(collection)).to match({
:used_in_bytes => 5 * collection.size,
:committed_in_bytes => 11 * collection.size,
:max_in_bytes => 21 * collection.size,
:peak_max_in_bytes => 51 * collection.size,
:peak_used_in_bytes => 61 * collection.size
})
end
end
context "with only one data set in a collection" do
include_examples "heap_information"
end
context "with multiples data set in a collection" do
include_examples "heap_information" do
let(:collection) { ar = []; ar << data_set; ar << data_set; ar }
end
end
end
describe "collections" do describe "collections" do
subject(:collection) { jvm.collect } subject(:collection) { jvm.collect }
it "should run cleanly" do it "should run cleanly" do