|
| 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.cache; |
| 20 | + |
| 21 | +import io.opentelemetry.api.OpenTelemetry; |
| 22 | +import java.util.concurrent.Executors; |
| 23 | +import java.util.concurrent.ScheduledExecutorService; |
| 24 | +import java.util.concurrent.TimeUnit; |
| 25 | +import java.util.concurrent.atomic.AtomicInteger; |
| 26 | +import org.apache.bookkeeper.mledger.impl.EntryImpl; |
| 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 | +/** Measures real entry-copy/release and read-permit accounting with per-entry or per-batch callbacks. */ |
| 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 ReadBatchCallbackBenchmark { |
| 48 | + private static final long LIMIT_BYTES = 16 * 1024 * 1024; |
| 49 | + |
| 50 | + @Param({"1", "100", "1000"}) |
| 51 | + public int batchSize; |
| 52 | + |
| 53 | + @Param({"false", "true"}) |
| 54 | + public boolean sharedCallback; |
| 55 | + |
| 56 | + private EntryImpl[] cachedEntries; |
| 57 | + private EntryImpl[] readEntries; |
| 58 | + private InflightReadsLimiter limiter; |
| 59 | + private ScheduledExecutorService executor; |
| 60 | + |
| 61 | + @Setup |
| 62 | + public void setup() { |
| 63 | + executor = Executors.newSingleThreadScheduledExecutor(); |
| 64 | + limiter = new InflightReadsLimiter(LIMIT_BYTES, 1, 0, executor, OpenTelemetry.noop()); |
| 65 | + cachedEntries = new EntryImpl[batchSize]; |
| 66 | + readEntries = new EntryImpl[batchSize]; |
| 67 | + for (int i = 0; i < batchSize; i++) { |
| 68 | + cachedEntries[i] = EntryImpl.create(1, i, new byte[128]); |
| 69 | + cachedEntries[i].getPosition(); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + @Benchmark |
| 74 | + public long readAndReleaseBatch() { |
| 75 | + InflightReadsLimiter batchLimiter = limiter; |
| 76 | + InflightReadsLimiter.Handle handle = batchLimiter.acquire(batchSize * 128L, null).orElseThrow(); |
| 77 | + AtomicInteger remainingCount = new AtomicInteger(batchSize); |
| 78 | + Runnable shared = sharedCallback ? releaseCallback(remainingCount, batchLimiter, handle) : null; |
| 79 | + for (int i = 0; i < batchSize; i++) { |
| 80 | + EntryImpl entry = EntryImpl.create(cachedEntries[i]); |
| 81 | + entry.onDeallocate(sharedCallback ? shared : releaseCallback(remainingCount, batchLimiter, handle)); |
| 82 | + readEntries[i] = entry; |
| 83 | + } |
| 84 | + for (int i = batchSize - 1; i >= 0; i--) { |
| 85 | + readEntries[i].release(); |
| 86 | + readEntries[i] = null; |
| 87 | + } |
| 88 | + return batchLimiter.getRemainingBytes(); |
| 89 | + } |
| 90 | + |
| 91 | + private static Runnable releaseCallback(AtomicInteger remainingCount, InflightReadsLimiter limiter, |
| 92 | + InflightReadsLimiter.Handle handle) { |
| 93 | + return () -> { |
| 94 | + if (remainingCount.decrementAndGet() <= 0) { |
| 95 | + limiter.release(handle); |
| 96 | + } |
| 97 | + }; |
| 98 | + } |
| 99 | + |
| 100 | + @TearDown |
| 101 | + public void tearDown() { |
| 102 | + try { |
| 103 | + if (limiter.getRemainingBytes() != LIMIT_BYTES) { |
| 104 | + throw new IllegalStateException("Read permits were not released exactly once per batch"); |
| 105 | + } |
| 106 | + } finally { |
| 107 | + for (EntryImpl entry : cachedEntries) { |
| 108 | + entry.release(); |
| 109 | + } |
| 110 | + limiter.close(); |
| 111 | + executor.shutdownNow(); |
| 112 | + } |
| 113 | + } |
| 114 | +} |