-
Notifications
You must be signed in to change notification settings - Fork 328
UTF8 caching for v0.4 #9434
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
UTF8 caching for v0.4 #9434
Changes from 6 commits
8a59b29
68fdcb9
95767a6
5270f9c
69c4983
d725543
ebc3fb0
247bb02
69c94d1
d017b02
01aa284
bde8118
f15e1cc
947734a
f509c0a
6bfbf88
000de35
db82394
ff6e0f8
41d059d
3b69e62
4102a26
6902e80
9b78df7
3c33c38
41af3df
0b9f0d0
bdc1859
6ab19b0
49100cb
75bff75
bd17af9
f53ed6e
6d035cc
c923194
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,151 @@ | ||
| package datadog.trace.common.writer.ddagent; | ||
|
|
||
| import datadog.utf8.GeneratedTagUtf8Cache; | ||
| import datadog.utf8.GenerationalUtf8Cache; | ||
| import datadog.utf8.SimpleUtf8Cache; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.util.concurrent.ThreadLocalRandom; | ||
| import org.openjdk.jmh.annotations.Benchmark; | ||
| import org.openjdk.jmh.annotations.BenchmarkMode; | ||
| import org.openjdk.jmh.annotations.Mode; | ||
| import org.openjdk.jmh.infra.Blackhole; | ||
|
|
||
| /** | ||
| * This benchmark isn't really intended to used to measure throughput, but rather to be used with | ||
| * "-prof gc" to check bytes / op. | ||
| * | ||
| * <p>Since {@link String#getBytes(java.nio.charset.Charset)} is intrinsified the caches typically | ||
| * perform worse throughput wise, the benefit of the caches is to reduce allocation. | ||
| */ | ||
| @BenchmarkMode(Mode.Throughput) | ||
| public class Utf8Benchmark { | ||
| static final int NUM_LOOKUPS = 10_000; | ||
|
|
||
| static final String[] TAGS = { | ||
| "_dd.asm.keep", | ||
| "ci.provider", | ||
| "language", | ||
| "db.statement", | ||
| "ci.job.url", | ||
| "ci.pipeline.url", | ||
| "db.pool", | ||
| "http.forwarder", | ||
| "db.warehouse", | ||
| "custom" | ||
| }; | ||
|
|
||
| static int pos = 0; | ||
| static int standardVal = 0; | ||
|
|
||
| static final String nextTag() { | ||
| if (pos == TAGS.length - 1) { | ||
| pos = 0; | ||
| } else { | ||
| pos += 1; | ||
| } | ||
| return TAGS[pos]; | ||
| } | ||
|
|
||
| static final String nextValue(String tag) { | ||
| if (tag == "custom") { | ||
| return nextCustomValue(); | ||
| } else { | ||
| return nextStandardValue(tag); | ||
| } | ||
| } | ||
|
|
||
| static final String nextCustomValue() { | ||
| return "custom" + ThreadLocalRandom.current().nextInt(); | ||
| } | ||
|
|
||
| static final String nextStandardValue(String tag) { | ||
| return tag + ThreadLocalRandom.current().nextInt(20); | ||
| } | ||
|
|
||
| @Benchmark | ||
| public static final String tagUtf8_baseline() { | ||
| return nextTag(); | ||
| } | ||
|
|
||
| @Benchmark | ||
| public static final byte[] tagUtf8_nocache() { | ||
| String tag = nextTag(); | ||
| return tag.getBytes(StandardCharsets.UTF_8); | ||
| } | ||
|
|
||
| @Benchmark | ||
| public static final byte[] tagUtf8_w_generatedCache() { | ||
| String tag = nextTag(); | ||
|
|
||
| byte[] cache = GeneratedTagUtf8Cache.lookup(tag); | ||
| if (cache != null) return cache; | ||
|
|
||
| return tag.getBytes(StandardCharsets.UTF_8); | ||
| } | ||
|
|
||
| static final SimpleUtf8Cache TAG_CACHE = new SimpleUtf8Cache(); | ||
|
|
||
| @Benchmark | ||
| public static final byte[] tagUtf8_w_cache() { | ||
| String tag = nextTag(); | ||
|
|
||
| byte[] cache = TAG_CACHE.getUtf8(tag); | ||
| if (cache != null) return cache; | ||
|
|
||
| return tag.getBytes(StandardCharsets.UTF_8); | ||
| } | ||
|
|
||
| @Benchmark | ||
| public static final void valueUtf8_baseline(Blackhole bh) { | ||
| for (int i = 0; i < NUM_LOOKUPS; ++i) { | ||
| String tag = nextTag(); | ||
| String value = nextValue(tag); | ||
|
Comment on lines
+97
to
+98
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. question: Out of curiosity, should it it be better to generate a tag / value dataset outside the the benchmark methods ? Maybe this could allow to have datasets with wider range of values. I believe some customers have wide chars values (e.g. in korean) in their tag, would it be useful to have a benchmark for that, could the gains be more pronounced in this case ?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, probably. I need to experiment some more to figure out what's possible with JMH. |
||
|
|
||
| bh.consume(tag); | ||
| bh.consume(value); | ||
| } | ||
| } | ||
|
|
||
| static final GenerationalUtf8Cache VALUE_CACHE = new GenerationalUtf8Cache(); | ||
bric3 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| @Benchmark | ||
| public static final void valueUtf8_cache_generational(Blackhole bh) { | ||
| GenerationalUtf8Cache valueCache = VALUE_CACHE; | ||
| valueCache.recalibrate(); | ||
|
|
||
| for (int i = 0; i < NUM_LOOKUPS; ++i) { | ||
| String tag = nextTag(); | ||
| String value = nextValue(tag); | ||
|
|
||
| byte[] lookup = valueCache.getUtf8(value); | ||
| bh.consume(lookup); | ||
| } | ||
| } | ||
|
|
||
| static final SimpleUtf8Cache SIMPLE_VALUE_CACHE = new SimpleUtf8Cache(); | ||
|
|
||
| @Benchmark | ||
| public static final void valueUtf8_cache_simple(Blackhole bh) { | ||
| SimpleUtf8Cache valueCache = SIMPLE_VALUE_CACHE; | ||
| valueCache.recalibrate(); | ||
|
|
||
| for (int i = 0; i < NUM_LOOKUPS; ++i) { | ||
| String tag = nextTag(); | ||
| String value = nextValue(tag); | ||
|
|
||
| byte[] lookup = valueCache.getUtf8(value); | ||
| bh.consume(lookup); | ||
| } | ||
| } | ||
|
|
||
| @Benchmark | ||
| public static final void valueUtf8_nocache(Blackhole bh) { | ||
| for (int i = 0; i < NUM_LOOKUPS; ++i) { | ||
| String tag = nextTag(); | ||
| String value = nextValue(tag); | ||
|
|
||
| bh.consume(tag); | ||
| bh.consume(value.getBytes(StandardCharsets.UTF_8)); | ||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.