Skip to content

Instantly share code, notes, and snippets.

@Glavo
Created January 20, 2023 16:53
Show Gist options
  • Save Glavo/f3d2060d0bd13cd0ce2add70e6060ea0 to your computer and use it in GitHub Desktop.
Save Glavo/f3d2060d0bd13cd0ce2add70e6060ea0 to your computer and use it in GitHub Desktop.
New NoRepl.java
package org.glavo.jmh;
import com.google.common.jimfs.Jimfs;
import org.openjdk.jmh.annotations.*;
import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileSystem;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Random;
import java.util.concurrent.TimeUnit;
@Warmup(iterations = 10, time = 3)
@Measurement(iterations = 5, time = 5)
@Fork(value = 1, jvmArgsAppend = {"-XX:+UseG1GC", "-Xms8g", "-Xmx8g", "--add-opens=java.base/jdk.internal.access=ALL-UNNAMED"})
@BenchmarkMode(Mode.Throughput)
@OutputTimeUnit(TimeUnit.SECONDS)
@State(Scope.Benchmark)
public class NoRepl {
private static final Charset GBK = Charset.forName("GBK");
@Param({"0", "1024", "8192", "1048576", "33554432", "268435456"})
private int length;
private FileSystem fs;
private Path asciiFile;
private Path utf8File;
private Path gbkFile;
@Setup
public void setup() throws IOException {
fs = Jimfs.newFileSystem();
asciiFile = fs.getPath("ascii.txt");
utf8File = fs.getPath("utf8.txt");
gbkFile = fs.getPath("gbk.txt");
Random random = new Random(0);
try (OutputStream os = new BufferedOutputStream(Files.newOutputStream(asciiFile))) {
for (int i = 0; i < length; i += 8) {
os.write(random.nextInt(128));
}
}
try (BufferedWriter utf8 = Files.newBufferedWriter(utf8File, StandardCharsets.UTF_8);
BufferedWriter gbk = Files.newBufferedWriter(gbkFile, GBK)
) {
for (int i = 0; i < length; i++) {
char ch = (i % 1024) == 1023 ? (char) (random.nextInt(0x9fa5 - 0x4e00) + 0x4e00) : (char) random.nextInt(128);
utf8.write(ch);
gbk.write(ch);
}
}
}
@TearDown
public void cleanup() throws IOException {
fs.close();
}
@Benchmark
public String testReadAscii() throws IOException {
return Files.readString(asciiFile);
}
@Benchmark
public String testReadUTF8() throws IOException {
return Files.readString(utf8File);
}
@Benchmark
public String testReadAsciiAsGBK() throws IOException {
return Files.readString(asciiFile, GBK);
}
@Benchmark
public String testReadGBK() throws IOException {
return Files.readString(gbkFile, GBK);
}
}
@Glavo
Copy link
Author

Glavo commented Jan 30, 2023

Raw output based on default file system and larger data set:

baseline:

Benchmark                  (length)   Mode  Cnt       Score      Error  Units
NoRepl.testReadAscii         131072  thrpt    5  220365.805 ±  644.422  ops/s
NoRepl.testReadAscii         524288  thrpt    5   77850.837 ± 2130.763  ops/s
NoRepl.testReadAsciiAsGBK    131072  thrpt    5  219264.629 ± 2669.046  ops/s
NoRepl.testReadAsciiAsGBK    524288  thrpt    5   73105.384 ± 2648.063  ops/s
NoRepl.testReadGBK           131072  thrpt    5    5160.713 ±  105.865  ops/s
NoRepl.testReadGBK           524288  thrpt    5    1624.485 ±  428.108  ops/s
NoRepl.testReadUTF8          131072  thrpt    5    8354.071 ±   41.698  ops/s
NoRepl.testReadUTF8          524288  thrpt    5    2228.112 ±    5.720  ops/s

Benchmark                   (length)   Mode  Cnt      Score     Error  Units
NoRepl.testReadAscii         1048576  thrpt    5  39168.398 ± 771.909  ops/s
NoRepl.testReadAscii         4194304  thrpt    5   9726.926 ± 156.112  ops/s
NoRepl.testReadAscii        16777216  thrpt    5   2175.106 ±  19.886  ops/s
NoRepl.testReadAscii        67108864  thrpt    5    241.081 ±   1.439  ops/s
NoRepl.testReadAscii       134217728  thrpt    5    102.803 ±  32.349  ops/s
NoRepl.testReadAscii       268435456  thrpt    5     43.697 ±   0.175  ops/s
NoRepl.testReadAsciiAsGBK    1048576  thrpt    5  38680.741 ± 350.153  ops/s
NoRepl.testReadAsciiAsGBK    4194304  thrpt    5  10059.130 ± 138.710  ops/s
NoRepl.testReadAsciiAsGBK   16777216  thrpt    5   2157.955 ±   7.787  ops/s
NoRepl.testReadAsciiAsGBK   67108864  thrpt    5    255.092 ±   1.171  ops/s
NoRepl.testReadAsciiAsGBK  134217728  thrpt    5     99.015 ±  37.743  ops/s
NoRepl.testReadAsciiAsGBK  268435456  thrpt    5     43.694 ±   0.216  ops/s
NoRepl.testReadGBK           1048576  thrpt    5    866.536 ±   4.715  ops/s
NoRepl.testReadGBK           4194304  thrpt    5    174.772 ±   0.742  ops/s
NoRepl.testReadGBK          16777216  thrpt    5     39.330 ±   0.267  ops/s
NoRepl.testReadGBK          67108864  thrpt    5      9.790 ±   0.039  ops/s
NoRepl.testReadGBK         134217728  thrpt    5      4.873 ±   0.005  ops/s
NoRepl.testReadGBK         268435456  thrpt    5      2.461 ±   0.004  ops/s
NoRepl.testReadUTF8          1048576  thrpt    5   1059.954 ±   1.749  ops/s
NoRepl.testReadUTF8          4194304  thrpt    5    197.105 ±   0.580  ops/s
NoRepl.testReadUTF8         16777216  thrpt    5     43.228 ±   0.310  ops/s
NoRepl.testReadUTF8         67108864  thrpt    5     10.815 ±   0.034  ops/s
NoRepl.testReadUTF8        134217728  thrpt    5      5.410 ±   0.027  ops/s
NoRepl.testReadUTF8        268435456  thrpt    5      2.686 ±   0.006  ops/s

modified:

Benchmark                  (length)   Mode  Cnt       Score      Error  Units
NoRepl.testReadAscii         131072  thrpt    5  285557.208 ± 1037.651  ops/s
NoRepl.testReadAscii         524288  thrpt    5  111886.807 ±  363.270  ops/s
NoRepl.testReadAsciiAsGBK    131072  thrpt    5  286840.481 ± 1631.883  ops/s
NoRepl.testReadAsciiAsGBK    524288  thrpt    5  109689.015 ± 1216.165  ops/s
NoRepl.testReadGBK           131072  thrpt    5    5026.595 ±   11.297  ops/s
NoRepl.testReadGBK           524288  thrpt    5    1694.855 ±  149.394  ops/s
NoRepl.testReadUTF8          131072  thrpt    5    8313.073 ±  112.802  ops/s
NoRepl.testReadUTF8          524288  thrpt    5    2220.887 ±   12.909  ops/s

Benchmark                   (length)   Mode  Cnt      Score     Error  Units
NoRepl.testReadAscii         1048576  thrpt    5  59731.545 ± 651.235  ops/s
NoRepl.testReadAscii         4194304  thrpt    5  14694.939 ± 494.959  ops/s
NoRepl.testReadAscii        16777216  thrpt    5   3116.609 ±   6.074  ops/s
NoRepl.testReadAscii        67108864  thrpt    5    286.299 ±   0.577  ops/s
NoRepl.testReadAscii       134217728  thrpt    5    131.249 ±   0.976  ops/s
NoRepl.testReadAscii       268435456  thrpt    5     49.976 ±   0.216  ops/s
NoRepl.testReadAsciiAsGBK    1048576  thrpt    5  60314.341 ± 590.243  ops/s
NoRepl.testReadAsciiAsGBK    4194304  thrpt    5  14885.889 ± 255.933  ops/s
NoRepl.testReadAsciiAsGBK   16777216  thrpt    5   3094.683 ±  24.947  ops/s
NoRepl.testReadAsciiAsGBK   67108864  thrpt    5    284.211 ±   2.515  ops/s
NoRepl.testReadAsciiAsGBK  134217728  thrpt    5    128.779 ±  21.609  ops/s
NoRepl.testReadAsciiAsGBK  268435456  thrpt    5     50.047 ±   0.157  ops/s
NoRepl.testReadGBK           1048576  thrpt    5    866.181 ±   3.865  ops/s
NoRepl.testReadGBK           4194304  thrpt    5    176.129 ±   0.450  ops/s
NoRepl.testReadGBK          16777216  thrpt    5     39.169 ±   0.041  ops/s
NoRepl.testReadGBK          67108864  thrpt    5      9.801 ±   0.014  ops/s
NoRepl.testReadGBK         134217728  thrpt    5      4.905 ±   0.010  ops/s
NoRepl.testReadGBK         268435456  thrpt    5      2.461 ±   0.005  ops/s
NoRepl.testReadUTF8          1048576  thrpt    5   1056.155 ±   6.970  ops/s
NoRepl.testReadUTF8          4194304  thrpt    5    198.556 ±   0.778  ops/s
NoRepl.testReadUTF8         16777216  thrpt    5     43.406 ±   0.137  ops/s
NoRepl.testReadUTF8         67108864  thrpt    5     10.755 ±   0.046  ops/s
NoRepl.testReadUTF8        134217728  thrpt    5      5.399 ±   0.009  ops/s
NoRepl.testReadUTF8        268435456  thrpt    5      2.709 ±   0.001  ops/s

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment