8
8
/**
9
9
* Simple micro-benchmark for evaluating performance of various UUID generation
10
10
* techniques, including JDK's method as well as JUG's variants.
11
+ *<p>
12
+ * Notes: for name-based variant we will pass plain Strings, assuming this is the
13
+ * most common use case; even though it is possible to also pass raw byte arrays.
14
+ * JDK and Jug implementations have similar performance so this only changes
15
+ * relative speeds of name- vs time-based variants.
11
16
*
12
- * @since 3.0
17
+ * @since 3.1
13
18
*/
14
19
public class MeasurePerformance
15
20
{
16
21
// Let's generate quarter million UUIDs per test
17
22
18
23
private static final int ROUNDS = 250 ;
19
24
private static final int COUNT = 1000 ;
25
+
26
+ private final static UUID NAMESPACE = NameBasedGenerator .NAMESPACE_DNS ;
27
+
28
+ // also: let's just use a single name for name-based, to avoid extra overhead:
29
+ final String NAME = "http://www.cowtowncoder.com/blog/blog.html" ;
30
+ final byte [] NAME_BYTES ;
31
+
32
+ public MeasurePerformance () throws java .io .IOException
33
+ {
34
+ NAME_BYTES = NAME .getBytes ("UTF-8" );
35
+ }
20
36
21
37
public void test () throws Exception
22
38
{
@@ -27,19 +43,18 @@ public void test() throws Exception
27
43
// can either use bogus address; or local one, no difference perf-wise
28
44
EthernetAddress nic = EthernetAddress .fromInterface ();
29
45
30
- UUID namespaceForNamed = NameBasedGenerator .NAMESPACE_DNS ;
31
-
46
+ // Whether to include namespace? Depends on whether we compare with JDK (which does not)
47
+ // UUID namespaceForNamed = NAMESPACE;
48
+ UUID namespaceForNamed = null ;
49
+
32
50
final NoArgGenerator secureRandomGen = Generators .randomBasedGenerator ();
33
51
final NoArgGenerator utilRandomGen = Generators .randomBasedGenerator (new java .util .Random (123 ));
34
52
final NoArgGenerator timeGen = Generators .timeBasedGenerator (nic );
35
53
final StringArgGenerator nameGen = Generators .nameBasedGenerator (namespaceForNamed );
36
-
37
- // also: let's just use a single name for name-based, to avoid extra overhead:
38
- final String NAME = "http://www.cowtowncoder.com/blog/blog.html" ;
39
54
40
55
while (true ) {
41
56
try { Thread .sleep (100L ); } catch (InterruptedException ie ) { }
42
- int round = (i ++ % 5 );
57
+ int round = (i ++ % 6 );
43
58
44
59
long curr = System .currentTimeMillis ();
45
60
String msg ;
@@ -48,28 +63,33 @@ public void test() throws Exception
48
63
switch (round ) {
49
64
50
65
case 0 :
51
- msg = "JDK" ;
66
+ msg = "JDK, random " ;
52
67
testJDK (uuids , ROUNDS );
53
68
break ;
54
-
69
+
55
70
case 1 :
71
+ msg = "JDK, name" ;
72
+ testJDKNames (uuids , ROUNDS );
73
+ break ;
74
+
75
+ case 2 :
56
76
msg = "Jug, SecureRandom" ;
57
77
testNoArgs (uuids , ROUNDS , secureRandomGen );
58
78
break ;
59
79
60
- case 2 :
80
+ case 3 :
61
81
msg = "Jug, java.util.Random" ;
62
82
testNoArgs (uuids , ROUNDS , utilRandomGen );
63
83
break ;
64
84
65
- case 3 :
85
+ case 4 :
66
86
msg = "Jug, time-based" ;
67
87
testNoArgs (uuids , ROUNDS , timeGen );
68
88
break ;
69
89
70
- case 4 :
90
+ case 5 :
71
91
msg = "Jug, name-based" ;
72
- testStringArg (uuids , ROUNDS , nameGen , NAME );
92
+ testStringArg (uuids , ROUNDS , nameGen );
73
93
break ;
74
94
75
95
default :
@@ -93,6 +113,16 @@ private final void testJDK(UUID[] uuids, int rounds)
93
113
}
94
114
}
95
115
116
+ private final void testJDKNames (UUID [] uuids , int rounds ) throws java .io .IOException
117
+ {
118
+ while (--rounds >= 0 ) {
119
+ for (int i = 0 , len = uuids .length ; i < len ; ++i ) {
120
+ final byte [] nameBytes = NAME .getBytes ("UTF-8" );
121
+ uuids [i ] = UUID .nameUUIDFromBytes (nameBytes );
122
+ }
123
+ }
124
+ }
125
+
96
126
private final void testNoArgs (UUID [] uuids , int rounds , NoArgGenerator uuidGen )
97
127
{
98
128
while (--rounds >= 0 ) {
@@ -102,12 +132,11 @@ private final void testNoArgs(UUID[] uuids, int rounds, NoArgGenerator uuidGen)
102
132
}
103
133
}
104
134
105
- private final void testStringArg (UUID [] uuids , int rounds , StringArgGenerator uuidGen ,
106
- String name )
135
+ private final void testStringArg (UUID [] uuids , int rounds , StringArgGenerator uuidGen )
107
136
{
108
137
while (--rounds >= 0 ) {
109
138
for (int i = 0 , len = uuids .length ; i < len ; ++i ) {
110
- uuids [i ] = uuidGen .generate (name );
139
+ uuids [i ] = uuidGen .generate (NAME );
111
140
}
112
141
}
113
142
}
0 commit comments