1 /*
2  * Copyright (c) 2006, 2011, Oracle and/or its affiliates. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  *   - Redistributions of source code must retain the above copyright
9  *     notice, this list of conditions and the following disclaimer.
10  *
11  *   - Redistributions in binary form must reproduce the above copyright
12  *     notice, this list of conditions and the following disclaimer in the
13  *     documentation and/or other materials provided with the distribution.
14  *
15  *   - Neither the name of Oracle nor the names of its
16  *     contributors may be used to endorse or promote products derived
17  *     from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
20  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /*
33  * This source code is provided to illustrate the usage of a given feature
34  * or technique and has been deliberately simplified. Additional steps
35  * required for a production-quality application, such as security checks,
36  * input validation and proper error handling, might not be present in
37  * this sample code.
38  */
39 
40 
41 package j2dbench.tests.iio;
42 
43 import java.io.File;
44 import java.io.IOException;
45 import java.io.FileOutputStream;
46 import java.io.OutputStream;
47 import javax.imageio.ImageIO;
48 import javax.imageio.stream.ImageInputStream;
49 
50 import j2dbench.Group;
51 import j2dbench.Result;
52 import j2dbench.TestEnvironment;
53 
54 abstract class InputStreamTests extends InputTests {
55 
56     private static Group streamRoot;
57     private static Group streamTestRoot;
58 
init()59     public static void init() {
60         streamRoot = new Group(inputRoot, "stream",
61                                "Image Stream Benchmarks");
62         streamTestRoot = new Group(streamRoot, "tests",
63                                    "ImageInputStream Tests");
64 
65         new IISConstruct();
66         new IISRead();
67         new IISReadByteArray();
68         new IISReadFullyByteArray();
69         new IISReadBit();
70         new IISReadByte();
71         new IISReadUnsignedByte();
72         new IISReadShort();
73         new IISReadUnsignedShort();
74         new IISReadInt();
75         new IISReadUnsignedInt();
76         new IISReadFloat();
77         new IISReadLong();
78         new IISReadDouble();
79         new IISSkipBytes();
80     }
81 
InputStreamTests(Group parent, String nodeName, String description)82     protected InputStreamTests(Group parent,
83                                String nodeName, String description)
84     {
85         super(parent, nodeName, description);
86         addDependency(generalSourceRoot);
87         addDependencies(imageioGeneralOptRoot, true);
88     }
89 
cleanupTest(TestEnvironment env, Object ctx)90     public void cleanupTest(TestEnvironment env, Object ctx) {
91         Context iioctx = (Context)ctx;
92         iioctx.cleanup(env);
93     }
94 
95     private static class Context extends InputTests.Context {
96         ImageInputStream inputStream;
97         int scanlineStride; // width of a scanline (in bytes)
98         int length; // length of the entire stream (in bytes)
99         byte[] byteBuf;
100 
Context(TestEnvironment env, Result result)101         Context(TestEnvironment env, Result result) {
102             super(env, result);
103 
104             // 4 bytes per "pixel"
105             scanlineStride = size * 4;
106 
107             // tack on an extra 4 bytes, so that in the 1x1 case we can
108             // call readLong() or readDouble() without hitting EOF
109             length = (scanlineStride * size) + 4;
110 
111             // big enough for one scanline
112             byteBuf = new byte[scanlineStride];
113 
114             initInput();
115 
116             try {
117                 inputStream = createImageInputStream();
118             } catch (IOException e) {
119                 System.err.println("Error creating ImageInputStream");
120             }
121         }
122 
initContents(File f)123         void initContents(File f) throws IOException {
124             FileOutputStream fos = null;
125             try {
126                 fos = new FileOutputStream(f);
127                 initContents(fos);
128             } finally {
129                 fos.close();
130             }
131         }
132 
initContents(OutputStream out)133         void initContents(OutputStream out) throws IOException {
134             for (int i = 0; i < size; i++) {
135                 out.write(byteBuf);
136             }
137             out.write(new byte[4]); // add the 4 byte pad
138             out.flush();
139         }
140 
cleanup(TestEnvironment env)141         void cleanup(TestEnvironment env) {
142             super.cleanup(env);
143             if (inputStream != null) {
144                 try {
145                     inputStream.close();
146                 } catch (IOException e) {
147                     System.err.println("error closing stream");
148                 }
149                 inputStream = null;
150             }
151         }
152     }
153 
154     private static class IISConstruct extends InputStreamTests {
IISConstruct()155         public IISConstruct() {
156             super(streamTestRoot,
157                   "construct",
158                   "Construct");
159         }
160 
initTest(TestEnvironment env, Result result)161         public Object initTest(TestEnvironment env, Result result) {
162             Context ctx = new Context(env, result);
163             result.setUnits(1);
164             result.setUnitName("stream");
165             return ctx;
166         }
167 
runTest(Object ctx, int numReps)168         public void runTest(Object ctx, int numReps) {
169             final Context ictx = (Context)ctx;
170             try {
171                 do {
172                     ImageInputStream iis = ictx.createImageInputStream();
173                     iis.close();
174                     ictx.closeOriginalStream();
175                 } while (--numReps >= 0);
176             } catch (IOException e) {
177                 e.printStackTrace();
178             }
179         }
180     }
181 
182     private static class IISRead extends InputStreamTests {
IISRead()183         public IISRead() {
184             super(streamTestRoot,
185                   "read",
186                   "read()");
187         }
188 
initTest(TestEnvironment env, Result result)189         public Object initTest(TestEnvironment env, Result result) {
190             Context ctx = new Context(env, result);
191             result.setUnits(1);
192             result.setUnitName("byte");
193             return ctx;
194         }
195 
runTest(Object ctx, int numReps)196         public void runTest(Object ctx, int numReps) {
197             final Context ictx = (Context)ctx;
198             final ImageInputStream iis = ictx.inputStream;
199             final int length = ictx.length;
200             int pos = 0;
201             try {
202                 iis.mark();
203                 do {
204                     if (pos >= length) {
205                         iis.reset();
206                         iis.mark();
207                         pos = 0;
208                     }
209                     iis.read();
210                     pos++;
211                 } while (--numReps >= 0);
212             } catch (IOException e) {
213                 e.printStackTrace();
214             } finally {
215                 try { iis.reset(); } catch (IOException e) {}
216             }
217         }
218     }
219 
220     private static class IISReadByteArray extends InputStreamTests {
IISReadByteArray()221         public IISReadByteArray() {
222             super(streamTestRoot,
223                   "readByteArray",
224                   "read(byte[]) (one \"scanline\" at a time)");
225         }
226 
initTest(TestEnvironment env, Result result)227         public Object initTest(TestEnvironment env, Result result) {
228             Context ctx = new Context(env, result);
229             result.setUnits(ctx.scanlineStride);
230             result.setUnitName("byte");
231             return ctx;
232         }
233 
runTest(Object ctx, int numReps)234         public void runTest(Object ctx, int numReps) {
235             final Context ictx = (Context)ctx;
236             final ImageInputStream iis = ictx.inputStream;
237             final byte[] buf = ictx.byteBuf;
238             final int scanlineStride = ictx.scanlineStride;
239             final int length = ictx.length;
240             int pos = 0;
241             try {
242                 iis.mark();
243                 do {
244                     if (pos + scanlineStride > length) {
245                         iis.reset();
246                         iis.mark();
247                         pos = 0;
248                     }
249                     iis.read(buf);
250                     pos += scanlineStride;
251                 } while (--numReps >= 0);
252             } catch (IOException e) {
253                 e.printStackTrace();
254             } finally {
255                 try { iis.reset(); } catch (IOException e) {}
256             }
257         }
258     }
259 
260     private static class IISReadFullyByteArray extends InputStreamTests {
IISReadFullyByteArray()261         public IISReadFullyByteArray() {
262             super(streamTestRoot,
263                   "readFullyByteArray",
264                   "readFully(byte[]) (one \"scanline\" at a time)");
265         }
266 
initTest(TestEnvironment env, Result result)267         public Object initTest(TestEnvironment env, Result result) {
268             Context ctx = new Context(env, result);
269             result.setUnits(ctx.scanlineStride);
270             result.setUnitName("byte");
271             return ctx;
272         }
273 
runTest(Object ctx, int numReps)274         public void runTest(Object ctx, int numReps) {
275             final Context ictx = (Context)ctx;
276             final ImageInputStream iis = ictx.inputStream;
277             final byte[] buf = ictx.byteBuf;
278             final int scanlineStride = ictx.scanlineStride;
279             final int length = ictx.length;
280             int pos = 0;
281             try {
282                 iis.mark();
283                 do {
284                     if (pos + scanlineStride > length) {
285                         iis.reset();
286                         iis.mark();
287                         pos = 0;
288                     }
289                     iis.readFully(buf);
290                     pos += scanlineStride;
291                 } while (--numReps >= 0);
292             } catch (IOException e) {
293                 e.printStackTrace();
294             } finally {
295                 try { iis.reset(); } catch (IOException e) {}
296             }
297         }
298     }
299 
300     private static class IISReadBit extends InputStreamTests {
IISReadBit()301         public IISReadBit() {
302             super(streamTestRoot,
303                   "readBit",
304                   "readBit()");
305         }
306 
initTest(TestEnvironment env, Result result)307         public Object initTest(TestEnvironment env, Result result) {
308             Context ctx = new Context(env, result);
309             result.setUnits(1);
310             result.setUnitName("bit");
311             return ctx;
312         }
313 
runTest(Object ctx, int numReps)314         public void runTest(Object ctx, int numReps) {
315             final Context ictx = (Context)ctx;
316             final ImageInputStream iis = ictx.inputStream;
317             final int length = ictx.length * 8;
318             int pos = 0;
319             try {
320                 iis.mark();
321                 do {
322                     if (pos >= length) {
323                         iis.reset();
324                         iis.mark();
325                         pos = 0;
326                     }
327                     iis.readBit();
328                     pos++;
329                 } while (--numReps >= 0);
330             } catch (IOException e) {
331                 e.printStackTrace();
332             } finally {
333                 try { iis.reset(); } catch (IOException e) {}
334             }
335         }
336     }
337 
338     private static class IISReadByte extends InputStreamTests {
IISReadByte()339         public IISReadByte() {
340             super(streamTestRoot,
341                   "readByte",
342                   "readByte()");
343         }
344 
initTest(TestEnvironment env, Result result)345         public Object initTest(TestEnvironment env, Result result) {
346             Context ctx = new Context(env, result);
347             result.setUnits(1);
348             result.setUnitName("byte");
349             return ctx;
350         }
351 
runTest(Object ctx, int numReps)352         public void runTest(Object ctx, int numReps) {
353             final Context ictx = (Context)ctx;
354             final ImageInputStream iis = ictx.inputStream;
355             final int length = ictx.length;
356             int pos = 0;
357             try {
358                 iis.mark();
359                 do {
360                     if (pos >= length) {
361                         iis.reset();
362                         iis.mark();
363                         pos = 0;
364                     }
365                     iis.readByte();
366                     pos++;
367                 } while (--numReps >= 0);
368             } catch (IOException e) {
369                 e.printStackTrace();
370             } finally {
371                 try { iis.reset(); } catch (IOException e) {}
372             }
373         }
374     }
375 
376     private static class IISReadUnsignedByte extends InputStreamTests {
IISReadUnsignedByte()377         public IISReadUnsignedByte() {
378             super(streamTestRoot,
379                   "readUnsignedByte",
380                   "readUnsignedByte()");
381         }
382 
initTest(TestEnvironment env, Result result)383         public Object initTest(TestEnvironment env, Result result) {
384             Context ctx = new Context(env, result);
385             result.setUnits(1);
386             result.setUnitName("byte");
387             return ctx;
388         }
389 
runTest(Object ctx, int numReps)390         public void runTest(Object ctx, int numReps) {
391             final Context ictx = (Context)ctx;
392             final ImageInputStream iis = ictx.inputStream;
393             final int length = ictx.length;
394             int pos = 0;
395             try {
396                 iis.mark();
397                 do {
398                     if (pos >= length) {
399                         iis.reset();
400                         iis.mark();
401                         pos = 0;
402                     }
403                     iis.readUnsignedByte();
404                     pos++;
405                 } while (--numReps >= 0);
406             } catch (IOException e) {
407                 e.printStackTrace();
408             } finally {
409                 try { iis.reset(); } catch (IOException e) {}
410             }
411         }
412     }
413 
414     private static class IISReadShort extends InputStreamTests {
IISReadShort()415         public IISReadShort() {
416             super(streamTestRoot,
417                   "readShort",
418                   "readShort()");
419         }
420 
initTest(TestEnvironment env, Result result)421         public Object initTest(TestEnvironment env, Result result) {
422             Context ctx = new Context(env, result);
423             result.setUnits(2);
424             result.setUnitName("byte");
425             return ctx;
426         }
427 
runTest(Object ctx, int numReps)428         public void runTest(Object ctx, int numReps) {
429             final Context ictx = (Context)ctx;
430             final ImageInputStream iis = ictx.inputStream;
431             final int length = ictx.length;
432             int pos = 0;
433             try {
434                 iis.mark();
435                 do {
436                     if (pos + 2 > length) {
437                         iis.reset();
438                         iis.mark();
439                         pos = 0;
440                     }
441                     iis.readShort();
442                     pos += 2;
443                 } while (--numReps >= 0);
444             } catch (IOException e) {
445                 e.printStackTrace();
446             } finally {
447                 try { iis.reset(); } catch (IOException e) {}
448             }
449         }
450     }
451 
452     private static class IISReadUnsignedShort extends InputStreamTests {
IISReadUnsignedShort()453         public IISReadUnsignedShort() {
454             super(streamTestRoot,
455                   "readUnsignedShort",
456                   "readUnsignedShort()");
457         }
458 
initTest(TestEnvironment env, Result result)459         public Object initTest(TestEnvironment env, Result result) {
460             Context ctx = new Context(env, result);
461             result.setUnits(2);
462             result.setUnitName("byte");
463             return ctx;
464         }
465 
runTest(Object ctx, int numReps)466         public void runTest(Object ctx, int numReps) {
467             final Context ictx = (Context)ctx;
468             final ImageInputStream iis = ictx.inputStream;
469             final int length = ictx.length;
470             int pos = 0;
471             try {
472                 iis.mark();
473                 do {
474                     if (pos + 2 > length) {
475                         iis.reset();
476                         iis.mark();
477                         pos = 0;
478                     }
479                     iis.readUnsignedShort();
480                     pos += 2;
481                 } while (--numReps >= 0);
482             } catch (IOException e) {
483                 e.printStackTrace();
484             } finally {
485                 try { iis.reset(); } catch (IOException e) {}
486             }
487         }
488     }
489 
490     private static class IISReadInt extends InputStreamTests {
IISReadInt()491         public IISReadInt() {
492             super(streamTestRoot,
493                   "readInt",
494                   "readInt()");
495         }
496 
initTest(TestEnvironment env, Result result)497         public Object initTest(TestEnvironment env, Result result) {
498             Context ctx = new Context(env, result);
499             result.setUnits(4);
500             result.setUnitName("byte");
501             return ctx;
502         }
503 
runTest(Object ctx, int numReps)504         public void runTest(Object ctx, int numReps) {
505             final Context ictx = (Context)ctx;
506             final ImageInputStream iis = ictx.inputStream;
507             final int length = ictx.length;
508             int pos = 0;
509             try {
510                 iis.mark();
511                 do {
512                     if (pos + 4 > length) {
513                         iis.reset();
514                         iis.mark();
515                         pos = 0;
516                     }
517                     iis.readInt();
518                     pos += 4;
519                 } while (--numReps >= 0);
520             } catch (IOException e) {
521                 e.printStackTrace();
522             } finally {
523                 try { iis.reset(); } catch (IOException e) {}
524             }
525         }
526     }
527 
528     private static class IISReadUnsignedInt extends InputStreamTests {
IISReadUnsignedInt()529         public IISReadUnsignedInt() {
530             super(streamTestRoot,
531                   "readUnsignedInt",
532                   "readUnsignedInt()");
533         }
534 
initTest(TestEnvironment env, Result result)535         public Object initTest(TestEnvironment env, Result result) {
536             Context ctx = new Context(env, result);
537             result.setUnits(4);
538             result.setUnitName("byte");
539             return ctx;
540         }
541 
runTest(Object ctx, int numReps)542         public void runTest(Object ctx, int numReps) {
543             final Context ictx = (Context)ctx;
544             final ImageInputStream iis = ictx.inputStream;
545             final int length = ictx.length;
546             int pos = 0;
547             try {
548                 iis.mark();
549                 do {
550                     if (pos + 4 > length) {
551                         iis.reset();
552                         iis.mark();
553                         pos = 0;
554                     }
555                     iis.readUnsignedInt();
556                     pos += 4;
557                 } while (--numReps >= 0);
558             } catch (IOException e) {
559                 e.printStackTrace();
560             } finally {
561                 try { iis.reset(); } catch (IOException e) {}
562             }
563         }
564     }
565 
566     private static class IISReadFloat extends InputStreamTests {
IISReadFloat()567         public IISReadFloat() {
568             super(streamTestRoot,
569                   "readFloat",
570                   "readFloat()");
571         }
572 
initTest(TestEnvironment env, Result result)573         public Object initTest(TestEnvironment env, Result result) {
574             Context ctx = new Context(env, result);
575             result.setUnits(4);
576             result.setUnitName("byte");
577             return ctx;
578         }
579 
runTest(Object ctx, int numReps)580         public void runTest(Object ctx, int numReps) {
581             final Context ictx = (Context)ctx;
582             final ImageInputStream iis = ictx.inputStream;
583             final int length = ictx.length;
584             int pos = 0;
585             try {
586                 iis.mark();
587                 do {
588                     if (pos + 4 > length) {
589                         iis.reset();
590                         iis.mark();
591                         pos = 0;
592                     }
593                     iis.readFloat();
594                     pos += 4;
595                 } while (--numReps >= 0);
596             } catch (IOException e) {
597                 e.printStackTrace();
598             } finally {
599                 try { iis.reset(); } catch (IOException e) {}
600             }
601         }
602     }
603 
604     private static class IISReadLong extends InputStreamTests {
IISReadLong()605         public IISReadLong() {
606             super(streamTestRoot,
607                   "readLong",
608                   "readLong()");
609         }
610 
initTest(TestEnvironment env, Result result)611         public Object initTest(TestEnvironment env, Result result) {
612             Context ctx = new Context(env, result);
613             result.setUnits(8);
614             result.setUnitName("byte");
615             return ctx;
616         }
617 
runTest(Object ctx, int numReps)618         public void runTest(Object ctx, int numReps) {
619             final Context ictx = (Context)ctx;
620             final ImageInputStream iis = ictx.inputStream;
621             final int length = ictx.length;
622             int pos = 0;
623             try {
624                 iis.mark();
625                 do {
626                     if (pos + 8 > length) {
627                         iis.reset();
628                         iis.mark();
629                         pos = 0;
630                     }
631                     iis.readLong();
632                     pos += 8;
633                 } while (--numReps >= 0);
634             } catch (IOException e) {
635                 e.printStackTrace();
636             } finally {
637                 try { iis.reset(); } catch (IOException e) {}
638             }
639         }
640     }
641 
642     private static class IISReadDouble extends InputStreamTests {
IISReadDouble()643         public IISReadDouble() {
644             super(streamTestRoot,
645                   "readDouble",
646                   "readDouble()");
647         }
648 
initTest(TestEnvironment env, Result result)649         public Object initTest(TestEnvironment env, Result result) {
650             Context ctx = new Context(env, result);
651             result.setUnits(8);
652             result.setUnitName("byte");
653             return ctx;
654         }
655 
runTest(Object ctx, int numReps)656         public void runTest(Object ctx, int numReps) {
657             final Context ictx = (Context)ctx;
658             final ImageInputStream iis = ictx.inputStream;
659             final int length = ictx.length;
660             int pos = 0;
661             try {
662                 iis.mark();
663                 do {
664                     if (pos + 8 > length) {
665                         iis.reset();
666                         iis.mark();
667                         pos = 0;
668                     }
669                     iis.readDouble();
670                     pos += 8;
671                 } while (--numReps >= 0);
672             } catch (IOException e) {
673                 e.printStackTrace();
674             } finally {
675                 try { iis.reset(); } catch (IOException e) {}
676             }
677         }
678     }
679 
680     private static class IISSkipBytes extends InputStreamTests {
IISSkipBytes()681         public IISSkipBytes() {
682             super(streamTestRoot,
683                   "skipBytes",
684                   "skipBytes() (one \"scanline\" at a time)");
685         }
686 
initTest(TestEnvironment env, Result result)687         public Object initTest(TestEnvironment env, Result result) {
688             Context ctx = new Context(env, result);
689             result.setUnits(ctx.scanlineStride);
690             result.setUnitName("byte");
691             return ctx;
692         }
693 
runTest(Object ctx, int numReps)694         public void runTest(Object ctx, int numReps) {
695             final Context ictx = (Context)ctx;
696             final ImageInputStream iis = ictx.inputStream;
697             final int scanlineStride = ictx.scanlineStride;
698             final int length = ictx.length;
699             int pos = 0;
700             try {
701                 iis.mark();
702                 do {
703                     if (pos + scanlineStride > length) {
704                         iis.reset();
705                         iis.mark();
706                         pos = 0;
707                     }
708                     iis.skipBytes(scanlineStride);
709                     pos += scanlineStride;
710                 } while (--numReps >= 0);
711             } catch (IOException e) {
712                 e.printStackTrace();
713             } finally {
714                 try { iis.reset(); } catch (IOException e) {}
715             }
716         }
717     }
718 }
719