Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MNG-8436] Fix wrong transfer rates displayed in console #1985

Merged
merged 1 commit into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import java.io.PrintWriter;
import java.time.Duration;
import java.util.concurrent.TimeUnit;

import org.apache.maven.api.MonotonicClock;
import org.apache.maven.api.services.MessageBuilder;
Expand Down Expand Up @@ -83,8 +84,10 @@ public void transferSucceeded(TransferEvent event) {
message.style(STYLE).append(" (").append(format.format(contentLength));

Duration duration = Duration.between(resource.getStartTime(), MonotonicClock.now());
if ((duration.getSeconds() | duration.getNano()) > 0) { // duration.isPositive()
double bytesPerSecond = contentLength / (double) duration.toSeconds();
long nanos = duration.toNanos();
if (nanos > 0) {
double seconds = nanos / (double) TimeUnit.SECONDS.toNanos(1); // Convert to fractional seconds
double bytesPerSecond = contentLength / seconds;
message.append(" at ");
format.formatRate(message, bytesPerSecond);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,19 +153,16 @@ public void format(MessageBuilder builder, long size, ScaleUnit unit) {
}

public void formatRate(MessageBuilder builder, double rate) {
// Handle invalid rates gracefully (including negative values)
if (Double.isNaN(rate) || Double.isInfinite(rate) || rate < 0) {
builder.append("? B/s");
return;
}

ScaleUnit unit = ScaleUnit.getScaleUnit(Math.round(rate));
double scaledRate = rate / unit.bytes();
if (unit == ScaleUnit.BYTE || scaledRate < 0.05d || scaledRate >= 10.0d) {
builder.append(Long.toString(Math.round(scaledRate)));
} else {
builder.append(Double.toString(Math.round(scaledRate * 10d) / 10d));
}
if (unit == ScaleUnit.BYTE) {
builder.append(" B");
} else {
builder.append(" ").append(unit.symbol());
}
builder.append("/s");
builder.append(String.format("%.1f", scaledRate));
builder.append(" ").append(unit.symbol()).append("/s");
}

private void format(MessageBuilder builder, long size, ScaleUnit unit, boolean omitSymbol) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ void testFormatRate() {
// Test bytes per second
MessageBuilder builder = new DefaultMessageBuilder();
format.formatRate(builder, 5.0);
assertEquals("5 B/s", builder.build());
assertEquals("5.0 B/s", builder.build());

// Test kilobytes per second
builder = new DefaultMessageBuilder();
Expand All @@ -319,19 +319,33 @@ void testFormatRateThresholds() {
FileSizeFormat format = new FileSizeFormat();

// Test value less than 0.05
// Test exact unit thresholds
MessageBuilder builder = new DefaultMessageBuilder();
format.formatRate(builder, 45.0); // 45 B/s
assertEquals("45 B/s", builder.build());
assertEquals("45.0 B/s", builder.build());

// Test value greater than or equal to 10
builder = new DefaultMessageBuilder();
format.formatRate(builder, 15000.0); // 15 kB/s
assertEquals("15 kB/s", builder.build());
assertEquals("15.0 kB/s", builder.build());

// Test value between 0.05 and 10
builder = new DefaultMessageBuilder();
format.formatRate(builder, 5500.0); // 5.5 kB/s
assertEquals("5.5 kB/s", builder.build());

// Test exact unit thresholds
builder = new DefaultMessageBuilder();
format.formatRate(builder, 1000.0); // 1 kB/s
assertEquals("1.0 kB/s", builder.build());

builder = new DefaultMessageBuilder();
format.formatRate(builder, 1000000.0); // 1 MB/s
assertEquals("1.0 MB/s", builder.build());

builder = new DefaultMessageBuilder();
format.formatRate(builder, 1000000000.0); // 1 GB/s
assertEquals("1.0 GB/s", builder.build());
}

@Test
Expand All @@ -341,7 +355,7 @@ void testFormatRateEdgeCases() {
// Test zero rate
MessageBuilder builder = new DefaultMessageBuilder();
format.formatRate(builder, 0.0);
assertEquals("0 B/s", builder.build());
assertEquals("0.0 B/s", builder.build());

// Test rate at exactly 1000 (1 kB/s)
builder = new DefaultMessageBuilder();
Expand All @@ -353,4 +367,43 @@ void testFormatRateEdgeCases() {
format.formatRate(builder, 1000000.0);
assertEquals("1.0 MB/s", builder.build());
}

@Test
void testFormatRateLargeValues() {
FileSizeFormat format = new FileSizeFormat();

// Test large but valid rates
MessageBuilder builder = new DefaultMessageBuilder();
format.formatRate(builder, 5e12); // 5 TB/s
assertEquals("5000.0 GB/s", builder.build());

// Test very large rate
builder = new DefaultMessageBuilder();
format.formatRate(builder, 1e15); // 1 PB/s
assertEquals("1000000.0 GB/s", builder.build());
}

@Test
void testFormatRateInvalidValues() {
FileSizeFormat format = new FileSizeFormat();

// Test negative rate
MessageBuilder builder = new DefaultMessageBuilder();
format.formatRate(builder, -1.0);
assertEquals("? B/s", builder.build());

// Test NaN
builder = new DefaultMessageBuilder();
format.formatRate(builder, Double.NaN);
assertEquals("? B/s", builder.build());

// Test Infinity
builder = new DefaultMessageBuilder();
format.formatRate(builder, Double.POSITIVE_INFINITY);
assertEquals("? B/s", builder.build());

builder = new DefaultMessageBuilder();
format.formatRate(builder, Double.NEGATIVE_INFINITY);
assertEquals("? B/s", builder.build());
}
}
Loading