|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.apache.bookkeeper.mledger.impl; |
| 20 | + |
| 21 | +import io.netty.buffer.ByteBuf; |
| 22 | +import io.netty.buffer.PooledByteBufAllocator; |
| 23 | +import io.netty.util.concurrent.FastThreadLocalThread; |
| 24 | +import java.util.concurrent.TimeUnit; |
| 25 | +import org.apache.bookkeeper.mledger.Position; |
| 26 | +import org.apache.bookkeeper.mledger.PositionFactory; |
| 27 | +import org.openjdk.jmh.annotations.Benchmark; |
| 28 | +import org.openjdk.jmh.annotations.BenchmarkMode; |
| 29 | +import org.openjdk.jmh.annotations.Fork; |
| 30 | +import org.openjdk.jmh.annotations.Measurement; |
| 31 | +import org.openjdk.jmh.annotations.Mode; |
| 32 | +import org.openjdk.jmh.annotations.OutputTimeUnit; |
| 33 | +import org.openjdk.jmh.annotations.Param; |
| 34 | +import org.openjdk.jmh.annotations.Scope; |
| 35 | +import org.openjdk.jmh.annotations.Setup; |
| 36 | +import org.openjdk.jmh.annotations.State; |
| 37 | +import org.openjdk.jmh.annotations.TearDown; |
| 38 | +import org.openjdk.jmh.annotations.Warmup; |
| 39 | + |
| 40 | +/** Isolates position construction with real pooled entry copies; does not model persistence or cache maps. */ |
| 41 | +@State(Scope.Thread) |
| 42 | +@BenchmarkMode(Mode.AverageTime) |
| 43 | +@OutputTimeUnit(TimeUnit.NANOSECONDS) |
| 44 | +@Warmup(iterations = 3, time = 1) |
| 45 | +@Measurement(iterations = 5, time = 1) |
| 46 | +@Fork(2) |
| 47 | +public class PublishPositionSharingBenchmark { |
| 48 | + @Param({"false", "true"}) |
| 49 | + public boolean sharePosition; |
| 50 | + |
| 51 | + @Param({"false", "true"}) |
| 52 | + public boolean cacheEntry; |
| 53 | + |
| 54 | + private ByteBuf data; |
| 55 | + private long nextEntryId; |
| 56 | + private volatile Position cachedPosition; |
| 57 | + private volatile Position confirmedPosition; |
| 58 | + |
| 59 | + @Setup |
| 60 | + public void setup() { |
| 61 | + if (!FastThreadLocalThread.currentThreadWillCleanupFastThreadLocals()) { |
| 62 | + throw new IllegalStateException("Run with FastThreadLocalBenchmarkExecutor in the fork JVM"); |
| 63 | + } |
| 64 | + data = PooledByteBufAllocator.DEFAULT.directBuffer(128).writeZero(128); |
| 65 | + } |
| 66 | + |
| 67 | + @Benchmark |
| 68 | + public long completePublish() { |
| 69 | + long entryId = nextEntryId++; |
| 70 | + Position lastEntry = sharePosition ? PositionFactory.create(1, entryId) : null; |
| 71 | + if (cacheEntry) { |
| 72 | + EntryImpl entry = sharePosition ? EntryImpl.create(lastEntry, data, 0) |
| 73 | + : EntryImpl.create(1, entryId, data, 0); |
| 74 | + try { |
| 75 | + EntryImpl copy = EntryImpl.createWithRetainedDuplicate(entry.getPosition(), entry.getDataBuffer(), 0); |
| 76 | + try { |
| 77 | + cachedPosition = copy.getPosition(); |
| 78 | + } finally { |
| 79 | + copy.release(); |
| 80 | + } |
| 81 | + } finally { |
| 82 | + entry.release(); |
| 83 | + } |
| 84 | + } |
| 85 | + if (!sharePosition) { |
| 86 | + lastEntry = PositionFactory.create(1, entryId); |
| 87 | + } |
| 88 | + confirmedPosition = lastEntry; |
| 89 | + return lastEntry.getEntryId(); |
| 90 | + } |
| 91 | + |
| 92 | + @TearDown |
| 93 | + public void tearDown() { |
| 94 | + cachedPosition = null; |
| 95 | + confirmedPosition = null; |
| 96 | + data.release(); |
| 97 | + } |
| 98 | +} |