Skip to content

Commit 56c2a1d

Browse files
committed
Fixed #74
TimerBase no longer fails if closed without processing any data.
1 parent 426d224 commit 56c2a1d

File tree

3 files changed

+110
-55
lines changed

3 files changed

+110
-55
lines changed

src/main/java/org/culturegraph/mf/stream/pipe/TimerBase.java

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
/**
2525
* @author Christoph Böhme
26-
*
26+
*
2727
* @param <R>
2828
* receiver type.
2929
*/
@@ -42,13 +42,18 @@ public class TimerBase<R extends LifeCycle> implements Sender<R> {
4242

4343
private R receiver;
4444

45+
protected TimerBase(final String logPrefix) {
46+
super();
47+
this.logPrefix = logPrefix;
48+
}
49+
4550
@Override
4651
public final <S extends R> S setReceiver(final S receiver) {
4752
this.receiver = receiver;
4853
return receiver;
4954
}
50-
51-
public R getReceiver() {
55+
56+
public final R getReceiver() {
5257
return receiver;
5358
}
5459

@@ -63,27 +68,27 @@ public final void resetStream() {
6368

6469
@Override
6570
public final void closeStream() {
66-
final long averageDuration = cumulativeDuration / count;
71+
final long averageDuration;
72+
if (count > 0) {
73+
averageDuration = cumulativeDuration / count;
74+
} else {
75+
averageDuration = 0;
76+
}
6777
LOG.info(logPrefix
6878
+ String.format("Executions: %d; Cumulative duration: %s; Average duration: %s", Long.valueOf(count),
6979
scaleTime(cumulativeDuration), scaleTime(averageDuration)));
80+
7081
startMeasurement();
7182
if (receiver != null) {
7283
receiver.closeStream();
7384
}
7485
stopMeasurement("Time to close stream: ");
75-
76-
}
77-
78-
protected TimerBase(final String logPrefix) {
79-
super();
80-
this.logPrefix = logPrefix;
8186
}
8287

8388
protected final void startMeasurement() {
8489
startTime = System.nanoTime();
8590
}
86-
91+
8792
protected final void stopMeasurement(){
8893
stopMeasurement("Execution %1$d:");
8994
}

src/test/java/org/culturegraph/mf/stream/pipe/ObjectTimerTest.java

Lines changed: 45 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,45 +16,70 @@
1616
package org.culturegraph.mf.stream.pipe;
1717

1818
import org.culturegraph.mf.framework.DefaultObjectReceiver;
19+
import org.junit.Before;
1920
import org.junit.Test;
2021

2122

2223
/**
2324
* Tests {@link ObjectTimer}.
24-
*
25+
*
2526
* @author Christoph Böhme
2627
*/
2728
public final class ObjectTimerTest {
2829

29-
private static final long[] DURATIONS = { 150L, 20L, 30L, 202L };
30-
3130
/**
3231
* A module with a slow process method.
3332
*/
34-
private static final class BenchmarkedModule extends DefaultObjectReceiver<Long> {
35-
33+
private static final class BenchmarkedModule extends DefaultObjectReceiver<String> {
34+
35+
private static final long[] DURATIONS = { 150L, 20L, 30L, 202L };
36+
37+
private int i;
38+
3639
@Override
37-
public void process(final Long duration) {
40+
public void process(final String obj) {
3841
try {
39-
Thread.sleep(duration.longValue());
40-
} catch (InterruptedException e) {
42+
Thread.sleep(getDuration());
43+
} catch (final InterruptedException e) {
4144
return;
4245
}
4346
}
47+
48+
private long getDuration() {
49+
final long duration = DURATIONS[i];
50+
i += 1;
51+
if (i == DURATIONS.length) {
52+
i = 0;
53+
}
54+
return duration;
55+
}
56+
57+
}
58+
59+
private ObjectTimer<String> objectTimer;
60+
private BenchmarkedModule benchmarkedModule;
61+
62+
@Before
63+
public void setup() {
64+
objectTimer = new ObjectTimer<String>();
65+
benchmarkedModule = new BenchmarkedModule();
66+
objectTimer.setReceiver(benchmarkedModule);
4467
}
45-
68+
4669
@Test
47-
public void testObjectTimer() {
48-
final ObjectTimer<Long> timer = new ObjectTimer<Long>();
49-
final BenchmarkedModule benchmarkedModule = new BenchmarkedModule();
50-
51-
timer.setReceiver(benchmarkedModule);
52-
53-
for (int i=0; i < DURATIONS.length; ++i) {
54-
timer.process(Long.valueOf(DURATIONS[i]));
55-
}
56-
57-
timer.closeStream();
70+
public void testShouldMeasureExecutionTime() {
71+
72+
objectTimer.process("");
73+
objectTimer.process("");
74+
objectTimer.process("");
75+
objectTimer.process("");
76+
objectTimer.closeStream();
77+
}
78+
79+
@Test
80+
public void testShouldHandleImmediateCloseStreamWithNoProcessing() {
81+
82+
objectTimer.closeStream();
5883
}
5984

6085
}

src/test/java/org/culturegraph/mf/stream/pipe/StreamTimerTest.java

Lines changed: 49 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,50 +16,75 @@
1616
package org.culturegraph.mf.stream.pipe;
1717

1818
import org.culturegraph.mf.framework.DefaultStreamReceiver;
19-
import org.culturegraph.mf.stream.pipe.ObjectTimer;
20-
import org.culturegraph.mf.stream.pipe.StreamTimer;
19+
import org.junit.Before;
2120
import org.junit.Test;
2221

2322

2423
/**
2524
* Tests {@link ObjectTimer}.
26-
*
25+
*
2726
* @author Christoph Böhme
2827
*/
2928
public final class StreamTimerTest {
3029

31-
private static final long[] DURATIONS = { 150L, 20L, 30L, 202L };
32-
3330
/**
3431
* A module with a slow process method.
3532
*/
3633
private static final class BenchmarkedModule extends DefaultStreamReceiver {
37-
34+
35+
private static final long[] DURATIONS = { 150L, 20L, 30L, 202L };
36+
37+
private int i;
38+
3839
@Override
39-
public void literal(final String name, final String value) {
40-
final long duration = Long.parseLong(value);
40+
public void startRecord(final String id) {
4141
try {
42-
Thread.sleep(duration);
43-
} catch (InterruptedException e) {
42+
Thread.sleep(getDuration());
43+
} catch (final InterruptedException e) {
4444
return;
4545
}
4646
}
47+
48+
private long getDuration() {
49+
final long duration = DURATIONS[i];
50+
i += 1;
51+
if (i == DURATIONS.length) {
52+
i = 0;
53+
}
54+
return duration;
55+
}
56+
4757
}
48-
58+
59+
private StreamTimer streamTimer;
60+
private BenchmarkedModule benchmarkedModule;
61+
62+
@Before
63+
public void setup() {
64+
streamTimer = new StreamTimer();
65+
benchmarkedModule = new BenchmarkedModule();
66+
streamTimer.setReceiver(benchmarkedModule);
67+
}
68+
4969
@Test
50-
public void testObjectTimer() {
51-
final StreamTimer timer = new StreamTimer();
52-
final BenchmarkedModule benchmarkedModule = new BenchmarkedModule();
53-
54-
timer.setReceiver(benchmarkedModule);
55-
56-
for (int i=0; i < DURATIONS.length; ++i) {
57-
timer.startRecord(Integer.toString(i));
58-
timer.literal("duration", Long.toString(DURATIONS[i]));
59-
timer.endRecord();
60-
}
61-
62-
timer.closeStream();
70+
public void testShouldMeasureExecutionTime() {
71+
72+
streamTimer.startRecord("");
73+
streamTimer.endRecord();
74+
streamTimer.startRecord("");
75+
streamTimer.endRecord();
76+
streamTimer.startRecord("");
77+
streamTimer.endRecord();
78+
streamTimer.startRecord("");
79+
streamTimer.endRecord();
80+
81+
streamTimer.closeStream();
82+
}
83+
84+
@Test
85+
public void testShouldHandleImmediateCloseStreamWithNoProcessing() {
86+
87+
streamTimer.closeStream();
6388
}
6489

6590
}

0 commit comments

Comments
 (0)