|
| 1 | +/* |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package software.amazon.smithy.java.benchmarks.serde; |
| 7 | + |
| 8 | +import software.amazon.smithy.java.core.schema.ApiOperation; |
| 9 | +import software.amazon.smithy.java.core.schema.SerializableStruct; |
| 10 | +import software.amazon.smithy.java.core.serde.TypeRegistry; |
| 11 | +import software.amazon.smithy.java.dynamicclient.DynamicOperation; |
| 12 | +import software.amazon.smithy.java.dynamicschemas.SchemaConverter; |
| 13 | +import software.amazon.smithy.model.shapes.OperationShape; |
| 14 | +import software.amazon.smithy.model.shapes.ServiceShape; |
| 15 | +import software.amazon.smithy.model.shapes.ShapeId; |
| 16 | + |
| 17 | +/** |
| 18 | + * Resolves the {@link ApiOperation} a serde benchmark drives, in one of two |
| 19 | + * modes selected by the {@code smithy-java.benchmark.client} system property: |
| 20 | + * |
| 21 | + * <ul> |
| 22 | + * <li>{@code codegen} (default) — the codegen-generated {@code ApiOperation} |
| 23 | + * singleton, resolved by reflection from the protocol's generated |
| 24 | + * package. This is what ships and what the published numbers reflect.</li> |
| 25 | + * <li>{@code dynamic} — a {@link DynamicOperation} built at runtime from the |
| 26 | + * assembled Smithy model via {@link SchemaConverter}, with a |
| 27 | + * {@code StructDocument} input. Lets us measure the dynamic client on the |
| 28 | + * exact same payloads and protocol entry points.</li> |
| 29 | + * </ul> |
| 30 | + * |
| 31 | + * <p>Both modes return an {@code ApiOperation} whose {@code inputBuilder()} / |
| 32 | + * {@code outputBuilder()} accept the same schema-guided document deserialization |
| 33 | + * the benchmarks already use, so the only difference is operation construction — |
| 34 | + * the {@code protocol.createRequest} / {@code deserializeResponse} calls in the |
| 35 | + * benchmark loop are identical. |
| 36 | + */ |
| 37 | +final class BenchmarkOperations { |
| 38 | + |
| 39 | + /** System property selecting codegen (default) vs dynamic operation build. */ |
| 40 | + static final String CLIENT_MODE_PROPERTY = "smithy-java.benchmark.client"; |
| 41 | + |
| 42 | + private BenchmarkOperations() {} |
| 43 | + |
| 44 | + static boolean isDynamic() { |
| 45 | + return "dynamic".equalsIgnoreCase(System.getProperty(CLIENT_MODE_PROPERTY, "codegen")); |
| 46 | + } |
| 47 | + |
| 48 | + /** A short label for the active mode, for result metadata / logging. */ |
| 49 | + static String modeLabel() { |
| 50 | + return isDynamic() ? "dynamic" : "codegen"; |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Resolve the operation for a benchmark test case. |
| 55 | + * |
| 56 | + * @param opShape the operation shape from the benchmark model |
| 57 | + * @param generatedPackage codegen package for the protocol projection |
| 58 | + * @param serviceId the service the operation is bound to (needed by |
| 59 | + * the dynamic path to convert the service schema) |
| 60 | + */ |
| 61 | + static ApiOperation<? extends SerializableStruct, ? extends SerializableStruct> resolve( |
| 62 | + OperationShape opShape, |
| 63 | + String generatedPackage, |
| 64 | + ShapeId serviceId |
| 65 | + ) { |
| 66 | + if (isDynamic()) { |
| 67 | + return dynamicOperation(opShape, serviceId); |
| 68 | + } |
| 69 | + return SerializeState.resolveOperation(generatedPackage, opShape.getId().getName()); |
| 70 | + } |
| 71 | + |
| 72 | + private static ApiOperation<? extends SerializableStruct, ? extends SerializableStruct> dynamicOperation( |
| 73 | + OperationShape opShape, |
| 74 | + ShapeId serviceId |
| 75 | + ) { |
| 76 | + var model = BenchmarkTestCases.model(); |
| 77 | + ServiceShape service = model.expectShape(serviceId, ServiceShape.class); |
| 78 | + var converter = new SchemaConverter(model); |
| 79 | + return DynamicOperation.create( |
| 80 | + opShape, |
| 81 | + converter, |
| 82 | + model, |
| 83 | + service, |
| 84 | + TypeRegistry.empty(), |
| 85 | + (id, builder) -> {}); |
| 86 | + } |
| 87 | +} |
0 commit comments