package com.emc.mongoose.base.metrics.type;

import static org.junit.Assert.assertEquals;

import com.emc.mongoose.base.metrics.snapshot.RateMetricSnapshot;
import java.time.Clock;
import java.util.concurrent.TimeUnit;
import org.junit.Test;

/**
 * @author veronika K. on 16.10.18
 */
public class RateMeterTest {

	private static final int SLEEP_MILLISEC = 1000;
	private static final int COUNT_BYTES_1 = 1234;
	private static final int COUNT_BYTES_2 = 567;

	@Test
	public void test() throws InterruptedException {
		final RateMeter<RateMetricSnapshot> meter = new RateMeterImpl(Clock.systemUTC(),
			"SOME_RATE");
		final long t0 = System.currentTimeMillis();
		meter.update(COUNT_BYTES_1);
		TimeUnit.MILLISECONDS.sleep(SLEEP_MILLISEC);
		final long t1 = System.currentTimeMillis();
		final RateMetricSnapshot snapshot1 = meter.snapshot();
		assertEquals(1000.0 * COUNT_BYTES_1 / (t1 - t0), snapshot1.mean(), 2.0);
		assertEquals(1000.0 * COUNT_BYTES_1 / (t1 - t0), snapshot1.last(), 2.0);
		meter.update(COUNT_BYTES_2);
		TimeUnit.MILLISECONDS.sleep(SLEEP_MILLISEC);
		final long t2 = System.currentTimeMillis();
		final RateMetricSnapshot snapshot2 = meter.snapshot();
		assertEquals(1000.0 * (COUNT_BYTES_1 + COUNT_BYTES_2) / (t2 - t0), snapshot2.mean(),
			snapshot2.mean() * 0.05);
		assertEquals(1000.0 * (COUNT_BYTES_1 + COUNT_BYTES_2) / (t2 - t0), snapshot2.last(), 100.0);
	}
}

@@ -239,19 +239,16 @@
 st;%0A%0A/**
-%0A *
  @author
@@ -271,17 +271,16 @@
 16.10.18
-%0A
  */%0Apubl
@@ -305,17 +305,18 @@
 Test %7B%0A%0A
-%09
+  
 private 
@@ -355,17 +355,18 @@
 = 1000;%0A
-%09
+  
 private 
@@ -404,17 +404,18 @@
 = 1234;%0A
-%09
+  
 private 
@@ -457,16 +457,18 @@
 7;%0A%0A
-%09
+  
 @Test%0A
-%09
+  
 publ
@@ -512,18 +512,20 @@
 ption %7B%0A
-%09%09
+    
 final Ra
@@ -596,20 +596,17 @@
 emUTC(),
-%0A%09%09%09
+ 
 %22SOME_RA
@@ -611,18 +611,20 @@
 RATE%22);%0A
-%09%09
+    
 final lo
@@ -659,18 +659,20 @@
 llis();%0A
-%09%09
+    
 meter.up
@@ -692,18 +692,20 @@
 TES_1);%0A
-%09%09
+    
 TimeUnit
@@ -733,34 +733,36 @@
 LEEP_MILLISEC);%0A
-%09%09
+    
 final long t1 = 
@@ -781,34 +781,36 @@
 ntTimeMillis();%0A
-%09%09
+    
 final RateMetric
@@ -840,34 +840,36 @@
 ter.snapshot();%0A
-%09%09
+    
 assertEquals(100
@@ -925,18 +925,20 @@
 , 2.0);%0A
-%09%09
+    
 assertEq
@@ -1002,18 +1002,20 @@
 , 2.0);%0A
-%09%09
+    
 meter.up
@@ -1035,18 +1035,20 @@
 TES_2);%0A
-%09%09
+    
 TimeUnit
@@ -1084,18 +1084,20 @@
 LISEC);%0A
-%09%09
+    
 final lo
@@ -1132,18 +1132,20 @@
 llis();%0A
-%09%09
+    
 final Ra
@@ -1191,18 +1191,20 @@
 shot();%0A
-%09%09
+    
 assertEq
@@ -1200,32 +1200,41 @@
    assertEquals(
+%0A        
 1000.0 * (COUNT_
@@ -1262,32 +1262,40 @@
 _2) / (t2 - t0),
+%0A       
  snapshot2.mean(
@@ -1301,11 +1301,16 @@
 (),%0A
-%09%09%09
+        
 snap
@@ -1331,14 +1331,16 @@
  0.0
-5);%0A%09%09
+1);%0A    
 asse
@@ -1432,9 +1432,10 @@
 0);%0A
-%09
+  
 %7D%0A%7D%0A

save [skip ci]