[close #515] Slowlog: Always calculate duration (#516)

Co-authored-by: Andy Lok <andylokandy@hotmail.com>
Co-authored-by: ti-srebot <66930949+ti-srebot@users.noreply.github.com>
This commit is contained in:
Peng Guanwen 2022-02-15 16:51:52 +08:00 committed by GitHub
parent 662d6f3bad
commit a0e35b50f9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 1 deletions

View File

@ -83,6 +83,7 @@ public class SlowLogImpl implements SlowLog {
@Override
public void log() {
recordTime();
if (error != null || timeExceeded()) {
SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss.SSS");
logger.warn(
@ -95,9 +96,12 @@ public class SlowLogImpl implements SlowLog {
}
}
boolean timeExceeded() {
private void recordTime() {
long currentNS = System.nanoTime();
durationMS = (currentNS - startNS) / 1_000_000;
}
boolean timeExceeded() {
return slowThresholdMS >= 0 && durationMS > slowThresholdMS;
}

View File

@ -28,10 +28,17 @@ public class SlowLogImplTest {
public void testThresholdTime() throws InterruptedException {
SlowLogImpl slowLog = new SlowLogImpl(1000);
Thread.sleep(1100);
slowLog.log();
Assert.assertTrue(slowLog.timeExceeded());
slowLog = new SlowLogImpl(1000);
Thread.sleep(500);
slowLog.log();
Assert.assertFalse(slowLog.timeExceeded());
slowLog = new SlowLogImpl(-1);
Thread.sleep(500);
slowLog.log();
Assert.assertFalse(slowLog.timeExceeded());
}