|
| 1 | +/* |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
1 | 5 | package software.amazon.smithy.codegen.core.jmh;
|
2 | 6 |
|
| 7 | +import java.util.ArrayList; |
| 8 | +import java.util.Collections; |
| 9 | +import java.util.Comparator; |
| 10 | +import java.util.LinkedHashMap; |
| 11 | +import java.util.List; |
| 12 | +import java.util.Map; |
| 13 | +import java.util.concurrent.TimeUnit; |
| 14 | +import org.openjdk.jmh.annotations.Benchmark; |
| 15 | +import org.openjdk.jmh.annotations.BenchmarkMode; |
| 16 | +import org.openjdk.jmh.annotations.Fork; |
| 17 | +import org.openjdk.jmh.annotations.Measurement; |
| 18 | +import org.openjdk.jmh.annotations.Mode; |
| 19 | +import org.openjdk.jmh.annotations.Scope; |
| 20 | +import org.openjdk.jmh.annotations.Setup; |
| 21 | +import org.openjdk.jmh.annotations.State; |
| 22 | +import org.openjdk.jmh.annotations.Warmup; |
| 23 | +import software.amazon.smithy.codegen.core.CodegenContext; |
| 24 | +import software.amazon.smithy.codegen.core.ImportContainer; |
| 25 | +import software.amazon.smithy.codegen.core.SmithyIntegration; |
| 26 | +import software.amazon.smithy.codegen.core.Symbol; |
| 27 | +import software.amazon.smithy.codegen.core.SymbolWriter; |
| 28 | +import software.amazon.smithy.utils.DependencyGraph; |
| 29 | + |
| 30 | +@Warmup(iterations = 3) |
| 31 | +@Measurement(iterations = 3, timeUnit = TimeUnit.MICROSECONDS) |
| 32 | +@BenchmarkMode(Mode.AverageTime) |
| 33 | +@Fork(1) |
3 | 34 | public class SmithyIntegrations {
|
| 35 | + |
| 36 | + @State(Scope.Thread) |
| 37 | + public static class SmithyIntegrationsState { |
| 38 | + public Map<String, SmithyIntegration<?, ?, ?>> independentIntegrations10; |
| 39 | + public Map<String, SmithyIntegration<?, ?, ?>> dependentIntegrations10; |
| 40 | + public Map<String, SmithyIntegration<?, ?, ?>> independentIntegrations100; |
| 41 | + public Map<String, SmithyIntegration<?, ?, ?>> dependentIntegrations100; |
| 42 | + public Map<String, SmithyIntegration<?, ?, ?>> independentIntegrations1000; |
| 43 | + public Map<String, SmithyIntegration<?, ?, ?>> dependentIntegrations1000; |
| 44 | + |
| 45 | + @Setup |
| 46 | + public void setup() { |
| 47 | + independentIntegrations10 = new LinkedHashMap<>(10); |
| 48 | + independentIntegrations100 = new LinkedHashMap<>(100); |
| 49 | + independentIntegrations1000 = new LinkedHashMap<>(1000); |
| 50 | + for (int i = 0; i < 1000; i++) { |
| 51 | + String name = "integration" + i; |
| 52 | + TestIntegration integration = new TestIntegration(name); |
| 53 | + if (i < 10) { |
| 54 | + independentIntegrations10.put(name, integration); |
| 55 | + } |
| 56 | + if (i < 100) { |
| 57 | + independentIntegrations100.put(name, integration); |
| 58 | + } |
| 59 | + independentIntegrations1000.put(name, integration); |
| 60 | + } |
| 61 | + independentIntegrations100 = Collections.unmodifiableMap(independentIntegrations100); |
| 62 | + |
| 63 | + dependentIntegrations10 = new LinkedHashMap<>(10); |
| 64 | + dependentIntegrations100 = new LinkedHashMap<>(100); |
| 65 | + dependentIntegrations1000 = new LinkedHashMap<>(1000); |
| 66 | + for (int i = 0; i < 1000; i++) { |
| 67 | + String name = "integration" + i; |
| 68 | + String next = "integration" + (i + 1); |
| 69 | + String afterNext = "integration" + (i + 2); |
| 70 | + |
| 71 | + |
| 72 | + if (i < 10) { |
| 73 | + List<String> dependencies = new ArrayList<>(); |
| 74 | + if (i < 9) { |
| 75 | + dependencies.add(next); |
| 76 | + } |
| 77 | + if (i < 8) { |
| 78 | + dependencies.add(afterNext); |
| 79 | + } |
| 80 | + dependentIntegrations10.put(name, new TestIntegration(name, (byte) 0, Collections.emptyList(), dependencies)); |
| 81 | + } |
| 82 | + |
| 83 | + if (i < 100) { |
| 84 | + List<String> dependencies = new ArrayList<>(); |
| 85 | + if (i < 99) { |
| 86 | + dependencies.add(next); |
| 87 | + } |
| 88 | + if (i < 98) { |
| 89 | + dependencies.add(afterNext); |
| 90 | + } |
| 91 | + dependentIntegrations100.put(name, new TestIntegration(name, (byte) 0, Collections.emptyList(), dependencies)); |
| 92 | + } |
| 93 | + |
| 94 | + List<String> dependencies = new ArrayList<>(); |
| 95 | + if (i < 999) { |
| 96 | + dependencies.add(next); |
| 97 | + } |
| 98 | + if (i < 998) { |
| 99 | + dependencies.add(afterNext); |
| 100 | + } |
| 101 | + dependentIntegrations1000.put(name, |
| 102 | + new TestIntegration(name, (byte) 0, Collections.emptyList(), dependencies)); |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + @Benchmark |
| 108 | + public List<SmithyIntegration<?, ?, ?>> sortIndependentIntegrations10(SmithyIntegrationsState state) { |
| 109 | + return SmithyIntegration.sort(state.independentIntegrations10.values()); |
| 110 | + } |
| 111 | + |
| 112 | + @Benchmark |
| 113 | + public List<SmithyIntegration<?, ?, ?>> sortIndependentIntegrations100(SmithyIntegrationsState state) { |
| 114 | + return SmithyIntegration.sort(state.independentIntegrations100.values()); |
| 115 | + } |
| 116 | + |
| 117 | + @Benchmark |
| 118 | + public List<SmithyIntegration<?, ?, ?>> sortIndependentIntegrations1000(SmithyIntegrationsState state) { |
| 119 | + return SmithyIntegration.sort(state.independentIntegrations1000.values()); |
| 120 | + } |
| 121 | + |
| 122 | + @Benchmark |
| 123 | + public List<SmithyIntegration<?, ?, ?>> sortDependentIntegrations10(SmithyIntegrationsState state) { |
| 124 | + return SmithyIntegration.sort(state.dependentIntegrations10.values()); |
| 125 | + } |
| 126 | + |
| 127 | + @Benchmark |
| 128 | + public List<SmithyIntegration<?, ?, ?>> sortDependentIntegrations100(SmithyIntegrationsState state) { |
| 129 | + return SmithyIntegration.sort(state.dependentIntegrations100.values()); |
| 130 | + } |
| 131 | + |
| 132 | + @Benchmark |
| 133 | + public List<SmithyIntegration<?, ?, ?>> sortDependentIntegrations1000(SmithyIntegrationsState state) { |
| 134 | + return SmithyIntegration.sort(state.dependentIntegrations1000.values()); |
| 135 | + } |
| 136 | + |
| 137 | + private static class IntegrationComparator implements Comparator<String> { |
| 138 | + |
| 139 | + private final Map<String, SmithyIntegration<?, ?, ?>> lookup; |
| 140 | + |
| 141 | + IntegrationComparator(Map<String, SmithyIntegration<?, ?, ?>> lookup) { |
| 142 | + this.lookup = lookup; |
| 143 | + } |
| 144 | + |
| 145 | + @Override |
| 146 | + public int compare(String o1, String o2) { |
| 147 | + SmithyIntegration<?, ?, ?> left = lookup.get(o1); |
| 148 | + SmithyIntegration<?, ?, ?> right = lookup.get(o2); |
| 149 | + if (left == null || right == null) { |
| 150 | + return 0; |
| 151 | + } |
| 152 | + return Byte.compare(left.priority(), right.priority()); |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + private static class TestImportContainer implements ImportContainer { |
| 157 | + @Override |
| 158 | + public void importSymbol(Symbol symbol, String alias) { |
| 159 | + |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + private static class TestSymbolWriter extends SymbolWriter<TestSymbolWriter, TestImportContainer> { |
| 164 | + public TestSymbolWriter(TestImportContainer importContainer) { |
| 165 | + super(importContainer); |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + private static class TestSettings {} |
| 170 | + |
| 171 | + private static class TestIntegration implements SmithyIntegration< |
| 172 | + TestSettings, |
| 173 | + TestSymbolWriter, |
| 174 | + CodegenContext<TestSettings, TestSymbolWriter, TestIntegration>> { |
| 175 | + |
| 176 | + private final String name; |
| 177 | + private final byte priority; |
| 178 | + private final List<String> runBefore; |
| 179 | + private final List<String> runAfter; |
| 180 | + |
| 181 | + TestIntegration(String name) { |
| 182 | + this(name, (byte) 0); |
| 183 | + } |
| 184 | + |
| 185 | + TestIntegration(String name, byte priority) { |
| 186 | + this(name, priority, Collections.emptyList()); |
| 187 | + } |
| 188 | + |
| 189 | + TestIntegration(String name, byte priority, List<String> runBefore) { |
| 190 | + this(name, priority, runBefore, Collections.emptyList()); |
| 191 | + } |
| 192 | + |
| 193 | + TestIntegration(String name, byte priority, List<String> runBefore, List<String> runAfter) { |
| 194 | + this.name = name; |
| 195 | + this.priority = priority; |
| 196 | + this.runBefore = runBefore; |
| 197 | + this.runAfter = runAfter; |
| 198 | + } |
| 199 | + |
| 200 | + @Override |
| 201 | + public String name() { |
| 202 | + return name; |
| 203 | + } |
| 204 | + |
| 205 | + @Override |
| 206 | + public byte priority() { |
| 207 | + return priority; |
| 208 | + } |
| 209 | + |
| 210 | + @Override |
| 211 | + public List<String> runBefore() { |
| 212 | + return runBefore; |
| 213 | + } |
| 214 | + |
| 215 | + @Override |
| 216 | + public List<String> runAfter() { |
| 217 | + return runAfter; |
| 218 | + } |
| 219 | + |
| 220 | + @Override |
| 221 | + public String toString() { |
| 222 | + return name(); |
| 223 | + } |
| 224 | + } |
4 | 225 | }
|
0 commit comments