├── .gitignore ├── LICENSE.txt ├── README.md ├── log4j2.xml ├── pom.xml └── src ├── main └── java │ ├── Manifest.mf │ └── org │ └── jperf │ ├── AbstractPerfTest.java │ ├── ConfigBuilder.java │ ├── DefaultConfigBuilder.java │ ├── JPerf.java │ ├── PerfTest.java │ ├── PerfTestConfig.java │ ├── PerfTestFactory.java │ ├── ResultWriter.java │ ├── ResultWriterCSV.java │ ├── ResultWriterStdout.java │ ├── SingleTestRunner.java │ ├── jdbc │ ├── JdbcPerfTest.java │ └── JdbcPerfTestFactory.java │ └── noop │ └── NoOpTest.java └── test └── java └── org └── jperf └── test └── Test.java /.gitignore: -------------------------------------------------------------------------------- 1 | _* 2 | .* 3 | dist 4 | *.csv 5 | JPerf*.xml 6 | target 7 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | This project contains annotations derived from JCIP-ANNOTATIONS 179 | Copyright (c) 2005 Brian Goetz and Tim Peierls. 180 | See http://www.jcip.net and the Creative Commons Attribution License 181 | (http://creativecommons.org/licenses/by/2.5) 182 | 183 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | JPerf is a simple performance and scalability testing framework for Java. Think of it as JUnit for performance tests. 2 | 3 | JPerf is distributed under the Apache License 2.0 (see LICENSE.txt). 4 | 5 | The latest version can be downloaded from https://github.com/codefutures/jperf/releases 6 | 7 | JPerf can be used programmatically or from the command line to run scalability tests again any class 8 | implementing the PerfTest interface. 9 | 10 | Programmatic use: 11 | 12 | ```java 13 | // create config 14 | PerfTestConfig config = JPerf.newConfigBuilder() 15 | .minThreads(1) 16 | .maxThreads(10) 17 | .duration(100) 18 | .testFactory(() -> new EmptyTest()) 19 | .build(); 20 | 21 | // run test 22 | JPerf.run(config); 23 | ``` 24 | 25 | Command-line use: 26 | 27 | ``` 28 | usage: jperf [-class ] [-duration ] [-increment ] [-max 29 | ] [-min ] 30 | -class Name of class that implemented org.jperf.PerfTest 31 | -duration The duration in milliseconds (per thread level) 32 | -increment The number of threads to increment by 33 | -max The maximum number of threads to test with 34 | -min The number of threads to start testing with 35 | 36 | All arguments are optional, except for 'class'. 37 | ``` 38 | 39 | Example of command line use: 40 | 41 | ``` 42 | java -classpath yourclasspathhere org.jperf.JPerf -class org.jperf.noop.NoOpTest 43 | ``` 44 | 45 | In either case, output similar to the following will be written to stdout. 46 | 47 | ``` 48 | Running on Nov 9, 2014 11:15:39 AM with config: PerfTestConfig{minThreads=1, maxThreads=10, threadIncrement=1, duration=1000} 49 | With 1 threads there were 200,382,238 samples 50 | With 2 threads there were 390,074,461 samples 51 | With 3 threads there were 304,151,933 samples 52 | With 4 threads there were 277,960,408 samples 53 | With 5 threads there were 231,347,787 samples 54 | With 6 threads there were 180,998,431 samples 55 | With 7 threads there were 172,647,235 samples 56 | With 8 threads there were 145,501,665 samples 57 | With 9 threads there were 135,152,189 samples 58 | With 10 threads there were 124,737,834 samples 59 | Stopping threads 60 | Finished 61 | ``` 62 | 63 | JPerf has dependencies on the following open source projects: 64 | 65 | - Apache CLI 66 | - Google Guava 67 | 68 | Copyright (C) 2007-2014 CodeFutures 69 | -------------------------------------------------------------------------------- /log4j2.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | org.jperf 8 | jperf 9 | jperf 10 | 2.0.3 11 | jar 12 | 13 | 14 | 15 | Apache License 2.0 16 | http://www.apache.org/licenses/LICENSE-2.0 17 | 18 | 19 | 20 | 21 | CodeFutures 22 | http://www.codefutures.com 23 | 24 | 25 | 26 | 27 | 28 | com.google.guava 29 | guava 30 | 18.0 31 | 32 | 33 | 34 | commons-cli 35 | commons-cli 36 | 1.2 37 | 38 | 39 | 40 | 41 | 42 | junit 43 | junit 44 | 4.8.2 45 | test 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | org.apache.maven.plugins 56 | maven-compiler-plugin 57 | 58 | 1.8 59 | 1.8 60 | 61 | 62 | 63 | 64 | maven-assembly-plugin 65 | 66 | 67 | package 68 | 69 | single 70 | 71 | 72 | 73 | 74 | 75 | jar-with-dependencies 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | -------------------------------------------------------------------------------- /src/main/java/Manifest.mf: -------------------------------------------------------------------------------- 1 | Main-Class: org.jperf.Main -------------------------------------------------------------------------------- /src/main/java/org/jperf/AbstractPerfTest.java: -------------------------------------------------------------------------------- 1 | package org.jperf; 2 | 3 | public abstract class AbstractPerfTest implements PerfTest { 4 | 5 | @Override 6 | public void setUp() throws Exception { 7 | } 8 | 9 | @Override 10 | public void tearDown() throws Exception { 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/org/jperf/ConfigBuilder.java: -------------------------------------------------------------------------------- 1 | package org.jperf; 2 | 3 | public interface ConfigBuilder { 4 | ConfigBuilder minThreads(int minThreads); 5 | ConfigBuilder maxThreads(int maxThreads); 6 | ConfigBuilder threadIncrement(int threadIncrement); 7 | @Deprecated ConfigBuilder duration(int duration); 8 | ConfigBuilder durationPerThread(int duration); 9 | ConfigBuilder durationTotal(int duration); 10 | ConfigBuilder testFactory(PerfTestFactory testFactory); 11 | ConfigBuilder resultWriter(ResultWriter resultWriter); 12 | void run() throws Exception; 13 | 14 | @Deprecated 15 | PerfTestConfig build(); 16 | 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/org/jperf/DefaultConfigBuilder.java: -------------------------------------------------------------------------------- 1 | package org.jperf; 2 | 3 | public class DefaultConfigBuilder implements ConfigBuilder { 4 | int minThreads = 1; 5 | int maxThreads = 10; 6 | int threadIncrement = 10; 7 | int duration = 1000; 8 | ResultWriter resultWriter; 9 | PerfTestFactory testFactory; 10 | 11 | protected final PerfTestConfig config = new PerfTestConfig(); 12 | 13 | public DefaultConfigBuilder() { 14 | } 15 | 16 | @Override 17 | public ConfigBuilder minThreads(int minThreads) { 18 | config.minThreads = minThreads; 19 | return this; 20 | } 21 | 22 | @Override 23 | public ConfigBuilder maxThreads(int maxThreads) { 24 | config.maxThreads = maxThreads; 25 | return this; 26 | } 27 | 28 | @Override 29 | public ConfigBuilder threadIncrement(int threadIncrement) { 30 | config.threadIncrement = threadIncrement; 31 | return this; 32 | } 33 | 34 | @Override 35 | public ConfigBuilder duration(int durationPerThread) { 36 | config.durationPerThread = durationPerThread; 37 | return this; 38 | } 39 | 40 | @Override 41 | public ConfigBuilder durationPerThread(int durationPerThread) { 42 | config.durationPerThread = durationPerThread; 43 | return this; 44 | } 45 | 46 | @Override 47 | public ConfigBuilder durationTotal(int durationTotal) { 48 | config.durationTotal = durationTotal; 49 | return this; 50 | } 51 | 52 | @Override 53 | public ConfigBuilder testFactory(PerfTestFactory testFactory) { 54 | config.testFactory = testFactory; 55 | return this; 56 | } 57 | 58 | @Override 59 | public ConfigBuilder resultWriter(ResultWriter resultWriter) { 60 | config.resultWriter = resultWriter; 61 | return this; 62 | } 63 | 64 | @Deprecated // use run() instead 65 | @Override 66 | public PerfTestConfig build() { 67 | return config; 68 | } 69 | 70 | @Override 71 | public void run() throws Exception { 72 | JPerf.run(config); 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /src/main/java/org/jperf/JPerf.java: -------------------------------------------------------------------------------- 1 | package org.jperf; 2 | 3 | import org.apache.commons.cli.*; 4 | 5 | import java.util.concurrent.ExecutorService; 6 | import java.util.concurrent.Executors; 7 | import java.util.concurrent.TimeUnit; 8 | 9 | public class JPerf { 10 | 11 | protected static final String CMD_LINE_ARG_CLASS = "class"; 12 | protected static final String CMD_LINE_ARG_MIN_THREAD = "min"; 13 | protected static final String CMD_LINE_ARG_MAX_THREAD = "max"; 14 | protected static final String CMD_LINE_ARG_THREAD_INCREMENT = "increment"; 15 | protected static final String CMD_LINE_ARG_DURATION = "duration"; 16 | 17 | /** Command-line entry point. */ 18 | public static void main(String[] args) throws Exception { 19 | System.exit(doMain(args)); 20 | } 21 | 22 | /** Command-line entry point for unit tests. */ 23 | public static int doMain(String[] args) throws Exception { 24 | 25 | // parse command-line args 26 | CommandLine cmd = new BasicParser().parse(getCommandLineOptions(), args); 27 | if (!cmd.hasOption(CMD_LINE_ARG_CLASS)) { 28 | showUsage(); 29 | return -1; 30 | } 31 | 32 | final Class testClass = Class.forName(cmd.getOptionValue(CMD_LINE_ARG_CLASS)); 33 | 34 | // build the configuration 35 | final ConfigBuilder builder = newConfigBuilder(); 36 | if (cmd.hasOption(CMD_LINE_ARG_MIN_THREAD)) { 37 | builder.minThreads(Integer.parseInt(cmd.getOptionValue(CMD_LINE_ARG_MIN_THREAD))); 38 | } 39 | if (cmd.hasOption(CMD_LINE_ARG_MAX_THREAD)) { 40 | builder.maxThreads(Integer.parseInt(cmd.getOptionValue(CMD_LINE_ARG_MAX_THREAD))); 41 | } 42 | if (cmd.hasOption(CMD_LINE_ARG_THREAD_INCREMENT)) { 43 | builder.threadIncrement(Integer.parseInt(cmd.getOptionValue(CMD_LINE_ARG_THREAD_INCREMENT))); 44 | } 45 | if (cmd.hasOption(CMD_LINE_ARG_DURATION)) { 46 | builder.duration(Integer.parseInt(cmd.getOptionValue(CMD_LINE_ARG_DURATION))); 47 | } 48 | builder.testFactory(() -> (PerfTest) testClass.newInstance()); 49 | 50 | // execute the test 51 | run(builder.build()); 52 | 53 | return 0; 54 | } 55 | 56 | private static Options getCommandLineOptions() { 57 | Options options = new Options(); 58 | options.addOption(new Option(CMD_LINE_ARG_CLASS, true, "Name of class that implemented org.jperf.PerfTest")); 59 | options.addOption(new Option(CMD_LINE_ARG_MIN_THREAD, true, "The number of threads to start testing with")); 60 | options.addOption(new Option(CMD_LINE_ARG_MAX_THREAD, true, "The maximum number of threads to test with")); 61 | options.addOption(new Option(CMD_LINE_ARG_THREAD_INCREMENT, true, "The number of threads to increment by")); 62 | options.addOption(new Option(CMD_LINE_ARG_DURATION, true, "The duration in milliseconds (per thread level)")); 63 | return options; 64 | } 65 | 66 | private static void showUsage() { 67 | new HelpFormatter().printHelp("jperf", getCommandLineOptions(), true); 68 | System.out.println(); 69 | System.out.println("All arguments are optional, except for 'class'."); 70 | } 71 | 72 | public static void run(final PerfTestConfig config) throws Exception { 73 | 74 | // validate configs and set defaults 75 | if (config.durationPerThread==0) { 76 | if (config.durationTotal==0) { 77 | throw new RuntimeException("No duration specified"); 78 | } 79 | config.durationPerThread = config.durationTotal / config.maxThreads; 80 | } 81 | 82 | // create executors and test runners (one of each per thread) 83 | final ExecutorService exec[] = new ExecutorService[config.maxThreads]; 84 | final SingleTestRunner testRunner[] = new SingleTestRunner[config.maxThreads]; 85 | 86 | try { 87 | 88 | config.resultWriter.writeHeader(config); 89 | 90 | // start testing 91 | for (int threadCount = config.minThreads; threadCount <= config.maxThreads; threadCount += config.threadIncrement) { 92 | 93 | for (int i = 0; i < threadCount; i++) { 94 | if (exec[i] == null) { 95 | // create the executor 96 | exec[i] = Executors.newSingleThreadExecutor(); 97 | // creat the test runner 98 | testRunner[i] = new SingleTestRunner(config.testFactory.create()); 99 | // start the test runner 100 | exec[i].execute(testRunner[i]); 101 | } 102 | } 103 | 104 | // reset the counters 105 | for (int i = 0; i < threadCount; i++) { 106 | testRunner[i].getCounter().set(0); 107 | } 108 | 109 | final long start = System.currentTimeMillis(); 110 | Thread.sleep(config.durationPerThread); 111 | final long actualDuration = System.currentTimeMillis() - start; 112 | 113 | // collect the totals 114 | long samples = 0; 115 | for (int i = 0; i < threadCount; i++) { 116 | samples += testRunner[i].getCounter().get(); 117 | } 118 | 119 | config.resultWriter.writeResult(threadCount, actualDuration, samples); 120 | } 121 | 122 | } finally { 123 | 124 | config.resultWriter.close(); 125 | 126 | // stop threads 127 | for (SingleTestRunner r : testRunner) { 128 | if (r != null) { 129 | r.stop(); 130 | } 131 | } 132 | for (ExecutorService e : exec) { 133 | if (e != null) { 134 | e.shutdown(); 135 | } 136 | } 137 | for (ExecutorService e : exec) { 138 | if (e != null) { 139 | e.awaitTermination(5, TimeUnit.SECONDS); 140 | } 141 | } 142 | } 143 | } 144 | 145 | public static ConfigBuilder newTestRun() { 146 | return new DefaultConfigBuilder(); 147 | } 148 | 149 | @Deprecated 150 | public static ConfigBuilder newConfigBuilder() { 151 | return newTestRun(); 152 | } 153 | 154 | } 155 | -------------------------------------------------------------------------------- /src/main/java/org/jperf/PerfTest.java: -------------------------------------------------------------------------------- 1 | package org.jperf; 2 | 3 | /** 4 | * Interface for performance tests. 5 | * 6 | * @author Andy Grove 7 | */ 8 | public interface PerfTest { 9 | 10 | /** 11 | * The setUp() method is called ONCE ONLY for each instance of the PerfTest. The setUp() method can 12 | * be used to initialise resources needed by the test. The cost of the setUp() call is not included 13 | * in the timings collected by JPerf. 14 | * 15 | * @throws Exception 16 | */ 17 | public void setUp() throws Exception; 18 | 19 | /** 20 | * Implement the code you want to test for performance and scalability in this method. JPerf will 21 | * repeatedly call this method for the testDuration. 22 | * 23 | * @throws Exception 24 | */ 25 | public void test() throws Exception; 26 | 27 | /** 28 | * The tearDown() method is called ONCE ONLY for each instance of the PerfTest at the end of testing. 29 | * 30 | * @throws Exception 31 | */ 32 | public void tearDown() throws Exception; 33 | 34 | } 35 | -------------------------------------------------------------------------------- /src/main/java/org/jperf/PerfTestConfig.java: -------------------------------------------------------------------------------- 1 | package org.jperf; 2 | 3 | /** Test configuration. Should be created via builder obtained from JPerf.newConfigBuilder(). */ 4 | public class PerfTestConfig { 5 | 6 | protected int minThreads = 1; 7 | protected int maxThreads = 10; 8 | protected int threadIncrement = 1; 9 | protected int durationPerThread = 0; 10 | protected int durationTotal = 0; 11 | protected ResultWriter resultWriter = new ResultWriterStdout(); 12 | protected PerfTestFactory testFactory; 13 | 14 | @Override 15 | public String toString() { 16 | return "PerfTestConfig{" + 17 | "minThreads=" + minThreads + 18 | ", maxThreads=" + maxThreads + 19 | ", threadIncrement=" + threadIncrement + 20 | ", durationPerThread=" + durationPerThread + 21 | ", durationTotal=" + durationTotal + 22 | '}'; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/org/jperf/PerfTestFactory.java: -------------------------------------------------------------------------------- 1 | package org.jperf; 2 | 3 | public interface PerfTestFactory { 4 | PerfTest create() throws Exception; 5 | } 6 | -------------------------------------------------------------------------------- /src/main/java/org/jperf/ResultWriter.java: -------------------------------------------------------------------------------- 1 | package org.jperf; 2 | 3 | public interface ResultWriter { 4 | 5 | void writeHeader(PerfTestConfig config) throws Exception; 6 | 7 | void writeResult(int threadCount, long duration, long samples) throws Exception; 8 | 9 | void close() throws Exception; 10 | } 11 | -------------------------------------------------------------------------------- /src/main/java/org/jperf/ResultWriterCSV.java: -------------------------------------------------------------------------------- 1 | package org.jperf; 2 | 3 | import com.google.common.base.Throwables; 4 | 5 | import java.io.File; 6 | import java.io.FileOutputStream; 7 | import java.io.IOException; 8 | import java.text.DecimalFormat; 9 | 10 | public class ResultWriterCSV implements ResultWriter { 11 | 12 | final DecimalFormat formatter = new DecimalFormat("0"); 13 | 14 | private FileOutputStream os; 15 | 16 | public ResultWriterCSV() { 17 | } 18 | 19 | public ResultWriterCSV(File file) throws IOException { 20 | System.out.println("Writing results to " + file.getAbsolutePath()); 21 | this.os = new FileOutputStream(file); 22 | } 23 | 24 | @Override 25 | public void writeHeader(PerfTestConfig config) throws Exception { 26 | String header = "Threads,Samples,Duration,Throughput"; 27 | System.out.println(header); 28 | if (os != null) { 29 | os.write((header+"\n").getBytes()); 30 | os.flush(); 31 | } 32 | } 33 | 34 | @Override 35 | public void writeResult(int threadCount, long duration, long samples) throws Exception { 36 | String line = String.format("%s,%s,%s,%s", 37 | formatter.format(threadCount), 38 | formatter.format(samples), 39 | formatter.format(duration), 40 | formatter.format(samples * 1000.0f / duration) 41 | ); 42 | System.out.println(line); 43 | if (os != null) { 44 | os.write((line + "\n").getBytes()); 45 | os.flush(); 46 | } 47 | } 48 | 49 | @Override 50 | public void close() { 51 | if (os != null) { 52 | try { 53 | os.close(); 54 | } catch (IOException e) { 55 | throw Throwables.propagate(e); 56 | } 57 | } 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/main/java/org/jperf/ResultWriterStdout.java: -------------------------------------------------------------------------------- 1 | package org.jperf; 2 | 3 | import java.text.DateFormat; 4 | import java.text.DecimalFormat; 5 | import java.util.Date; 6 | 7 | public class ResultWriterStdout implements ResultWriter { 8 | 9 | final DecimalFormat formatter = new DecimalFormat("#,##0"); 10 | 11 | @Override 12 | public void writeHeader(PerfTestConfig config) throws Exception { 13 | System.out.println(String.format("Running on %s with config: %s", 14 | DateFormat.getDateTimeInstance().format(new Date()), 15 | config 16 | )); 17 | 18 | } 19 | 20 | @Override 21 | public void writeResult(int threadCount, long duration, long samples) throws Exception { 22 | System.out.println(String.format("Threads: %s: Samples: %s; Duration: %s; Throughput: %s", 23 | formatter.format(threadCount), 24 | formatter.format(samples), 25 | formatter.format(duration), 26 | formatter.format(samples * 1000.0f / duration) 27 | )); 28 | 29 | } 30 | 31 | @Override 32 | public void close() { 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/main/java/org/jperf/SingleTestRunner.java: -------------------------------------------------------------------------------- 1 | package org.jperf; 2 | 3 | import com.google.common.base.Throwables; 4 | 5 | import java.util.concurrent.atomic.AtomicBoolean; 6 | import java.util.concurrent.atomic.AtomicLong; 7 | 8 | /** 9 | * Thread that repeatedly invokes the PerfTest in a tight loop and updates the shared counter after each 10 | * invocation. 11 | */ 12 | public class SingleTestRunner implements Runnable { 13 | 14 | /** The test to invoke. */ 15 | protected final PerfTest test; 16 | 17 | /** Counter to track number of invocations. */ 18 | protected final AtomicLong counter = new AtomicLong(); 19 | 20 | /** Flag to indicate if test is alive or not. */ 21 | protected final AtomicBoolean alive = new AtomicBoolean(true); 22 | 23 | /** Create a perf thread for the specified test instance. */ 24 | public SingleTestRunner(PerfTest test) { 25 | this.test = test; 26 | } 27 | 28 | /** 29 | * This is the main testing loop. The setup() method is called once and then the test method is 30 | * called in a tight loop until the test runner calls the stop() method, then the test tearDown() 31 | * method is invoked. 32 | */ 33 | public void run() { 34 | try { 35 | test.setUp(); 36 | while (alive.get()) { 37 | test.test(); 38 | counter.incrementAndGet(); 39 | } 40 | } catch (Exception e) { 41 | throw Throwables.propagate(e); 42 | } finally { 43 | try { 44 | test.tearDown(); 45 | } catch (Exception e) { 46 | // ignore errors in tearDown for now 47 | } 48 | } 49 | } 50 | 51 | /** Terminate the thread. */ 52 | public void stop() { 53 | alive.set(false); 54 | } 55 | 56 | /** Get reference to the counter. */ 57 | public AtomicLong getCounter() { 58 | return counter; 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /src/main/java/org/jperf/jdbc/JdbcPerfTest.java: -------------------------------------------------------------------------------- 1 | package org.jperf.jdbc; 2 | 3 | import org.jperf.PerfTest; 4 | 5 | import java.sql.Connection; 6 | import java.sql.ResultSet; 7 | import java.sql.Statement; 8 | 9 | public class JdbcPerfTest implements PerfTest { 10 | 11 | private Connection conn; 12 | 13 | private String sql; 14 | 15 | public JdbcPerfTest(Connection conn, String sql) { 16 | this.conn = conn; 17 | this.sql = sql; 18 | } 19 | 20 | @Override 21 | public void setUp() throws Exception { 22 | } 23 | 24 | @Override 25 | public void test() throws Exception { 26 | Statement stmt = conn.createStatement(); 27 | stmt.execute(sql); 28 | ResultSet rs = stmt.getResultSet(); 29 | if (rs != null) { 30 | // read the first column from each row in the result set 31 | while (rs.next()) { 32 | rs.getObject(1); 33 | } 34 | rs.close(); 35 | } 36 | stmt.close(); 37 | } 38 | 39 | @Override 40 | public void tearDown() throws Exception { 41 | conn.close(); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/main/java/org/jperf/jdbc/JdbcPerfTestFactory.java: -------------------------------------------------------------------------------- 1 | package org.jperf.jdbc; 2 | 3 | import org.jperf.PerfTest; 4 | import org.jperf.PerfTestFactory; 5 | 6 | import java.sql.Connection; 7 | import java.sql.DriverManager; 8 | 9 | public class JdbcPerfTestFactory implements PerfTestFactory { 10 | 11 | private String jdbcURL; 12 | private String jdbcUser; 13 | private String jdbcPass; 14 | private String sql; 15 | 16 | public JdbcPerfTestFactory(String jdbcURL, String jdbcUser, String jdbcPass, String sql) throws Exception { 17 | this.jdbcURL = jdbcURL; 18 | this.jdbcUser = jdbcUser; 19 | this.jdbcPass = jdbcPass; 20 | this.sql = sql; 21 | } 22 | 23 | @Override 24 | public PerfTest create() throws Exception { 25 | Connection conn = DriverManager.getConnection(jdbcURL, jdbcUser, jdbcPass); 26 | conn.setAutoCommit(true); 27 | return new JdbcPerfTest(conn, sql); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/org/jperf/noop/NoOpTest.java: -------------------------------------------------------------------------------- 1 | package org.jperf.noop; 2 | 3 | import org.jperf.PerfTest; 4 | 5 | /** No-op test to measure raw performance of JPerf. */ 6 | public class NoOpTest implements PerfTest { 7 | @Override 8 | public void setUp() throws Exception { 9 | 10 | } 11 | 12 | @Override 13 | public void test() throws Exception { 14 | } 15 | 16 | @Override 17 | public void tearDown() throws Exception { 18 | 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/test/java/org/jperf/test/Test.java: -------------------------------------------------------------------------------- 1 | package org.jperf.test; 2 | 3 | import org.jperf.PerfTestConfig; 4 | import org.jperf.JPerf; 5 | import org.jperf.PerfTest; 6 | import org.jperf.ResultWriterCSV; 7 | import org.jperf.noop.NoOpTest; 8 | 9 | public class Test { 10 | 11 | @org.junit.Test 12 | public void test() throws Exception { 13 | 14 | // create config 15 | PerfTestConfig config = JPerf.newConfigBuilder() 16 | .minThreads(1) 17 | .maxThreads(8) 18 | .duration(100) 19 | .testFactory(() -> new NoOpTest()) 20 | .build(); 21 | 22 | // run test 23 | JPerf.run(config); 24 | } 25 | 26 | @org.junit.Test 27 | public void testOutputCSV() throws Exception { 28 | 29 | // create config 30 | PerfTestConfig config = JPerf.newConfigBuilder() 31 | .minThreads(1) 32 | .maxThreads(8) 33 | .duration(100) 34 | .testFactory(() -> new NoOpTest()) 35 | .resultWriter(new ResultWriterCSV()) 36 | .build(); 37 | 38 | // run test 39 | JPerf.run(config); 40 | } 41 | 42 | } 43 | --------------------------------------------------------------------------------