From 25e63c52c026baf254df516cdc1c5cb80e2eac35 Mon Sep 17 00:00:00 2001 From: squid233 <60126026+squid233@users.noreply.github.com> Date: Sat, 24 Feb 2024 19:18:14 +0800 Subject: [PATCH] Update MemoryUtilTest.java --- .../overrungl/demo/mem/MemoryUtilTest.java | 56 +++++++++---------- 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/modules/samples/src/jmh/java/overrungl/demo/mem/MemoryUtilTest.java b/modules/samples/src/jmh/java/overrungl/demo/mem/MemoryUtilTest.java index 09488302..24998f5e 100644 --- a/modules/samples/src/jmh/java/overrungl/demo/mem/MemoryUtilTest.java +++ b/modules/samples/src/jmh/java/overrungl/demo/mem/MemoryUtilTest.java @@ -17,10 +17,12 @@ package overrungl.demo.mem; import org.openjdk.jmh.annotations.*; +import org.openjdk.jmh.infra.Blackhole; import org.openjdk.jmh.results.format.ResultFormatType; import org.openjdk.jmh.runner.Runner; import org.openjdk.jmh.runner.RunnerException; import org.openjdk.jmh.runner.options.OptionsBuilder; +import overrun.marshal.MemoryStack; import overrungl.util.MemoryUtil; import java.lang.foreign.Arena; @@ -36,57 +38,55 @@ @BenchmarkMode(Mode.Throughput) @State(Scope.Thread) @OutputTimeUnit(TimeUnit.SECONDS) -@Warmup(iterations = 5, time = 1) -@Measurement(iterations = 5, time = 1) +@Warmup(iterations = 5, time = 250, timeUnit = TimeUnit.MILLISECONDS, batchSize = 100) +@Measurement(iterations = 5, time = 250, timeUnit = TimeUnit.MILLISECONDS, batchSize = 100) @Threads(Threads.MAX) @Fork(1) public class MemoryUtilTest { @Param({"0", "1", "10", "128", "1024"}) private long size; - private Arena arena; - - @Setup(Level.Iteration) - public void setup() { - arena = Arena.ofConfined(); - } @Benchmark - public MemorySegment testArenaInside() { - try (Arena arena1 = Arena.ofConfined()) { - return arena1.allocate(size); + public void measureArena(Blackhole bh) { + try (Arena arena = Arena.ofConfined()) { + bh.consume(arena.allocate(size)); } } @Benchmark - public MemorySegment testArenaOutside() { - return arena.allocate(size); + public void measureMalloc(Blackhole bh) { + MemorySegment segment = MemorySegment.NULL; + try { + segment = MemoryUtil.malloc(size); + bh.consume(segment); + } finally { + MemoryUtil.free(segment); + } } @Benchmark - public MemorySegment memoryUtilMalloc() { - MemorySegment seg = null; + public void measureCalloc(Blackhole bh) { + MemorySegment segment = MemorySegment.NULL; try { - seg = MemoryUtil.malloc(size); - return seg; + segment = MemoryUtil.calloc(1L, size); + bh.consume(segment); } finally { - MemoryUtil.free(seg); + MemoryUtil.free(segment); } } @Benchmark - public MemorySegment memoryUtilCalloc() { - MemorySegment seg = null; - try { - seg = MemoryUtil.calloc(1, size); - return seg; - } finally { - MemoryUtil.free(seg); + public void measureStackMalloc(Blackhole bh) { + try (MemoryStack stack = MemoryStack.stackPush()) { + bh.consume(stack.malloc(size, 1L)); } } - @TearDown(Level.Iteration) - public void dispose() { - arena.close(); + @Benchmark + public void measureStackCalloc(Blackhole bh) { + try (MemoryStack stack = MemoryStack.stackPush()) { + bh.consume(stack.allocate(size)); + } } public static void main(String[] args) throws RunnerException {