|
| 1 | +package org.elasticsearch.benchmark.search.fetch.subphase; |
| 2 | + |
| 3 | +import org.elasticsearch.common.Strings; |
| 4 | +import org.elasticsearch.common.bytes.BytesArray; |
| 5 | +import org.elasticsearch.common.bytes.BytesReference; |
| 6 | +import org.elasticsearch.common.io.Streams; |
| 7 | +import org.elasticsearch.common.io.stream.BytesStreamOutput; |
| 8 | +import org.elasticsearch.common.xcontent.DeprecationHandler; |
| 9 | +import org.elasticsearch.common.xcontent.NamedXContentRegistry; |
| 10 | +import org.elasticsearch.common.xcontent.XContentBuilder; |
| 11 | +import org.elasticsearch.common.xcontent.XContentParser; |
| 12 | +import org.elasticsearch.common.xcontent.XContentType; |
| 13 | +import org.elasticsearch.common.xcontent.support.filtering.FilterPath; |
| 14 | +import org.elasticsearch.search.fetch.subphase.FetchSourceContext; |
| 15 | +import org.elasticsearch.search.fetch.subphase.FetchSourcePhase; |
| 16 | +import org.elasticsearch.search.lookup.SourceLookup; |
| 17 | +import org.openjdk.jmh.annotations.Benchmark; |
| 18 | +import org.openjdk.jmh.annotations.BenchmarkMode; |
| 19 | +import org.openjdk.jmh.annotations.Fork; |
| 20 | +import org.openjdk.jmh.annotations.Measurement; |
| 21 | +import org.openjdk.jmh.annotations.Mode; |
| 22 | +import org.openjdk.jmh.annotations.OutputTimeUnit; |
| 23 | +import org.openjdk.jmh.annotations.Param; |
| 24 | +import org.openjdk.jmh.annotations.Scope; |
| 25 | +import org.openjdk.jmh.annotations.Setup; |
| 26 | +import org.openjdk.jmh.annotations.State; |
| 27 | +import org.openjdk.jmh.annotations.Warmup; |
| 28 | + |
| 29 | +import java.io.IOException; |
| 30 | +import java.util.Collections; |
| 31 | +import java.util.Set; |
| 32 | +import java.util.concurrent.TimeUnit; |
| 33 | + |
| 34 | +@Fork(1) |
| 35 | +@Warmup(iterations = 5) |
| 36 | +@Measurement(iterations = 5) |
| 37 | +@BenchmarkMode(Mode.AverageTime) |
| 38 | +@OutputTimeUnit(TimeUnit.NANOSECONDS) |
| 39 | +@State(Scope.Benchmark) |
| 40 | +public class FetchSourcePhaseBenchmark { |
| 41 | + private BytesReference sourceBytes; |
| 42 | + private FetchSourceContext fetchContext; |
| 43 | + private Set<String> includesSet; |
| 44 | + private Set<String> excludesSet; |
| 45 | + private FilterPath[] includesFilters; |
| 46 | + private FilterPath[] excludesFilters; |
| 47 | + |
| 48 | + @Param({ "tiny", "short", "one_4k_field", "one_4m_field" }) |
| 49 | + private String source; |
| 50 | + @Param({ "message" }) |
| 51 | + private String includes; |
| 52 | + @Param({ "" }) |
| 53 | + private String excludes; |
| 54 | + |
| 55 | + @Setup |
| 56 | + public void setup() throws IOException { |
| 57 | + switch (source) { |
| 58 | + case "tiny": |
| 59 | + sourceBytes = new BytesArray("{\"message\": \"short\"}"); |
| 60 | + break; |
| 61 | + case "short": |
| 62 | + sourceBytes = read300BytesExample(); |
| 63 | + break; |
| 64 | + case "one_4k_field": |
| 65 | + sourceBytes = buildBigExample(String.join("", Collections.nCopies(1024, "huge"))); |
| 66 | + break; |
| 67 | + case "one_4m_field": |
| 68 | + sourceBytes = buildBigExample(String.join("", Collections.nCopies(1024 * 1024, "huge"))); |
| 69 | + break; |
| 70 | + default: |
| 71 | + throw new IllegalArgumentException("Unknown source [" + source + "]"); |
| 72 | + } |
| 73 | + fetchContext = new FetchSourceContext( |
| 74 | + true, |
| 75 | + Strings.splitStringByCommaToArray(includes), |
| 76 | + Strings.splitStringByCommaToArray(excludes) |
| 77 | + ); |
| 78 | + includesSet = org.elasticsearch.core.Set.of(fetchContext.includes()); |
| 79 | + excludesSet = org.elasticsearch.core.Set.of(fetchContext.excludes()); |
| 80 | + includesFilters = FilterPath.compile(includesSet); |
| 81 | + excludesFilters = FilterPath.compile(excludesSet); |
| 82 | + } |
| 83 | + |
| 84 | + private BytesReference read300BytesExample() throws IOException { |
| 85 | + return Streams.readFully(FetchSourcePhaseBenchmark.class.getResourceAsStream("300b_example.json")); |
| 86 | + } |
| 87 | + |
| 88 | + private BytesReference buildBigExample(String extraText) throws IOException { |
| 89 | + String bigger = read300BytesExample().utf8ToString(); |
| 90 | + bigger = "{\"huge\": \"" + extraText + "\"," + bigger.substring(1); |
| 91 | + return new BytesArray(bigger); |
| 92 | + } |
| 93 | + |
| 94 | + @Benchmark |
| 95 | + public BytesReference filterObjects() throws IOException { |
| 96 | + SourceLookup lookup = new SourceLookup(); |
| 97 | + lookup.setSource(sourceBytes); |
| 98 | + Object value = lookup.filter(fetchContext); |
| 99 | + return FetchSourcePhase.objectToBytes(value, XContentType.JSON, Math.min(1024, lookup.internalSourceRef().length())); |
| 100 | + } |
| 101 | + |
| 102 | + @Benchmark |
| 103 | + public BytesReference filterXContentOnParser() throws IOException { |
| 104 | + BytesStreamOutput streamOutput = new BytesStreamOutput(Math.min(1024, sourceBytes.length())); |
| 105 | + XContentBuilder builder = new XContentBuilder(XContentType.JSON.xContent(), streamOutput); |
| 106 | + try ( |
| 107 | + XContentParser parser = XContentType.JSON.xContent() |
| 108 | + .createParser( |
| 109 | + NamedXContentRegistry.EMPTY, |
| 110 | + DeprecationHandler.THROW_UNSUPPORTED_OPERATION, |
| 111 | + sourceBytes.streamInput(), |
| 112 | + includesFilters, |
| 113 | + excludesFilters |
| 114 | + ) |
| 115 | + ) { |
| 116 | + builder.copyCurrentStructure(parser); |
| 117 | + return BytesReference.bytes(builder); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + @Benchmark |
| 122 | + public BytesReference filterXContentOnBuilder() throws IOException { |
| 123 | + BytesStreamOutput streamOutput = new BytesStreamOutput(Math.min(1024, sourceBytes.length())); |
| 124 | + XContentBuilder builder = new XContentBuilder(XContentType.JSON.xContent(), streamOutput, includesSet, excludesSet); |
| 125 | + try ( |
| 126 | + XContentParser parser = XContentType.JSON.xContent() |
| 127 | + .createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, sourceBytes.streamInput()) |
| 128 | + ) { |
| 129 | + builder.copyCurrentStructure(parser); |
| 130 | + return BytesReference.bytes(builder); |
| 131 | + } |
| 132 | + } |
| 133 | +} |
0 commit comments