diff --git a/common/perf-test-core/src/main/java/com/azure/perf/test/core/NumberFormatter.java b/common/perf-test-core/src/main/java/com/azure/perf/test/core/NumberFormatter.java new file mode 100644 index 000000000000..0543f050bcb8 --- /dev/null +++ b/common/perf-test-core/src/main/java/com/azure/perf/test/core/NumberFormatter.java @@ -0,0 +1,38 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.azure.perf.test.core; + +import java.lang.Math; + +public final class NumberFormatter { + private NumberFormatter() { } + + // Formats a number with a minimum number of significant digits. + // Digits to the left of the decimal point are always significant. + // Examples: + // - Format(0, 4) -> "0.000" + // - Format(12345, 4) -> "12,345" + // - Format(1.2345, 4) -> "1.235" + // - Format(0.00012345, 4) -> "0.0001235" + public static String Format(double value, int minSignificantDigits) { + if (minSignificantDigits < 0) { + throw new IllegalArgumentException("minSignificantDigits must be greater than zero"); + } + + // Special case since log(0) is undefined + if (value == 0) { + return String.format("%." + (minSignificantDigits - 1) + "f", value); + } + + double log = Math.log10(Math.abs(value)); + int significantDigits = (int)Math.max(Math.ceil(log), minSignificantDigits); + + double divisor = Math.pow(10, Math.ceil(log) - significantDigits); + double rounded = divisor * Math.round(value / divisor); + + int decimals = (int)Math.max(0, significantDigits - Math.floor(log) - 1); + + return String.format("%,." + decimals + "f", rounded); + } +} diff --git a/common/perf-test-core/src/main/java/com/azure/perf/test/core/PerfStressProgram.java b/common/perf-test-core/src/main/java/com/azure/perf/test/core/PerfStressProgram.java index 5fc590b4d9e3..8ca329511da8 100644 --- a/common/perf-test-core/src/main/java/com/azure/perf/test/core/PerfStressProgram.java +++ b/common/perf-test-core/src/main/java/com/azure/perf/test/core/PerfStressProgram.java @@ -278,8 +278,11 @@ public static void runTests(PerfTestBase[] tests, boolean sync, int parallel, double secondsPerOperation = 1 / operationsPerSecond; double weightedAverageSeconds = totalOperations / operationsPerSecond; - System.out.printf("Completed %,d operations in a weighted-average of %,.2fs (%,.2f ops/s, %,.3f s/op)%n", - totalOperations, weightedAverageSeconds, operationsPerSecond, secondsPerOperation); + System.out.printf("Completed %,d operations in a weighted-average of %ss (%s ops/s, %s s/op)%n", + totalOperations, + NumberFormatter.Format(weightedAverageSeconds, 4), + NumberFormatter.Format(operationsPerSecond, 4), + NumberFormatter.Format(secondsPerOperation, 4)); System.out.println(); }