1
1
package perf ;
2
2
3
+ import java .nio .charset .StandardCharsets ;
3
4
import java .util .UUID ;
4
5
5
6
import com .fasterxml .uuid .*;
19
20
*/
20
21
public class MeasurePerformance
21
22
{
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
+
27
24
// 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" ;
30
26
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 ;
34
41
}
35
-
42
+
36
43
public void test () throws Exception
37
44
{
38
45
int i = 0 ;
@@ -53,8 +60,11 @@ public void test() throws Exception
53
60
new com .fasterxml .uuid .ext .FileBasedTimestampSynchronizer ());
54
61
final StringArgGenerator nameGen = Generators .nameBasedGenerator (namespaceForNamed );
55
62
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 );
58
68
int round = (i ++ % 7 );
59
69
60
70
long curr = System .currentTimeMillis ();
@@ -65,44 +75,49 @@ public void test() throws Exception
65
75
66
76
case 0 :
67
77
msg = "JDK, random" ;
68
- testJDK (uuids , ROUNDS );
78
+ testJDK (uuids , rounds );
69
79
break ;
70
80
71
81
case 1 :
72
82
msg = "JDK, name" ;
73
- testJDKNames (uuids , ROUNDS );
83
+ testJDKNames (uuids , rounds );
74
84
break ;
75
85
76
86
case 2 :
77
87
msg = "Jug, time-based (non-sync)" ;
78
- testTimeBased (uuids , ROUNDS , timeGenPlain );
88
+ testTimeBased (uuids , rounds , timeGenPlain );
79
89
break ;
80
90
81
91
case 3 :
82
92
msg = "Jug, time-based (SYNC)" ;
83
- testTimeBased (uuids , ROUNDS , timeGenSynced );
93
+ testTimeBased (uuids , rounds , timeGenSynced );
84
94
break ;
85
95
86
96
case 4 :
87
97
msg = "Jug, SecureRandom" ;
88
- testRandom (uuids , ROUNDS , secureRandomGen );
98
+ testRandom (uuids , rounds , secureRandomGen );
89
99
break ;
90
100
91
101
case 5 :
92
102
msg = "Jug, java.util.Random" ;
93
- testRandom (uuids , ROUNDS , utilRandomGen );
103
+ testRandom (uuids , rounds , utilRandomGen );
94
104
break ;
95
105
96
106
97
107
case 6 :
98
108
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
+ }
100
115
break ;
101
116
102
117
/*
103
118
case 7:
104
119
msg = "http://johannburkard.de/software/uuid/";
105
- testUUID32(uuids, ROUNDS );
120
+ testUUID32(uuids, rounds );
106
121
break;
107
122
*/
108
123
@@ -143,7 +158,7 @@ private final void testJDKNames(Object[] uuids, int rounds) throws java.io.IOExc
143
158
{
144
159
while (--rounds >= 0 ) {
145
160
for (int i = 0 , len = uuids .length ; i < len ; ++i ) {
146
- final byte [] nameBytes = NAME . getBytes ( "UTF-8" ) ;
161
+ final byte [] nameBytes = NAME_BYTES ;
147
162
uuids [i ] = UUID .nameUUIDFromBytes (nameBytes );
148
163
}
149
164
}
@@ -171,13 +186,13 @@ private final void testNameBased(Object[] uuids, int rounds, StringArgGenerator
171
186
{
172
187
while (--rounds >= 0 ) {
173
188
for (int i = 0 , len = uuids .length ; i < len ; ++i ) {
174
- uuids [i ] = uuidGen .generate (NAME );
189
+ uuids [i ] = uuidGen .generate (NAME_STRING );
175
190
}
176
191
}
177
192
}
178
193
179
194
public static void main (String [] args ) throws Exception
180
195
{
181
- new MeasurePerformance ().test ();
196
+ new MeasurePerformance (DEFAULT_ROUNDS , true ).test ();
182
197
}
183
198
}
0 commit comments