Skip to content

Instantly share code, notes, and snippets.

@Glavo
Created January 11, 2023 12:53
Show Gist options
  • Save Glavo/0aa47d47f329ceabf7dd4c3b9d2848e2 to your computer and use it in GitHub Desktop.
Save Glavo/0aa47d47f329ceabf7dd4c3b9d2848e2 to your computer and use it in GitHub Desktop.
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.file.FileSystem;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Random;
import java.util.concurrent.TimeUnit;
@Warmup(iterations = 5, time = 2)
@Measurement(iterations = 5, time = 3)
@Fork(value = 1, jvmArgsAppend = {"-XX:+UseG1GC", "-Xms8g", "-Xmx8g", "--add-opens=java.base/jdk.internal.access=ALL-UNNAMED"})
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@State(Scope.Benchmark)
public class NoRepl {
@Param({"0", "1024", "8192", "1048576", "33554432"})
private int length;
private FileSystem fs;
private Path asciiFile;
private Path utf8File;
@Setup
public void setup() throws IOException {
fs = Jimfs.newFileSystem();
asciiFile = fs.getPath("ascii.txt");
utf8File = fs.getPath("utf8.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 writer = Files.newBufferedWriter(utf8File)) {
for (int i = 0; i < length; i++) {
if (i % 16 != 0)
writer.write(random.nextInt(128));
else
writer.write(random.nextInt(0x9fa5 - 0x4e00) + 0x4e00);
}
}
}
@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);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment