jdk/test/java/util/zip/FlaterTest.java
author martin
Tue, 15 Sep 2015 21:56:04 -0700
changeset 32649 2ee9017c7597
parent 30046 cf2c86e1819e
permissions -rw-r--r--
8136583: Core libraries should use blessed modifier order Summary: Run blessed-modifier-order script (see bug) Reviewed-by: psandoz, chegar, alanb, plevart
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 6348045
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary GZipOutputStream/InputStream goes critical(calls JNI_Get*Critical)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * and causes slowness.  This test uses Deflater and Inflater directly.
30046
cf2c86e1819e 8078334: Mark regression tests using randomness
darcy
parents: 5506
diff changeset
    29
 * @key randomness
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.nio.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.zip.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * This test runs Inflater and Defalter in a number of simultaneous threads,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * validating that the deflated & then inflated data matches the original
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
public class FlaterTest extends Thread {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    private static final int DATA_LEN = 1024 * 128;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    private static byte[] data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    // If true, print extra info.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private static final boolean debug = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    // Set of Flater threads running.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private static Set flaters =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        Collections.synchronizedSet(new HashSet());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    /** Fill in {@code data} with random values. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    static void createData() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        ByteBuffer bb = ByteBuffer.allocate(8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        for (int i = 0; i < DATA_LEN; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            bb.putDouble(0, Math.random());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            baos.write(bb.array(), 0, 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        data = baos.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        if (debug) System.out.println("data length is " + data.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    /** @return the length of the deflated {@code data}. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    private static int getDeflatedLength() throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        int rc = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        Deflater deflater = new Deflater();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        deflater.setInput(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        deflater.finish();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        byte[] out = new byte[data.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        rc = deflater.deflate(out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        deflater.end();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        if (debug) System.out.println("deflatedLength is " + rc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        return rc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    /** Compares given bytes with those in {@code data}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * @throws Exception if given bytes don't match {@code data}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    private static void validate(byte[] buf, int offset, int len) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        for (int i = 0; i < len; i++ ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            if (buf[i] != data[offset+i]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                throw new Exception("mismatch at " + (offset + i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    public static void realMain(String[] args) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        createData();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        int numThreads = args.length > 0 ? Integer.parseInt(args[0]) : 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        new FlaterTest().go(numThreads);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 30046
diff changeset
    95
    private synchronized void go(int numThreads) throws Throwable {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        int deflatedLength = getDeflatedLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        long time = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        for (int i = 0; i < numThreads; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            Flater f = new Flater(deflatedLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            flaters.add(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            f.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        while (flaters.size() != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                Thread.currentThread().sleep(10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            } catch (InterruptedException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                unexpected(ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        time = System.currentTimeMillis() - time;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        System.out.println("Time needed for " + numThreads
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                           + " threads to deflate/inflate: " + time + " ms.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    /** Deflates and inflates data. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    static class Flater extends Thread {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        private final int deflatedLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        private Flater(int length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            this.deflatedLength = length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        /** Deflates and inflates {@code data}. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            if (debug) System.out.println(getName() + " starting run()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                byte[] deflated = DeflateData(deflatedLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                InflateData(deflated);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            } catch (Throwable t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                t.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                fail(getName() + " failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                flaters.remove(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        /** Returns a copy of {@code data} in deflated form. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        private byte[] DeflateData(int length) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            Deflater deflater = new Deflater();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            deflater.setInput(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            deflater.finish();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            byte[] out = new byte[length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            deflater.deflate(out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            return out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        /** Inflate a byte array, comparing it with {@code data} during
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
         * inflation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
         * @throws Exception if inflated bytes don't match {@code data}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        private void InflateData(byte[] bytes) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            Inflater inflater = new Inflater();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            inflater.setInput(bytes, 0, bytes.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            int len = 1024 * 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            int offset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            while (inflater.getRemaining() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                byte[] buf = new byte[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                int inflated = inflater.inflate(buf, 0, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                validate(buf, offset, inflated);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                offset += inflated;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    //--------------------- Infrastructure ---------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    static volatile int passed = 0, failed = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    static void pass() {passed++;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    static void fail() {failed++; Thread.dumpStack();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    static void fail(String msg) {System.out.println(msg); fail();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    static void unexpected(Throwable t) {failed++; t.printStackTrace();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    static void check(boolean cond) {if (cond) pass(); else fail();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    static void equal(Object x, Object y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        if (x == null ? y == null : x.equals(y)) pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        else fail(x + " not equal to " + y);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    public static void main(String[] args) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        try {realMain(args);} catch (Throwable t) {unexpected(t);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        System.out.println("\nPassed = " + passed + " failed = " + failed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        if (failed > 0) throw new AssertionError("Some tests failed");}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
}