From cd3d41eb5d1e57b52e38ed51c5d0869fc6416be5 Mon Sep 17 00:00:00 2001 From: weizijun Date: Thu, 9 Dec 2021 10:39:29 +0800 Subject: [PATCH 1/4] add benchmark --- .../xcontent/FilterContentBenchmark.java | 82 ++++++++++++++++++- 1 file changed, 80 insertions(+), 2 deletions(-) diff --git a/benchmarks/src/main/java/org/elasticsearch/benchmark/xcontent/FilterContentBenchmark.java b/benchmarks/src/main/java/org/elasticsearch/benchmark/xcontent/FilterContentBenchmark.java index c6ff83762b014..06636c96b12c2 100644 --- a/benchmarks/src/main/java/org/elasticsearch/benchmark/xcontent/FilterContentBenchmark.java +++ b/benchmarks/src/main/java/org/elasticsearch/benchmark/xcontent/FilterContentBenchmark.java @@ -8,11 +8,18 @@ package org.elasticsearch.benchmark.xcontent; +import org.elasticsearch.common.Strings; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.io.Streams; import org.elasticsearch.common.io.stream.BytesStreamOutput; import org.elasticsearch.common.util.Maps; import org.elasticsearch.common.xcontent.XContentHelper; +import org.elasticsearch.common.xcontent.support.XContentMapValues; +import org.elasticsearch.search.fetch.subphase.FetchSourceContext; +import org.elasticsearch.search.fetch.subphase.FetchSourcePhase; +import org.elasticsearch.search.lookup.SourceLookup; +import org.elasticsearch.xcontent.DeprecationHandler; +import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentParserConfiguration; @@ -39,8 +46,8 @@ import java.util.stream.Collectors; @Fork(1) -@Warmup(iterations = 2) -@Measurement(iterations = 3) +@Warmup(iterations = 1) +@Measurement(iterations = 2) @BenchmarkMode(Mode.AverageTime) @OutputTimeUnit(TimeUnit.NANOSECONDS) @State(Scope.Benchmark) @@ -58,6 +65,7 @@ public class FilterContentBenchmark { private BytesReference source; private XContentParserConfiguration parserConfig; private Set filters; + private FetchSourceContext fetchContext; @Setup public void setup() throws IOException { @@ -78,6 +86,7 @@ public void setup() throws IOException { source = readSource(sourceFile); filters = buildFilters(); parserConfig = buildParseConfig(); + fetchContext = buildFetchSourceContext(); } private Set buildFilters() { @@ -122,6 +131,62 @@ public BytesReference filterWithNewParserConfig() throws IOException { return filter(contentParserConfiguration); } + @Benchmark + public BytesReference filterSourceLookupWithMap() throws IOException { + SourceLookup lookup = new SourceLookup(); + lookup.setSource(source); + Object value = lookup.filter(fetchContext); + return FetchSourcePhase.objectToBytes(value, XContentType.JSON, Math.min(1024, lookup.internalSourceRef().length())); + } + + @Benchmark + public BytesReference filterWithMap() throws IOException { + Map sourceMap = XContentHelper.convertToMap(source, false).v2(); + String[] includes; + String[] excludes; + if (inclusive) { + includes = filters.toArray(Strings.EMPTY_ARRAY); + excludes = null; + } else { + includes = null; + excludes = filters.toArray(Strings.EMPTY_ARRAY); + } + Map filterMap = XContentMapValues.filter(sourceMap, includes, excludes); + try (BytesStreamOutput os = new BytesStreamOutput()) { + XContentBuilder builder = new XContentBuilder(XContentType.JSON.xContent(), os); + builder.map(filterMap); + return BytesReference.bytes(builder); + } + } + + @Benchmark + public BytesReference filterWithBuilder() throws IOException { + BytesStreamOutput streamOutput = new BytesStreamOutput(Math.min(1024, source.length())); + Set includes; + Set excludes; + if (inclusive) { + includes = filters; + excludes = null; + } else { + includes = null; + excludes = filters; + } + XContentBuilder builder = new XContentBuilder( + XContentType.JSON.xContent(), + streamOutput, + includes, + excludes, + XContentType.JSON.toParsedMediaType() + ); + try ( + XContentParser parser = XContentType.JSON.xContent() + .createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, source.streamInput()) + ) { + builder.copyCurrentStructure(parser); + return BytesReference.bytes(builder); + } + } + private XContentParserConfiguration buildParseConfig() { Set includes; Set excludes; @@ -135,6 +200,19 @@ private XContentParserConfiguration buildParseConfig() { return XContentParserConfiguration.EMPTY.withFiltering(includes, excludes); } + private FetchSourceContext buildFetchSourceContext() { + String[] includes; + String[] excludes; + if (inclusive) { + includes = filters.toArray(Strings.EMPTY_ARRAY); + excludes = null; + } else { + includes = null; + excludes = filters.toArray(Strings.EMPTY_ARRAY); + } + return new FetchSourceContext(true, includes, excludes); + } + private BytesReference filter(XContentParserConfiguration contentParserConfiguration) throws IOException { try (BytesStreamOutput os = new BytesStreamOutput()) { XContentBuilder builder = new XContentBuilder(XContentType.JSON.xContent(), os); From de22e6fd5f653b64417d566dc2ecbc1e2f7b7544 Mon Sep 17 00:00:00 2001 From: weizijun Date: Thu, 9 Dec 2021 11:32:36 +0800 Subject: [PATCH 2/4] add benchmark --- .../xcontent/FilterContentBenchmark.java | 18 +++--------------- 1 file changed, 3 insertions(+), 15 deletions(-) diff --git a/benchmarks/src/main/java/org/elasticsearch/benchmark/xcontent/FilterContentBenchmark.java b/benchmarks/src/main/java/org/elasticsearch/benchmark/xcontent/FilterContentBenchmark.java index 06636c96b12c2..8c19839fb1062 100644 --- a/benchmarks/src/main/java/org/elasticsearch/benchmark/xcontent/FilterContentBenchmark.java +++ b/benchmarks/src/main/java/org/elasticsearch/benchmark/xcontent/FilterContentBenchmark.java @@ -131,14 +131,6 @@ public BytesReference filterWithNewParserConfig() throws IOException { return filter(contentParserConfiguration); } - @Benchmark - public BytesReference filterSourceLookupWithMap() throws IOException { - SourceLookup lookup = new SourceLookup(); - lookup.setSource(source); - Object value = lookup.filter(fetchContext); - return FetchSourcePhase.objectToBytes(value, XContentType.JSON, Math.min(1024, lookup.internalSourceRef().length())); - } - @Benchmark public BytesReference filterWithMap() throws IOException { Map sourceMap = XContentHelper.convertToMap(source, false).v2(); @@ -152,11 +144,7 @@ public BytesReference filterWithMap() throws IOException { excludes = filters.toArray(Strings.EMPTY_ARRAY); } Map filterMap = XContentMapValues.filter(sourceMap, includes, excludes); - try (BytesStreamOutput os = new BytesStreamOutput()) { - XContentBuilder builder = new XContentBuilder(XContentType.JSON.xContent(), os); - builder.map(filterMap); - return BytesReference.bytes(builder); - } + return FetchSourcePhase.objectToBytes(filterMap, XContentType.JSON, Math.min(1024, source.length())); } @Benchmark @@ -166,9 +154,9 @@ public BytesReference filterWithBuilder() throws IOException { Set excludes; if (inclusive) { includes = filters; - excludes = null; + excludes = Set.of(); } else { - includes = null; + includes = Set.of(); excludes = filters; } XContentBuilder builder = new XContentBuilder( From d01b3b7ad763ba673a9c156ca1bef9dffce9d068 Mon Sep 17 00:00:00 2001 From: weizijun Date: Thu, 9 Dec 2021 15:10:04 +0800 Subject: [PATCH 3/4] add benchmark --- .../xcontent/FilterContentBenchmark.java | 17 ----------------- 1 file changed, 17 deletions(-) diff --git a/benchmarks/src/main/java/org/elasticsearch/benchmark/xcontent/FilterContentBenchmark.java b/benchmarks/src/main/java/org/elasticsearch/benchmark/xcontent/FilterContentBenchmark.java index 8c19839fb1062..3c029a8586ee4 100644 --- a/benchmarks/src/main/java/org/elasticsearch/benchmark/xcontent/FilterContentBenchmark.java +++ b/benchmarks/src/main/java/org/elasticsearch/benchmark/xcontent/FilterContentBenchmark.java @@ -15,9 +15,7 @@ import org.elasticsearch.common.util.Maps; import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.common.xcontent.support.XContentMapValues; -import org.elasticsearch.search.fetch.subphase.FetchSourceContext; import org.elasticsearch.search.fetch.subphase.FetchSourcePhase; -import org.elasticsearch.search.lookup.SourceLookup; import org.elasticsearch.xcontent.DeprecationHandler; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.XContentBuilder; @@ -65,7 +63,6 @@ public class FilterContentBenchmark { private BytesReference source; private XContentParserConfiguration parserConfig; private Set filters; - private FetchSourceContext fetchContext; @Setup public void setup() throws IOException { @@ -86,7 +83,6 @@ public void setup() throws IOException { source = readSource(sourceFile); filters = buildFilters(); parserConfig = buildParseConfig(); - fetchContext = buildFetchSourceContext(); } private Set buildFilters() { @@ -188,19 +184,6 @@ private XContentParserConfiguration buildParseConfig() { return XContentParserConfiguration.EMPTY.withFiltering(includes, excludes); } - private FetchSourceContext buildFetchSourceContext() { - String[] includes; - String[] excludes; - if (inclusive) { - includes = filters.toArray(Strings.EMPTY_ARRAY); - excludes = null; - } else { - includes = null; - excludes = filters.toArray(Strings.EMPTY_ARRAY); - } - return new FetchSourceContext(true, includes, excludes); - } - private BytesReference filter(XContentParserConfiguration contentParserConfiguration) throws IOException { try (BytesStreamOutput os = new BytesStreamOutput()) { XContentBuilder builder = new XContentBuilder(XContentType.JSON.xContent(), os); From 717e2ca63fde25fdc8da71571624316ee3034ce9 Mon Sep 17 00:00:00 2001 From: weizijun Date: Thu, 9 Dec 2021 15:28:27 +0800 Subject: [PATCH 4/4] add benchmark --- .../benchmark/xcontent/FilterContentBenchmark.java | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/benchmarks/src/main/java/org/elasticsearch/benchmark/xcontent/FilterContentBenchmark.java b/benchmarks/src/main/java/org/elasticsearch/benchmark/xcontent/FilterContentBenchmark.java index 3c029a8586ee4..fe918861db5ba 100644 --- a/benchmarks/src/main/java/org/elasticsearch/benchmark/xcontent/FilterContentBenchmark.java +++ b/benchmarks/src/main/java/org/elasticsearch/benchmark/xcontent/FilterContentBenchmark.java @@ -16,8 +16,6 @@ import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.common.xcontent.support.XContentMapValues; import org.elasticsearch.search.fetch.subphase.FetchSourcePhase; -import org.elasticsearch.xcontent.DeprecationHandler; -import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentParserConfiguration; @@ -162,10 +160,7 @@ public BytesReference filterWithBuilder() throws IOException { excludes, XContentType.JSON.toParsedMediaType() ); - try ( - XContentParser parser = XContentType.JSON.xContent() - .createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, source.streamInput()) - ) { + try (XContentParser parser = XContentType.JSON.xContent().createParser(XContentParserConfiguration.EMPTY, source.streamInput())) { builder.copyCurrentStructure(parser); return BytesReference.bytes(builder); }