spec/gauge_spec.rb in fnordmetric-0.5.1 vs spec/gauge_spec.rb in fnordmetric-0.5.2

- old
+ new

@@ -10,15 +10,25 @@ before(:each) do @redis.keys("fnordmetric-myns*").each { |k| @redis.del(k) } end - it "should remember it's own name" do + it "should remember its own name" do gauge = FnordMetric::Gauge.new({:key_prefix => "foo", :key => "fnordgauge"}) gauge.name.should == "fnordgauge" end + it "should return its key as title if none specified" do + gauge = FnordMetric::Gauge.new({:key_prefix => "foo", :key => "fnordgauge"}) + gauge.title.should == "fnordgauge" + end + + it "should return its title as title if none specified" do + gauge = FnordMetric::Gauge.new({:key_prefix => "foo", :key => "fnordgauge", :title => "Fnord Gauge"}) + gauge.title.should == "Fnord Gauge" + end + it "should raise an error when initialize without key" do lambda{ FnordMetric::Gauge.new({:key_prefix => "foo"}) }.should raise_error(KeyError) end @@ -39,13 +49,19 @@ gauge.key(:fnord).should == "fnordmetrics-myns-gauge-mygauge-23-fnord" end describe "ticks" do - it "should return the correct tick if configured" - it "should return the default tick if none configured" + it "should return the correct tick if configured" do + gauge = FnordMetric::Gauge.new({:tick => 23, :key_prefix => "fnordmetrics-myns", :key => "mygauge"}) + gauge.tick.should == 23 + end + it "should return the default tick if none configured" do + gauge = FnordMetric::Gauge.new({:key_prefix => "fnordmetrics-myns", :key => "mygauge"}) + gauge.tick.should == 3600 + end it "should return the correct tick_at" do gauge = FnordMetric::Gauge.new({:tick => 10, :key_prefix => "fnordmetrics-myns", :key => "mygauge"}) gauge.tick_at(@now).should == 695280200 gauge.tick_at(@now+6).should == 695280200 @@ -79,25 +95,73 @@ it "should call the value calculation block and return the result" do @gauge.value_at(@now){ |v| v.to_i + 123 }.should == 177 end it "should return the correct value_at per session" do + @redis.hset(@gauge_key, "695280200", "76") @redis.set(@gauge_key+"-695280200-sessions-count", "23") - @gauge.value_at(@now, :avg_per_session => 1).should == (54.0/23.0) + _gauge = FnordMetric::Gauge.new({ + :tick => 10, + :key_prefix => "fnordmetric-myns", + :key => "mygauge_966", + :unique => true, + :redis => @redis + }) + _gauge.value_at(@now).should == "76" end + it "should return the correct value_at per session with avg" do + @redis.hset(@gauge_key, "695280200", "76") + @redis.set(@gauge_key+"-695280200-sessions-count", "23") + _gauge = FnordMetric::Gauge.new({ + :tick => 10, + :key_prefix => "fnordmetric-myns", + :key => "mygauge_966", + :unique => true, + :average => true, + :redis => @redis + }) + _gauge.value_at(@now).should == (76.0/23.0) + end + it "should receive gauge values for multiple ticks" do @gauge.values_at([@now, @now+8]).should == { 695280200 => "54", 695280210 => "123" } end it "should receive gauge values per session for multiple ticks" do @redis.set(@gauge_key+"-695280200-sessions-count", "23") @redis.set(@gauge_key+"-695280210-sessions-count", "8") - @gauge.values_at([@now, @now+8], :avg_per_session => 1).should == { + @redis.hset(@gauge_key, "695280200", "76") + @redis.hset(@gauge_key, "695280210", "56") + _gauge = FnordMetric::Gauge.new({ + :tick => 10, + :key_prefix => "fnordmetric-myns", + :key => "mygauge_966", + :unique => true, + :redis => @redis + }) + _gauge.values_at([@now, @now+8]).should == { + 695280200 => "76", + 695280210 => "56" + } + end + + it "should receive gauge values per session for multiple ticks with avg" do + @redis.set(@gauge_key+"-695280200-sessions-count", "23") + @redis.set(@gauge_key+"-695280210-sessions-count", "8") + _gauge = FnordMetric::Gauge.new({ + :tick => 10, + :key_prefix => "fnordmetric-myns", + :key => "mygauge_966", + :unique => true, + :average => true, + :redis => @redis + }) + _gauge.values_at([@now, @now+8]).should == { 695280200 => (54.0/23.0), 695280210 => (123.0/8.0) } end @@ -109,20 +173,87 @@ 695280210 => 153 } end it "should receive gauge values for all ticks in a given range" do - @gauge.values_in(@now..@now+8).should == { + @gauge.values_in(@now+10..@now+18).should == { 695280200 => "54", 695280210 => "123" } - @gauge.values_in(@now..@now+6).should == { - 695280200 => "54" - } - @gauge.values_in(@now+8..@now+10).should == { - 695280210 => "123" - } end end + describe "three-dim value retrival" do + + before(:each) do + @gauge = FnordMetric::Gauge.new({ + :tick => 10, + :key_prefix => "fnordmetric-myns", + :three_dimensional => true, + :key => "mygauge_966", + :redis => @redis + }) + @redis.keys("fnordmetric-myns*").each { |k| @redis.del(k) } + @gauge_key = "fnordmetric-myns-gauge-mygauge_966-10-1323691200" + @redis.zadd(@gauge_key, 18, "fnordyblubb") + @redis.zadd(@gauge_key, 23, "uberfoo") + @redis.set(@gauge_key+"-count", 41) + end + + it "should retrieve field_values at a given time" do + @gauge.field_values_at(1323691200).should be_a(Array) + @gauge.field_values_at(1323691200).length.should == 2 + @gauge.field_values_at(1323691200)[0].should == ["uberfoo", "23"] + @gauge.field_values_at(1323691200)[1].should == ["fnordyblubb", "18"] + end + + it "should retrieve the correct total count" do + @gauge.field_values_total(1323691200).should == 41 + end + + it "should retrieve max 50 fields per default" do + 70.times{ |n| @redis.zadd(@gauge_key, 23, "field#{n}") } + @gauge.field_values_at(1323691200).should be_a(Array) + @gauge.field_values_at(1323691200).length.should == 50 + end + + it "should retrieve more than 50 fields if requested" do + 70.times{ |n| @redis.zadd(@gauge_key, 23, "field#{n}") } + @gauge.field_values_at(1323691200, :max_fields => 60).should be_a(Array) + @gauge.field_values_at(1323691200, :max_fields => 60).length.should == 60 + end + + it "should retrieve all fields if requested" do + 70.times{ |n| @redis.zadd(@gauge_key, 23, "field#{n}") } + @gauge.field_values_at(1323691200, :max_fields => 0).should be_a(Array) + @gauge.field_values_at(1323691200, :max_fields => 0).length.should == 72 + end + + it "should call the value calculation block and return the result" do + vals = @gauge.field_values_at(1323691200){ |v| v.to_i + 123 } + vals.should be_a(Array) + vals.length.should == 2 + vals[0].should == ["uberfoo", 146] + vals[1].should == ["fnordyblubb", 141] + end + + it "should return the correct field_values per session with avg" do + @redis.set(@gauge_key+"-sessions-count", "3") + @gauge = FnordMetric::Gauge.new({ + :tick => 10, + :key_prefix => "fnordmetric-myns", + :three_dimensional => true, + :unique => true, + :average => true, + :key => "mygauge_966", + :redis => @redis + }) + vals = @gauge.field_values_at(1323691200) + vals.should be_a(Array) + vals.length.should == 2 + vals[0].should == ["uberfoo", 23/3.0] + vals[1].should == ["fnordyblubb", 18/3.0] + end + + end end \ No newline at end of file