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 28, 2023

Source code based on the default file system and small data set test:

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 = 5)
@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", "8", "16", "64", "256", "1024", "4096", "8192", "16384", "32768"})
    private int length;

    private Path dir;
    private Path asciiFile;
    private Path utf8File;
    private Path gbkFile;

    @Setup
    public void setup() throws IOException {
        dir = Files.createTempDirectory("no-repl");
        asciiFile = dir.resolve("ascii.txt");
        utf8File = dir.resolve("utf8.txt");
        gbkFile = dir.resolve("gbk.txt");

        dir.toFile().deleteOnExit();
        asciiFile.toFile().deleteOnExit();
        utf8File.toFile().deleteOnExit();
        gbkFile.toFile().deleteOnExit();

        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 % 32) == 31 ? (char) (random.nextInt(0x9fa5 - 0x4e00) + 0x4e00) : (char) random.nextInt(128);
                utf8.write(ch);
                gbk.write(ch);
            }
        }

        System.gc();
    }

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

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

baseline:

Benchmark                  (length)   Mode  Cnt       Score       Error  Units
NoRepl.testReadAscii              0  thrpt    5  716720.089 ±  8536.310  ops/s
NoRepl.testReadAscii              8  thrpt    5  592476.116 ± 14125.014  ops/s
NoRepl.testReadAscii             16  thrpt    5  582528.853 ±  6159.972  ops/s
NoRepl.testReadAscii             64  thrpt    5  589621.310 ±  5097.578  ops/s
NoRepl.testReadAscii            256  thrpt    5  599236.673 ±  5505.055  ops/s
NoRepl.testReadAscii           1024  thrpt    5  583457.373 ± 10240.942  ops/s
NoRepl.testReadAscii           4096  thrpt    5  562265.613 ±  3219.950  ops/s
NoRepl.testReadAscii           8192  thrpt    5  547554.137 ±  5745.101  ops/s
NoRepl.testReadAscii          16384  thrpt    5  505770.276 ±  3073.659  ops/s
NoRepl.testReadAscii          32768  thrpt    5  431693.980 ±  3457.742  ops/s
NoRepl.testReadAsciiAsGBK         0  thrpt    5  713061.563 ± 11507.739  ops/s
NoRepl.testReadAsciiAsGBK         8  thrpt    5  591090.654 ±  9862.053  ops/s
NoRepl.testReadAsciiAsGBK        16  thrpt    5  587519.063 ±  4759.056  ops/s
NoRepl.testReadAsciiAsGBK        64  thrpt    5  595402.930 ±  2019.954  ops/s
NoRepl.testReadAsciiAsGBK       256  thrpt    5  595395.558 ±  1988.105  ops/s
NoRepl.testReadAsciiAsGBK      1024  thrpt    5  578259.942 ±  3859.072  ops/s
NoRepl.testReadAsciiAsGBK      4096  thrpt    5  571341.717 ±  3173.850  ops/s
NoRepl.testReadAsciiAsGBK      8192  thrpt    5  551055.856 ±  6850.659  ops/s
NoRepl.testReadAsciiAsGBK     16384  thrpt    5  510949.500 ±  6346.436  ops/s
NoRepl.testReadAsciiAsGBK     32768  thrpt    5  426452.894 ±  1997.349  ops/s
NoRepl.testReadGBK                0  thrpt    5  718428.987 ± 10971.884  ops/s
NoRepl.testReadGBK                8  thrpt    5  593682.490 ±  4527.449  ops/s
NoRepl.testReadGBK               16  thrpt    5  583829.879 ± 10896.120  ops/s
NoRepl.testReadGBK               64  thrpt    5  564323.206 ±  5974.289  ops/s
NoRepl.testReadGBK              256  thrpt    5  501984.713 ±  2141.020  ops/s
NoRepl.testReadGBK             1024  thrpt    5  281786.766 ±   907.779  ops/s
NoRepl.testReadGBK             4096  thrpt    5  109481.383 ±   550.744  ops/s
NoRepl.testReadGBK             8192  thrpt    5   59906.137 ±   236.851  ops/s
NoRepl.testReadGBK            16384  thrpt    5   29725.112 ±   204.746  ops/s
NoRepl.testReadGBK            32768  thrpt    5   19002.301 ±   147.860  ops/s
NoRepl.testReadUTF8               0  thrpt    5  712744.775 ±  8867.438  ops/s
NoRepl.testReadUTF8               8  thrpt    5  596196.425 ±  9773.696  ops/s
NoRepl.testReadUTF8              16  thrpt    5  591454.409 ±  7282.650  ops/s
NoRepl.testReadUTF8              64  thrpt    5  576840.214 ±  3040.402  ops/s
NoRepl.testReadUTF8             256  thrpt    5  530280.372 ±  6683.797  ops/s
NoRepl.testReadUTF8            1024  thrpt    5  398253.072 ±  3697.028  ops/s
NoRepl.testReadUTF8            4096  thrpt    5  193374.282 ±  1071.726  ops/s
NoRepl.testReadUTF8            8192  thrpt    5  114967.242 ±   431.236  ops/s
NoRepl.testReadUTF8           16384  thrpt    5   63657.563 ±   268.171  ops/s
NoRepl.testReadUTF8           32768  thrpt    5   33547.135 ±   314.328  ops/s

modified:

Benchmark                  (length)   Mode  Cnt       Score       Error  Units
NoRepl.testReadAscii              0  thrpt    5  717736.303 ±  5106.144  ops/s
NoRepl.testReadAscii              8  thrpt    5  599792.634 ±  5291.150  ops/s
NoRepl.testReadAscii             16  thrpt    5  606480.861 ±  1472.627  ops/s
NoRepl.testReadAscii             64  thrpt    5  603872.536 ±  6148.860  ops/s
NoRepl.testReadAscii            256  thrpt    5  601047.723 ±  2662.518  ops/s
NoRepl.testReadAscii           1024  thrpt    5  591191.354 ±  6525.391  ops/s
NoRepl.testReadAscii           4096  thrpt    5  583393.897 ±  3202.580  ops/s
NoRepl.testReadAscii           8192  thrpt    5  574149.337 ±  7496.823  ops/s
NoRepl.testReadAscii          16384  thrpt    5  544501.800 ±  3816.059  ops/s
NoRepl.testReadAscii          32768  thrpt    5  468009.406 ±  3104.555  ops/s
NoRepl.testReadAsciiAsGBK         0  thrpt    5  718961.843 ±  9664.163  ops/s
NoRepl.testReadAsciiAsGBK         8  thrpt    5  592755.203 ±  7937.871  ops/s
NoRepl.testReadAsciiAsGBK        16  thrpt    5  604915.587 ±  4610.796  ops/s
NoRepl.testReadAsciiAsGBK        64  thrpt    5  598320.817 ±  3536.197  ops/s
NoRepl.testReadAsciiAsGBK       256  thrpt    5  599326.539 ±  7904.934  ops/s
NoRepl.testReadAsciiAsGBK      1024  thrpt    5  598116.396 ±  5540.122  ops/s
NoRepl.testReadAsciiAsGBK      4096  thrpt    5  581913.379 ±  2684.014  ops/s
NoRepl.testReadAsciiAsGBK      8192  thrpt    5  585002.690 ±  7239.988  ops/s
NoRepl.testReadAsciiAsGBK     16384  thrpt    5  537742.167 ±  2838.062  ops/s
NoRepl.testReadAsciiAsGBK     32768  thrpt    5  489237.834 ±  5328.158  ops/s
NoRepl.testReadGBK                0  thrpt    5  718179.225 ±  8376.668  ops/s
NoRepl.testReadGBK                8  thrpt    5  598905.099 ±  3050.929  ops/s
NoRepl.testReadGBK               16  thrpt    5  599530.113 ±  5353.034  ops/s
NoRepl.testReadGBK               64  thrpt    5  570715.297 ±  2146.081  ops/s
NoRepl.testReadGBK              256  thrpt    5  504726.449 ±  4239.458  ops/s
NoRepl.testReadGBK             1024  thrpt    5  287657.044 ±  1070.992  ops/s
NoRepl.testReadGBK             4096  thrpt    5  110020.755 ±   648.338  ops/s
NoRepl.testReadGBK             8192  thrpt    5   58029.180 ±   531.967  ops/s
NoRepl.testReadGBK            16384  thrpt    5   29696.930 ±   166.140  ops/s
NoRepl.testReadGBK            32768  thrpt    5   19037.812 ±   160.922  ops/s
NoRepl.testReadUTF8               0  thrpt    5  716624.447 ±  6654.713  ops/s
NoRepl.testReadUTF8               8  thrpt    5  597160.011 ±  5794.799  ops/s
NoRepl.testReadUTF8              16  thrpt    5  594713.120 ± 11686.177  ops/s
NoRepl.testReadUTF8              64  thrpt    5  573242.989 ±  2137.696  ops/s
NoRepl.testReadUTF8             256  thrpt    5  532918.897 ±  3420.021  ops/s
NoRepl.testReadUTF8            1024  thrpt    5  393563.497 ±  2630.083  ops/s
NoRepl.testReadUTF8            4096  thrpt    5  193383.603 ±   174.806  ops/s
NoRepl.testReadUTF8            8192  thrpt    5  114977.638 ±   181.358  ops/s
NoRepl.testReadUTF8           16384  thrpt    5   61438.528 ±   171.593  ops/s
NoRepl.testReadUTF8           32768  thrpt    5   33895.971 ±   128.546  ops/s

@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