KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sleepycat > je > util > Adler32Test


1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2002,2006 Oracle. All rights reserved.
5  *
6  * $Id: Adler32Test.java,v 1.11 2006/10/30 21:14:53 bostic Exp $
7  */

8
9 package com.sleepycat.je.util;
10
11 import java.util.Random JavaDoc;
12 import java.util.zip.Checksum JavaDoc;
13
14 import junit.framework.TestCase;
15
16 public class Adler32Test extends TestCase {
17
18     static private int N_ITERS = 1000;
19
20     public void testRandomAdler32ByteArray() {
21     Checksum JavaDoc javaChecksum = new java.util.zip.Adler32 JavaDoc();
22     Checksum JavaDoc jeChecksum = new com.sleepycat.je.utilint.Adler32();
23     Checksum JavaDoc chunkingChecksum =
24         new com.sleepycat.je.utilint.Adler32.ChunkingAdler32(128);
25     Random JavaDoc rnd = new Random JavaDoc();
26     for (int i = 0; i < N_ITERS; i++) {
27         int nBytes = rnd.nextInt(65535);
28         byte[] b = new byte[nBytes];
29         rnd.nextBytes(b);
30         javaChecksum.reset();
31         jeChecksum.reset();
32         chunkingChecksum.reset();
33         javaChecksum.update(b, 0, nBytes);
34         jeChecksum.update(b, 0, nBytes);
35         chunkingChecksum.update(b, 0, nBytes);
36         assertEquals(javaChecksum.getValue(), jeChecksum.getValue());
37         assertEquals(javaChecksum.getValue(), chunkingChecksum.getValue());
38     }
39     }
40
41     public void xtestRandomAdler32ByteArrayPerformance() {
42     Checksum JavaDoc javaChecksum = new java.util.zip.Adler32 JavaDoc();
43     Checksum JavaDoc jeChecksum = new com.sleepycat.je.utilint.Adler32();
44     Random JavaDoc rnd = new Random JavaDoc();
45     byte[][] baa = new byte[N_ITERS][];
46     int[] lengths = new int[N_ITERS];
47     long totalBytes = 0;
48     for (int i = 0; i < N_ITERS; i++) {
49         int nBytes = rnd.nextInt(65535);
50         byte[] b = new byte[nBytes];
51         baa[i] = b;
52         lengths[i] = nBytes;
53         totalBytes += nBytes;
54         rnd.nextBytes(b);
55     }
56     long jeChecksumTime =
57         measureChecksum(baa, lengths, jeChecksum, false);
58     long javaChecksumTime =
59         measureChecksum(baa, lengths, javaChecksum, false);
60     long jeChecksumTimeByteAtATime =
61         measureChecksum(baa, lengths, jeChecksum, true);
62     long javaChecksumTimeByteAtATime =
63         measureChecksum(baa, lengths, javaChecksum, true);
64     System.out.println(N_ITERS + " Iterations, " +
65                totalBytes + " bytes:\n " +
66                javaChecksumTime + " millis. for java\n" +
67                jeChecksumTime + " millis. for je\n" +
68                javaChecksumTimeByteAtATime +
69                " millis. for java byte at a time\n" +
70                jeChecksumTimeByteAtATime +
71                " millis. for je byte at a time");
72     }
73
74     private long measureChecksum(byte[][] baa,
75                  int[] lengths,
76                  Checksum JavaDoc cksum,
77                  boolean byteAtATime) {
78     long startTime = System.currentTimeMillis();
79     for (int i = 0; i < N_ITERS; i++) {
80         byte[] b = baa[i];
81         int len = lengths[i];
82         cksum.reset();
83         if (byteAtATime) {
84         for (int j = 0; j < len; j++) {
85             cksum.update(b[j]);
86         }
87         } else {
88         cksum.update(b, 0, len);
89         }
90     }
91     long endTime = System.currentTimeMillis();
92     return (endTime - startTime);
93     }
94
95     public void testRandomAdler32SingleBytes() {
96     Checksum JavaDoc javaChecksum = new java.util.zip.Adler32 JavaDoc();
97     Checksum JavaDoc jeChecksum = new com.sleepycat.je.utilint.Adler32();
98     Random JavaDoc rnd = new Random JavaDoc();
99     for (int i = 0; i < N_ITERS; i++) {
100         int nBytes = rnd.nextInt(65535);
101         javaChecksum.reset();
102         jeChecksum.reset();
103         for (int j = 0; j < nBytes; j++) {
104         byte b = (byte) (rnd.nextInt(256) & 0xff);
105         javaChecksum.update(b);
106         jeChecksum.update(b);
107         }
108         assertEquals(javaChecksum.getValue(), jeChecksum.getValue());
109     }
110     }
111 }
112
Popular Tags