summaryrefslogtreecommitdiffstats
path: root/src/junit/com/jogamp/common/nio/TestByteBufferInputStream.java
blob: 195bef34b6ea39240a1436d55a9228caa8d52313 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/**
 * Copyright 2014 JogAmp Community. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification, are
 * permitted provided that the following conditions are met:
 *
 *    1. Redistributions of source code must retain the above copyright notice, this list of
 *       conditions and the following disclaimer.
 *
 *    2. Redistributions in binary form must reproduce the above copyright notice, this list
 *       of conditions and the following disclaimer in the documentation and/or other materials
 *       provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * The views and conclusions contained in the software and documentation are those of the
 * authors and should not be interpreted as representing official policies, either expressed
 * or implied, of JogAmp Community.
 */
package com.jogamp.common.nio;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;

import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;

import com.jogamp.common.util.IOUtil;
import com.jogamp.junit.util.JunitTracer;

import org.junit.FixMethodOrder;
import org.junit.runners.MethodSorters;

/**
 * Testing serial read of {@link ByteBufferInputStream} and {@link MappedByteBufferInputStream},
 * i.e. basic functionality only.
 * <p>
 * Focusing on comparison with {@link BufferedInputStream} regarding
 * performance, used memory heap and used virtual memory.
 * </p>
 */
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class TestByteBufferInputStream extends JunitTracer {
    /** {@value} */
    static final int buffer__8KiB = 1 << 13;

    /** {@value} */
    static final int halfMiB = 1 << 19;
    /** {@value} */
    static final int oneMiB = 1 << 20;
    /** {@value} */
    static final int tenMiB = 1 << 24;
    /** {@value} */
    static final int hunMiB = 1 << 27;
    /** {@value} */
    static final int halfGiB = 1 << 29;
    /** {@value} */
    static final int oneGiB = 1 << 30;
    /** {@value} */
    static final long twoPlusGiB = ( 2L << 30 ) + halfMiB;

    static final String fileHalfMiB = "./testHalfMiB.bin" ;
    static final String fileOneMiB = "./testOneMiB.bin" ;
    static final String fileTenMiB = "./testTenMiB.bin" ;
    static final String fileHunMiB = "./testHunMiB.bin" ;
    static final String fileHalfGiB = "./testHalfGiB.bin" ;
    static final String fileOneGiB = "./testOneGiB.bin" ;
    static final String fileTwoPlusGiB = "./testTwoPlusGiB.bin" ;
    static final String fileOut = "./testOut.bin" ;

    static final String printPrecision = "%8.3f";
    static final double mib = 1024.0*1024.0;


    @BeforeClass
    public static void setup() throws IOException {
        final Runtime runtime = Runtime.getRuntime();
        System.err.printf("Total Memory : "+printPrecision+" MiB%n", runtime.totalMemory() / mib);
        System.err.printf("Max Memory   : "+printPrecision+" MiB%n", runtime.maxMemory() / mib);

        setup(fileHalfMiB, halfMiB);
        setup(fileOneMiB, oneMiB);
        setup(fileTenMiB, tenMiB);
        setup(fileHunMiB, hunMiB);
        setup(fileHalfGiB, halfGiB);
        setup(fileOneGiB, oneGiB);
        setup(fileTwoPlusGiB, twoPlusGiB);
    }
    static void setup(final String fname, final long size) throws IOException {
        final File file = new File(fname);
        final RandomAccessFile out = new RandomAccessFile(file, "rws");
        out.setLength(size);
        out.close();
    }

    @AfterClass
    public static void cleanup() {
        cleanup(fileHalfMiB);
        cleanup(fileOneMiB);
        cleanup(fileTenMiB);
        cleanup(fileHunMiB);
        cleanup(fileHalfGiB);
        cleanup(fileOneGiB);
        cleanup(fileTwoPlusGiB);
        cleanup(fileOut);
    }
    static void cleanup(final String fname) {
        final File file = new File(fname);
        file.delete();
    }

    @Test
    public void test01MixedIntSize() throws IOException {
        // testCopyIntSize1Impl(fileHalfMiB, halfMiB);

        // testCopyIntSize1Impl(fileOneMiB, oneMiB);

        testCopyIntSize1Impl(fileTenMiB, tenMiB);

        testCopyIntSize1Impl(fileHunMiB, hunMiB);

        testCopyIntSize1Impl(fileHalfGiB, halfGiB);

        testCopyIntSize1Impl(fileOneGiB, oneGiB);
    }

    static enum SrcType { COPY, MMAP1, MMAP2_NONE, MMAP2_SOFT, MMAP2_HARD };

    @Test
    public void test11MMapFlushNone() throws IOException {
        testCopyIntSize1Impl2(0, SrcType.MMAP2_NONE, 0, fileOneGiB, oneGiB);
        testCopyIntSize1Impl2(0, SrcType.MMAP2_NONE, 0, fileTwoPlusGiB, twoPlusGiB);
    }

    @Test
    public void test12MMapFlushSoft() throws IOException {
        testCopyIntSize1Impl2(0, SrcType.MMAP2_SOFT, 0, fileOneGiB, oneGiB);
        testCopyIntSize1Impl2(0, SrcType.MMAP2_SOFT, 0, fileTwoPlusGiB, twoPlusGiB);
    }

    @Test
    public void test13MMapFlushHard() throws IOException {
        testCopyIntSize1Impl2(0, SrcType.MMAP2_HARD, 0, fileOneGiB, oneGiB);
        testCopyIntSize1Impl2(0, SrcType.MMAP2_HARD, 0, fileTwoPlusGiB, twoPlusGiB);
    }

    void testCopyIntSize1Impl(final String testFileName, final long expSize) throws IOException {
        testCopyIntSize1Impl(SrcType.COPY, buffer__8KiB, testFileName, expSize);
        testCopyIntSize1Impl(SrcType.COPY,       hunMiB, testFileName, expSize);
        testCopyIntSize1Impl(SrcType.MMAP1,           0, testFileName, expSize);
        testCopyIntSize1Impl(SrcType.MMAP2_SOFT,           0, testFileName, expSize);
        System.err.println();
    }
    void testCopyIntSize1Impl(final SrcType srcType, final int reqBufferSize, final String testFileName, final long expSize) throws IOException {
        if( testCopyIntSize1Impl2(0, srcType, reqBufferSize, testFileName, expSize) ) {
            if( testCopyIntSize1Impl2(1, srcType, reqBufferSize, testFileName, expSize) ) {
                // testCopyIntSize1Impl2(2, srcType, reqBufferSize, testFileName, expSize);
            }
        }
        System.err.println();
    }
    boolean testCopyIntSize1Impl2(final int iter, final SrcType srcType, final int reqBufferSize, final String testFileName, final long expSize) throws IOException {
        final int expSizeI = (int) ( expSize <= Integer.MAX_VALUE ? expSize : Integer.MAX_VALUE );
        final int bufferSize = reqBufferSize < expSizeI ? reqBufferSize : expSizeI;
        final File testFile = new File(testFileName);
        final long hasSize1 = testFile.length();
        final long t0 = System.currentTimeMillis();
        Assert.assertEquals(expSize, hasSize1);

        final Runtime runtime = Runtime.getRuntime();
        final long[] usedMem0 = { 0 };
        final long[] freeMem0 = { 0 };
        final long[] usedMem1 = { 0 };
        final long[] freeMem1 = { 0 };

        final String prefix = "test #"+iter+" "+String.format(printPrecision+" MiB", expSize/mib);
        System.err.printf("%s: mode %-5s, bufferSize %9d: BEGIN%n", prefix, srcType.toString(), bufferSize);
        dumpMem(prefix+" before", runtime, -1, -1, usedMem0, freeMem0 );

        final IOException[] ioe = { null };
        OutOfMemoryError oome = null;
        InputStream bis = null;
        FileInputStream fis = null;
        FileChannel fic = null;
        boolean isMappedByteBufferInputStream = false;
        try {
            fis = new FileInputStream(testFile);
            if( SrcType.COPY == srcType ) {
                if( hasSize1 > Integer.MAX_VALUE ) {
                    fis.close();
                    throw new IllegalArgumentException("file size > MAX_INT for "+srcType+": "+hasSize1+" of "+testFile);
                }
                bis = new BufferedInputStream(fis, bufferSize);
            } else if( SrcType.MMAP1 == srcType ) {
                if( hasSize1 > Integer.MAX_VALUE ) {
                    fis.close();
                    throw new IllegalArgumentException("file size > MAX_INT for "+srcType+": "+hasSize1+" of "+testFile);
                }
                fic = fis.getChannel();
                final MappedByteBuffer fmap = fic.map(FileChannel.MapMode.READ_ONLY, 0, hasSize1); // survives channel/stream closing until GC'ed!
                bis = new ByteBufferInputStream(fmap);
            } else {
                isMappedByteBufferInputStream = true;
                MappedByteBufferInputStream.CacheMode cmode;
                switch(srcType) {
                    case MMAP2_NONE: cmode = MappedByteBufferInputStream.CacheMode.FLUSH_NONE;
                                     break;
                    case MMAP2_SOFT: cmode = MappedByteBufferInputStream.CacheMode.FLUSH_PRE_SOFT;
                                     break;
                    case MMAP2_HARD: cmode = MappedByteBufferInputStream.CacheMode.FLUSH_PRE_HARD;
                                     break;
                    default:         fis.close();
                                     throw new InternalError("XX: "+srcType);
                }
                final MappedByteBufferInputStream mis = new MappedByteBufferInputStream(fis.getChannel(), FileChannel.MapMode.READ_ONLY, cmode);
                Assert.assertEquals(expSize, mis.remaining());
                Assert.assertEquals(expSize, mis.length());
                Assert.assertEquals(0, mis.position());
                bis = mis;
            }
        } catch (final IOException e) {
            if( e.getCause() instanceof OutOfMemoryError ) {
                oome = (OutOfMemoryError) e.getCause(); // oops
            } else {
                ioe[0] = e;
            }
        } catch (final OutOfMemoryError m) {
            oome = m; // oops
        }
        IOException ioe2 = null;
        try {
            if( null != ioe[0] || null != oome ) {
                if( null != oome ) {
                    System.err.printf("%s: mode %-5s, bufferSize %9d: OutOfMemoryError.1 %s%n",
                                      prefix, srcType.toString(), bufferSize, oome.getMessage());
                    oome.printStackTrace();
                } else {
                    Assert.assertNull(ioe[0]);
                }
                return false;
            }
            Assert.assertEquals(expSizeI, bis.available());

            final long t1 = System.currentTimeMillis();

            final File out = new File(fileOut);
            IOUtil.copyStream2File(bis, out, -1);
            final long t2 = System.currentTimeMillis();

            final String suffix;
            if( isMappedByteBufferInputStream ) {
                suffix = ", cacheMode "+((MappedByteBufferInputStream)bis).getCacheMode();
            } else {
                suffix = "";
            }
            System.err.printf("%s: mode %-5s, bufferSize %9d: total %5d, setup %5d, copy %5d ms%s%n",
                              prefix, srcType.toString(), bufferSize, t2-t0, t1-t0, t2-t1, suffix);

            Assert.assertEquals(expSize, out.length());
            out.delete();

            Assert.assertEquals(0, bis.available());
            if( isMappedByteBufferInputStream ) {
                final MappedByteBufferInputStream mis = (MappedByteBufferInputStream)bis;
                Assert.assertEquals(0, mis.remaining());
                Assert.assertEquals(expSize, mis.length());
                Assert.assertEquals(expSize, mis.position());
            }
            dumpMem(prefix+" after ", runtime, usedMem0[0], freeMem0[0], usedMem1, freeMem1 );
            System.gc();
            try {
                Thread.sleep(500);
            } catch (final InterruptedException e) { }
            dumpMem(prefix+" gc'ed ", runtime, usedMem0[0], freeMem0[0], usedMem1, freeMem1 );
        } catch( final IOException e ) {
            if( e.getCause() instanceof OutOfMemoryError ) {
                oome = (OutOfMemoryError) e.getCause(); // oops
            } else {
                ioe2 = e;
            }
        } catch (final OutOfMemoryError m) {
            oome = m; // oops
        } finally {
            if( null != fic ) {
                fic.close();
            }
            if( null != fis ) {
                fis.close();
            }
            if( null != bis ) {
                bis.close();
            }
            System.err.printf("%s: mode %-5s, bufferSize %9d: END%n", prefix, srcType.toString(), bufferSize);
            System.err.println();
        }
        if( null != ioe2 || null != oome ) {
            if( null != oome ) {
                System.err.printf("%s: mode %-5s, bufferSize %9d: OutOfMemoryError.2 %s%n",
                        prefix, srcType.toString(), bufferSize, oome.getMessage());
                oome.printStackTrace();
            } else {
                Assert.assertNull(ioe2);
            }
            return false;
        } else {
            return true;
        }
    }

    static void dumpMem(final String pre,
                        final Runtime runtime, final long usedMem0,
                        final long freeMem0, final long[] usedMemN,
                        final long[] freeMemN )
    {
        usedMemN[0] = runtime.totalMemory() - runtime.freeMemory();
        freeMemN[0] = runtime.freeMemory();

        System.err.printf("%s Used Memory  : "+printPrecision, pre, usedMemN[0] / mib);
        if( 0 < usedMem0 ) {
            System.err.printf(", delta "+printPrecision, (usedMemN[0]-usedMem0) / mib);
        }
        System.err.println(" MiB");
        /**
        System.err.printf("%s Free Memory  : "+printPrecision, pre, freeMemN[0] / mib);
        if( 0 < freeMem0 ) {
            System.err.printf(", delta "+printPrecision, (freeMemN[0]-freeMem0) / mib);
        }
        System.err.println(" MiB"); */
    }

    public static void main(final String args[]) throws IOException {
        final String tstname = TestByteBufferInputStream.class.getName();
        org.junit.runner.JUnitCore.main(tstname);
    }
}