jamesfredley commented on code in PR #2390: URL: https://github.com/apache/groovy/pull/2390#discussion_r2878202417
########## subprojects/performance/src/jmh/groovy/org/apache/groovy/perf/grails/CallSiteInvalidationBench.groovy: ########## @@ -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 Review Comment: Removed in 1af1c45. ########## subprojects/performance/src/jmh/groovy/org/apache/groovy/perf/grails/MetaclassVariationBench.groovy: ########## @@ -0,0 +1,259 @@ +/* + * 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 + +/** + * Per-instance metaclass variation overhead (GORM domain class enhancement pattern). + * + * @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 MetaclassVariationBench { + static final int ITERATIONS = 100_000 + static final int INSTANCE_COUNT = 20 + + // Simulates a GORM domain class + static class DomainEntity { + Long id + String name + String email + boolean active = true + int version = 0 + + String getFullName() { name ?: 'Unknown' } + boolean isActive() { active } + int getVersion() { version } + + DomainEntity save() { + version++ + if (id == null) id = System.nanoTime() + this + } + + Map toMap() { + [id: id, name: name, email: email, active: active, version: version] + } + } + + // Additional domain class types + static class DomainTypeB { + String label = "dept" + int count = 5 + int getCount() { count } + } + + static class DomainTypeC { + String status = "ACTIVE" + BigDecimal budget = 100000.0 + String getStatus() { status } + } + + static class DomainTypeD { + int priority = 5 + String assignee = "unassigned" + int getPriority() { priority } + } + + // Unrelated type for cross-type invalidation + static class ServiceType { + String config = "default" + } + + List<DomainEntity> sharedMetaclassInstances + List<DomainEntity> perInstanceMetaclassInstances + DomainTypeB typeB + DomainTypeC typeC + DomainTypeD typeD + + @Setup(Level.Iteration) + void setup() { + GroovySystem.metaClassRegistry.removeMetaClass(DomainEntity) + GroovySystem.metaClassRegistry.removeMetaClass(DomainTypeB) + GroovySystem.metaClassRegistry.removeMetaClass(DomainTypeC) + GroovySystem.metaClassRegistry.removeMetaClass(DomainTypeD) + GroovySystem.metaClassRegistry.removeMetaClass(ServiceType) + + // Shared default class metaclass + sharedMetaclassInstances = (1..INSTANCE_COUNT).collect { i -> + new DomainEntity(id: i, name: "User$i", email: "user${i}@test.com") + } + + // Per-instance ExpandoMetaClass (GORM trait pattern) + perInstanceMetaclassInstances = (1..INSTANCE_COUNT).collect { i -> + def entity = new DomainEntity(id: i, name: "Enhanced$i", email: "e${i}@test.com") + def emc = new ExpandoMetaClass(DomainEntity, false, true) + // GORM-injected methods + emc.validate = { -> delegate.name != null && delegate.email != null } + emc.delete = { -> delegate.id = null; delegate } + emc.addToDependencies = { item -> delegate } + emc.initialize() + entity.metaClass = emc + entity + } + + typeB = new DomainTypeB() + typeC = new DomainTypeC() + typeD = new DomainTypeD() + } + + /** Method calls on instances sharing default class metaclass. */ + @Benchmark + void baselineSharedMetaclass(Blackhole bh) { + int sum = 0 + for (int i = 0; i < ITERATIONS; i++) { + def entity = sharedMetaclassInstances[i % INSTANCE_COUNT] + sum += entity.getFullName().length() + sum += entity.getVersion() + } + bh.consume(sum) + } + + /** Method calls on instances each with their own ExpandoMetaClass. */ + @Benchmark + void perInstanceMetaclass(Blackhole bh) { + int sum = 0 + for (int i = 0; i < ITERATIONS; i++) { + def entity = perInstanceMetaclassInstances[i % INSTANCE_COUNT] + sum += entity.getFullName().length() + sum += entity.getVersion() + } + bh.consume(sum) + } + + /** Calling GORM-injected methods on per-instance EMC objects. */ + @Benchmark + void perInstanceInjectedMethodCalls(Blackhole bh) { + int sum = 0 + for (int i = 0; i < ITERATIONS; i++) { + def entity = perInstanceMetaclassInstances[i % INSTANCE_COUNT] + boolean valid = entity.validate() + sum += valid ? 1 : 0 + } + bh.consume(sum) + } + + /** GORM startup: enhance 4 domain types then steady-state calls. */ + @Benchmark + void multiClassStartupThenSteadyState(Blackhole bh) { + // Phase 1: Enhance 4 domain class types + DomainEntity.metaClass.static.findAllByName = { String n -> [] } + DomainEntity.metaClass.static.countByActive = { boolean a -> 0 } + + DomainTypeB.metaClass.static.findAllByLabel = { String l -> [] } + DomainTypeB.metaClass.static.countByCount = { int c -> 0 } + + DomainTypeC.metaClass.static.findAllByStatus = { String s -> [] } + DomainTypeC.metaClass.static.findByBudgetGreaterThan = { BigDecimal b -> null } + + DomainTypeD.metaClass.static.findAllByPriority = { int p -> [] } + DomainTypeD.metaClass.static.findByAssignee = { String a -> null } + + // Phase 2: Steady-state calls + int sum = 0 + for (int i = 0; i < ITERATIONS; i++) { + def entity = sharedMetaclassInstances[i % INSTANCE_COUNT] + sum += entity.getFullName().length() + sum += typeB.getCount() + sum += typeC.getStatus().length() + sum += typeD.getPriority() + } + bh.consume(sum) + } + + /** Baseline: same steady-state work without preceding metaclass enhancements. */ + @Benchmark + void baselineMultiClassNoStartup(Blackhole bh) { + int sum = 0 + for (int i = 0; i < ITERATIONS; i++) { + def entity = sharedMetaclassInstances[i % INSTANCE_COUNT] + sum += entity.getFullName().length() + sum += typeB.getCount() + sum += typeC.getStatus().length() + sum += typeD.getPriority() + } + bh.consume(sum) + } + + /** Calling dynamic finders injected via static metaclass. */ + @Benchmark + void dynamicFinderCalls(Blackhole bh) { + // Inject dynamic finders + DomainEntity.metaClass.static.findByName = { String n -> + [new DomainEntity(name: n)] + } + DomainEntity.metaClass.static.findAllByActive = { boolean a -> Review Comment: The injection inside the benchmark method is intentional - it models the per-request cost in Grails where GORM dynamic finders are resolved/injected at runtime, not a one-time startup cost. Updated the Javadoc to make this intent explicit in 1af1c45. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
