Created
August 6, 2017 10:55
-
-
Save mmimica/9074ab03fa111e1ef3f3cb7e862eff78 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.mmimica; | |
import java.io.*; | |
import java.util.concurrent.TimeUnit; | |
import java.util.zip.ZipEntry; | |
import java.util.zip.ZipOutputStream; | |
import org.openjdk.jmh.annotations.*; | |
import org.openjdk.jmh.infra.Blackhole; | |
@State(Scope.Benchmark) | |
@BenchmarkMode(Mode.Throughput) | |
@Warmup(iterations = 1, time = 1, timeUnit = TimeUnit.SECONDS) | |
@Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS) | |
@Fork(1) | |
public class ZipBenchmark { | |
private final ByteArrayOutputStream baos = new ByteArrayOutputStream(100); | |
@Param({ "1", "3", "6", "9" }) | |
int level; | |
@Setup | |
public void setup() throws FileNotFoundException, IOException { | |
byte[] buffer = new byte[1024]; | |
try (FileInputStream in = new FileInputStream("data.dat")) { | |
int len; | |
while ((len = in.read(buffer)) > 0) { | |
baos.write(buffer, 0, len); | |
} | |
} | |
} | |
@Benchmark | |
public void systemZlib(Blackhole bh) throws FileNotFoundException, IOException { | |
byte[] buffer = new byte[1024]; | |
try (OutputStream fos = new NullOutputStream(bh); | |
ZipOutputStream zos = new ZipOutputStream(fos); | |
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray())) { | |
ZipEntry ze = new ZipEntry("data.dat"); | |
zos.putNextEntry(ze); | |
zos.setLevel(level); | |
int len; | |
while ((len = bais.read(buffer)) > 0) { | |
zos.write(buffer, 0, len); | |
} | |
zos.closeEntry(); | |
} | |
} | |
private static class NullOutputStream extends OutputStream { | |
private final Blackhole hole; | |
public NullOutputStream(Blackhole hole) { | |
this.hole = hole; | |
} | |
@Override | |
public void write(int b) throws IOException { | |
hole.consume(b); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment