On Tue, 27 Dec 2022 14:55:31 GMT, Peter Levart <plev...@openjdk.org> wrote:

> Hello Markus! Could you show the JMH code that produced the benchmark results?

The following lines make use of a custom method I have added to `InputStream` 
in a custom build of JDK 21, so JMH can control the size of the buffer. The 
test runs approx. 42 Minutes, and the result depends on the actual hardware you 
are using. The common sense of all tested hardware so far is that going from 8K 
to 16K you have a *strong* gain, but the *additional* gain is lower with 
duplicating buffer size further. You have *double* performance with the 
specific cloud VM I was using, while you only have approx. only *20%* gain on 
my personal laptop with local SSD. Anyways, you always have a *considerable* 
gain.


public class TransferToPerformance {

    public static void main(final String[] args) throws IOException {
        Main.main(args);
    }

    @State(Scope.Benchmark)
    public static class Config {

        @Param({ "1048576" })
        public int streamSize;

        @Param({ "8192", "16384", "32768", "65536", "131072" })
        public int bufferSize;

        public InputStream source;
        public OutputStream target;
        private Path path;

        @Setup(Level.Invocation)
        public void setUp() throws IOException {
            InputStream.setBufferSize(bufferSize); // static utility method 
using custom build of JDK 21
            path = Files.createTempFile("a-", ".bin");
            var bytes = createRandomBytes(streamSize);
            Files.deleteIfExists(this.path);
            Files.write(this.path, bytes, CREATE, TRUNCATE_EXISTING, WRITE);
            source = new FileInputStream(this.path.toFile());
            target = new ByteArrayOutputStream();
        }

        @TearDown(Level.Invocation)
        public void tearDown() throws IOException {
            source.close();
            target.close();
            source = null;
            target = null;
            Files.deleteIfExists(this.path);
        }
    }

    private static final Random RND = new Random(); 

    private static byte[] createRandomBytes(int size) {
        var bytes = new byte[size];
        RND.nextBytes(bytes);
        return bytes;
    }

    @Benchmark
    public final void transferTo(Config config, Blackhole blackhole) throws 
IOException {
        blackhole.consume(config.source.transferTo(config.target));
        config.target.flush();
        config.target.close();
    }

}

-------------

PR: https://git.openjdk.org/jdk/pull/11783

Reply via email to