Skip to content

Commit

Permalink
Fix zero consumer behaviour (#94)
Browse files Browse the repository at this point in the history
  • Loading branch information
chinghongfang authored Nov 5, 2021
1 parent f87984b commit ff1ef2e
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
1 change: 1 addition & 0 deletions app/src/main/java/org/astraea/performance/Metrics.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public synchronized double avgBytes() {
var value = new BigDecimal(bytes);
var scale = new BigDecimal(1024 * 1024);
var time = BigDecimal.valueOf((double) (System.currentTimeMillis() - startTime) / 1000);
if (time.doubleValue() == 0.0) return 0.0;
return value.divide(scale, 3, RoundingMode.UP).divide(time, 3, RoundingMode.UP).doubleValue();
}

Expand Down
4 changes: 4 additions & 0 deletions app/src/main/java/org/astraea/performance/Tracker.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ public State execute() throws InterruptedException {
System.out.printf(
" producer[%d]的發送average latency: %.3fms%n", i, producerData.get(i).avgLatency());
}
if (consumerData.isEmpty()) {
if (completed >= records) return State.DONE;
else return State.RUNNING;
}
/* consumer */
completed = 0;
bytes = 0L;
Expand Down
31 changes: 31 additions & 0 deletions app/src/test/java/org/astraea/performance/TrackerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.astraea.performance;

import java.util.List;
import org.astraea.concurrent.ThreadPool;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class TrackerTest {
@Test
public void testTerminate() throws InterruptedException {
var producerData = List.of(new Metrics());
var consumerData = List.of(new Metrics());
List<Metrics> empty = List.of();
int records = 1;

try (Tracker tracker = new Tracker(producerData, consumerData, records)) {
Assertions.assertEquals(ThreadPool.Executor.State.RUNNING, tracker.execute());
producerData.get(0).put(1, 1);
consumerData.get(0).put(1, 1);
Assertions.assertEquals(ThreadPool.Executor.State.DONE, tracker.execute());
}

// Zero consumer
producerData = List.of(new Metrics());
try (Tracker tracker = new Tracker(producerData, empty, records)) {
Assertions.assertEquals(ThreadPool.Executor.State.RUNNING, tracker.execute());
producerData.get(0).put(1, 1);
Assertions.assertEquals(ThreadPool.Executor.State.DONE, tracker.execute());
}
}
}

0 comments on commit ff1ef2e

Please sign in to comment.