|
| 1 | +/* |
| 2 | + * Copyright © 2025 Apple Inc. and the ServiceTalk project authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package io.servicetalk.concurrent.api; |
| 17 | + |
| 18 | +import io.servicetalk.context.api.ContextMap; |
| 19 | + |
| 20 | +import org.openjdk.jmh.annotations.Benchmark; |
| 21 | +import org.openjdk.jmh.annotations.BenchmarkMode; |
| 22 | +import org.openjdk.jmh.annotations.Fork; |
| 23 | +import org.openjdk.jmh.annotations.Measurement; |
| 24 | +import org.openjdk.jmh.annotations.Mode; |
| 25 | +import org.openjdk.jmh.annotations.OutputTimeUnit; |
| 26 | +import org.openjdk.jmh.annotations.Scope; |
| 27 | +import org.openjdk.jmh.annotations.Setup; |
| 28 | +import org.openjdk.jmh.annotations.State; |
| 29 | +import org.openjdk.jmh.annotations.Warmup; |
| 30 | + |
| 31 | +import java.util.concurrent.TimeUnit; |
| 32 | +import java.util.function.Function; |
| 33 | + |
| 34 | +/** |
| 35 | + * |
| 36 | + */ |
| 37 | +@Fork(1) |
| 38 | +@State(Scope.Benchmark) |
| 39 | +@Warmup(iterations = 5, time = 3) |
| 40 | +@Measurement(iterations = 5, time = 3) |
| 41 | +@OutputTimeUnit(TimeUnit.NANOSECONDS) |
| 42 | +@BenchmarkMode(Mode.AverageTime) |
| 43 | +public class AsyncContextProviderBenchmark { |
| 44 | + |
| 45 | + /** |
| 46 | + * gc profiling of the DefaultAsyncContextProvider shows that the Scope based detachment can be stack allocated |
| 47 | + * at least under some conditions. |
| 48 | + * |
| 49 | + * Benchmark Mode Cnt Score Error Units |
| 50 | + * AsyncContextProviderBenchmark.contextRestoreCost avgt 5 3.932 ± 0.022 ns/op |
| 51 | + * AsyncContextProviderBenchmark.contextRestoreCost:gc.alloc.rate avgt 5 ≈ 10⁻⁴ MB/sec |
| 52 | + * AsyncContextProviderBenchmark.contextRestoreCost:gc.alloc.rate.norm avgt 5 ≈ 10⁻⁶ B/op |
| 53 | + * AsyncContextProviderBenchmark.contextRestoreCost:gc.count avgt 5 ≈ 0 counts |
| 54 | + * AsyncContextProviderBenchmark.contextSaveAndRestoreCost avgt 5 1.712 ± 0.005 ns/op |
| 55 | + * AsyncContextProviderBenchmark.contextSaveAndRestoreCost:gc.alloc.rate avgt 5 ≈ 10⁻⁴ MB/sec |
| 56 | + * AsyncContextProviderBenchmark.contextSaveAndRestoreCost:gc.alloc.rate.norm avgt 5 ≈ 10⁻⁷ B/op |
| 57 | + * AsyncContextProviderBenchmark.contextSaveAndRestoreCost:gc.count avgt 5 ≈ 0 counts |
| 58 | + */ |
| 59 | + |
| 60 | + private static final ContextMap.Key<String> KEY = ContextMap.Key.newKey("test-key", String.class); |
| 61 | + private static final String EXPECTED = "hello, world!"; |
| 62 | + |
| 63 | + private static Function<String, String> wrappedFunction; |
| 64 | + |
| 65 | + @Setup |
| 66 | + public void setup() { |
| 67 | + // This will capture the current context |
| 68 | + wrappedFunction = AsyncContext.wrapFunction(ignored -> AsyncContext.context().get(KEY)); |
| 69 | + AsyncContext.context().put(KEY, EXPECTED); |
| 70 | + } |
| 71 | + |
| 72 | + @Benchmark |
| 73 | + public String contextRestoreCost() { |
| 74 | + return wrappedFunction.apply("ignored"); |
| 75 | + } |
| 76 | + |
| 77 | + @Benchmark |
| 78 | + public String contextSaveAndRestoreCost() { |
| 79 | + return AsyncContext.wrapFunction(Function.<String>identity()).apply("ignored"); |
| 80 | + } |
| 81 | +} |
0 commit comments