/** * Get bytes per-second and only if duration is greater than or equal to 1 second will flush and * re-calculate speed. */ public synchronized long getBytesPerSecondAndFlush() { final long interval = nowMillis() - timestamp; if (interval < 1000 && bytesPerSecond != 0) return bytesPerSecond; // the first time we using 500 milliseconds to let speed valid more quick if (bytesPerSecond == 0 && interval < 500) return 0; return getInstantBytesPerSecondAndFlush(); }
/** * With wikipedia: https://en.wikipedia.org/wiki/Kibibyte * <p> * 1KiB = 2^10B = 1024B * 1MiB = 2^10KB = 1024KB */ public String getSpeedWithBinaryAndFlush() { return humanReadableSpeed(getInstantBytesPerSecondAndFlush(), false); }
/** * With wikipedia: https://en.wikipedia.org/wiki/Kilobyte * <p> * 1KB = 1000B * 1MB = 1000KB */ public String getSpeedWithSIAndFlush() { return humanReadableSpeed(getInstantBytesPerSecondAndFlush(), true); }
@Test public void getSpeedWithSIAndFlush() { doReturn(1054L).when(calculator).getInstantBytesPerSecondAndFlush(); assertThat(calculator.getSpeedWithSIAndFlush()).isEqualTo("1.1 kB/s"); }
@Test public void getSpeedWithBinaryAndFlush() { doReturn(1054L).when(calculator).getInstantBytesPerSecondAndFlush(); assertThat(calculator.getSpeedWithBinaryAndFlush()).isEqualTo("1.0 KiB/s"); }
assertThat(calculator.getInstantBytesPerSecondAndFlush()).isEqualTo(30000); assertThat(calculator.getInstantBytesPerSecondAndFlush()).isEqualTo(3000); assertThat(calculator.timestamp).isEqualTo(now); assertThat(calculator.beginTimestamp).isEqualTo(firstTimestamp); assertThat(calculator.getInstantBytesPerSecondAndFlush()).isEqualTo(1000);