markrmiller commented on a change in pull request #214:
URL: https://github.com/apache/solr/pull/214#discussion_r683081024



##########
File path: solr/benchmark/src/java/org/apache/solr/bench/MiniClusterState.java
##########
@@ -0,0 +1,370 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.solr.bench;
+
+import static org.apache.commons.io.file.PathUtils.deleteDirectory;
+
+import com.codahale.metrics.Meter;
+import java.io.IOException;
+import java.lang.management.ManagementFactory;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Random;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.ThreadLocalRandom;
+import java.util.concurrent.ThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
+import org.apache.commons.io.output.NullPrintStream;
+import org.apache.solr.client.solrj.SolrClient;
+import org.apache.solr.client.solrj.SolrQuery;
+import org.apache.solr.client.solrj.embedded.JettySolrRunner;
+import org.apache.solr.client.solrj.impl.Http2SolrClient;
+import org.apache.solr.client.solrj.request.CollectionAdminRequest;
+import org.apache.solr.client.solrj.request.QueryRequest;
+import org.apache.solr.client.solrj.request.UpdateRequest;
+import org.apache.solr.cloud.MiniSolrCloudCluster;
+import org.apache.solr.common.SolrInputDocument;
+import org.apache.solr.common.params.ModifiableSolrParams;
+import org.apache.solr.common.util.IOUtils;
+import org.apache.solr.common.util.NamedList;
+import org.apache.solr.common.util.SolrNamedThreadFactory;
+import org.apache.solr.common.util.SuppressForbidden;
+import org.openjdk.jmh.annotations.Level;
+import org.openjdk.jmh.annotations.Scope;
+import org.openjdk.jmh.annotations.Setup;
+import org.openjdk.jmh.annotations.State;
+import org.openjdk.jmh.annotations.TearDown;
+import org.openjdk.jmh.infra.BenchmarkParams;
+import org.openjdk.jmh.infra.Control;
+
+/** The base class for Solr JMH benchmarks that operate against a {@link 
MiniSolrCloudCluster}. */
+public class MiniClusterState {
+
+  public static final boolean DEBUG_OUTPUT = false;
+
+  public static final int PROC_COUNT =
+      ManagementFactory.getOperatingSystemMXBean().getAvailableProcessors();
+
+  private static boolean quietLog = Boolean.getBoolean("quietLog");
+
+  @SuppressForbidden(reason = "JMH uses std out for user output")
+  public static void log(String value) {
+    if (!quietLog) {
+      System.out.println((value.equals("") ? "" : "--> ") + value);
+    }
+  }
+
+  @State(Scope.Benchmark)
+  public static class MiniClusterBenchState {
+
+    boolean metricsEnabled = true;
+
+    public List<String> nodes;
+    MiniSolrCloudCluster cluster;
+    public SolrClient client;
+
+    int runCnt = 0;
+
+    boolean createCollectionAndIndex = true;
+
+    boolean deleteMiniCluster = true;
+
+    Path baseDir;
+    boolean allowClusterReuse = false;
+
+    ThreadPoolExecutor exec;
+
+    boolean isWarmup;
+
+    @TearDown(Level.Iteration)
+    public void tearDown(BenchmarkParams benchmarkParams) throws Exception {
+
+      // dump Solr metrics
+      Path metricsResults =
+          Paths.get(
+              "work/metrics-results",
+              benchmarkParams.id(),
+              String.valueOf(runCnt++),
+              benchmarkParams.getBenchmark() + ".txt");
+      if (!Files.exists(metricsResults.getParent())) {
+        Files.createDirectories(metricsResults.getParent());
+      }
+
+      cluster.outputMetrics(
+          metricsResults.getParent().toFile(), 
metricsResults.getFileName().toString());
+    }
+
+    @Setup(Level.Iteration)
+    public void checkWarmUp(Control control) throws Exception {
+      isWarmup = control.stopMeasurement;
+    }
+
+    @TearDown(Level.Trial)
+    public void shutdownMiniCluster() throws Exception {
+      if (DEBUG_OUTPUT) log("closing client and shutting down minicluster");
+      IOUtils.closeQuietly(client);
+      cluster.shutdown();
+    }
+
+    @Setup(Level.Trial)
+    public void doSetup(BenchmarkParams benchmarkParams) throws Exception {
+
+      MiniClusterState.log("");
+      Path currentRelativePath = Paths.get("");
+      String s = currentRelativePath.toAbsolutePath().toString();
+      log("current relative path is: " + s);
+
+      Long seed = Long.getLong("solr.bench.seed");
+
+      if (seed == null) {
+        seed = ThreadLocalRandom.current().nextLong();
+      }
+
+      // set the seed used by ThreadLocalRandom
+      System.setProperty("randomSeed", Long.toString(new 
Random(seed).nextLong()));
+
+      System.setProperty("pkiHandlerPrivateKeyPath", "");
+      System.setProperty("pkiHandlerPublicKeyPath", "");
+
+      System.setProperty("solr.log.name", benchmarkParams.id());
+
+      System.setProperty("solr.default.confdir", 
"../server/solr/configsets/_default");
+
+      // not currently usable, but would enable JettySolrRunner's 
ill-conceived jetty.testMode and
+      // allow using SSL
+
+      // System.getProperty("jetty.testMode", "true");
+      // SolrCloudTestCase.sslConfig = SolrTestCaseJ4.buildSSLConfig();
+
+      String baseDirSysProp = System.getProperty("miniClusterBaseDir");
+      if (baseDirSysProp != null) {
+        deleteMiniCluster = false;
+        baseDir = Paths.get(baseDirSysProp);
+        if (Files.exists(baseDir)) {
+          createCollectionAndIndex = false;
+        }
+      } else {
+        baseDir = Paths.get("work/mini-cluster");
+      }
+
+      System.setProperty("metricsEnabled", String.valueOf(metricsEnabled));
+    }
+
+    public void allowClusterReuse(boolean allowClusterReuse) {
+      this.allowClusterReuse = allowClusterReuse;
+    }
+
+    public void metricsEnabled(boolean metricsEnabled) {
+      this.metricsEnabled = metricsEnabled;
+    }
+
+    public void startMiniCluster(int nodeCount) {
+      log("starting mini cluster at base directory: " + 
baseDir.toAbsolutePath());
+
+      if (!allowClusterReuse && Files.exists(baseDir)) {
+        log(
+            "mini cluster base directory exists, removing according to 
allowClusterReuse="
+                + allowClusterReuse);
+        try {
+          deleteDirectory(baseDir);
+        } catch (IOException e) {
+          throw new RuntimeException(e);
+        }
+        createCollectionAndIndex = true;
+      } else if (Files.exists(baseDir)) {
+        createCollectionAndIndex = false;
+        deleteMiniCluster = false;
+      }
+
+      try {
+        cluster =
+            new MiniSolrCloudCluster.Builder(nodeCount, baseDir)
+                .formatZkServer(false)
+                .addConfig("conf", 
Paths.get("src/resources/configs/cloud-minimal/conf"))
+                .configure();
+      } catch (Exception e) {
+        if (Files.exists(baseDir)) {
+          try {
+            deleteDirectory(baseDir);
+          } catch (IOException ex) {
+            e.addSuppressed(ex);
+          }
+        }
+        throw new RuntimeException(e);
+      }
+
+      nodes = new ArrayList<>(nodeCount);
+      List<JettySolrRunner> jetties = cluster.getJettySolrRunners();
+      for (JettySolrRunner runner : jetties) {
+        nodes.add(runner.getBaseUrl().toString());
+      }
+
+      client = new Http2SolrClient.Builder().build();
+
+      log("done starting mini cluster");
+      log("");
+    }
+
+    public void createCollection(String collection, int numShards, int 
numReplicas)
+        throws Exception {
+      if (createCollectionAndIndex) {
+        try {
+
+          CollectionAdminRequest.Create request =
+              CollectionAdminRequest.createCollection(collection, "conf", 
numShards, numReplicas);
+          request.setBasePath(
+              
nodes.get(ThreadLocalRandom.current().nextInt(cluster.getJettySolrRunners().size())));
+
+          client.request(request);
+
+          cluster.waitForActiveCollection(
+              collection, 15, TimeUnit.SECONDS, numShards, numShards * 
numReplicas);
+        } catch (Exception e) {
+          if (Files.exists(baseDir)) {
+            deleteDirectory(baseDir);
+          }
+          throw e;
+        }
+      }
+    }
+
+    @SuppressForbidden(reason = "This module does not need to deal with 
logging context")
+    public void index(String collection, DocMaker docMaker, int docCount) 
throws Exception {
+      if (createCollectionAndIndex) {
+
+        log("indexing data for benchmark...");
+        Meter meter = new Meter();
+        ExecutorService executorService =
+            Executors.newFixedThreadPool(
+                Runtime.getRuntime().availableProcessors(),
+                new SolrNamedThreadFactory("SolrJMH Indexer Progress"));
+        ScheduledExecutorService scheduledExecutor =
+            Executors.newSingleThreadScheduledExecutor(
+                new SolrNamedThreadFactory("SolrJMH Indexer"));
+        scheduledExecutor.scheduleAtFixedRate(
+            () -> {
+              if (meter.getCount() == docCount) {
+                scheduledExecutor.shutdown();
+              } else {
+                log(meter.getCount() + " docs at " + meter.getMeanRate() + " 
doc/s");
+              }
+            },
+            10,
+            10,
+            TimeUnit.SECONDS);
+        for (int i = 0; i < docCount; i++) {
+          executorService.submit(
+              () -> {
+                UpdateRequest updateRequest = new UpdateRequest();
+                updateRequest.setBasePath(
+                    nodes.get(
+                        
ThreadLocalRandom.current().nextInt(cluster.getJettySolrRunners().size())));
+                SolrInputDocument doc = docMaker.getDocument();
+                // log("add doc " + doc);
+                updateRequest.add(doc);
+                meter.mark();
+
+                try {
+                  client.request(updateRequest, collection);
+                } catch (Exception e) {
+                  throw new RuntimeException(e);
+                }
+              });
+        }
+
+        log("done adding docs, waiting for executor to terminate...");
+
+        executorService.shutdown();
+        boolean result = executorService.awaitTermination(600, 
TimeUnit.MINUTES);
+
+        scheduledExecutor.shutdown();
+
+        if (!result) {
+          throw new RuntimeException("Timeout waiting for doc adds to finish");
+        }
+        log("done indexing data for benchmark");
+
+        if (allowClusterReuse) {
+          docMaker.clear();
+        }
+
+        log("committing data ...");
+        UpdateRequest commitRequest = new UpdateRequest();
+        commitRequest.setBasePath(
+            
nodes.get(ThreadLocalRandom.current().nextInt(cluster.getJettySolrRunners().size())));
+        commitRequest.setAction(UpdateRequest.ACTION.COMMIT, false, true);
+        commitRequest.process(client, collection);
+        log("done committing data");
+      } else {
+        cluster.waitForActiveCollection(collection, 15, TimeUnit.SECONDS);
+      }
+
+      QueryRequest queryRequest = new QueryRequest(new SolrQuery("q", "*:*", 
"rows", "1"));
+      queryRequest.setBasePath(
+          
nodes.get(ThreadLocalRandom.current().nextInt(cluster.getJettySolrRunners().size())));

Review comment:
       @sonatype-lift ignore

##########
File path: 
solr/benchmark/src/java/org/apache/solr/bench/search/JsonFaceting.java
##########
@@ -0,0 +1,194 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.solr.bench.search;
+
+import java.util.concurrent.ThreadLocalRandom;
+import java.util.concurrent.TimeUnit;
+import org.apache.solr.bench.DocMaker;
+import org.apache.solr.bench.FieldDef;
+import org.apache.solr.bench.MiniClusterState;
+import org.apache.solr.client.solrj.request.QueryRequest;
+import org.apache.solr.common.params.ModifiableSolrParams;
+import org.apache.solr.common.util.NamedList;
+import org.openjdk.jmh.annotations.Benchmark;
+import org.openjdk.jmh.annotations.BenchmarkMode;
+import org.openjdk.jmh.annotations.Fork;
+import org.openjdk.jmh.annotations.Level;
+import org.openjdk.jmh.annotations.Measurement;
+import org.openjdk.jmh.annotations.Mode;
+import org.openjdk.jmh.annotations.OutputTimeUnit;
+import org.openjdk.jmh.annotations.Param;
+import org.openjdk.jmh.annotations.Scope;
+import org.openjdk.jmh.annotations.Setup;
+import org.openjdk.jmh.annotations.State;
+import org.openjdk.jmh.annotations.Threads;
+import org.openjdk.jmh.annotations.Timeout;
+import org.openjdk.jmh.annotations.Warmup;
+import org.openjdk.jmh.infra.BenchmarkParams;
+
+/** A benchmark to experiment with the performance of json faceting. */
+@BenchmarkMode(Mode.Throughput)
+@OutputTimeUnit(TimeUnit.SECONDS)
+@Threads(1)
+@Warmup(time = 5, iterations = 3)
+@Measurement(time = 15, iterations = 5)
+@Fork(value = 1)
+@Timeout(time = 60)
+public class JsonFaceting {
+
+  @State(Scope.Benchmark)
+  public static class BenchState {
+
+    public String collection = "testCollection";
+
+    @Param({"10000"})
+    public int docCount;
+
+    int nodeCount = 2;
+    int numReplicas = 1;
+
+    @Param("2")
+    int numShards;
+
+    // DV,  // DocValues, collect into ordinal array
+    // UIF, // UnInvertedField, collect into ordinal array
+    // DVHASH, // DocValues, collect into hash
+    // ENUM, // TermsEnum then intersect DocSet (stream-able)
+    // STREAM, // presently equivalent to ENUM
+    // SMART,
+    //  "dv"
+    //  "uif"
+    //  "dvhash">
+    //  "enum"
+    //  "stream"
+    //  "smart"
+    @Param({"smart"})
+    String fm;
+
+    @Param({"80000"})
+    int facetCard;
+
+    private ModifiableSolrParams params;
+
+    @Setup(Level.Trial)
+    public void setup(
+        BenchmarkParams benchmarkParams, 
MiniClusterState.MiniClusterBenchState miniClusterState)
+        throws Exception {
+      System.setProperty("maxMergeAtOnce", "20");
+      System.setProperty("segmentsPerTier", "20");
+
+      miniClusterState.allowClusterReuse(true);
+
+      miniClusterState.startMiniCluster(nodeCount);
+
+      miniClusterState.createCollection(collection, numShards, numReplicas);
+
+      // Define random documents
+      DocMaker docMaker = new DocMaker();
+      docMaker.addField(
+          "id", 
FieldDef.FieldDefBuilder.aFieldDef().withContent(DocMaker.Content.UNIQUE_INT));
+      docMaker.addField(
+          "facet_s",
+          FieldDef.FieldDefBuilder.aFieldDef()
+              .withContent(DocMaker.Content.ALPHEBETIC)
+              .withMaxLength(64)
+              .withMaxCardinality(facetCard));
+      docMaker.addField(
+          "facet2_s",
+          FieldDef.FieldDefBuilder.aFieldDef()
+              .withContent(DocMaker.Content.ALPHEBETIC)
+              .withMaxLength(16)
+              .withMaxCardinality(facetCard));
+      docMaker.addField(
+          "facet3_s",
+          FieldDef.FieldDefBuilder.aFieldDef()
+              .withContent(DocMaker.Content.UNICODE)
+              .withMaxLength(128)
+              .withMaxCardinality(12000));
+      docMaker.addField(
+          "text",
+          FieldDef.FieldDefBuilder.aFieldDef()
+              .withContent(DocMaker.Content.ALPHEBETIC)
+              .withMaxLength(64)
+              .withTokenCount(ThreadLocalRandom.current().nextInt(512) + 1));
+      docMaker.addField(
+          "int_i", 
FieldDef.FieldDefBuilder.aFieldDef().withContent(DocMaker.Content.INTEGER));
+      docMaker.addField(
+          "int2_i",
+          FieldDef.FieldDefBuilder.aFieldDef()
+              .withContent(DocMaker.Content.INTEGER)
+              .withMaxCardinality(500));
+      docMaker.addField(
+          "int3_i",
+          FieldDef.FieldDefBuilder.aFieldDef()
+              .withContent(DocMaker.Content.INTEGER)
+              .withMaxCardinality(200000));
+      docMaker.addField(
+          "int4_i",
+          FieldDef.FieldDefBuilder.aFieldDef()
+              .withContent(DocMaker.Content.INTEGER)
+              .withMaxCardinality(5));
+
+      miniClusterState.index(collection, docMaker, docCount);
+      miniClusterState.waitForMerges(collection);
+
+      params = new ModifiableSolrParams();
+
+      MiniClusterState.params(
+          params,
+          "q",
+          "*:*",
+          "json.facet",
+          "{f1:{method:'"
+              + fm
+              + "', type:terms, field:'facet_s', sort:'x desc', 
facet:{x:'min(int3_i)'}  }"
+              + " , f2:{method:'"
+              + fm
+              + "',, type:terms, field:'facet_s', sort:'x desc', 
facet:{x:'max(int3_i)'}  } "
+              + " , f3:{method:'"
+              + fm
+              + "', type:terms, field:'facet_s', sort:'x desc', 
facet:{x:'unique(facet2_s)'}  } "
+              + " , f4:{method:'"
+              + fm
+              + "', type:terms, field:'facet_s', sort:'x desc', 
facet:{x:'hll(facet2_s)'}  } "
+              + " , f5:{method:'"
+              + fm
+              + "', type:terms, field:'facet_s', sort:'x desc', 
facet:{x:'variance(int3_i)'}  } "
+              + " , f6:{type:terms, field:'int3_i', limit:1, sort:'x desc', 
facet:{x:'hll(int2_i)'}  } "
+              + " , f7:{type:terms, field:'facet_s', limit:2, sort:'x desc', 
facet:{x:'missing(int4_i)'}  } "
+              + " , f8:{type:terms, field:'facet_s', limit:2, sort:'x desc', 
facet:{x:'countvals(int4_i)'}  } "
+              + "}");
+
+      // MiniClusterState.log("params: " + params + "\n");
+    }
+  }
+
+  @Benchmark
+  @Timeout(time = 500, timeUnit = TimeUnit.SECONDS)
+  public Object jsonFacet(MiniClusterState.MiniClusterBenchState 
miniClusterState, BenchState state)
+      throws Exception {
+    QueryRequest queryRequest = new QueryRequest(state.params);
+    queryRequest.setBasePath(
+        
miniClusterState.nodes.get(ThreadLocalRandom.current().nextInt(state.nodeCount)));

Review comment:
       @sonatype-lift ignore




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscr...@solr.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@solr.apache.org
For additional commands, e-mail: issues-h...@solr.apache.org

Reply via email to