1
1
/*
2
- * Copyright (c) 2000, 2019 , Oracle and/or its affiliates. All rights reserved.
2
+ * Copyright (c) 2000, 2022 , 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
36
36
import static java .nio .file .StandardOpenOption .*;
37
37
import static java .nio .charset .StandardCharsets .*;
38
38
import java .util .Random ;
39
+ import java .util .concurrent .TimeUnit ;
39
40
40
41
41
42
/**
@@ -56,21 +57,42 @@ public class MapTest {
56
57
public static void main (String [] args ) throws Exception {
57
58
blah = File .createTempFile ("blah" , null );
58
59
blah .deleteOnExit ();
60
+ long t0 = System .nanoTime ();
59
61
initTestFile (blah );
62
+ long t1 = System .nanoTime ();
63
+ out .printf ("Test file %s initialized in %d ns (%d ms) %n" ,
64
+ blah , t1 - t0 , TimeUnit .NANOSECONDS .toMillis (t1 - t0 ));
65
+ t0 = t1 ;
60
66
try {
61
- out .println ("Test file " + blah + " initialized" );
62
67
testZero ();
63
- out .println ("Zero size: OK" );
68
+ t1 = System .nanoTime ();
69
+ out .printf ("Zero size: done in %d ns (%d ms) %n" ,
70
+ t1 - t0 , TimeUnit .NANOSECONDS .toMillis (t1 - t0 ));
71
+ t0 = t1 ;
64
72
testRead ();
65
- out .println ("Read: OK" );
73
+ t1 = System .nanoTime ();
74
+ out .printf ("Read: done in %d ns (%d ms) %n" ,
75
+ t1 - t0 , TimeUnit .NANOSECONDS .toMillis (t1 - t0 ));
76
+ t0 = t1 ;
66
77
testWrite ();
67
- out .println ("Write: OK" );
78
+ t1 = System .nanoTime ();
79
+ out .printf ("Write: done in %d ns (%d ms) %n" ,
80
+ t1 - t0 , TimeUnit .NANOSECONDS .toMillis (t1 - t0 ));
81
+ t0 = t1 ;
68
82
testHighOffset ();
69
- out .println ("High offset: OK" );
83
+ t1 = System .nanoTime ();
84
+ out .printf ("High offset: done in %d ns (%d ms) %n" ,
85
+ t1 - t0 , TimeUnit .NANOSECONDS .toMillis (t1 - t0 ));
86
+ t0 = t1 ;
70
87
testForce ();
71
- out .println ("Force: OK" );
88
+ t1 = System .nanoTime ();
89
+ out .printf ("Force: done in %d ns (%d ms) %n" ,
90
+ t1 - t0 , TimeUnit .NANOSECONDS .toMillis (t1 - t0 ));
91
+ t0 = t1 ;
72
92
testExceptions ();
73
- out .println ("Exceptions: OK" );
93
+ t1 = System .nanoTime ();
94
+ out .printf ("Exceptions: done in %d ns (%d ms) %n" ,
95
+ t1 - t0 , TimeUnit .NANOSECONDS .toMillis (t1 - t0 ));
74
96
} finally {
75
97
blah .delete ();
76
98
}
@@ -195,44 +217,42 @@ private static void testHighOffset() throws Exception {
195
217
* the data exercising various valid and invalid writeback ranges.
196
218
*/
197
219
private static void testForce () throws Exception {
198
- for (int x =0 ; x <50 ; x ++) {
199
- try (RandomAccessFile raf = new RandomAccessFile (blah , "rw" )) {
200
- FileChannel fc = raf .getChannel ();
201
- final int BLOCK_SIZE = 64 ;
202
- final int BLOCK_COUNT = (4096 * 2 )/ BLOCK_SIZE ;
203
- int offset = 0 ;
204
- MappedByteBuffer b = fc .map (MapMode .READ_WRITE ,
205
- 0 , BLOCK_SIZE * (BLOCK_COUNT + 1 ));
206
-
207
- for (int blocks = 0 ; blocks < BLOCK_COUNT ; blocks ++) {
208
- for (int i = 0 ; i < BLOCK_SIZE ; i ++) {
209
- b .put (offset + i , (byte )('0' + i ));
210
- }
211
- b .force (offset , BLOCK_SIZE );
212
- offset += BLOCK_SIZE ;
220
+ try (RandomAccessFile raf = new RandomAccessFile (blah , "rw" )) {
221
+ FileChannel fc = raf .getChannel ();
222
+ final int BLOCK_SIZE = 64 ;
223
+ final int BLOCK_COUNT = (4096 * 2 )/ BLOCK_SIZE ;
224
+ int offset = 0 ;
225
+ MappedByteBuffer b = fc .map (MapMode .READ_WRITE ,
226
+ 0 , BLOCK_SIZE * (BLOCK_COUNT + 1 ));
227
+
228
+ for (int blocks = 0 ; blocks < BLOCK_COUNT ; blocks ++) {
229
+ for (int i = 0 ; i < BLOCK_SIZE ; i ++) {
230
+ b .put (offset + i , (byte )('0' + i ));
213
231
}
232
+ b .force (offset , BLOCK_SIZE );
233
+ offset += BLOCK_SIZE ;
234
+ }
214
235
215
- Exception exc = null ;
216
- try {
217
- // start and end are out of range
218
- b .force (offset + BLOCK_SIZE , BLOCK_SIZE );
219
- } catch (IndexOutOfBoundsException e ) {
220
- exc = e ;
221
- }
222
- if (exc == null ) {
223
- throw new RuntimeException ("expected Exception for force beyond buffer extent" );
224
- }
236
+ Exception exc = null ;
237
+ try {
238
+ // start and end are out of range
239
+ b .force (offset + BLOCK_SIZE , BLOCK_SIZE );
240
+ } catch (IndexOutOfBoundsException e ) {
241
+ exc = e ;
242
+ }
243
+ if (exc == null ) {
244
+ throw new RuntimeException ("expected Exception for force beyond buffer extent" );
245
+ }
225
246
226
- exc = null ;
227
- try {
228
- // start is in range but end is out of range
229
- b .force (offset , 2 * BLOCK_SIZE );
230
- } catch (IndexOutOfBoundsException e ) {
231
- exc = e ;
232
- }
233
- if (exc == null ) {
234
- throw new RuntimeException ("expected Exception for force beyond write limit" );
235
- }
247
+ exc = null ;
248
+ try {
249
+ // start is in range but end is out of range
250
+ b .force (offset , 2 * BLOCK_SIZE );
251
+ } catch (IndexOutOfBoundsException e ) {
252
+ exc = e ;
253
+ }
254
+ if (exc == null ) {
255
+ throw new RuntimeException ("expected Exception for force beyond write limit" );
236
256
}
237
257
}
238
258
}
0 commit comments