Path: blob/master/test/micro/org/openjdk/bench/java/util/ZipFind.java
41161 views
/*1* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/22package org.openjdk.bench.java.util;2324import org.openjdk.jmh.annotations.Benchmark;25import org.openjdk.jmh.annotations.BenchmarkMode;26import org.openjdk.jmh.annotations.Mode;27import org.openjdk.jmh.annotations.OutputTimeUnit;28import org.openjdk.jmh.annotations.Scope;29import org.openjdk.jmh.annotations.Setup;30import org.openjdk.jmh.annotations.State;31import org.openjdk.jmh.annotations.TearDown;32import org.openjdk.jmh.infra.Blackhole;3334import java.io.IOException;35import java.net.URISyntaxException;36import java.util.concurrent.TimeUnit;37import java.util.zip.ZipEntry;38import java.util.zip.ZipFile;3940/**41* Tests ZipFile.getEntry() on the microbenchmarks.jar zip file42*/43@BenchmarkMode(Mode.AverageTime)44@OutputTimeUnit(TimeUnit.NANOSECONDS)45@State(Scope.Thread)46public class ZipFind {4748// Files that exist in the microbenchmarks.jar zip file49public static final String[] existingFiles = {"org/openjdk/bench/java/util/ZipFind.class",50"org/openjdk/bench/vm/lang/Throw.class",51"org/openjdk/bench/java/nio/ByteBuffers.class"};52public static String[] nonExistingFiles = {"/try/to/findme.not", "needle/in/a/HayStack.class"};5354private ZipFile zip;5556@Setup57public void prepare() throws IOException, URISyntaxException {58String zipFile = this.getClass().getProtectionDomain().getCodeSource().getLocation().toURI().getPath();59zip = new ZipFile(zipFile);6061// Verify no typos in the filename lists above62assert zip.getEntry(ZipFind.nonExistingFiles[0]) == null;63assert zip.getEntry(ZipFind.existingFiles[0]) != null;64assert zip.getEntry(ZipFind.existingFiles[1]) != null;65assert zip.getEntry(ZipFind.existingFiles[2]) != null;66}6768@TearDown69public void cleanup() throws IOException {70zip.close();71}7273@Benchmark74public ZipEntry testOneNonExisting() throws IOException {75return zip.getEntry(ZipFind.nonExistingFiles[0]);76}7778@Benchmark79public void testTwoNonExisting(Blackhole bh) throws IOException {80bh.consume(zip.getEntry(nonExistingFiles[0]));81bh.consume(zip.getEntry(nonExistingFiles[1]));82}8384@Benchmark85public void testNonExistingAndExisting(Blackhole bh) throws IOException {86bh.consume(zip.getEntry(nonExistingFiles[0]));87bh.consume(zip.getEntry(existingFiles[0]));88}8990@Benchmark91public ZipEntry testOneExisting() throws IOException {92return zip.getEntry(ZipFind.existingFiles[0]);93}9495@Benchmark96public void testTwoExisting(Blackhole bh) throws IOException {97bh.consume(zip.getEntry(existingFiles[0]));98bh.consume(zip.getEntry(existingFiles[1]));99}100101@Benchmark102public void testThreeExisting(Blackhole bh) throws IOException {103bh.consume(zip.getEntry(existingFiles[0]));104bh.consume(zip.getEntry(existingFiles[1]));105bh.consume(zip.getEntry(existingFiles[2]));106}107}108109110