-
Notifications
You must be signed in to change notification settings - Fork 1.9k
GROOVY-10307: Add targeted JMH benchmarks for SwitchPoint invalidation #2390
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
paulk-asert
merged 2 commits into
apache:master
from
jamesfredley:groovy-10307-grails-like-benchmarks
Mar 4, 2026
+951
−0
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
225 changes: 225 additions & 0 deletions
225
...performance/src/jmh/groovy/org/apache/groovy/perf/grails/CallSiteInvalidationBench.groovy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,225 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
| package org.apache.groovy.perf.grails | ||
|
|
||
| import groovy.lang.ExpandoMetaClass | ||
| import groovy.lang.GroovySystem | ||
|
|
||
| import org.openjdk.jmh.annotations.* | ||
| import org.openjdk.jmh.infra.Blackhole | ||
|
|
||
| import java.util.concurrent.TimeUnit | ||
|
|
||
| /** | ||
| * SwitchPoint invalidation overhead for Grails-like metaclass change patterns. | ||
| * | ||
| * @see <a href="https://issues.apache.org/jira/browse/GROOVY-10307">GROOVY-10307</a> | ||
| */ | ||
| @Warmup(iterations = 3, time = 2, timeUnit = TimeUnit.SECONDS) | ||
| @Measurement(iterations = 5, time = 2, timeUnit = TimeUnit.SECONDS) | ||
| @Fork(2) | ||
| @BenchmarkMode(Mode.AverageTime) | ||
| @OutputTimeUnit(TimeUnit.MILLISECONDS) | ||
| @State(Scope.Thread) | ||
| class CallSiteInvalidationBench { | ||
| static final int ITERATIONS = 100_000 | ||
|
|
||
| static class HotTarget { | ||
| int value = 42 | ||
| int compute() { value * 2 } | ||
| String describe() { "v=$value" } | ||
| } | ||
|
|
||
| static class HotTargetB { | ||
| int count = 10 | ||
| int getCount() { count } | ||
| } | ||
|
|
||
| static class HotTargetC { | ||
| List items = [1, 2, 3] | ||
| int itemCount() { items.size() } | ||
| } | ||
|
|
||
| static class ColdType { | ||
| String label = "cold" | ||
| } | ||
|
|
||
| HotTarget hotTarget | ||
| HotTargetB hotTargetB | ||
| HotTargetC hotTargetC | ||
| List<Integer> sampleList | ||
|
|
||
| @Setup(Level.Iteration) | ||
| void setup() { | ||
| GroovySystem.metaClassRegistry.removeMetaClass(HotTarget) | ||
| GroovySystem.metaClassRegistry.removeMetaClass(HotTargetB) | ||
| GroovySystem.metaClassRegistry.removeMetaClass(HotTargetC) | ||
| GroovySystem.metaClassRegistry.removeMetaClass(ColdType) | ||
| hotTarget = new HotTarget() | ||
| hotTargetB = new HotTargetB() | ||
| hotTargetC = new HotTargetC() | ||
| sampleList = [1, 2, 3, 4, 5] | ||
| } | ||
|
|
||
| /** Single method call in tight loop, no invalidation. */ | ||
| @Benchmark | ||
| void baselineHotLoop(Blackhole bh) { | ||
| int sum = 0 | ||
| for (int i = 0; i < ITERATIONS; i++) { | ||
| sum += hotTarget.compute() | ||
| } | ||
| bh.consume(sum) | ||
| } | ||
|
|
||
| /** list.size() in tight loop, no invalidation. */ | ||
| @Benchmark | ||
| void baselineListSize(Blackhole bh) { | ||
| int sum = 0 | ||
| for (int i = 0; i < ITERATIONS; i++) { | ||
| sum += sampleList.size() | ||
| } | ||
| bh.consume(sum) | ||
| } | ||
|
|
||
| /** Cross-type invalidation every 1000 calls. */ | ||
| @Benchmark | ||
| void crossTypeInvalidationEvery1000(Blackhole bh) { | ||
| int sum = 0 | ||
| for (int i = 0; i < ITERATIONS; i++) { | ||
| sum += hotTarget.compute() | ||
| if (i % 1000 == 0) { | ||
| ColdType.metaClass."dynamic${i % 5}" = { -> i } | ||
| } | ||
| } | ||
| bh.consume(sum) | ||
| } | ||
|
|
||
| /** Cross-type invalidation every 100 calls. */ | ||
| @Benchmark | ||
| void crossTypeInvalidationEvery100(Blackhole bh) { | ||
| int sum = 0 | ||
| for (int i = 0; i < ITERATIONS; i++) { | ||
| sum += hotTarget.compute() | ||
| if (i % 100 == 0) { | ||
| ColdType.metaClass."dynamic${i % 5}" = { -> i } | ||
| } | ||
| } | ||
| bh.consume(sum) | ||
| } | ||
|
|
||
| /** Cross-type invalidation every 10000 calls. */ | ||
| @Benchmark | ||
| void crossTypeInvalidationEvery10000(Blackhole bh) { | ||
| int sum = 0 | ||
| for (int i = 0; i < ITERATIONS; i++) { | ||
| sum += hotTarget.compute() | ||
| if (i % 10000 == 0) { | ||
| ColdType.metaClass."dynamic${i % 5}" = { -> i } | ||
| } | ||
| } | ||
| bh.consume(sum) | ||
| } | ||
|
|
||
| /** list.size() with cross-type invalidation every 1000 calls. */ | ||
| @Benchmark | ||
| void listSizeWithCrossTypeInvalidation(Blackhole bh) { | ||
| int sum = 0 | ||
| for (int i = 0; i < ITERATIONS; i++) { | ||
| sum += sampleList.size() | ||
| if (i % 1000 == 0) { | ||
| ColdType.metaClass."dynamic${i % 5}" = { -> i } | ||
| } | ||
| } | ||
| bh.consume(sum) | ||
| } | ||
|
|
||
| /** Same-type invalidation every 1000 calls. */ | ||
| @Benchmark | ||
| void sameTypeInvalidationEvery1000(Blackhole bh) { | ||
| int sum = 0 | ||
| for (int i = 0; i < ITERATIONS; i++) { | ||
| sum += hotTarget.compute() | ||
| if (i % 1000 == 0) { | ||
| HotTarget.metaClass."dynamic${i % 5}" = { -> i } | ||
| } | ||
| } | ||
| bh.consume(sum) | ||
| } | ||
|
|
||
| /** Five method calls across three types, no invalidation. */ | ||
| @Benchmark | ||
| void baselineMultipleCallSites(Blackhole bh) { | ||
| int sum = 0 | ||
| for (int i = 0; i < ITERATIONS; i++) { | ||
| sum += hotTarget.compute() | ||
| sum += hotTarget.describe().length() | ||
| sum += hotTargetB.getCount() | ||
| sum += hotTargetC.itemCount() | ||
| sum += sampleList.size() | ||
| } | ||
| bh.consume(sum) | ||
| } | ||
|
|
||
| /** Five call sites with cross-type invalidation every 1000 calls. */ | ||
| @Benchmark | ||
| void multipleCallSitesWithInvalidation(Blackhole bh) { | ||
| int sum = 0 | ||
| for (int i = 0; i < ITERATIONS; i++) { | ||
| sum += hotTarget.compute() | ||
| sum += hotTarget.describe().length() | ||
| sum += hotTargetB.getCount() | ||
| sum += hotTargetC.itemCount() | ||
| sum += sampleList.size() | ||
| if (i % 1000 == 0) { | ||
| ColdType.metaClass."dynamic${i % 5}" = { -> i } | ||
| } | ||
| } | ||
| bh.consume(sum) | ||
| } | ||
|
|
||
| /** 100 metaclass changes then steady-state method calls. */ | ||
| @Benchmark | ||
| void burstThenSteadyState(Blackhole bh) { | ||
| // Phase 1: Burst of metaclass changes (framework startup) | ||
| for (int i = 0; i < 100; i++) { | ||
| ColdType.metaClass."startup${i % 20}" = { -> i } | ||
| } | ||
|
|
||
| // Phase 2: Steady-state method calls (request handling) | ||
| int sum = 0 | ||
| for (int i = 0; i < ITERATIONS; i++) { | ||
| sum += hotTarget.compute() | ||
| sum += hotTargetB.getCount() | ||
| sum += sampleList.size() | ||
| } | ||
| bh.consume(sum) | ||
| } | ||
|
|
||
| /** Steady-state method calls with no preceding burst. */ | ||
| @Benchmark | ||
| void baselineSteadyStateNoBurst(Blackhole bh) { | ||
| int sum = 0 | ||
| for (int i = 0; i < ITERATIONS; i++) { | ||
| sum += hotTarget.compute() | ||
| sum += hotTargetB.getCount() | ||
| sum += sampleList.size() | ||
| } | ||
| bh.consume(sum) | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.