package com.github.vedenin.perfomace_test;

import com.google.common.base.Charsets;
import com.google.common.io.CharStreams;
import org.apache.commons.io.IOUtils;
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * Created by vvedenin on 2/15/2016.
 *
 *
 */
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MICROSECONDS)
@Warmup(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
@Measurement(iterations = 10, time = 1, timeUnit = TimeUnit.SECONDS)
@Fork(1)
@State(Scope.Benchmark)
public class ConvertInputStreamToStringBenchmark {
    private final static  String test1 = "test184768612876481276487612876417826487216478216784621784672816478216784621784621786478216478216784261784621782178647281647821647821697421687126784621874621786478216478216874";
    private final static  InputStream inputStream = IOUtils.toInputStream(test1, StandardCharsets.UTF_8);

    /*             1. Using ToInputStream of Apache Utils */
    @Benchmark
    public String apacheToInputStream() throws IOException {
        inputStream.mark(0);
        String result = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
        inputStream.reset();
        return result;
    }

    /*             2. Using JDK BufferedReader */
    @Benchmark
    public String jdkBufferedReader() throws IOException {
        inputStream.mark(0);
        BufferedReader str = new BufferedReader(new InputStreamReader(inputStream));
        String result = str.readLine();
        inputStream.reset();
        return result;
    }

    /*             3. Using guava InputStreamReader */
    @Benchmark
    public String guavaReaderInputStream() throws IOException {
        inputStream.mark(0);
        InputStream inputStream = IOUtils.toInputStream(test1, StandardCharsets.UTF_8);
        String result = CharStreams.toString(new InputStreamReader(inputStream, Charsets.UTF_8));
        inputStream.reset();
        return result;
    }

    /*             4. Using JDK Scanner */
    @Benchmark
    public String jdkScanner() throws IOException {
        inputStream.mark(0);
        java.util.Scanner s = new java.util.Scanner(inputStream).useDelimiter("\\A");
        String result = s.hasNext() ? s.next() : "";
        inputStream.reset();
        return result;
    }

    /*             5. Using Java 8 */
    @Benchmark
    public String jdkJava8() throws IOException {
        inputStream.mark(0);
        String result = new BufferedReader(new InputStreamReader(inputStream)).lines().collect(Collectors.joining("\n"));
        inputStream.reset();
        return result;
    }

    /*             6. Using Java 8 */
    @Benchmark
    public String jfkJava8parallel() throws IOException {
        inputStream.mark(0);
        String result = new BufferedReader(new InputStreamReader(inputStream)).lines().parallel().collect(Collectors.joining("\n"));
        inputStream.reset();
        return result;
    }


    public static void main(String[] args) throws Exception {
        ConvertInputStreamToStringBenchmark test = new ConvertInputStreamToStringBenchmark();
        System.out.println();
        System.out.println("apacheToInputStream : " + test.apacheToInputStream());
        System.out.println("jdkBufferedReader : " + test.jdkBufferedReader());
        System.out.println("guavaReaderInputStream : " + test.guavaReaderInputStream());
        System.out.println("jdkScanner : " + test.jdkScanner());
        System.out.println("jdkJava8 : " + test.jdkJava8());
        System.out.println("jfkJava8parallel : " + test.jfkJava8parallel());
        System.out.println();

        Options opt = new OptionsBuilder()
                .include(ConvertInputStreamToStringBenchmark.class.getSimpleName())
                .build();

        new Runner(opt).run();
    }

}

@@ -353,112 +353,9 @@
 .io.
-BufferedReader;%0Aimport java.io.IOException;%0Aimport java.io.InputStream;%0Aimport java.io.InputStreamReader
+*
 ;%0Aim
@@ -514,14 +514,8 @@
 16.%0A
- *%0A *%0A
  */%0A
@@ -827,17 +827,16 @@
  static 
- 
 String t
@@ -1046,17 +1046,16 @@
  static 
- 
 InputStr
@@ -3158,16 +3158,1169 @@
 %0A    %7D%0A%0A
+    /*             7. Using InputStreamReader manually */%0A    @Benchmark%0A    public String manually(final InputStream is, final int bufferSize) throws IOException %7B%0A        inputStream.mark(0);%0A        final char%5B%5D buffer = new char%5BbufferSize%5D;%0A        final StringBuilder out = new StringBuilder();%0A        Reader in = new InputStreamReader(inputStream, %22UTF-8%22);%0A        for (; ; ) %7B%0A            int rsz = in.read(buffer, 0, buffer.length);%0A            if (rsz %3C 0)%0A                break;%0A            out.append(buffer, 0, rsz);%0A        %7D%0A        inputStream.reset();%0A        return out.toString();%0A    %7D%0A%0A    /*             8. Using InputStreamReader manually 2 */%0A    @Benchmark%0A    public String manually2(final InputStream is, final int bufferSize) throws IOException %7B%0A        inputStream.mark(0);%0A        StringBuilder sb = new StringBuilder();%0A        BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));%0A        String read;%0A%0A        while ((read = br.readLine()) != null) %7B%0A            //System.out.println(read);%0A            sb.append(read);%0A        %7D%0A        inputStream.reset();%0A        return sb.toString();%0A    %7D%0A
 %0A    pub

Add new test to ConvertInputStreamToStringBenchmark