sigram commented on code in PR #2244:
URL: https://github.com/apache/solr/pull/2244#discussion_r1491040941


##########
solr/core/src/test/org/apache/solr/search/TestCpuAllowedLimit.java:
##########
@@ -0,0 +1,182 @@
+/*
+ * 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.search;
+
+import java.lang.invoke.MethodHandles;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.concurrent.TimeUnit;
+import org.apache.solr.SolrTestCaseJ4;
+import org.apache.solr.client.solrj.SolrClient;
+import org.apache.solr.client.solrj.response.QueryResponse;
+import org.apache.solr.util.SolrJettyTestRule;
+import org.apache.solr.util.ThreadCpuTimer;
+import org.junit.AfterClass;
+import org.junit.Assume;
+import org.junit.BeforeClass;
+import org.junit.ClassRule;
+import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class TestCpuAllowedLimit extends SolrTestCaseJ4 {
+  private static final Logger log = 
LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
+
+  @ClassRule public static final SolrJettyTestRule solrRule = new 
SolrJettyTestRule();
+
+  @BeforeClass
+  public static void setup() {
+    System.setProperty(ThreadCpuTimer.ENABLE_CPU_TIME, "true");
+  }
+
+  @AfterClass
+  public static void teardown() {
+    System.clearProperty(ThreadCpuTimer.ENABLE_CPU_TIME);
+  }
+
+  @Test
+  public void testCompareToWallClock() throws Exception {
+    Assume.assumeTrue("Thread CPU time monitoring is not available", 
ThreadCpuTimer.isSupported());
+    long limitMs = 100;
+    CpuAllowedLimit cpuLimit = new CpuAllowedLimit(limitMs);
+    int[] randoms = new int[100];
+    long startNs = System.nanoTime();
+    int wakeups = 0;
+    while (!cpuLimit.shouldExit()) {
+      Thread.sleep(1);
+      // do some busywork
+      for (int i = 0; i < randoms.length; i++) {
+        randoms[i] = random().nextInt();
+      }
+      wakeups++;
+    }
+    long endNs = System.nanoTime();
+    long wallTimeDeltaMs = TimeUnit.MILLISECONDS.convert(endNs - startNs, 
TimeUnit.NANOSECONDS);
+    log.info(
+        "CPU limit: {} ms, elapsed wall-clock: {} ms, wakeups: {}",
+        limitMs,
+        wallTimeDeltaMs,
+        wakeups);
+    assertTrue(
+        "Elapsed wall-clock time expected much larger than 100ms but was " + 
wallTimeDeltaMs,
+        limitMs < wallTimeDeltaMs);
+  }
+
+  private Path createConfigSet() throws Exception {
+    Path configSet = createTempDir();
+    copyMinConf(configSet.toFile());
+    // insert an expensive search component
+    Path solrConfig = configSet.resolve("conf/solrconfig.xml");
+    Files.writeString(
+        solrConfig,
+        Files.readString(solrConfig)
+            .replace(
+                "<requestHandler",
+                "<searchComponent name=\"expensiveSearchComponent\"\n"
+                    + "                   
class=\"org.apache.solr.search.ExpensiveSearchComponent\"/>\n"
+                    + "\n"
+                    + "  <requestHandler")
+            .replace(
+                "class=\"solr.SearchHandler\">",
+                "class=\"solr.SearchHandler\">\n"
+                    + "    <arr name=\"first-components\">\n"
+                    + "      <str>expensiveSearchComponent</str>\n"
+                    + "    </arr>\n"));
+    return configSet.resolve("conf");
+  }
+
+  @Test
+  public void testDistribLimit() throws Exception {

Review Comment:
   Hmm... yes, that would be useful. `TestCpuTimeSearch` has an example, I'm 
going to use it (though it's concerning that the distrib test there is Ignored).



-- 
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