|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.solr.bench.search; |
| 18 | + |
| 19 | +import static org.apache.solr.bench.Docs.docs; |
| 20 | +import static org.apache.solr.bench.generators.SourceDSL.integers; |
| 21 | +import static org.apache.solr.bench.generators.SourceDSL.longs; |
| 22 | +import static org.apache.solr.bench.generators.SourceDSL.strings; |
| 23 | + |
| 24 | +import java.io.IOException; |
| 25 | +import java.io.OutputStream; |
| 26 | +import java.util.Iterator; |
| 27 | +import java.util.function.Supplier; |
| 28 | +import org.apache.commons.io.output.NullOutputStream; |
| 29 | +import org.apache.solr.bench.Docs; |
| 30 | +import org.apache.solr.client.solrj.request.JavaBinRequestWriter; |
| 31 | +import org.apache.solr.client.solrj.request.RequestWriter; |
| 32 | +import org.apache.solr.client.solrj.request.UpdateRequest; |
| 33 | +import org.apache.solr.client.solrj.request.XMLRequestWriter; |
| 34 | +import org.apache.solr.common.SolrInputDocument; |
| 35 | +import org.openjdk.jmh.annotations.Benchmark; |
| 36 | +import org.openjdk.jmh.annotations.BenchmarkMode; |
| 37 | +import org.openjdk.jmh.annotations.Fork; |
| 38 | +import org.openjdk.jmh.annotations.Level; |
| 39 | +import org.openjdk.jmh.annotations.Measurement; |
| 40 | +import org.openjdk.jmh.annotations.Mode; |
| 41 | +import org.openjdk.jmh.annotations.Param; |
| 42 | +import org.openjdk.jmh.annotations.Scope; |
| 43 | +import org.openjdk.jmh.annotations.Setup; |
| 44 | +import org.openjdk.jmh.annotations.State; |
| 45 | +import org.openjdk.jmh.annotations.Threads; |
| 46 | +import org.openjdk.jmh.annotations.Warmup; |
| 47 | + |
| 48 | +/** |
| 49 | + * Benchmark for serialization of requests by the client. This only focuses on the serialization |
| 50 | + * itself, ignoring sending the request over the network (and for sure ignoring processing the |
| 51 | + * request). |
| 52 | + */ |
| 53 | +@Fork(value = 1) |
| 54 | +@BenchmarkMode(Mode.Throughput) |
| 55 | +@Warmup(time = 2, iterations = 1) |
| 56 | +@Measurement(time = 5, iterations = 5) |
| 57 | +@Threads(value = 1) |
| 58 | +public class RequestWriters { |
| 59 | + |
| 60 | + @State(Scope.Benchmark) |
| 61 | + public static class BenchState { |
| 62 | + |
| 63 | + @Param({"xml", "binary"}) |
| 64 | + String type; |
| 65 | + |
| 66 | + @Param({"10", "100", "1000", "10000"}) |
| 67 | + int batchSize; |
| 68 | + |
| 69 | + private final int docCount = 50000; |
| 70 | + |
| 71 | + private Supplier<RequestWriter> writerSupplier; |
| 72 | + private Iterator<SolrInputDocument> docIterator; |
| 73 | + |
| 74 | + @Setup(Level.Trial) |
| 75 | + public void setup() throws Exception { |
| 76 | + preGenerateDocs(); |
| 77 | + |
| 78 | + switch (type) { |
| 79 | + case "xml": |
| 80 | + writerSupplier = XMLRequestWriter::new; |
| 81 | + break; |
| 82 | + case "javabin": |
| 83 | + writerSupplier = JavaBinRequestWriter::new; |
| 84 | + break; |
| 85 | + |
| 86 | + default: |
| 87 | + throw new Error("Unsupported type: " + type); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + private void preGenerateDocs() throws Exception { |
| 92 | + Docs docs = |
| 93 | + docs() |
| 94 | + .field("id", integers().incrementing()) |
| 95 | + .field(strings().basicLatinAlphabet().ofLengthBetween(10, 64)) |
| 96 | + .field(strings().basicLatinAlphabet().ofLengthBetween(10, 64)) |
| 97 | + .field(strings().basicLatinAlphabet().multi(312).ofLengthBetween(10, 64)) |
| 98 | + .field(strings().basicLatinAlphabet().multi(312).ofLengthBetween(10, 64)) |
| 99 | + .field(integers().all()) |
| 100 | + .field(integers().all()) |
| 101 | + .field(longs().all()); |
| 102 | + |
| 103 | + docs.preGenerate(docCount); |
| 104 | + docIterator = docs.generatedDocsCircularIterator(); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + @Benchmark |
| 109 | + public void writeUpdate(BenchState state) throws IOException { |
| 110 | + |
| 111 | + OutputStream sink = NullOutputStream.INSTANCE; |
| 112 | + |
| 113 | + UpdateRequest request = new UpdateRequest(); |
| 114 | + for (int i = 0; i < state.batchSize; i++) { |
| 115 | + request.add(state.docIterator.next()); |
| 116 | + } |
| 117 | + |
| 118 | + RequestWriter writer = state.writerSupplier.get(); |
| 119 | + writer.write(request, sink); |
| 120 | + } |
| 121 | +} |
0 commit comments