This is an automated email from the ASF dual-hosted git repository.
gnodet pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven.git
The following commit(s) were added to refs/heads/master by this push:
new 6b3dfaee5e Fix transfer rates logged in batch mode (#13845)
6b3dfaee5e is described below
commit 6b3dfaee5e3e4cff70fd7cd04aaeddad2075ebcc
Author: Sharang Gupta <[email protected]>
AuthorDate: Thu Nov 5 11:49:03 2026 +0000
Fix transfer rates logged in batch mode (#13845)
* Fix transfer rates logged in batch mode
Slf4jMavenTransferListener divided by Duration.toSeconds(), which is 0 for
any transfer shorter than a second, so batch builds logged rates such as
9223372037 GB/s. Share the fractional-seconds calculation that #1985 put
in the console listener, and print the rate with FileSizeFormat.formatRate.
* Format transfer rates independently of the locale
formatRate used the default locale, so under de_DE a rate read 31,6 kB/s
next to a size of 7.9 kB.
* Make the transfer rate test independent of timing
Creating the Logger mock after the transfer start counted Mockito's class
loading as transfer time, so the rate's lower bound depended on the
machine. bytesPerSecond now also takes the end instant, which lets the
calculation be tested exactly, and the listener test only bounds the
rate from above.
---
.../transfer/AbstractMavenTransferListener.java | 41 ++++++++++--
.../maven/cling/transfer/FileSizeFormat.java | 16 +++--
.../cling/transfer/Slf4jMavenTransferListener.java | 14 ++---
.../AbstractMavenTransferListenerTest.java | 46 ++++++++++++++
.../maven/cling/transfer/FileSizeFormatTest.java | 30 +++++++++
.../transfer/Slf4jMavenTransferListenerTest.java | 73 ++++++++++++++++++++++
6 files changed, 201 insertions(+), 19 deletions(-)
diff --git
a/impl/maven-cli/src/main/java/org/apache/maven/cling/transfer/AbstractMavenTransferListener.java
b/impl/maven-cli/src/main/java/org/apache/maven/cling/transfer/AbstractMavenTransferListener.java
index bf6971c18f..b2ebf4e70e 100644
---
a/impl/maven-cli/src/main/java/org/apache/maven/cling/transfer/AbstractMavenTransferListener.java
+++
b/impl/maven-cli/src/main/java/org/apache/maven/cling/transfer/AbstractMavenTransferListener.java
@@ -20,6 +20,8 @@
import java.io.PrintWriter;
import java.time.Duration;
+import java.time.Instant;
+import java.util.OptionalDouble;
import java.util.concurrent.TimeUnit;
import org.apache.maven.api.MonotonicClock;
@@ -44,6 +46,36 @@ protected
AbstractMavenTransferListener(MessageBuilderFactory messageBuilderFact
this.out = out;
}
+ /**
+ * Returns the average rate of a transfer that started at {@code start}
and is still running.
+ *
+ * @param start when the transfer started
+ * @param transferredBytes the number of bytes transferred so far
+ * @return the rate in bytes per second, or empty if no time has elapsed
+ * @see #bytesPerSecond(Instant, Instant, long)
+ */
+ static OptionalDouble bytesPerSecond(Instant start, long transferredBytes)
{
+ return bytesPerSecond(start, MonotonicClock.now(), transferredBytes);
+ }
+
+ /**
+ * Returns the average rate of a transfer, measured in fractional seconds
so that transfers shorter than a second
+ * get a rate too.
+ *
+ * @param start when the transfer started
+ * @param end when the rate is measured
+ * @param transferredBytes the number of bytes transferred by {@code end}
+ * @return the rate in bytes per second, or empty if no time has elapsed
+ */
+ static OptionalDouble bytesPerSecond(Instant start, Instant end, long
transferredBytes) {
+ long nanos = Duration.between(start, end).toNanos();
+ if (nanos <= 0) {
+ return OptionalDouble.empty();
+ }
+ double seconds = nanos / (double) TimeUnit.SECONDS.toNanos(1);
+ return OptionalDouble.of(transferredBytes / seconds);
+ }
+
/**
* Escapes control characters so that transfer messages render literally
on a terminal.
* Tab and newline are preserved; every other C0 control, DEL and the C1
controls are escaped.
@@ -112,13 +144,10 @@ public void transferSucceeded(TransferEvent event) {
message.resetStyle().append(sanitize(resource.getResourceName()));
message.style(STYLE).append(" (").append(format.format(contentLength));
- Duration duration = Duration.between(resource.getStartTime(),
MonotonicClock.now());
- long nanos = duration.toNanos();
- if (nanos > 0) {
- double seconds = nanos / (double) TimeUnit.SECONDS.toNanos(1); //
Convert to fractional seconds
- double bytesPerSecond = contentLength / seconds;
+ OptionalDouble bytesPerSecond =
bytesPerSecond(resource.getStartTime(), contentLength);
+ if (bytesPerSecond.isPresent()) {
message.append(" at ");
- format.formatRate(message, bytesPerSecond);
+ format.formatRate(message, bytesPerSecond.getAsDouble());
}
message.append(')').resetStyle();
diff --git
a/impl/maven-cli/src/main/java/org/apache/maven/cling/transfer/FileSizeFormat.java
b/impl/maven-cli/src/main/java/org/apache/maven/cling/transfer/FileSizeFormat.java
index 4721750a83..3ee602f264 100644
---
a/impl/maven-cli/src/main/java/org/apache/maven/cling/transfer/FileSizeFormat.java
+++
b/impl/maven-cli/src/main/java/org/apache/maven/cling/transfer/FileSizeFormat.java
@@ -18,6 +18,8 @@
*/
package org.apache.maven.cling.transfer;
+import java.util.Locale;
+
import org.apache.maven.api.services.MessageBuilder;
/**
@@ -151,17 +153,23 @@ public void format(MessageBuilder builder, long size,
ScaleUnit unit) {
format(builder, size, unit, false);
}
+ void formatRate(StringBuilder builder, double rate) {
+ builder.append(formatRate(rate));
+ }
+
public void formatRate(MessageBuilder builder, double rate) {
+ builder.append(formatRate(rate));
+ }
+
+ private String formatRate(double rate) {
// Handle invalid rates gracefully (including negative values)
if (Double.isNaN(rate) || Double.isInfinite(rate) || rate < 0) {
- builder.append("? B/s");
- return;
+ return "? B/s";
}
ScaleUnit unit = ScaleUnit.getScaleUnit(Math.round(rate));
double scaledRate = rate / unit.bytes();
- builder.append(String.format("%.1f", scaledRate));
- builder.append(" ").append(unit.symbol()).append("/s");
+ return String.format(Locale.ROOT, "%.1f", scaledRate) + " " +
unit.symbol() + "/s";
}
private void format(MessageBuilder builder, long size, ScaleUnit unit,
boolean omitSymbol) {
diff --git
a/impl/maven-cli/src/main/java/org/apache/maven/cling/transfer/Slf4jMavenTransferListener.java
b/impl/maven-cli/src/main/java/org/apache/maven/cling/transfer/Slf4jMavenTransferListener.java
index 32732dd6a4..5049d6e729 100644
---
a/impl/maven-cli/src/main/java/org/apache/maven/cling/transfer/Slf4jMavenTransferListener.java
+++
b/impl/maven-cli/src/main/java/org/apache/maven/cling/transfer/Slf4jMavenTransferListener.java
@@ -18,10 +18,8 @@
*/
package org.apache.maven.cling.transfer;
-import java.time.Duration;
-import java.time.Instant;
+import java.util.OptionalDouble;
-import org.apache.maven.api.MonotonicClock;
import org.eclipse.aether.transfer.AbstractTransferListener;
import org.eclipse.aether.transfer.TransferCancelledException;
import org.eclipse.aether.transfer.TransferEvent;
@@ -96,13 +94,11 @@ public void transferSucceeded(TransferEvent event) {
.append(" (");
format.format(message, contentLength);
- Duration duration =
-
Duration.between(Instant.ofEpochMilli(resource.getTransferStartTime()),
MonotonicClock.now());
- if ((duration.getSeconds() | duration.getNano()) > 0) { //
duration.isPositive()
- long bytesPerSecond = Math.round(contentLength / (double)
duration.toSeconds());
+ OptionalDouble bytesPerSecond =
+
AbstractMavenTransferListener.bytesPerSecond(resource.getStartTime(),
contentLength);
+ if (bytesPerSecond.isPresent()) {
message.append(" at ");
- format.format(message, bytesPerSecond);
- message.append("/s");
+ format.formatRate(message, bytesPerSecond.getAsDouble());
}
message.append(')');
diff --git
a/impl/maven-cli/src/test/java/org/apache/maven/cling/transfer/AbstractMavenTransferListenerTest.java
b/impl/maven-cli/src/test/java/org/apache/maven/cling/transfer/AbstractMavenTransferListenerTest.java
new file mode 100644
index 0000000000..c7d88a1ec9
--- /dev/null
+++
b/impl/maven-cli/src/test/java/org/apache/maven/cling/transfer/AbstractMavenTransferListenerTest.java
@@ -0,0 +1,46 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.maven.cling.transfer;
+
+import java.time.Instant;
+import java.util.OptionalDouble;
+
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+class AbstractMavenTransferListenerTest {
+
+ private static final Instant START = Instant.parse("2026-01-01T00:00:00Z");
+
+ @Test
+ void bytesPerSecondUsesFractionalSeconds() {
+ assertEquals(
+ OptionalDouble.of(31_600),
+ AbstractMavenTransferListener.bytesPerSecond(START,
START.plusMillis(250), 7_900));
+ }
+
+ @Test
+ void bytesPerSecondIsEmptyWhenNoTimeHasElapsed() {
+ assertEquals(OptionalDouble.empty(),
AbstractMavenTransferListener.bytesPerSecond(START, START, 7_900));
+ assertEquals(
+ OptionalDouble.empty(),
+ AbstractMavenTransferListener.bytesPerSecond(START,
START.minusMillis(1), 7_900));
+ }
+}
diff --git
a/impl/maven-cli/src/test/java/org/apache/maven/cling/transfer/FileSizeFormatTest.java
b/impl/maven-cli/src/test/java/org/apache/maven/cling/transfer/FileSizeFormatTest.java
index 217b1ac99f..da4fa9069d 100644
---
a/impl/maven-cli/src/test/java/org/apache/maven/cling/transfer/FileSizeFormatTest.java
+++
b/impl/maven-cli/src/test/java/org/apache/maven/cling/transfer/FileSizeFormatTest.java
@@ -27,6 +27,8 @@
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.parallel.ResourceLock;
+import org.junit.jupiter.api.parallel.Resources;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
@@ -375,4 +377,32 @@ void testFormatRateInvalidValues() {
format.formatRate(builder, Double.NEGATIVE_INFINITY);
assertEquals("? B/s", builder.build());
}
+
+ @Test
+ void testFormatRateToStringBuilder() {
+ FileSizeFormat format = new FileSizeFormat();
+
+ StringBuilder builder = new StringBuilder();
+ format.formatRate(builder, 5500.0);
+ assertEquals("5.5 kB/s", builder.toString());
+
+ builder = new StringBuilder();
+ format.formatRate(builder, Double.POSITIVE_INFINITY);
+ assertEquals("? B/s", builder.toString());
+ }
+
+ @Test
+ @ResourceLock(Resources.LOCALE)
+ void testFormatRateIgnoresDefaultLocale() {
+ FileSizeFormat format = new FileSizeFormat();
+ Locale previous = Locale.getDefault();
+ Locale.setDefault(Locale.GERMANY);
+ try {
+ MessageBuilder builder = new DefaultMessageBuilder();
+ format.formatRate(builder, 5500.0);
+ assertEquals("5.5 kB/s", builder.build());
+ } finally {
+ Locale.setDefault(previous);
+ }
+ }
}
diff --git
a/impl/maven-cli/src/test/java/org/apache/maven/cling/transfer/Slf4jMavenTransferListenerTest.java
b/impl/maven-cli/src/test/java/org/apache/maven/cling/transfer/Slf4jMavenTransferListenerTest.java
new file mode 100644
index 0000000000..f87970c813
--- /dev/null
+++
b/impl/maven-cli/src/test/java/org/apache/maven/cling/transfer/Slf4jMavenTransferListenerTest.java
@@ -0,0 +1,73 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.maven.cling.transfer;
+
+import java.io.File;
+import java.time.Clock;
+import java.time.ZoneOffset;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import org.apache.maven.api.MonotonicClock;
+import org.eclipse.aether.DefaultRepositorySystemSession;
+import org.eclipse.aether.transfer.TransferEvent;
+import org.eclipse.aether.transfer.TransferResource;
+import org.junit.jupiter.api.Test;
+import org.mockito.ArgumentCaptor;
+import org.slf4j.Logger;
+
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+
+class Slf4jMavenTransferListenerTest {
+
+ @Test
+ void subSecondTransferReportsItsRate() {
+ // Create the mock first: loading Mockito must not count towards the
transfer time.
+ Logger logger = mock(Logger.class);
+ TransferResource resource = resourceStartedMillisAgo(250);
+ TransferEvent event = new TransferEvent.Builder(new
DefaultRepositorySystemSession(h -> false), resource)
+ .setType(TransferEvent.EventType.SUCCEEDED)
+ .setTransferredBytes(7_900)
+ .build();
+
+ new Slf4jMavenTransferListener(logger).transferSucceeded(event);
+
+ ArgumentCaptor<String> message = ArgumentCaptor.forClass(String.class);
+ verify(logger).info(message.capture());
+ Matcher rate =
+ Pattern.compile("\\(7\\.9 kB at (\\d+\\.\\d)
(B|kB)/s\\)$").matcher(message.getValue());
+ assertTrue(rate.find(), message.getValue());
+ // The transfer took at least 250 ms, so 7.9 kB cannot have moved
faster than 31.6 kB/s.
+ double bytesPerSecond = Double.parseDouble(rate.group(1)) *
("kB".equals(rate.group(2)) ? 1000 : 1);
+ assertTrue(bytesPerSecond <= 31_600, message.getValue());
+ }
+
+ private static TransferResource resourceStartedMillisAgo(long millis) {
+ Clock previous = TransferResource.getClock();
+
TransferResource.setClock(Clock.fixed(MonotonicClock.now().minusMillis(millis),
ZoneOffset.UTC));
+ try {
+ return new TransferResource(
+ "central", "https://repo.maven.apache.org/maven2/",
"a/b/1/b-1.pom", new File(""), null);
+ } finally {
+ TransferResource.setClock(previous);
+ }
+ }
+}