1
1
/*
2
- * Copyright (c) 1997, 2016 , Oracle and/or its affiliates. All rights reserved.
2
+ * Copyright (c) 1997, 2024 , Oracle and/or its affiliates. All rights reserved.
3
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
4
*
5
5
* This code is free software; you can redistribute it and/or modify it
26
26
* @bug 0000000
27
27
* @summary This test checks performance of various ciphers.
28
28
* @author Jan Luehe
29
- * @run main/manual PerformanceTest
30
29
*/
31
- import java .security .*;
32
30
import java .security .spec .*;
33
- import java .io .*;
34
31
import javax .crypto .*;
35
32
import javax .crypto .spec .*;
36
33
@@ -178,14 +175,16 @@ public void runTest(byte[] data, int count) throws Exception {
178
175
long start , end ;
179
176
cipher .init (Cipher .ENCRYPT_MODE , cipherKey , params );
180
177
181
- start = System . currentTimeMillis ();
178
+ start = getTimeInMicroseconds ();
182
179
for (int i =0 ; i <count -1 ; i ++) {
183
180
cipher .update (data , 0 , data .length );
184
181
}
185
182
cipher .doFinal (data , 0 , data .length );
186
- end = System . currentTimeMillis ();
183
+ end = getTimeInMicroseconds ();
187
184
188
- int speed = (int )((data .length * count )/(end - start ));
185
+ // To avoid dividing by zero in the rare case where end is equal to start
186
+ long executionTime = end != start ? end - start : 1L ;
187
+ int speed = (int ) ((data .length * count ) / executionTime );
189
188
sum += speed ;
190
189
col .append (speed );
191
190
}
@@ -198,8 +197,12 @@ public void printHeadings() {
198
197
System .out .println
199
198
("=========================================================" );
200
199
System .out .println
201
- ("Algorithm DataSize Rounds Kbytes/sec " );
200
+ ("Algorithm DataSize Rounds Bytes/microsec " );
202
201
203
202
}
204
203
204
+ private static long getTimeInMicroseconds () {
205
+ return System .nanoTime () / 1000 ;
206
+ }
207
+
205
208
}
0 commit comments