Skip to content

Commit d42f907

Browse files
authored
Bit silly but for good code coverage, need to "test" our manually run perf test class too (#112)
1 parent 64297d0 commit d42f907

File tree

2 files changed

+56
-24
lines changed

2 files changed

+56
-24
lines changed

src/main/java/perf/MeasurePerformance.java

Lines changed: 39 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package perf;
22

3+
import java.nio.charset.StandardCharsets;
34
import java.util.UUID;
45

56
import com.fasterxml.uuid.*;
@@ -19,20 +20,26 @@
1920
*/
2021
public class MeasurePerformance
2122
{
22-
// Let's generate quarter million UUIDs per test
23-
24-
private static final int ROUNDS = 250;
25-
private static final int COUNT = 1000;
26-
23+
2724
// also: let's just use a single name for name-based, to avoid extra overhead:
28-
final String NAME = "http://www.cowtowncoder.com/blog/blog.html";
29-
final byte[] NAME_BYTES;
25+
private final static String NAME_STRING = "http://www.cowtowncoder.com/blog/blog.html";
3026

31-
public MeasurePerformance() throws java.io.IOException
32-
{
33-
NAME_BYTES = NAME.getBytes("UTF-8");
27+
private final static byte[] NAME_BYTES = NAME_STRING.getBytes(StandardCharsets.UTF_8);
28+
29+
// Let's generate 50k UUIDs per test round
30+
private static final int COUNT = 1000;
31+
private static final int DEFAULT_ROUNDS = 50;
32+
33+
private final int rounds;
34+
private final boolean runForever;
35+
36+
public MeasurePerformance() { this(DEFAULT_ROUNDS, true); }
37+
38+
public MeasurePerformance(int rounds, boolean runForever) {
39+
this.rounds = rounds;
40+
this.runForever = runForever;
3441
}
35-
42+
3643
public void test() throws Exception
3744
{
3845
int i = 0;
@@ -53,8 +60,11 @@ public void test() throws Exception
5360
new com.fasterxml.uuid.ext.FileBasedTimestampSynchronizer());
5461
final StringArgGenerator nameGen = Generators.nameBasedGenerator(namespaceForNamed);
5562

56-
while (true) {
57-
try { Thread.sleep(100L); } catch (InterruptedException ie) { }
63+
boolean running = true;
64+
final long sleepTime = runForever ? 350L : 1L;
65+
66+
while (running) {
67+
Thread.sleep(sleepTime);
5868
int round = (i++ % 7);
5969

6070
long curr = System.currentTimeMillis();
@@ -65,44 +75,49 @@ public void test() throws Exception
6575

6676
case 0:
6777
msg = "JDK, random";
68-
testJDK(uuids, ROUNDS);
78+
testJDK(uuids, rounds);
6979
break;
7080

7181
case 1:
7282
msg = "JDK, name";
73-
testJDKNames(uuids, ROUNDS);
83+
testJDKNames(uuids, rounds);
7484
break;
7585

7686
case 2:
7787
msg = "Jug, time-based (non-sync)";
78-
testTimeBased(uuids, ROUNDS, timeGenPlain);
88+
testTimeBased(uuids, rounds, timeGenPlain);
7989
break;
8090

8191
case 3:
8292
msg = "Jug, time-based (SYNC)";
83-
testTimeBased(uuids, ROUNDS, timeGenSynced);
93+
testTimeBased(uuids, rounds, timeGenSynced);
8494
break;
8595

8696
case 4:
8797
msg = "Jug, SecureRandom";
88-
testRandom(uuids, ROUNDS, secureRandomGen);
98+
testRandom(uuids, rounds, secureRandomGen);
8999
break;
90100

91101
case 5:
92102
msg = "Jug, java.util.Random";
93-
testRandom(uuids, ROUNDS, utilRandomGen);
103+
testRandom(uuids, rounds, utilRandomGen);
94104
break;
95105

96106

97107
case 6:
98108
msg = "Jug, name-based";
99-
testNameBased(uuids, ROUNDS, nameGen);
109+
testNameBased(uuids, rounds, nameGen);
110+
111+
// Last one, quit unless running forever
112+
if (!runForever) {
113+
running = false;
114+
}
100115
break;
101116

102117
/*
103118
case 7:
104119
msg = "http://johannburkard.de/software/uuid/";
105-
testUUID32(uuids, ROUNDS);
120+
testUUID32(uuids, rounds);
106121
break;
107122
*/
108123

@@ -143,7 +158,7 @@ private final void testJDKNames(Object[] uuids, int rounds) throws java.io.IOExc
143158
{
144159
while (--rounds >= 0) {
145160
for (int i = 0, len = uuids.length; i < len; ++i) {
146-
final byte[] nameBytes = NAME.getBytes("UTF-8");
161+
final byte[] nameBytes = NAME_BYTES;
147162
uuids[i] = UUID.nameUUIDFromBytes(nameBytes);
148163
}
149164
}
@@ -171,13 +186,13 @@ private final void testNameBased(Object[] uuids, int rounds, StringArgGenerator
171186
{
172187
while (--rounds >= 0) {
173188
for (int i = 0, len = uuids.length; i < len; ++i) {
174-
uuids[i] = uuidGen.generate(NAME);
189+
uuids[i] = uuidGen.generate(NAME_STRING);
175190
}
176191
}
177192
}
178193

179194
public static void main(String[] args) throws Exception
180195
{
181-
new MeasurePerformance().test();
196+
new MeasurePerformance(DEFAULT_ROUNDS, true).test();
182197
}
183198
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package perf;
2+
3+
import org.junit.Test;
4+
5+
// Things we do for Code Coverage... altough "perf/MeasurePerformance.java"
6+
// is only to be manually run, it is included in build, so
7+
// we get code coverage whether we want it or not. So let's have
8+
// a silly little driver to exercise it from unit tests and avoid dinging
9+
// overall test coverage
10+
public class MeasurePerformanceTest
11+
{
12+
@Test
13+
public void runMinimalPerfTest() throws Exception
14+
{
15+
new MeasurePerformance(10, false).test();
16+
}
17+
}

0 commit comments

Comments
 (0)