1 /*
   2  * Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 
  25 /* @test
  26  * @bug 7109837
  27  * @summary Test Adler32/CRC32.update(ByteBuffer)
  28  */
  29 
  30 import java.util.*;
  31 import java.util.zip.*;
  32 import java.nio.*;
  33 
  34 public class TimeChecksum {
  35 
  36     static long time(Adler32 adler32, byte[] data, int iters, int len) {
  37         long start_t = System.nanoTime();
  38         for (int i = 0; i < iters; i++) {
  39             adler32.reset();
  40             adler32.update(data, 0, len);
  41         }
  42         long t = System.nanoTime() - start_t;
  43         System.out.printf("%,12d", t / len);
  44         return t;
  45     }
  46 
  47     static long time(Adler32 adler32, ByteBuffer buf, int iters) {
  48         long start_t = System.nanoTime();
  49         for (int i = 0; i < iters; i++) {
  50             adler32.reset();
  51             buf.mark();
  52             adler32.update(buf);
  53             buf.reset();
  54         }
  55         long t = System.nanoTime() - start_t;
  56         System.out.printf("%,12d", t / buf.remaining());
  57         return t;
  58     }
  59 
  60     static void testPosLimit(Adler32 adler32, ByteBuffer buf) {
  61         int pos = buf.position();
  62         int limit = buf.limit();
  63         adler32.update(buf);
  64         if (limit != buf.position() || limit != buf.limit()) {
  65             System.out.printf("%nFAILED: pos,limit=(%d, %d), expected (%d, %d)%n",
  66                     buf.position(), buf.limit(), limit, limit);
  67             throw new RuntimeException();
  68         }
  69         buf.position(pos);
  70     }
  71 
  72     static long time(CRC32 crc32, byte[] data, int iters, int len) {
  73         long start_t = System.nanoTime();
  74         for (int i = 0; i < iters; i++) {
  75             crc32.reset();
  76             crc32.update(data, 0, len);
  77         }
  78         long t = System.nanoTime() - start_t;
  79         System.out.printf("%,12d", t / len);
  80         return t;
  81     }
  82 
  83     static long time(CRC32 crc32, ByteBuffer buf, int iters) {
  84         long start_t = System.nanoTime();
  85         for (int i = 0; i < iters; i++) {
  86             crc32.reset();
  87             buf.mark();
  88             crc32.update(buf);
  89             buf.reset();
  90         }
  91         long t = System.nanoTime() - start_t;
  92         System.out.printf("%,12d", t / buf.remaining());
  93         return t;
  94     }
  95 
  96     static void testPosLimit(CRC32 crc32, ByteBuffer buf) {
  97         int pos = buf.position();
  98         int limit = buf.limit();
  99         crc32.update(buf);
 100         if (limit != buf.position() || limit != buf.limit()) {
 101             System.out.printf("%nFAILED: pos,limit=(%d, %d), expected (%d, %d)%n",
 102                     buf.position(), buf.limit(), limit, limit);
 103             throw new RuntimeException();
 104         }
 105         buf.position(pos);
 106     }
 107 
 108     public static void main(String[] args) {
 109         int len     = 1024 * 32;
 110         int iters   = 1;
 111         if (args.length != 0 && "-benchmark".equals(args[0]))
 112             iters = 100000;
 113         Adler32 adler32 = new Adler32();
 114         CRC32 crc32 = new CRC32();
 115         Random rdm = new Random();
 116         byte[] data = new byte[len];
 117         new Random().nextBytes(data);
 118         ByteBuffer buf;
 119 
 120         System.out.println("---------- Adler32 ----------");
 121         System.out.print("Warmup...");
 122         time(adler32, data, iters, len);
 123         time(adler32, data, 2*iters, 16); // warmup short case, too
 124         time(adler32, ByteBuffer.wrap(data), iters);
 125         buf = ByteBuffer.allocateDirect(len);
 126         buf.put(data, 0, len);
 127         buf.flip();
 128         time(adler32, buf, iters);
 129         System.out.println("\n");
 130 
 131         System.out.println("Length    byte[](ns/len)  ByteBuffer(direct)   ByteBuffer");
 132         for (int testlen = 1; testlen < data.length; testlen <<= 1) {
 133             System.out.print(testlen + "\t");
 134             long baT = time(adler32, data, iters, testlen);
 135             long baV = adler32.getValue();
 136             System.out.print("\t");
 137 
 138             buf = ByteBuffer.allocateDirect(testlen);
 139             buf.put(data, 0, testlen);
 140             buf.flip();
 141             long bbdT = time(adler32, buf, iters);
 142             long bbdV = adler32.getValue();
 143             if (baV != bbdV) {
 144                 System.out.printf("%nFAILED: baV=%x,bbdV=%x%n", baV, bbdV);
 145                 throw new RuntimeException();
 146             }
 147             System.out.printf(" (%.2f)", (float)bbdT/baT);
 148             testPosLimit(adler32, buf);
 149 
 150             buf = ByteBuffer.allocate(testlen);
 151             buf.put(data, 0, testlen);
 152             buf.flip();
 153             long bbT = time(adler32, buf, iters);
 154             long bbV = adler32.getValue();
 155             if (baV != bbV) {
 156                 System.out.printf("%nFAILED: baV=%x,bbV=%x%n", baV, bbV);
 157                 throw new RuntimeException();
 158             }
 159             testPosLimit(adler32, buf);
 160             System.out.printf(" (%.2f)     checksum=%x%n", (float)bbT/baT, bbV);
 161         }
 162 
 163         System.out.println("\n---------- CRC32 ----------");
 164         System.out.print("Warmup...");
 165         time(crc32, data, iters, len);
 166         time(crc32, data, 2*iters, 16); // warmup short case, too
 167         time(crc32, ByteBuffer.wrap(data), iters);
 168         buf = ByteBuffer.allocateDirect(len);
 169         buf.put(data, 0, len);
 170         buf.flip();
 171         time(crc32, buf, iters);
 172         System.out.println("\n");
 173 
 174 
 175         System.out.println("Length    byte[](ns/len)  ByteBuffer(direct)   ByteBuffer");
 176         for (int testlen = 1; testlen < data.length; testlen <<= 1) {
 177             System.out.print(testlen + "\t");
 178             long baT = time(crc32, data, iters, testlen);
 179             long baV = crc32.getValue();
 180             System.out.print("\t");
 181 
 182             buf = ByteBuffer.allocateDirect(testlen);
 183             buf.put(data, 0,  testlen);
 184             buf.flip();
 185             long bbdT = time(crc32, buf, iters);
 186             long bbdV = crc32.getValue();
 187             if (baV != bbdV) {
 188                 System.out.printf("%nFAILED: baV=%x,bbdV=%x%n", baV, bbdV);
 189                 throw new RuntimeException();
 190             }
 191             System.out.printf(" (%.2f)", (float)bbdT/baT);
 192             testPosLimit(crc32, buf);
 193 
 194             buf = ByteBuffer.allocate(testlen);
 195             buf.put(data, 0, testlen);
 196             buf.flip();
 197             long bbT = time(crc32, buf, iters);
 198             long bbV = crc32.getValue();
 199             if (baV != bbV) {
 200                 System.out.printf("%nFAILED: baV=%x,bbV=%x%n", baV, bbV);
 201                 throw new RuntimeException();
 202             }
 203             testPosLimit(crc32, buf);
 204             System.out.printf(" (%.2f)     checksum=%x%n", (float)bbT / baT, bbV);
 205         }
 206     }
 207 }