-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Benchmarking #5
Comments
@Fork(4)
@State(Scope.Benchmark)
@BenchmarkMode(Mode.AverageTime)
@Warmup(iterations = 30, time = 1, timeUnit = TimeUnit.SECONDS)
@Measurement(iterations = 4, time = 1, timeUnit = TimeUnit.SECONDS)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
public static abstract class BaseLineReadingBenchmark {
@Param({})
String path;
Path nioPath;
@Setup(Level.Trial)
public void setUp() {
nioPath = Paths.get(path);
}
}
public static class LineReaderBenchmark extends BaseLineReadingBenchmark {
@Benchmark
public void read(Blackhole bh) {
new LineReader(nioPath).subscribe(new LineSubscriber(bh));
}
@Benchmark
public void lineCount(Blackhole bh) {
new LineReader(nioPath).subscribe(new LineCountSubscriber(bh));
}
private static class LineSubscriber implements Subscriber<ByteBuffer> {
private Blackhole blackhole;
LineSubscriber(Blackhole blackhole) {
this.blackhole = blackhole;
}
@Override
public void onSubscribe(Subscription s) {
s.request(Long.MAX_VALUE);
}
@Override
public void onNext(ByteBuffer line) {
blackhole.consume(new String(line.array(), line.position(), line.remaining(), UTF_8));
}
@Override
public void onError(Throwable e) {
throw new RuntimeException(e);
}
@Override
public void onComplete() {
}
}
private static class LineCountSubscriber implements Subscriber<ByteBuffer> {
private int count = 0;
private Blackhole blackhole;
LineCountSubscriber(Blackhole blackhole) {
this.blackhole = blackhole;
}
@Override
public void onSubscribe(Subscription s) {
s.request(Long.MAX_VALUE);
}
@Override
public void onNext(ByteBuffer line) {
++count;
}
@Override
public void onError(Throwable e) {
throw new RuntimeException(e);
}
@Override
public void onComplete() {
blackhole.consume(count);
}
}
}
public static class DefaultReadBenchmark extends BaseLineReadingBenchmark {
@Benchmark
public void read(Blackhole bh) throws IOException {
try (Stream<String> lines = Files.lines(nioPath, UTF_8)) {
lines.forEach(bh::consume);
}
}
@Benchmark
public void parallelRead(Blackhole bh) throws IOException {
try (Stream<String> lines = Files.lines(nioPath, UTF_8).parallel()) {
lines.forEach(bh::consume);
}
}
@Benchmark
public void lineCount(Blackhole bh) throws IOException {
try (Stream<String> lines = Files.lines(nioPath, UTF_8)) {
bh.consume(lines.count());
}
}
@Benchmark
public void parallelLineCount(Blackhole bh) throws IOException {
try (Stream<String> lines = Files.lines(nioPath, UTF_8).parallel()) {
bh.consume(lines.count());
}
}
} |
LineReaderBenchmark.read 8MB.txt avgt 16 50.193 ± 6.121 ms/op |
Write benchmarks
The text was updated successfully, but these errors were encountered: