papegaaij commented on code in PR #1595: URL: https://github.com/apache/wicket/pull/1595#discussion_r3992867289
########## wicket-benchmarks/src/main/java/org/apache/wicket/benchmarks/ComponentStateBenchmark.java: ########## @@ -0,0 +1,466 @@ +/* + * 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.wicket.benchmarks; + +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.TimeUnit; + +import org.apache.wicket.AttributeModifier; +import org.apache.wicket.Component; +import org.apache.wicket.MetaDataKey; +import org.apache.wicket.ajax.AjaxEventBehavior; +import org.apache.wicket.ajax.AjaxRequestTarget; +import org.apache.wicket.behavior.Behavior; +import org.apache.wicket.markup.html.WebMarkupContainer; +import org.apache.wicket.model.Model; +import org.openjdk.jmh.annotations.Benchmark; +import org.openjdk.jmh.annotations.BenchmarkMode; +import org.openjdk.jmh.annotations.Fork; +import org.openjdk.jmh.annotations.Level; +import org.openjdk.jmh.annotations.Measurement; +import org.openjdk.jmh.annotations.Mode; +import org.openjdk.jmh.annotations.OutputTimeUnit; +import org.openjdk.jmh.annotations.Param; +import org.openjdk.jmh.annotations.Scope; +import org.openjdk.jmh.annotations.Setup; +import org.openjdk.jmh.annotations.State; +import org.openjdk.jmh.annotations.Threads; +import org.openjdk.jmh.annotations.TearDown; +import org.openjdk.jmh.annotations.Warmup; +import org.openjdk.jmh.infra.Blackhole; + +/** + * Benchmarks the per-request accessors on {@link Component}'s flexible state (model, behaviors and + * meta data) plus the mutate-and-detach cycle. + * <p> + * Deliberately written against public Wicket API only, so that the exact same source can be run + * against different implementations of the state storage and compared. + * <p> + * Three things are measured separately, because they answer different questions: + * <ul> + * <li>{@code read*} - the cost of reading state, per state shape. Reads do not mutate, so a + * trial-scoped component is correct here and no per-invocation harness overhead is paid. + * <li>{@code read*MixedShapes} - the same reads, but over a component array holding every shape at + * once. This is the interesting one: with a single shape the call sites inside the state lookup are + * monomorphic and inline, which flatters any implementation that dispatches on the shape. Real + * pages interleave shapes. A large gap between the per-shape and mixed numbers is the signature of + * dispatch that stopped inlining. + * <li>{@code buildAndDetach} - construct a component, populate its state and detach it, as one + * operation. Detaching mutates state (temporary behaviors are removed, arrays are compacted), so it + * cannot be measured repeatedly against the same instance; folding construction into the operation + * keeps every invocation doing the real work without resorting to {@code Level.Invocation}. + * </ul> + * Single threaded on purpose: component state is per component and never contended, so extra + * threads measure nothing new while making the Wicket thread-local setup harder to get right. + * <p> + * Always run with {@code -prof gc}: {@code gc.alloc.rate.norm} (bytes per operation) is the number + * that matters for a framework that has to keep many pages in memory, and it is far more stable + * than throughput. + */ +@BenchmarkMode(Mode.AverageTime) +@OutputTimeUnit(TimeUnit.NANOSECONDS) +@Fork(3) +@Threads(1) +@Warmup(iterations = 3, time = 2, timeUnit = TimeUnit.SECONDS) +@Measurement(iterations = 5, time = 2, timeUnit = TimeUnit.SECONDS) +public class ComponentStateBenchmark +{ + static final MetaDataKey<String> KEY = new MetaDataKey<>() + { + private static final long serialVersionUID = 1L; + }; + + /** The eight shapes the flexible state of a component can take. */ + public enum Shape + { + NONE(false, false, false), + MODEL(true, false, false), + BEHAVIOR(false, true, false), + METADATA(false, false, true), + MODEL_BEHAVIOR(true, true, false), + MODEL_METADATA(true, false, true), + BEHAVIOR_METADATA(false, true, true), + MODEL_BEHAVIOR_METADATA(true, true, true), + /** + * A behavior with a stable id, as every link and ajax-enabled component has. Master keeps + * those ids in a {@code BehaviorIdList} held in the component's meta data; storing the id + * as the behavior's own array index removes that list, which WICKET-6774 claimed as its + * biggest saving. None of the other shapes exercise it. + */ + STABLE_ID_BEHAVIOR(false, false, false, true), + MODEL_STABLE_ID_BEHAVIOR(true, false, false, true), + /** + * A real {@link AjaxEventBehavior}, so the figure is comparable to the -36.2% serialized + * saving reported on WICKET-6774. {@link #STABLE_ID_BEHAVIOR} isolates the id storage but + * carries almost nothing of its own, which flatters the percentage. + */ + AJAX_BEHAVIOR(false, false, false, true, true); + + private final boolean model; + private final boolean behavior; + private final boolean metaData; + private final boolean stableId; + private final boolean ajax; + + Shape(boolean model, boolean behavior, boolean metaData) + { + this(model, behavior, metaData, false, false); + } + + Shape(boolean model, boolean behavior, boolean metaData, boolean stableId) + { + this(model, behavior, metaData, stableId, false); + } + + Shape(boolean model, boolean behavior, boolean metaData, boolean stableId, boolean ajax) + { + this.model = model; + this.behavior = behavior; + this.metaData = metaData; + this.stableId = stableId; + this.ajax = ajax; + } + + boolean hasModel() + { + return model; + } + + Component newComponent(String id) + { + Component c = new WebMarkupContainer(id); + populate(c); + return c; + } + + void populate(Component c) Review Comment: You are right about the symptom: `AJAX_BEHAVIOR(false, false, false, true, true)` was unreadable, and two of the three constructors existed only to fill in the defaults the shorter constants did not name. I went for a variation on your suggestion rather than constant-specific `populate` bodies. Eight of the eleven constants are exactly the combinations of three independent ingredients, so a `populate` per constant would repeat the same three snippets across those combinations, and `hasModel()` - which the `ModelShapes` and `NoModelShapes` states use to split the constants - would need an override per constant as well. So the ingredients became a `Trait` enum, collected into an `EnumSet` by a varargs constructor: ```java MODEL_BEHAVIOR(Trait.MODEL, Trait.BEHAVIOR), STABLE_ID_BEHAVIOR(Trait.STABLE_ID), AJAX_BEHAVIOR(Trait.STABLE_ID, Trait.AJAX); ``` One constructor, no positional booleans, each constant naming what it carries, `populate` still in one place and `hasModel()` reduced to `traits.contains(Trait.MODEL)`. The javadoc that hung on the stable-id and ajax constants moved onto the traits it actually describes. Constant order is unchanged, so the ordinals and the `@Param` strings the benchmarks are run with still line up. 3ef485a. ########## wicket-core-tests/src/test/java/org/apache/wicket/behavior/BehaviorTest.java: ########## @@ -89,6 +89,21 @@ public void consecutiveTemporaryBehaviorsAreRemoved() { assertFalse(container.getBehaviors().contains(temp2)); } + @Test + void nullBehaviorIsRejectedBeforeAnythingIsStored() { Review Comment: Added in f2c78b1. It now says what the test protects rather than only what it asserts: `add` validates the whole argument list before it stores any of it, because a behavior's id is its position in the list, so one that was already stored when a later `null` was rejected would keep the position it took and shift the id of everything added after it. -- 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]
