diff --git a/src/main/java/org/gradle/profiler/GradleBuildInvocationResult.java b/src/main/java/org/gradle/profiler/GradleBuildInvocationResult.java index 1cf8f8f3..e34f6502 100644 --- a/src/main/java/org/gradle/profiler/GradleBuildInvocationResult.java +++ b/src/main/java/org/gradle/profiler/GradleBuildInvocationResult.java @@ -27,7 +27,7 @@ protected Duration getDuration(GradleBuildInvocationResult result) { } }; - public static Sample sampleBuildOperation(String buildOperationDetailsClass) { + public static Sample sampleBuildOperationDuration(String buildOperationDetailsClass) { return new TimeSample() { @Override public String getName() { diff --git a/src/main/java/org/gradle/profiler/GradleScenarioInvoker.java b/src/main/java/org/gradle/profiler/GradleScenarioInvoker.java index d5063c12..7b118261 100644 --- a/src/main/java/org/gradle/profiler/GradleScenarioInvoker.java +++ b/src/main/java/org/gradle/profiler/GradleScenarioInvoker.java @@ -37,7 +37,7 @@ public List> samplesFor(InvocationSe builder.add(GradleBuildInvocationResult.TIME_TO_TASK_EXECUTION); } scenario.getMeasuredBuildOperations().stream() - .map(GradleBuildInvocationResult::sampleBuildOperation) + .map(GradleBuildInvocationResult::sampleBuildOperationDuration) .forEach(builder::add); return builder.build(); } diff --git a/src/main/java/org/gradle/profiler/buildops/BuildOperationInstrumentation.java b/src/main/java/org/gradle/profiler/buildops/BuildOperationInstrumentation.java index 5079028a..8d1d1542 100644 --- a/src/main/java/org/gradle/profiler/buildops/BuildOperationInstrumentation.java +++ b/src/main/java/org/gradle/profiler/buildops/BuildOperationInstrumentation.java @@ -90,7 +90,7 @@ public Map getCumulativeBuildOperationTimes() { private static Duration readCumulativeTimeFromDataFile(File dataFile) { try { try (Stream lines = Files.lines(dataFile.toPath(), StandardCharsets.UTF_8)) { - return Duration.ofMillis(lines.mapToLong(Long::parseLong).sum()); + return Duration.ofNanos((long) lines.mapToDouble(Double::parseDouble).sum() * 1000000); } } catch (IOException e) { throw new RuntimeException("Could not read result from file.", e); diff --git a/subprojects/build-operations/src/main/java/org/gradle/trace/buildops/BuildOperationTrace.java b/subprojects/build-operations/src/main/java/org/gradle/trace/buildops/BuildOperationTrace.java index fccd6765..267e6aa4 100644 --- a/subprojects/build-operations/src/main/java/org/gradle/trace/buildops/BuildOperationTrace.java +++ b/subprojects/build-operations/src/main/java/org/gradle/trace/buildops/BuildOperationTrace.java @@ -25,6 +25,7 @@ import java.util.List; import java.util.Map; import java.util.concurrent.atomic.AtomicLong; +import java.util.concurrent.atomic.AtomicReference; @SuppressWarnings("unused") public class BuildOperationTrace { @@ -95,7 +96,7 @@ public BuildOperationDurationRecordingListener() throws ClassNotFoundException { for (Map.Entry entry : getParameters().getCapturedBuildOperations().get().entrySet()) { String operationType = entry.getKey(); File outputFile = entry.getValue(); - collectors.add(new BuildOperationCollector(Class.forName(operationType + "$Details"), outputFile)); + collectors.add(new BuildOperationTimeCollector(Class.forName(operationType + "$Details"), outputFile)); } } @@ -126,24 +127,38 @@ public void close() throws IOException { } } - private static class BuildOperationCollector { - private final Class detailsType; + private abstract static class BuildOperationCollector { private final File outputFile; - private final AtomicLong buildOperationTime = new AtomicLong(0); + private final AtomicReference data = new AtomicReference<>(0d); - public BuildOperationCollector(Class detailsType, File outputFile) { - this.detailsType = detailsType; + public BuildOperationCollector(File outputFile) { this.outputFile = outputFile; } - public void collect(Object details, OperationFinishEvent operationFinishEvent) { - if (detailsType.isAssignableFrom(details.getClass())) { - buildOperationTime.addAndGet(operationFinishEvent.getEndTime() - operationFinishEvent.getStartTime()); - } + public abstract void collect(Object details, OperationFinishEvent operationFinishEvent); + + protected void accumulate(double delta) { + data.accumulateAndGet(delta, Double::sum); } public void write() throws IOException { - Files.write(outputFile.toPath(), String.valueOf(buildOperationTime.longValue()).getBytes(StandardCharsets.UTF_8)); + Files.write(outputFile.toPath(), String.valueOf(data.get().doubleValue()).getBytes(StandardCharsets.UTF_8)); + } + } + + private static class BuildOperationTimeCollector extends BuildOperationCollector { + private final Class detailsType; + + public BuildOperationTimeCollector(Class detailsType, File outputFile) { + super(outputFile); + this.detailsType = detailsType; + } + + @Override + public void collect(Object details, OperationFinishEvent operationFinishEvent) { + if (detailsType.isAssignableFrom(details.getClass())) { + accumulate(operationFinishEvent.getEndTime() - operationFinishEvent.getStartTime()); + } } } }