From 5b45184e17cf7893f1e2fb0480d70d207a0e3ef6 Mon Sep 17 00:00:00 2001 From: Finn Carroll Date: Wed, 9 Oct 2024 10:12:34 -0700 Subject: [PATCH] Clean up proto microbenchmark setup - Add method for each case --- .../search/SearchHitsProtobufBenchmark.java | 86 +++++++++++++++---- .../opensearch/search/SearchHitsTests.java | 22 +++-- 2 files changed, 81 insertions(+), 27 deletions(-) diff --git a/benchmarks/src/main/java/org/opensearch/benchmark/search/SearchHitsProtobufBenchmark.java b/benchmarks/src/main/java/org/opensearch/benchmark/search/SearchHitsProtobufBenchmark.java index 75cba8a0b6417..2b77851a165d7 100644 --- a/benchmarks/src/main/java/org/opensearch/benchmark/search/SearchHitsProtobufBenchmark.java +++ b/benchmarks/src/main/java/org/opensearch/benchmark/search/SearchHitsProtobufBenchmark.java @@ -30,10 +30,9 @@ import org.opensearch.common.xcontent.json.JsonXContent; import org.opensearch.core.common.io.stream.BytesStreamInput; import org.opensearch.core.xcontent.MediaType; -import org.opensearch.core.xcontent.MediaTypeRegistry; import org.opensearch.core.xcontent.ToXContent; import org.opensearch.core.xcontent.XContentBuilder; -import org.opensearch.proto.search.SearchHitsProtoDef; +import org.opensearch.proto.search.SearchHitsProtoDef.SearchHitsProto; import org.opensearch.search.SearchHits; import org.opensearch.transport.protobuf.SearchHitsProtobuf; @@ -44,35 +43,84 @@ @OutputTimeUnit(TimeUnit.MILLISECONDS) @State(Scope.Benchmark) public class SearchHitsProtobufBenchmark { + String READ_PATH = "/tmp/testFiles"; + int TEST_FILES = 400; - List searchHits = new ArrayList<>(); + // Setup proto and native representations of SearchHits + List searchHitsProtoItems = new ArrayList<>(); + List searchHitsNativePojoItems = new ArrayList<>(); + + /* + NOTE: Test items are read from disk. Randomized SearchHits are generated with SearchHitsTests.createTestItem(). + + To generate test items and run all microbenchmarks in this class: + ./gradlew server:test --tests "org.opensearch.search.SearchHitsTests.testMicroBenchmarkHackGenerateTestFiles" -Dtests.security.manager=false + ./gradlew -p benchmarks run --args 'SearchHitsProtobufBenchmark' + */ @Setup public void setup() throws IOException { - Path dir = Paths.get("/Users/carrofin/fdev/repos/OpenSearch/benchmarks/src/main/java/org/opensearch/benchmark/search/testFiles"); + Path dir = Paths.get(READ_PATH); - for(int i = 0; i < 100; i++) { + for(int i = 0; i < TEST_FILES; i++) { Path testFile = dir.resolve("testItem_" + i); - InputStream in = Files.newInputStream(testFile); - BytesStreamInput sin = new BytesStreamInput(in.readAllBytes()); - SearchHitsProtobuf testItem = new SearchHitsProtobuf(new SearchHits(sin)); - searchHits.add(testItem.toProto()); + try (InputStream in = Files.newInputStream(testFile)) { + BytesStreamInput sin = new BytesStreamInput(in.readAllBytes()); + + SearchHits sh = new SearchHits(sin); + SearchHitsProto shProto = new SearchHitsProtobuf(sh).toProto(); + + searchHitsNativePojoItems.add(sh); + searchHitsProtoItems.add(shProto); + } + } + } + + /* + ./gradlew -p benchmarks run --args 'SearchHitsProtobufBenchmark.writeToNativeBench' + */ + @Benchmark + public BytesStreamOutput writeToNativeBench() throws IOException { + BytesStreamOutput bytes = new BytesStreamOutput(); + for (SearchHits sh : searchHitsNativePojoItems) { + sh.writeTo(bytes); } + return bytes; } -// @Benchmark -// public BytesStreamOutput writeToBench() throws IOException { -// BytesStreamOutput bytes = new BytesStreamOutput(); -// for (SearchHitsProtobuf shProto : searchHits) { -// shProto.writeTo(bytes); -// } -// return bytes; -// } + /* + ./gradlew -p benchmarks run --args 'SearchHitsProtobufBenchmark.writeToProtoBench' + */ + @Benchmark + public BytesStreamOutput writeToProtoBench() throws IOException { + BytesStreamOutput bytes = new BytesStreamOutput(); + for (SearchHitsProto shProto : searchHitsProtoItems) { + shProto.writeTo(bytes); + } + return bytes; + } + + /* + ./gradlew -p benchmarks run --args 'SearchHitsProtobufBenchmark.toXContNativeProtoBench' + */ + public List toXContNativeBench() throws IOException { + List XContList = new ArrayList<>(); + for (SearchHits sh : searchHitsNativePojoItems) { + XContentBuilder builder = JsonXContent.contentBuilder(); + sh.toXContent(builder, ToXContent.EMPTY_PARAMS); + XContList.add(builder); + } + + return XContList; + } + /* + ./gradlew -p benchmarks run --args 'SearchHitsProtobufBenchmark.toXContProtoBench' + */ @Benchmark - public List toXContBench() throws IOException { + public List toXContProtoBench() throws IOException { List XContList = new ArrayList<>(); - for (SearchHitsProtoDef.SearchHitsProto sh : searchHits) { + for (SearchHitsProto sh : searchHitsProtoItems) { XContentBuilder builder = JsonXContent.contentBuilder(); builder.startObject(); builder.rawField("protobuf", sh.toByteString().newInput(), MediaType.fromMediaType("application/octet-stream")); diff --git a/server/src/test/java/org/opensearch/search/SearchHitsTests.java b/server/src/test/java/org/opensearch/search/SearchHitsTests.java index 35aae34bb97e6..769f8ce19c6ec 100644 --- a/server/src/test/java/org/opensearch/search/SearchHitsTests.java +++ b/server/src/test/java/org/opensearch/search/SearchHitsTests.java @@ -42,7 +42,6 @@ import org.opensearch.common.xcontent.XContentType; import org.opensearch.common.xcontent.json.JsonXContent; import org.opensearch.core.common.bytes.BytesReference; -import org.opensearch.core.common.io.stream.StreamOutput; import org.opensearch.core.common.io.stream.Writeable; import org.opensearch.core.index.Index; import org.opensearch.core.index.shard.ShardId; @@ -55,19 +54,28 @@ import java.io.FileOutputStream; import java.io.IOException; import java.nio.file.Path; +import java.nio.file.Paths; import java.util.Collections; import java.util.function.Predicate; import java.nio.file.Files; public class SearchHitsTests extends AbstractSerializingTestCase { - public void testFinnHack () throws IOException { - // HACK HACK HACK HACK HACK HACK HACK HACK HACK - Path tmp = createTempDir(); - for(int i = 0; i < 100; i++) { + /* + Hack to generate a randomized set of SearchHits objects for the purpose of benchmarking serialization ops. + See SearchHitsProtobufBenchmark.java. + + Note: Security manager needs to be disabled. + +f */ + public void testMicroBenchmarkHackGenerateTestFiles () throws IOException { + String WRITE_PATH = "/tmp/testFiles"; + int TEST_FILES = 400; + + Path tmp = Files.createDirectory(Paths.get(WRITE_PATH)); + for(int i = 0; i < TEST_FILES; i++) { Path pth = tmp.resolve("testItem_" + i); Files.createFile(pth); - SearchHits sh = createTestItem(XContentType.JSON, true, true); try (FileOutputStream out = new FileOutputStream(pth.toFile())) { @@ -76,8 +84,6 @@ public void testFinnHack () throws IOException { bytes.copyBytes().writeTo(out); } } - - System.out.println("DEBUG BREAK"); } public static SearchHits createTestItem(boolean withOptionalInnerHits, boolean withShardTarget) {