From a0d3680f494caa6275695fbd3397fd9b5805964d Mon Sep 17 00:00:00 2001 From: Alexa Panfil Date: Fri, 9 Oct 2020 16:52:04 +0000 Subject: [PATCH] Compute time differences with Duration Reason why this change is needed: Currently the durations of fetch events are computed by registering time instants with System.currentTimeMillis() and calculating the differences with simple minus operation, but multiple sources suggest that the best practice is to use the Java 8 Duration and Instant objects. What this patch does: Get time measurements with Instant.now() instead of System.currentTimeMillis() and calculate the duration of fetch events (Reachability checks and Negotiation) using Duration.between().toMillis(). Signed-off-by: Alexa Panfil Change-Id: I573a0a0562070083cf5a5a196d9710f69a7fa587 --- .../eclipse/jgit/transport/UploadPack.java | 21 +++++++++++-------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/transport/UploadPack.java b/org.eclipse.jgit/src/org/eclipse/jgit/transport/UploadPack.java index 26c7ab753..1242ef1b4 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/transport/UploadPack.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/transport/UploadPack.java @@ -42,6 +42,8 @@ import java.io.OutputStream; import java.io.UncheckedIOException; import java.text.MessageFormat; +import java.time.Duration; +import java.time.Instant; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; @@ -1008,7 +1010,7 @@ else if (requestValidator instanceof AnyRequestValidator) else advertised = refIdSet(getAdvertisedOrDefaultRefs().values()); - long negotiateStart = System.currentTimeMillis(); + Instant negotiateStart = Instant.now(); accumulator.advertised = advertised.size(); ProtocolV0Parser parser = new ProtocolV0Parser(transferConfig); @@ -1050,8 +1052,8 @@ else if (requestValidator instanceof AnyRequestValidator) if (!req.getClientShallowCommits().isEmpty()) walk.assumeShallow(req.getClientShallowCommits()); sendPack = negotiate(req, accumulator, pckOut); - accumulator.timeNegotiating += System.currentTimeMillis() - - negotiateStart; + accumulator.timeNegotiating = Duration + .between(negotiateStart, Instant.now()).toMillis(); if (sendPack && !biDirectionalPipe) { // Ensure the request was fully consumed. Any remaining input must @@ -1138,7 +1140,7 @@ private void fetchV2(PacketLineOut pckOut) throws IOException { } PackStatistics.Accumulator accumulator = new PackStatistics.Accumulator(); - long negotiateStart = System.currentTimeMillis(); + Instant negotiateStart = Instant.now(); ProtocolV2Parser parser = new ProtocolV2Parser(transferConfig); FetchV2Request req = parser.parseFetchRequest(pckIn); @@ -1244,8 +1246,8 @@ private void fetchV2(PacketLineOut pckOut) throws IOException { pckOut.writeString("packfile\n"); //$NON-NLS-1$ } - accumulator.timeNegotiating = System.currentTimeMillis() - - negotiateStart; + accumulator.timeNegotiating = Duration + .between(negotiateStart, Instant.now()).toMillis(); sendPack(accumulator, req, @@ -1795,12 +1797,13 @@ private void parseWants(PackStatistics.Accumulator accumulator) throws IOExcepti if (notAdvertisedWants != null) { accumulator.notAdvertisedWants = notAdvertisedWants.size(); - long startReachabilityChecking = System.currentTimeMillis(); + Instant startReachabilityChecking = Instant.now(); requestValidator.checkWants(this, notAdvertisedWants); - accumulator.reachabilityCheckDuration = System.currentTimeMillis() - - startReachabilityChecking; + accumulator.reachabilityCheckDuration = Duration + .between(startReachabilityChecking, Instant.now()) + .toMillis(); } AsyncRevObjectQueue q = walk.parseAny(wantIds, true);