|
8 | 8 |
|
9 | 9 | package org.elasticsearch.benchmark.xcontent;
|
10 | 10 |
|
| 11 | +import org.elasticsearch.common.Strings; |
11 | 12 | import org.elasticsearch.common.bytes.BytesReference;
|
12 | 13 | import org.elasticsearch.common.io.Streams;
|
13 | 14 | import org.elasticsearch.common.io.stream.BytesStreamOutput;
|
14 | 15 | import org.elasticsearch.common.util.Maps;
|
15 | 16 | import org.elasticsearch.common.xcontent.XContentHelper;
|
| 17 | +import org.elasticsearch.common.xcontent.support.XContentMapValues; |
| 18 | +import org.elasticsearch.search.fetch.subphase.FetchSourcePhase; |
16 | 19 | import org.elasticsearch.xcontent.XContentBuilder;
|
17 | 20 | import org.elasticsearch.xcontent.XContentParser;
|
18 | 21 | import org.elasticsearch.xcontent.XContentParserConfiguration;
|
|
39 | 42 | import java.util.stream.Collectors;
|
40 | 43 |
|
41 | 44 | @Fork(1)
|
42 |
| -@Warmup(iterations = 2) |
43 |
| -@Measurement(iterations = 3) |
| 45 | +@Warmup(iterations = 1) |
| 46 | +@Measurement(iterations = 2) |
44 | 47 | @BenchmarkMode(Mode.AverageTime)
|
45 | 48 | @OutputTimeUnit(TimeUnit.NANOSECONDS)
|
46 | 49 | @State(Scope.Benchmark)
|
@@ -122,6 +125,47 @@ public BytesReference filterWithNewParserConfig() throws IOException {
|
122 | 125 | return filter(contentParserConfiguration);
|
123 | 126 | }
|
124 | 127 |
|
| 128 | + @Benchmark |
| 129 | + public BytesReference filterWithMap() throws IOException { |
| 130 | + Map<String, Object> sourceMap = XContentHelper.convertToMap(source, false).v2(); |
| 131 | + String[] includes; |
| 132 | + String[] excludes; |
| 133 | + if (inclusive) { |
| 134 | + includes = filters.toArray(Strings.EMPTY_ARRAY); |
| 135 | + excludes = null; |
| 136 | + } else { |
| 137 | + includes = null; |
| 138 | + excludes = filters.toArray(Strings.EMPTY_ARRAY); |
| 139 | + } |
| 140 | + Map<String, Object> filterMap = XContentMapValues.filter(sourceMap, includes, excludes); |
| 141 | + return FetchSourcePhase.objectToBytes(filterMap, XContentType.JSON, Math.min(1024, source.length())); |
| 142 | + } |
| 143 | + |
| 144 | + @Benchmark |
| 145 | + public BytesReference filterWithBuilder() throws IOException { |
| 146 | + BytesStreamOutput streamOutput = new BytesStreamOutput(Math.min(1024, source.length())); |
| 147 | + Set<String> includes; |
| 148 | + Set<String> excludes; |
| 149 | + if (inclusive) { |
| 150 | + includes = filters; |
| 151 | + excludes = Set.of(); |
| 152 | + } else { |
| 153 | + includes = Set.of(); |
| 154 | + excludes = filters; |
| 155 | + } |
| 156 | + XContentBuilder builder = new XContentBuilder( |
| 157 | + XContentType.JSON.xContent(), |
| 158 | + streamOutput, |
| 159 | + includes, |
| 160 | + excludes, |
| 161 | + XContentType.JSON.toParsedMediaType() |
| 162 | + ); |
| 163 | + try (XContentParser parser = XContentType.JSON.xContent().createParser(XContentParserConfiguration.EMPTY, source.streamInput())) { |
| 164 | + builder.copyCurrentStructure(parser); |
| 165 | + return BytesReference.bytes(builder); |
| 166 | + } |
| 167 | + } |
| 168 | + |
125 | 169 | private XContentParserConfiguration buildParseConfig() {
|
126 | 170 | Set<String> includes;
|
127 | 171 | Set<String> excludes;
|
|
0 commit comments