Skip to content
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

Closed
Alexey911 opened this issue Feb 20, 2018 · 3 comments
Closed

Benchmarking #5

Alexey911 opened this issue Feb 20, 2018 · 3 comments

Comments

@Alexey911
Copy link
Owner

Write benchmarks

@Alexey911
Copy link
Owner Author

    @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());
            }
        }
    }

@Alexey911
Copy link
Owner Author

@Alexey911
Copy link
Owner Author

LineReaderBenchmark.read 8MB.txt avgt 16 50.193 ± 6.121 ms/op
LineReaderBenchmark.lineCount 8MB.txt avgt 16 12.625 ± 0.351 ms/op
DefaultReadBenchmark.read 8MB.txt avgt 16 49.078 ± 1.741 ms/op
DefaultReadBenchmark.parallelRead 8MB.txt avgt 16 23.462 ± 2.501 ms/op
DefaultReadBenchmark.lineCount 8MB.txt avgt 16 48.354 ± 1.890 ms/op
DefaultReadBenchmark.parallelLineCount 8MB.txt avgt 16 24.316 ± 0.696 ms/op

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant