Author: markt
Date: Fri Apr 6 08:47:52 2018
New Revision: 1828500
URL: http://svn.apache.org/viewvc?rev=1828500&view=rev
Log:
Refactor test to extract threaded test code for re-use
Added:
tomcat/trunk/test/org/apache/tomcat/unittest/TesterThreadedPerformance.java
(with props)
Modified:
tomcat/trunk/test/org/apache/jasper/runtime/TestTagHandlerPoolPerformance.java
Modified:
tomcat/trunk/test/org/apache/jasper/runtime/TestTagHandlerPoolPerformance.java
URL:
http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/jasper/runtime/TestTagHandlerPoolPerformance.java?rev=1828500&r1=1828499&r2=1828500&view=diff
==============================================================================
---
tomcat/trunk/test/org/apache/jasper/runtime/TestTagHandlerPoolPerformance.java
(original)
+++
tomcat/trunk/test/org/apache/jasper/runtime/TestTagHandlerPoolPerformance.java
Fri Apr 6 08:47:52 2018
@@ -16,6 +16,9 @@
*/
package org.apache.jasper.runtime;
+import java.util.function.IntConsumer;
+import java.util.function.Supplier;
+
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.Tag;
@@ -24,6 +27,7 @@ import org.junit.Test;
import org.apache.catalina.Wrapper;
import org.apache.catalina.startup.Tomcat;
import org.apache.catalina.startup.TomcatBaseTest;
+import org.apache.tomcat.unittest.TesterThreadedPerformance;
import org.apache.tomcat.unittest.tags.Bug53545;
@@ -39,51 +43,45 @@ public class TestTagHandlerPoolPerforman
tagHandlerPool.init(w.getServlet().getServletConfig());
for (int i = 1; i < 9; i++) {
- doTestConcurrency(tagHandlerPool, i);
+ TesterThreadedPerformance test = new TesterThreadedPerformance(
+ i, 5000000, new TestInstanceSupplier(tagHandlerPool));
+ long duration = test.doTest();
+ System.out.println(i + " threads completed in " + duration + "ns");
}
}
- private void doTestConcurrency(TagHandlerPool tagHandlerPool, int
threadCount) throws Exception {
- long start = System.nanoTime();
+ private static class TestInstanceSupplier implements Supplier<IntConsumer>
{
- Thread[] threads = new Thread[threadCount];
+ private final TagHandlerPool tagHandlerPool;
- for (int i = 0; i < threadCount; i++) {
- threads[i] = new Thread(new
TagHandlerPoolRunnable(tagHandlerPool));
+ public TestInstanceSupplier(TagHandlerPool tagHandlerPool) {
+ this.tagHandlerPool = tagHandlerPool;
}
- for (int i = 0; i < threadCount; i++) {
- threads[i].start();
- }
- for (int i = 0; i < threadCount; i++) {
- threads[i].join();
+ @Override
+ public IntConsumer get() {
+ return new TestInstance(tagHandlerPool);
}
-
- long duration = System.nanoTime() - start;
-
- System.out.println(threadCount + " threads completed in " + duration +
"ns");
}
- private class TagHandlerPoolRunnable implements Runnable {
+ private static class TestInstance implements IntConsumer {
private final TagHandlerPool tagHandlerPool;
- private TagHandlerPoolRunnable(TagHandlerPool tagHandlerPool) {
+ public TestInstance(TagHandlerPool tagHandlerPool) {
this.tagHandlerPool = tagHandlerPool;
}
@Override
- public void run() {
+ public void accept(int value) {
try {
- for (int i = 0; i < 500000; i++) {
- Tag t = tagHandlerPool.get(Bug53545.class);
- tagHandlerPool.reuse(t);
- }
+ Tag t = tagHandlerPool.get(Bug53545.class);
+ tagHandlerPool.reuse(t);
} catch (JspException e) {
- e.printStackTrace();
+ throw new RuntimeException(e);
}
}
}
Added:
tomcat/trunk/test/org/apache/tomcat/unittest/TesterThreadedPerformance.java
URL:
http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/tomcat/unittest/TesterThreadedPerformance.java?rev=1828500&view=auto
==============================================================================
--- tomcat/trunk/test/org/apache/tomcat/unittest/TesterThreadedPerformance.java
(added)
+++ tomcat/trunk/test/org/apache/tomcat/unittest/TesterThreadedPerformance.java
Fri Apr 6 08:47:52 2018
@@ -0,0 +1,78 @@
+/*
+ * 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.tomcat.unittest;
+
+import java.util.function.IntConsumer;
+import java.util.function.Supplier;
+
+public class TesterThreadedPerformance {
+
+ private final int threadCount;
+ private final int iterationCount;
+ private final Supplier<IntConsumer> testInstanceSupplier;
+
+
+ public TesterThreadedPerformance(int threadCount, int iterationCount,
+ Supplier<IntConsumer> testInstanceSupplier) {
+ this.threadCount = threadCount;
+ this.iterationCount = iterationCount;
+ this.testInstanceSupplier = testInstanceSupplier;
+ }
+
+
+ public long doTest() throws InterruptedException {
+ long start = System.nanoTime();
+
+ Thread[] threads = new Thread[threadCount];
+
+ for (int i = 0; i < threadCount; i++) {
+ IntConsumer testTarget = testInstanceSupplier.get();
+ threads[i] = new Thread(
+ new TesterThreadedPerformanceRunnable(testTarget,
iterationCount));
+ }
+
+ for (int i = 0; i < threadCount; i++) {
+ threads[i].start();
+ }
+
+ for (int i = 0; i < threadCount; i++) {
+ threads[i].join();
+ }
+
+ return System.nanoTime() - start;
+ }
+
+
+ private static class TesterThreadedPerformanceRunnable implements Runnable
{
+
+ private final IntConsumer testTarget;
+ private final int iterationCount;
+
+ public TesterThreadedPerformanceRunnable(IntConsumer testTarget, int
iterationCount) {
+ this.testTarget = testTarget;
+ this.iterationCount = iterationCount;
+ }
+
+
+ @Override
+ public void run() {
+ for (int i = 0; i < iterationCount; i++) {
+ testTarget.accept(i);
+ }
+ }
+ }
+}
Propchange:
tomcat/trunk/test/org/apache/tomcat/unittest/TesterThreadedPerformance.java
------------------------------------------------------------------------------
svn:eol-style = native
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]