|
| 1 | +package datadog.trace.agent.tooling; |
| 2 | + |
| 3 | +import datadog.trace.agent.tooling.bytebuddy.matcher.ProxyIgnoredClassNameTrie; |
| 4 | +import java.util.concurrent.TimeUnit; |
| 5 | +import org.openjdk.jmh.annotations.Benchmark; |
| 6 | +import org.openjdk.jmh.annotations.BenchmarkMode; |
| 7 | +import org.openjdk.jmh.annotations.Fork; |
| 8 | +import org.openjdk.jmh.annotations.Measurement; |
| 9 | +import org.openjdk.jmh.annotations.Mode; |
| 10 | +import org.openjdk.jmh.annotations.OutputTimeUnit; |
| 11 | +import org.openjdk.jmh.annotations.Param; |
| 12 | +import org.openjdk.jmh.annotations.Scope; |
| 13 | +import org.openjdk.jmh.annotations.State; |
| 14 | +import org.openjdk.jmh.annotations.Warmup; |
| 15 | +import org.openjdk.jmh.infra.Blackhole; |
| 16 | + |
| 17 | +/* |
| 18 | +Benchmark (className) Mode Cnt Score Error Units |
| 19 | +ProxyIgnoreBenchmark.useContains org.springframework.util.ConcurrentReferenceHashMap$4 thrpt 2 0.007 ops/ns |
| 20 | +ProxyIgnoreBenchmark.useContains java.lang.invoke.LambdaForm$DMH/0x00007fe9f0388000 thrpt 2 0.008 ops/ns |
| 21 | +ProxyIgnoreBenchmark.useContains org.springframework.core.annotation.RepeatableContainers$StandardRepeatableContainers$$Lambda$315/0x00007fe9f03adc70 thrpt 2 0.003 ops/ns |
| 22 | +ProxyIgnoreBenchmark.useContains datadog.test.package.redis.RedisTemplateProvider$$TestCGLIB$$FastClass$$0 thrpt 2 0.025 ops/ns |
| 23 | +ProxyIgnoreBenchmark.useTrie org.springframework.util.ConcurrentReferenceHashMap$4 thrpt 2 0.026 ops/ns |
| 24 | +ProxyIgnoreBenchmark.useTrie java.lang.invoke.LambdaForm$DMH/0x00007fe9f0388000 thrpt 2 0.026 ops/ns |
| 25 | +ProxyIgnoreBenchmark.useTrie org.springframework.core.annotation.RepeatableContainers$StandardRepeatableContainers$$Lambda$315/0x00007fe9f03adc70 thrpt 2 0.009 ops/ns |
| 26 | +ProxyIgnoreBenchmark.useTrie datadog.test.package.redis.RedisTemplateProvider$$TestCGLIB$$FastClass$$0 thrpt 2 0.029 ops/ns |
| 27 | +*/ |
| 28 | +@State(Scope.Benchmark) |
| 29 | +@BenchmarkMode(Mode.Throughput) |
| 30 | +@OutputTimeUnit(TimeUnit.NANOSECONDS) |
| 31 | +@Measurement(iterations = 2, time = 5, timeUnit = TimeUnit.SECONDS) |
| 32 | +@Warmup(iterations = 1) |
| 33 | +@Fork(value = 1) |
| 34 | +public class ProxyIgnoreBenchmark { |
| 35 | + @Param({ |
| 36 | + "org.springframework.util.ConcurrentReferenceHashMap$4", |
| 37 | + "java.lang.invoke.LambdaForm$DMH/0x00007fe9f0388000", |
| 38 | + "org.springframework.core.annotation.RepeatableContainers$StandardRepeatableContainers$$Lambda$315/0x00007fe9f03adc70", |
| 39 | + // worst case for trie based |
| 40 | + "datadog.test.package.redis.RedisTemplateProvider$$TestCGLIB$$FastClass$$0" |
| 41 | + }) |
| 42 | + public String className; |
| 43 | + |
| 44 | + @Benchmark |
| 45 | + public void useContains(Blackhole bh) { |
| 46 | + if (className.indexOf('$') > -1) { |
| 47 | + if (className.contains("$JaxbAccessor") |
| 48 | + || className.contains("CGLIB$$") |
| 49 | + || className.contains("$__sisu") |
| 50 | + || className.contains("$$EnhancerByGuice$$") |
| 51 | + || className.contains("$$EnhancerByProxool$$") |
| 52 | + || className.contains("$$$view") |
| 53 | + || className.contains("$$$endpoint") // jboss mdb proxies |
| 54 | + || className.contains("$$_Weld") |
| 55 | + || className.contains("_$$_jvst")) { |
| 56 | + bh.consume(true); |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + @Benchmark |
| 62 | + public void useTrie(Blackhole bh) { |
| 63 | + int last = -1; |
| 64 | + int idx; |
| 65 | + while (true) { |
| 66 | + idx = className.indexOf('$', last + 1); |
| 67 | + if (idx < 0) { |
| 68 | + break; |
| 69 | + } |
| 70 | + if (last < 0 && className.contains("CGLIB$$")) { |
| 71 | + break; |
| 72 | + } |
| 73 | + if (idx == last + 1) { |
| 74 | + // skip the trie if consecutive $$ since, to be efficient, we can match prefixes from the |
| 75 | + // first dollar |
| 76 | + last = idx; |
| 77 | + continue; |
| 78 | + } |
| 79 | + last = idx; |
| 80 | + if (ProxyIgnoredClassNameTrie.apply(className, idx) >= 0) { |
| 81 | + return; |
| 82 | + } |
| 83 | + } |
| 84 | + bh.consume(true); |
| 85 | + } |
| 86 | +} |
0 commit comments