(original) (raw)
/* * Copyright (c) 2017, Oracle America, Inc. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * * Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * * Neither the name of Oracle nor the names of its contributors may be used * to endorse or promote products derived from this software without * specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF * THE POSSIBILITY OF SUCH DAMAGE. */ package org.openjdk; import org.openjdk.jmh.annotations.*; import org.openjdk.jmh.infra.Blackhole; import java.io.*; import java.util.*; import java.util.concurrent.*; import java.util.jar.*; import java.util.stream.*; import java.util.zip.*; import java.time.*; @State(Scope.Benchmark) @BenchmarkMode(Mode.AverageTime) //@BenchmarkMode(Mode.Throughput) @OutputTimeUnit(TimeUnit.MILLISECONDS) @Warmup(iterations = 10, time = 1000, timeUnit = TimeUnit.MILLISECONDS) @Measurement(iterations = 10, time = 1000, timeUnit = TimeUnit.MILLISECONDS) @Fork(2) public class ZipCodingBM { static private String jf_name = "/tmp/test.jar"; @Setup public void setup() throws Throwable {} @Benchmark @Group("zf_stream") @GroupThreads(1) public void ZF_stream(ZipfileState s, Blackhole bh) throws IOException { s.zf.stream().forEach(e -> bh.consume(e)); } @Benchmark @Group("zf_entries") @GroupThreads(1) public void ZF_entries(ZipfileState s, Blackhole bh) throws IOException { Enumeration entries = s.zf.entries(); while (entries.hasMoreElements()) { bh.consume(entries.nextElement()); } } @Benchmark @Group("zf_getEntry") @GroupThreads(1) public void JF_getEntry(ZipfileState s, Blackhole bh) throws IOException { for (String name : s.entriesStr) { bh.consume(s.zf.getEntry(name)); } } /* @Benchmark @Group("zf_getInputStream") @GroupThreads(1) public void JF_getInputStream(ZipfileState s, Blackhole bh) throws IOException { for (ZipEntry e : s.entries) { try ( InputStream is = s.zf.getInputStream(e)) { bh.consume(is); } } } */ @Benchmark @Group("jf_stream") @GroupThreads(1) public void JF_stream(JarfileState s, Blackhole bh) throws IOException { s.jf.stream().forEach(e -> bh.consume(e)); } @Benchmark @Group("jf_entries") @GroupThreads(1) public void JF_entries(JarfileState s, Blackhole bh) throws IOException { Enumeration entries = s.jf.entries(); while (entries.hasMoreElements()) { bh.consume(entries.nextElement()); } } @Benchmark @Group("jf_getEntry") @GroupThreads(1) public void JF_getEntry(JarfileState s, Blackhole bh) throws IOException { for (String name : s.entriesStr) { bh.consume(s.jf.getEntry(name)); } } /* @Benchmark @Group("jf_versionedStream") @GroupThreads(1) public void JF_versionedStream(JarfileState s, Blackhole bh) throws IOException { bh.consume(s.jf.versionedStream().collect(Collectors.toList())); } */ @State(Scope.Group) public static class ZipfileState { ZipFile zf = init(); List entriesStr; List entries; private ZipFile init() { try { ZipFile zf = new ZipFile(new File(jf_name)); this.entries = zf.stream().collect(Collectors.toList()); this.entriesStr = zf.stream().map(ZipEntry::getName).collect(Collectors.toList()); return zf; } catch (IOException x) { x.printStackTrace(); } return null; } } @State(Scope.Group) public static class JarfileState { JarFile jf = init(); List entriesStr; List entries; private JarFile init() { try { JarFile jf = new JarFile(new File(jf_name)); // JarFile jf = new JarFile(new File(jf_name), false, ZipFile.OPEN_READ, Runtime.version()); this.entries = jf.stream().collect(Collectors.toList()); this.entriesStr = jf.stream().map(JarEntry::getName).collect(Collectors.toList()); return jf; } catch (IOException x) { x.printStackTrace(); } return null; } } }