-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
ConvertBigStringToInputStreamBenchmark.java
72 lines (62 loc) · 2.53 KB
/
ConvertBigStringToInputStreamBenchmark.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package other_examples;
import com.google.common.io.CharSource;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.input.ReaderInputStream;
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeUnit;
/**
* Created by vvedenin on 2/15/2016.
*/
@State(Scope.Benchmark)
public class ConvertBigStringToInputStreamBenchmark {
private static String test1;
/* 1. Using ToInputStream of Apache Utils */
@Benchmark
public InputStream apacheToInputStream() throws IOException {
return IOUtils.toInputStream(test1, StandardCharsets.UTF_8);
}
/* 2. Using JDK */
@Benchmark
public InputStream jdkByteArrayInputStream() throws IOException {
return new ByteArrayInputStream(test1.getBytes(StandardCharsets.UTF_8));
}
/* 3. Using ReaderInputStream of Apache Utils */
@Benchmark
public InputStream apacheReaderInputStream() throws IOException {
return new ReaderInputStream(CharSource.wrap(test1).openStream());
}
/* 4. Using Apache Utils and InputStreamReader*/
@Benchmark
public InputStream apacheInputStreamReader() throws IOException {
return IOUtils.toInputStream(test1);
}
@TearDown(Level.Iteration)
public void tearDown() {
String test = "test184768612876481276487612876417826487216478216784621784672816478216784621784621786478216478216784261784621782178647281647821647821697421687126784621874621786478216478216874";
StringBuilder builder = new StringBuilder(test);
for(int i = 0; i< 1000; i++) {
builder.append(test);
}
test1 = builder.toString();
System.out.println(test1.length());
}
public static void main(String[] args) throws RunnerException {
Options opt = new OptionsBuilder()
.include(ConvertStringToInputStreamBenchmark.class.getSimpleName())
.timeUnit(TimeUnit.MICROSECONDS)
.warmupIterations(5)
.measurementIterations(50)
.forks(1)
.mode(Mode.AverageTime)
.build();
new Runner(opt).run();
}
}