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

fix(s3stream/wal/benchmark): append records at a fixed rate #631

Merged
merged 2 commits into from
Nov 14, 2023
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 @@ -460,9 +460,7 @@ public AppendResult append0(ByteBuf body, int crc) throws OverCapacityException
slidingWindowService.tryWriteBlock();

final AppendResult appendResult = new AppendResultImpl(expectedWriteOffset, appendResultFuture);
appendResult.future().whenComplete((nil, ex) -> {
OperationMetricsStats.getHistogram(S3Operation.APPEND_STORAGE_WAL).update(timerUtil.elapsed());
});
appendResult.future().whenComplete((nil, ex) -> OperationMetricsStats.getHistogram(S3Operation.APPEND_STORAGE_WAL).update(timerUtil.elapsed()));
return appendResult;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.io.IOException;
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.locks.Lock;
Expand Down Expand Up @@ -81,17 +82,19 @@
ExecutorService executor = Threads.newFixedThreadPool(
config.threads, ThreadUtils.createThreadFactory("append-thread-%d", false), null);
AppendTaskConfig appendTaskConfig = new AppendTaskConfig(config);
Stat stat = new Stat();

Check warning on line 85 in s3stream/src/main/java/com/automq/stream/s3/wal/benchmark/WriteBench.java

View check run for this annotation

Codecov / codecov/patch

s3stream/src/main/java/com/automq/stream/s3/wal/benchmark/WriteBench.java#L85

Added line #L85 was not covered by tests
for (int i = 0; i < config.threads; i++) {
int index = i;
executor.submit(() -> {
try {
runAppendTask(index, appendTaskConfig);
runAppendTask(index, appendTaskConfig, stat);

Check warning on line 90 in s3stream/src/main/java/com/automq/stream/s3/wal/benchmark/WriteBench.java

View check run for this annotation

Codecov / codecov/patch

s3stream/src/main/java/com/automq/stream/s3/wal/benchmark/WriteBench.java#L90

Added line #L90 was not covered by tests
} catch (Exception e) {
System.err.printf("Append task %d failed, %s\n", index, e.getMessage());
e.printStackTrace();
}
});
}
logIt(config, stat);

Check warning on line 97 in s3stream/src/main/java/com/automq/stream/s3/wal/benchmark/WriteBench.java

View check run for this annotation

Codecov / codecov/patch

s3stream/src/main/java/com/automq/stream/s3/wal/benchmark/WriteBench.java#L97

Added line #L97 was not covered by tests

executor.shutdown();
try {
Expand All @@ -105,26 +108,37 @@
System.out.println("Benchmark finished");
}

private void runAppendTask(int index, AppendTaskConfig config) throws Exception {
private static void logIt(Config config, Stat stat) {
ScheduledExecutorService statExecutor = Threads.newSingleThreadScheduledExecutor(
ThreadUtils.createThreadFactory("stat-thread-%d", true), null);
statExecutor.scheduleAtFixedRate(() -> {
Stat.Result result = stat.reset();

Check warning on line 115 in s3stream/src/main/java/com/automq/stream/s3/wal/benchmark/WriteBench.java

View check run for this annotation

Codecov / codecov/patch

s3stream/src/main/java/com/automq/stream/s3/wal/benchmark/WriteBench.java#L112-L115

Added lines #L112 - L115 were not covered by tests
if (0 != result.count()) {
System.out.printf("Append task | Append Rate %d msg/s %d KB/s | Avg Latency %.3f ms | Max Latency %.3f ms\n",
TimeUnit.SECONDS.toNanos(1) * result.count() / result.elapsedTimeNanos(),
TimeUnit.SECONDS.toNanos(1) * (result.count() * config.recordSizeBytes) / result.elapsedTimeNanos() / 1024,
(double) result.costNanos() / TimeUnit.MILLISECONDS.toNanos(1) / result.count(),
(double) result.maxCostNanos() / TimeUnit.MILLISECONDS.toNanos(1));

Check warning on line 121 in s3stream/src/main/java/com/automq/stream/s3/wal/benchmark/WriteBench.java

View check run for this annotation

Codecov / codecov/patch

s3stream/src/main/java/com/automq/stream/s3/wal/benchmark/WriteBench.java#L117-L121

Added lines #L117 - L121 were not covered by tests
}
}, LOG_INTERVAL_SECONDS, LOG_INTERVAL_SECONDS, TimeUnit.SECONDS);
}

Check warning on line 124 in s3stream/src/main/java/com/automq/stream/s3/wal/benchmark/WriteBench.java

View check run for this annotation

Codecov / codecov/patch

s3stream/src/main/java/com/automq/stream/s3/wal/benchmark/WriteBench.java#L123-L124

Added lines #L123 - L124 were not covered by tests

private void runAppendTask(int index, AppendTaskConfig config, Stat stat) throws Exception {
System.out.printf("Append task %d started\n", index);

byte[] bytes = new byte[config.recordSizeBytes];
new Random().nextBytes(bytes);
ByteBuf payload = Unpooled.wrappedBuffer(bytes).retain();
int intervalNanos = (int) TimeUnit.SECONDS.toNanos(1) / Math.max(1, config.throughputBytes / config.recordSizeBytes);
long lastAppendTimeNanos = System.nanoTime();
long lastLogTimeMillis = System.currentTimeMillis();
long taskStartTimeMillis = System.currentTimeMillis();
AtomicLong count = new AtomicLong();
AtomicLong costNanos = new AtomicLong();
AtomicLong maxCostNanos = new AtomicLong();

while (true) {
while (true) {
long now = System.nanoTime();
long elapsedNanos = now - lastAppendTimeNanos;
if (elapsedNanos >= intervalNanos) {
lastAppendTimeNanos = now;
lastAppendTimeNanos += intervalNanos;

Check warning on line 141 in s3stream/src/main/java/com/automq/stream/s3/wal/benchmark/WriteBench.java

View check run for this annotation

Codecov / codecov/patch

s3stream/src/main/java/com/automq/stream/s3/wal/benchmark/WriteBench.java#L141

Added line #L141 was not covered by tests
break;
}
LockSupport.parkNanos((intervalNanos - elapsedNanos) >> 2);
Expand All @@ -135,21 +149,6 @@
break;
}

if (now - lastLogTimeMillis > TimeUnit.SECONDS.toMillis(LOG_INTERVAL_SECONDS)) {
long countValue = count.getAndSet(0);
long costNanosValue = costNanos.getAndSet(0);
long maxCostNanosValue = maxCostNanos.getAndSet(0);
if (0 != countValue) {
System.out.printf("Append task %d | Append Rate %d msg/s %d KB/s | Avg Latency %.3f ms | Max Latency %.3f ms\n",
index,
countValue / LOG_INTERVAL_SECONDS,
(countValue * config.recordSizeBytes) / LOG_INTERVAL_SECONDS / 1024,
costNanosValue / 1_000_000.0 / countValue,
maxCostNanosValue / 1_000_000.0);
lastLogTimeMillis = now;
}
}

long appendStartTimeNanos = System.nanoTime();
WriteAheadLog.AppendResult result;
try {
Expand All @@ -160,9 +159,7 @@
}
result.future().thenAccept(v -> {
long costNanosValue = System.nanoTime() - appendStartTimeNanos;
count.incrementAndGet();
costNanos.addAndGet(costNanosValue);
maxCostNanos.accumulateAndGet(costNanosValue, Math::max);
stat.update(costNanosValue);

Check warning on line 162 in s3stream/src/main/java/com/automq/stream/s3/wal/benchmark/WriteBench.java

View check run for this annotation

Codecov / codecov/patch

s3stream/src/main/java/com/automq/stream/s3/wal/benchmark/WriteBench.java#L162

Added line #L162 was not covered by tests
flushedOffset.update(v.flushedOffset());
}).whenComplete((v, e) -> {
if (e != null) {
Expand Down Expand Up @@ -251,6 +248,37 @@
}
}

static class Stat {
final AtomicLong count = new AtomicLong();
final AtomicLong costNanos = new AtomicLong();
final AtomicLong maxCostNanos = new AtomicLong();
long lastResetTimeNanos = System.nanoTime();

Check warning on line 255 in s3stream/src/main/java/com/automq/stream/s3/wal/benchmark/WriteBench.java

View check run for this annotation

Codecov / codecov/patch

s3stream/src/main/java/com/automq/stream/s3/wal/benchmark/WriteBench.java#L251-L255

Added lines #L251 - L255 were not covered by tests

public void update(long costNanosValue) {
count.incrementAndGet();
costNanos.addAndGet(costNanosValue);
maxCostNanos.accumulateAndGet(costNanosValue, Math::max);
}

Check warning on line 261 in s3stream/src/main/java/com/automq/stream/s3/wal/benchmark/WriteBench.java

View check run for this annotation

Codecov / codecov/patch

s3stream/src/main/java/com/automq/stream/s3/wal/benchmark/WriteBench.java#L258-L261

Added lines #L258 - L261 were not covered by tests

/**
* NOT thread-safe
*/
public Result reset() {
long countValue = count.getAndSet(0);
long costNanosValue = costNanos.getAndSet(0);
long maxCostNanosValue = maxCostNanos.getAndSet(0);

Check warning on line 269 in s3stream/src/main/java/com/automq/stream/s3/wal/benchmark/WriteBench.java

View check run for this annotation

Codecov / codecov/patch

s3stream/src/main/java/com/automq/stream/s3/wal/benchmark/WriteBench.java#L267-L269

Added lines #L267 - L269 were not covered by tests

long now = System.nanoTime();
long elapsedTimeNanos = now - lastResetTimeNanos;
lastResetTimeNanos = now;

Check warning on line 273 in s3stream/src/main/java/com/automq/stream/s3/wal/benchmark/WriteBench.java

View check run for this annotation

Codecov / codecov/patch

s3stream/src/main/java/com/automq/stream/s3/wal/benchmark/WriteBench.java#L271-L273

Added lines #L271 - L273 were not covered by tests

return new Result(countValue, costNanosValue, maxCostNanosValue, elapsedTimeNanos);

Check warning on line 275 in s3stream/src/main/java/com/automq/stream/s3/wal/benchmark/WriteBench.java

View check run for this annotation

Codecov / codecov/patch

s3stream/src/main/java/com/automq/stream/s3/wal/benchmark/WriteBench.java#L275

Added line #L275 was not covered by tests
}

public record Result(long count, long costNanos, long maxCostNanos, long elapsedTimeNanos) {

Check warning on line 278 in s3stream/src/main/java/com/automq/stream/s3/wal/benchmark/WriteBench.java

View check run for this annotation

Codecov / codecov/patch

s3stream/src/main/java/com/automq/stream/s3/wal/benchmark/WriteBench.java#L278

Added line #L278 was not covered by tests
}
}

public static class FlushedOffset {
private final Lock lock = new ReentrantLock();
// Offset before which all data has been flushed to disk
Expand Down