1 /*
   2  * Copyright (c) 2011, 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  * @key randomness
  29  */
  30 
  31 import java.util.*;
  32 import java.util.zip.*;
  33 import java.nio.*;
  34 
  35 public class TimeChecksum {
  36 
  37     static long time(Adler32 adler32, byte[] data, int iters, int len) {
  38         long start_t = System.nanoTime();
  39         for (int i = 0; i < iters; i++) {
  40             adler32.reset();
  41             adler32.update(data, 0, len);
  42         }
  43         long t = System.nanoTime() - start_t;
  44         System.out.printf("%,12d", t / len);
  45         return t;
  46     }
  47 
  48     static long time(Adler32 adler32, ByteBuffer buf, int iters) {
  49         long start_t = System.nanoTime();
  50         for (int i = 0; i < iters; i++) {
  51             adler32.reset();
  52             buf.mark();
  53             adler32.update(buf);
  54             buf.reset();
  55         }
  56         long t = System.nanoTime() - start_t;
  57         System.out.printf("%,12d", t / buf.remaining());
  58         return t;
  59     }
  60 
  61     static void testPosLimit(Adler32 adler32, ByteBuffer buf) {
  62         int pos = buf.position();
  63         int limit = buf.limit();
  64         adler32.update(buf);
  65         if (limit != buf.position() || limit != buf.limit()) {
  66             System.out.printf("%nFAILED: pos,limit=(%d, %d), expected (%d, %d)%n",
  67                     buf.position(), buf.limit(), limit, limit);
  68             throw new RuntimeException();
  69         }
  70         buf.position(pos);
  71     }
  72 
  73     static long time(CRC32 crc32, byte[] data, int iters, int len) {
  74         long start_t = System.nanoTime();
  75         for (int i = 0; i < iters; i++) {
  76             crc32.reset();
  77             crc32.update(data, 0, len);
  78         }
  79         long t = System.nanoTime() - start_t;
  80         System.out.printf("%,12d", t / len);
  81         return t;
  82     }
  83 
  84     static long time(CRC32 crc32, ByteBuffer buf, int iters) {
  85         long start_t = System.nanoTime();
  86         for (int i = 0; i < iters; i++) {
  87             crc32.reset();
  88             buf.mark();
  89             crc32.update(buf);
  90             buf.reset();
  91         }
  92         long t = System.nanoTime() - start_t;
  93         System.out.printf("%,12d", t / buf.remaining());
  94         return t;
  95     }
  96 
  97     static void testPosLimit(CRC32 crc32, ByteBuffer buf) {
  98         int pos = buf.position();
  99         int limit = buf.limit();
 100         crc32.update(buf);
 101         if (limit != buf.position() || limit != buf.limit()) {
 102             System.out.printf("%nFAILED: pos,limit=(%d, %d), expected (%d, %d)%n",
 103                     buf.position(), buf.limit(), limit, limit);
 104             throw new RuntimeException();
 105         }
 106         buf.position(pos);
 107     }
 108 
 109     public static void main(String[] args) {
 110         int len     = 1024 * 32;
 111         int iters   = 1;
 112         if (args.length != 0 && "-benchmark".equals(args[0]))
 113             iters = 100000;
 114         Adler32 adler32 = new Adler32();
 115         CRC32 crc32 = new CRC32();
 116         Random rdm = new Random();
 117         byte[] data = new byte[len];
 118         new Random().nextBytes(data);
 119         ByteBuffer buf;
 120 
 121         System.out.println("---------- Adler32 ----------");
 122         System.out.print("Warmup...");
 123         time(adler32, data, iters, len);
 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, ByteBuffer.wrap(data), iters);
 167         buf = ByteBuffer.allocateDirect(len);
 168         buf.put(data, 0, len);
 169         buf.flip();
 170         time(crc32, buf, iters);
 171         System.out.println("\n");
 172 
 173 
 174         System.out.println("Length    byte[](ns/len)  ByteBuffer(direct)   ByteBuffer");
 175         for (int testlen = 1; testlen < data.length; testlen <<= 1) {
 176             System.out.print(testlen + "\t");
 177             long baT = time(crc32, data, iters, testlen);
 178             long baV = crc32.getValue();
 179             System.out.print("\t");
 180 
 181             buf = ByteBuffer.allocateDirect(testlen);
 182             buf.put(data, 0,  testlen);
 183             buf.flip();
 184             long bbdT = time(crc32, buf, iters);
 185             long bbdV = crc32.getValue();
 186             if (baV != bbdV) {
 187                 System.out.printf("%nFAILED: baV=%x,bbdV=%x%n", baV, bbdV);
 188                 throw new RuntimeException();
 189             }
 190             System.out.printf(" (%.2f)", (float)bbdT/baT);
 191             testPosLimit(crc32, buf);
 192 
 193             buf = ByteBuffer.allocate(testlen);
 194             buf.put(data, 0, testlen);
 195             buf.flip();
 196             long bbT = time(crc32, buf, iters);
 197             long bbV = crc32.getValue();
 198             if (baV != bbV) {
 199                 System.out.printf("%nFAILED: baV=%x,bbV=%x%n", baV, bbV);
 200                 throw new RuntimeException();
 201             }
 202             testPosLimit(crc32, buf);
 203             System.out.printf(" (%.2f)     checksum=%x%n", (float)bbT / baT, bbV);
 204         }
 205     }
 206 }