1 /* 2 * Copyright (c) 2005, 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 6348045 27 * @summary GZipOutputStream/InputStream goes critical(calls JNI_Get*Critical) 28 * and causes slowness. This test uses Deflater and Inflater directly. 29 * @key randomness 30 */ 31 32 import java.io.*; 33 import java.nio.*; 34 import java.util.*; 35 import java.util.zip.*; 36 37 /** 38 * This test runs Inflater and Defalter in a number of simultaneous threads, 39 * validating that the deflated & then inflated data matches the original 40 * data. 41 */ 42 public class FlaterTest extends Thread { 43 private static final int DATA_LEN = 1024 * 128; 44 private static byte[] data; 45 46 // If true, print extra info. 47 private static final boolean debug = false; 48 49 // Set of Flater threads running. 50 private static Set flaters = 51 Collections.synchronizedSet(new HashSet()); 52 53 /** Fill in {@code data} with random values. */ createData()54 static void createData() { 55 ByteBuffer bb = ByteBuffer.allocate(8); 56 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 57 for (int i = 0; i < DATA_LEN; i++) { 58 bb.putDouble(0, Math.random()); 59 baos.write(bb.array(), 0, 8); 60 } 61 data = baos.toByteArray(); 62 if (debug) System.out.println("data length is " + data.length); 63 } 64 65 /** @return the length of the deflated {@code data}. */ getDeflatedLength()66 private static int getDeflatedLength() throws Throwable { 67 int rc = 0; 68 Deflater deflater = new Deflater(); 69 deflater.setInput(data); 70 deflater.finish(); 71 byte[] out = new byte[data.length]; 72 rc = deflater.deflate(out); 73 deflater.end(); 74 if (debug) System.out.println("deflatedLength is " + rc); 75 return rc; 76 } 77 78 /** Compares given bytes with those in {@code data}. 79 * @throws Exception if given bytes don't match {@code data}. 80 */ validate(byte[] buf, int offset, int len)81 private static void validate(byte[] buf, int offset, int len) throws Exception { 82 for (int i = 0; i < len; i++ ) { 83 if (buf[i] != data[offset+i]) { 84 throw new Exception("mismatch at " + (offset + i)); 85 } 86 } 87 } 88 realMain(String[] args)89 public static void realMain(String[] args) throws Throwable { 90 createData(); 91 int numThreads = args.length > 0 ? Integer.parseInt(args[0]) : 5; 92 new FlaterTest().go(numThreads); 93 } 94 go(int numThreads)95 synchronized private void go(int numThreads) throws Throwable { 96 int deflatedLength = getDeflatedLength(); 97 98 long time = System.currentTimeMillis(); 99 for (int i = 0; i < numThreads; i++) { 100 Flater f = new Flater(deflatedLength); 101 flaters.add(f); 102 f.start(); 103 } 104 while (flaters.size() != 0) { 105 try { 106 Thread.currentThread().sleep(10); 107 } catch (InterruptedException ex) { 108 unexpected(ex); 109 } 110 } 111 time = System.currentTimeMillis() - time; 112 System.out.println("Time needed for " + numThreads 113 + " threads to deflate/inflate: " + time + " ms."); 114 } 115 116 /** Deflates and inflates data. */ 117 static class Flater extends Thread { 118 private final int deflatedLength; 119 Flater(int length)120 private Flater(int length) { 121 this.deflatedLength = length; 122 } 123 124 /** Deflates and inflates {@code data}. */ run()125 public void run() { 126 if (debug) System.out.println(getName() + " starting run()"); 127 try { 128 byte[] deflated = DeflateData(deflatedLength); 129 InflateData(deflated); 130 } catch (Throwable t) { 131 t.printStackTrace(); 132 fail(getName() + " failed"); 133 } finally { 134 flaters.remove(this); 135 } 136 } 137 138 /** Returns a copy of {@code data} in deflated form. */ DeflateData(int length)139 private byte[] DeflateData(int length) throws Throwable { 140 Deflater deflater = new Deflater(); 141 deflater.setInput(data); 142 deflater.finish(); 143 byte[] out = new byte[length]; 144 deflater.deflate(out); 145 return out; 146 } 147 148 /** Inflate a byte array, comparing it with {@code data} during 149 * inflation. 150 * @throws Exception if inflated bytes don't match {@code data}. 151 */ InflateData(byte[] bytes)152 private void InflateData(byte[] bytes) throws Throwable { 153 Inflater inflater = new Inflater(); 154 inflater.setInput(bytes, 0, bytes.length); 155 int len = 1024 * 8; 156 int offset = 0; 157 while (inflater.getRemaining() > 0) { 158 byte[] buf = new byte[len]; 159 int inflated = inflater.inflate(buf, 0, len); 160 validate(buf, offset, inflated); 161 offset += inflated; 162 } 163 } 164 } 165 166 //--------------------- Infrastructure --------------------------- 167 static volatile int passed = 0, failed = 0; pass()168 static void pass() {passed++;} fail()169 static void fail() {failed++; Thread.dumpStack();} fail(String msg)170 static void fail(String msg) {System.out.println(msg); fail();} unexpected(Throwable t)171 static void unexpected(Throwable t) {failed++; t.printStackTrace();} check(boolean cond)172 static void check(boolean cond) {if (cond) pass(); else fail();} equal(Object x, Object y)173 static void equal(Object x, Object y) { 174 if (x == null ? y == null : x.equals(y)) pass(); 175 else fail(x + " not equal to " + y);} main(String[] args)176 public static void main(String[] args) throws Throwable { 177 try {realMain(args);} catch (Throwable t) {unexpected(t);} 178 System.out.println("\nPassed = " + passed + " failed = " + failed); 179 if (failed > 0) throw new AssertionError("Some tests failed");} 180 } 181