Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add RunnableMatrix.java and PerfHtmlBuilder.java.

  • Loading branch information...
commit 12a318b081039e453e185173ed6a73bfc2ecdf03 1 parent 34d9075
@CalvinChen CalvinChen authored
View
6 core-lang/pom.xml
@@ -26,5 +26,11 @@
<version>1.6.1</version>
<scope>test</scope>
</dependency>
+
+ <dependency>
+ <groupId>dom4j</groupId>
+ <artifactId>dom4j</artifactId>
+ <version>1.6.1</version>
+ </dependency>
</dependencies>
</project>
View
279 core-lang/src/main/java/bingo/lang/testing/Perf.java
@@ -15,39 +15,179 @@
*/
package bingo.lang.testing;
-import bingo.lang.StopWatch;
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.util.Date;
+
+import org.dom4j.Document;
+import org.dom4j.DocumentHelper;
+import org.dom4j.Element;
+import org.dom4j.io.XMLWriter;
+import org.xml.sax.SAXException;
+
import bingo.lang.Strings;
/**
- * Performance measurements utiltiy for unit test.
+ * Performance measurements utility for unit test.
*/
public final class Perf {
- public static void run(String test, Runnable action){
- run(test, action, 1);
- }
+ public static final int TO_CONSOLE = 0;
+ public static final int TO_HTML = 1;
+ public static final int TO_CONSOLE_AND_HTML = 2;
+
+ public static final String DEFAULT_PROJECT_NAME = "anonymous";
+ public static final String DEFAULT_FILE_NAME = "testing.html";
+ public static final String DEFAULT_DIR_NAME = "perf";
+
+ private String projectName = DEFAULT_PROJECT_NAME;
+ private Runnable runnableThing;
+ private int showResultTo = TO_CONSOLE;
+ private String fileName;
+
+ private PerfResult perfResult;
+
+ private int runnableType = IS_SIMPLE_RUNNABLE;
+ public static final int IS_SIMPLE_RUNNABLE = 0;
+ public static final int IS_RUNNABLE_GROUP = 1;
+ public static final int IS_RUNNABLE_MATRIX = 2;
- public static void run(String test,Runnable action,int times){
+ public Perf(){
+ this(DEFAULT_PROJECT_NAME, null, TO_CONSOLE, null);
+ }
+
+ public Perf(Runnable runnable){
+ this(DEFAULT_PROJECT_NAME, runnable, TO_CONSOLE, null);
+ }
+
+ public Perf(String projectName, Runnable runnable, int showResultTo, String fileName){
+ this.projectName = projectName;
+ checkAndSetRunnableType(runnable);
+ this.runnableThing = runnable;
+ this.showResultTo = showResultTo;
+ this.fileName = fileName;
+ }
+
+ public void run(){
+ if(null == runnableThing){
+ throw new RuntimeException("the runnable thing could not be null.");
+ }
+ if(runnableType == IS_RUNNABLE_GROUP || runnableType == IS_SIMPLE_RUNNABLE){
+ runGroup();
+ }
+ if(runnableType == IS_RUNNABLE_MATRIX){
+ runMatrix();
+ }
+ }
+
+ /**
+ * run a runnable group.
+ */
+ private void runGroup(){
/* wrapped with runnable group */
- String name = RunnableGroup.tryToGetName(action);
- times = RunnableGroup.tryToGetRunTimes(action);
+ String name = RunnableGroup.tryToGetName(runnableThing);
+ int times = RunnableGroup.tryToGetRunTimes(runnableThing);
- RunnableGroup runnableGroup = new RunnableGroup(name, action, times);
+ RunnableGroup runnableGroup = new RunnableGroup(name, times, runnableThing);
/* warm up */
runnableGroup.run();
runnableGroup.run();
- Perf.toConsole(test, runnableGroup.getPerfResult());
+ perfResult = runnableGroup.getPerfResult();
+
+ if(showResultTo == TO_CONSOLE){
+ groupToConsole();
+ }
+
+ if(showResultTo == TO_HTML){
+ groupToHtml();
+ }
+
+ if(showResultTo == TO_CONSOLE_AND_HTML){
+ groupToConsole();
+ groupToHtml();
+ }
}
- public static void toConsole(String name, PerfResult perfResult){
- System.out.println("PROJECT: " + name);
+ private void runMatrix(){
+ RunnableMatrix matrix = (RunnableMatrix) runnableThing;
+
+ matrix.run();
+
+ PerfResult[][] resultMatrix = matrix.getResultMatrix();
+
+ if(showResultTo == TO_CONSOLE){
+ matrixToConsole(resultMatrix);
+ }
+
+ if(showResultTo == TO_HTML){
+ matrixToHtml(resultMatrix);
+ }
+
+ if(showResultTo == TO_CONSOLE_AND_HTML){
+ matrixToConsole(resultMatrix);
+ matrixToHtml(resultMatrix);
+ }
+ }
+
+ private void matrixToHtml(PerfResult[][] resultMatrix) {
+ Document doc = PerfHtmlBuilder.buildUpMatrixHtml(projectName, resultMatrix);
+ writeToHtml(doc);
+ }
+
+
+
+ private void matrixToConsole(PerfResult[][] resultMatrix) {
+ System.out.println("------------------------------------------------");
+ for (int i = -1; i < resultMatrix.length; i++) {
+ if(-1 == i){
+ System.out.print("\t");
+ for (int k = 0; k < resultMatrix[0].length; k++) {
+ System.out.print(resultMatrix[0][k].getName() + "\t");
+ }
+ System.out.println();
+ continue;
+ } else {
+ System.out.print(resultMatrix[i][0].getRunTimes() + "\t");
+ }
+ for (int j = 0; j < resultMatrix[i].length; j++) {
+ System.out.print(resultMatrix[i][j].getElapsedNanoseconds() + "\t\t");
+ }
+ System.out.println();
+ }
+ }
+
+ /**
+ * show the specified named {@link PerfResult} to console.
+ * @param projectName name of this {@link PerfResult}.
+ * @param perfResult the specified {@link PerfResult}.
+ */
+ public void groupToConsole(){
+ System.out.println("PROJECT: " + projectName);
toConsole(perfResult.getChildren().get(0), "");
}
- private static void toConsole(PerfResult perfResult, String prefix){
+ public void groupToHtml(){
+ Document doc = buildUpGroupHtml();
+ writeToHtml(doc);
+ }
+
+ private void checkAndSetRunnableType(Runnable runnable){
+ if(runnable instanceof RunnableMatrix){
+ this.runnableType = IS_RUNNABLE_MATRIX;
+ return;
+ }
+ if(runnable instanceof RunnableGroup){
+ this.runnableType = IS_RUNNABLE_GROUP;
+ return;
+ }
+ this.runnableType = IS_SIMPLE_RUNNABLE;
+ }
+
+ private void toConsole(PerfResult perfResult, String prefix){
System.out.println(prefix + perfResult.toString());
prefix += " ";
if(perfResult.isEnd() == false){
@@ -56,4 +196,117 @@ private static void toConsole(PerfResult perfResult, String prefix){
}
}
}
+
+ private Document buildUpGroupHtml(){
+ Document doc = DocumentHelper.createDocument();
+ Element html = doc.addElement("html");
+ html.addAttribute("lang", "en");
+ Element head = html.addElement("head");
+ Element link = head.addElement("link");
+ link.addAttribute("href", "http://twitter.github.com/bootstrap/assets/css/bootstrap.css");
+ link.addAttribute("rel", "stylesheet");
+ Element body = html.addElement("body");
+ body.addElement("H2").addText(projectName);
+ Element table = body.addElement("table");
+ table.addAttribute("class", "table table-bordered table-striped");
+ Element thead = table.addElement("thead");
+ Element tr = thead.addElement("tr");
+ tr.addElement("th").addText("Hierarchy");
+ tr.addElement("th").addText("Name");
+ tr.addElement("th").addText("Run Times");
+ tr.addElement("th").addText("ms");
+ tr.addElement("th").addText("ns");
+ Element tbody = table.addElement("tbody");
+ buildUpGroupRow("|", 0, perfResult, tbody);
+ return doc;
+ }
+
+ private void buildUpGroupRow(String prefix, int number, PerfResult perfResult, Element tbody){
+ if(number != 0){
+ Element tr = tbody.addElement("tr");
+ tr.addElement("td").addText(prefix);
+ prefix += "--" + number + "--|";
+ tr.addElement("td").addText(perfResult.getName());
+ tr.addElement("td").addText(perfResult.getRunTimes() + "");
+ tr.addElement("td").addText(perfResult.getElapsedMilliseconds() + "");
+ tr.addElement("td").addText(perfResult.getElapsedNanoseconds() + "");
+ }
+ number++;
+
+ if(perfResult.isEnd() == false){
+ for (PerfResult perf : perfResult.getChildren()) {
+ buildUpGroupRow(prefix, number, perf, tbody);
+ }
+ }
+ }
+
+ private void writeToHtml(Document document){
+ XMLWriter writer = null;
+ try{
+ File dir = new File(DEFAULT_DIR_NAME);
+ File file = null;
+ dir.mkdir();
+ if(DEFAULT_PROJECT_NAME.equals(projectName)){
+ file = new File(dir, DEFAULT_FILE_NAME);
+ } else {
+ file = new File(dir, projectName + ".html");
+ }
+ file.createNewFile();
+ writer = new XMLWriter(new FileWriter(file));
+ writer.write(document);
+ } catch (Exception e) {
+ throw new RuntimeException("error when generating HTML file.");
+ } finally {
+ if(null != writer){
+ try {
+ writer.close();
+ } catch (IOException e) {
+ throw new RuntimeException("error when closing HTML file.");
+ }
+ }
+ }
+ }
+
+ /* getter and setter */
+
+ public String getProjectName() {
+ return projectName;
+ }
+
+ public void setProjectName(String projectName) {
+ this.projectName = projectName;
+ }
+
+ public Runnable getRunnableThing() {
+ return runnableThing;
+ }
+
+ public void setRunnableThing(Runnable runnableThing) {
+ this.runnableThing = runnableThing;
+ }
+
+ public int getShowResultTo() {
+ return showResultTo;
+ }
+
+ public void setShowResultTo(int showResultTo) {
+ this.showResultTo = showResultTo;
+ }
+
+ public String getFileName() {
+ return fileName;
+ }
+
+ public void setFileName(String fileName) {
+ this.fileName = fileName;
+ }
+
+ public int getRunnableType() {
+ return runnableType;
+ }
+
+ public void setRunnableType(int runnableType) {
+ this.runnableType = runnableType;
+ }
+
}
View
73 core-lang/src/main/java/bingo/lang/testing/PerfHtmlBuilder.java
@@ -0,0 +1,73 @@
+package bingo.lang.testing;
+
+import java.util.Date;
+
+import org.dom4j.Document;
+import org.dom4j.DocumentHelper;
+import org.dom4j.Element;
+
+public class PerfHtmlBuilder {
+
+ public static Document buildUpMatrixHtml(String projectName, PerfResult[][] resultMatrix) {
+ Document doc = DocumentHelper.createDocument();
+ Element html = doc.addElement("html");
+ html.addAttribute("lang", "en");
+ buildUpHead(projectName, html);
+ buildUpBody(projectName, html, resultMatrix);
+ return doc;
+ }
+
+ private static Element buildUpHead(String projectName, Element html){
+ Element head = html.addElement("head");
+ head.addElement("title").addText("The Performance Report of \"" + projectName + "\"");
+ Element link = head.addElement("link");
+ link.addAttribute("href", "http://twitter.github.com/bootstrap/assets/css/bootstrap.css");
+ link.addAttribute("rel", "stylesheet");
+ head.addElement("style").addText("body{padding:20px}");
+ return head;
+ }
+
+ private static Element buildUpBody(String projectName, Element html, PerfResult[][] resultMatrix){
+ Element body = html.addElement("body");
+ Element divContainer = body.addElement("div");
+ divContainer.addAttribute("class", "containner");
+ divContainer.addElement("H1").addText(projectName);
+ buildUpTable(divContainer, resultMatrix);
+ Element block = divContainer.addElement("blockquote");
+ block.addAttribute("class", "pull-right");
+ block.addElement("p").addText("Generated on " + new Date().toString() + ".");
+ block.addElement("small").addText("powered by bingo.core");
+ return body;
+ }
+
+ private static Element buildUpTable(Element divContainer, PerfResult[][] resultMatrix){
+ Element table = divContainer.addElement("table");
+ table.addAttribute("class", "table table-bordered table-striped");
+ Element thead = table.addElement("thead");
+ Element tr = thead.addElement("tr");
+ tr.addElement("th").addText("Repeat Time\\Code");
+ for (int i = 0; i < resultMatrix[0].length; i++) {
+ tr.addElement("th").addText(resultMatrix[0][i].getName());
+ }
+ buildUpTbody(table, resultMatrix);
+ return table;
+ }
+
+ private static Element buildUpTbody(Element table, PerfResult[][] resultMatrix){
+ Element tbody = table.addElement("tbody");
+ Element tr = null;
+ for (int i = 0; i < resultMatrix.length; i++) {
+ tr = tbody.addElement("tr");
+ tr.addElement("td").addText("")
+ .addElement("strong").addText("" + resultMatrix[i][0].getRunTimes());
+ for (int j = 0; j < resultMatrix[i].length; j++) {
+ PerfResult cell = resultMatrix[i][j];
+ tr.addElement("td").addText("")
+ .addElement("strong").addText(cell.getElapsedNanoseconds() + "")
+ .getParent().addText(" ns.(" + cell.getElapsedMilliseconds() +
+ " ms, " + (int)(cell.getElapsedNanoseconds()*1.0/cell.getRunTimes() + 0.5) + " ns each)");
+ }
+ }
+ return tbody;
+ }
+}
View
15 core-lang/src/main/java/bingo/lang/testing/PerfResult.java
@@ -1,3 +1,18 @@
+/*
+ * Copyright 2012 the original author or authors.
+ *
+ * Licensed 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 bingo.lang.testing;
import java.util.LinkedList;
View
232 core-lang/src/main/java/bingo/lang/testing/RunnableGroup.java
@@ -1,3 +1,18 @@
+/*
+ * Copyright 2012 the original author or authors.
+ *
+ * Licensed 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 bingo.lang.testing;
import java.util.ArrayList;
@@ -7,23 +22,60 @@
import bingo.lang.StopWatch;
import bingo.lang.Strings;
+/**
+ * represents a runnable group, which can contains other runnable groups or runnable actions or both.
+ * @author Calvin Chen
+ */
public class RunnableGroup implements Runnable{
+ /**
+ * anonymous name for groups or actions if their name isn't be specified.
+ */
public static final String ANONYMOUS_NAME = "anonymous name";
+
+ /**
+ * the default run times for groups or actions if their run times isn't be specified.
+ */
public static final int DEFAULT_RUN_TIMES = 1;
+ /**
+ * contained runnable groups.
+ */
private List<Runnable> runnableGroup;
+
+ /**
+ * if this group only contains a single action.
+ */
private Runnable singleAction;
+ /**
+ * name of this runnable group.
+ */
private String name = ANONYMOUS_NAME;
+
+ /**
+ * run times of this runnable group.
+ */
private int runTimes = DEFAULT_RUN_TIMES;
+ /**
+ * performance result of this runnable group.
+ */
private PerfResult perfResult;
+
+ /**
+ * mark this runnable group whether has run.
+ * true means Has Run and can get the performance result.
+ * false means Has Not Run and cannot get the performance result.
+ */
+ private boolean hasRun = false;
+ /* constructors */
+
/**
* new a runnable group with anonymous name.
*/
public RunnableGroup(){
- this(ANONYMOUS_NAME, null, DEFAULT_RUN_TIMES);
+ this(ANONYMOUS_NAME, DEFAULT_RUN_TIMES, null);
}
/**
@@ -31,7 +83,7 @@ public RunnableGroup(){
* @param name the specified group name.
*/
public RunnableGroup(String name){
- this(name, null, DEFAULT_RUN_TIMES);
+ this(name, DEFAULT_RUN_TIMES, null);
}
/**
@@ -39,14 +91,14 @@ public RunnableGroup(String name){
* @param actionOrGroup a runnable action or group to be added.
*/
public RunnableGroup(Runnable actionOrGroup){
- this(ANONYMOUS_NAME, actionOrGroup, DEFAULT_RUN_TIMES);
+ this(ANONYMOUS_NAME, DEFAULT_RUN_TIMES, actionOrGroup);
}
/**
* new a runnable group with anonymous name and specified run times.
*/
public RunnableGroup(int runTimes){
- this(ANONYMOUS_NAME, null, runTimes);
+ this(ANONYMOUS_NAME, runTimes, null);
}
/**
@@ -55,7 +107,7 @@ public RunnableGroup(int runTimes){
* @param runTimes the specified run times.
*/
public RunnableGroup(String name, int runTimes){
- this(name, null, runTimes);
+ this(name, runTimes, null);
}
/**
@@ -64,8 +116,8 @@ public RunnableGroup(String name, int runTimes){
* @param actionOrGroup a runnable action or group to be added.
* @param runTimes the specified run times.
*/
- public RunnableGroup(Runnable actionOrGroup, int runTimes){
- this(ANONYMOUS_NAME, actionOrGroup, runTimes);
+ public RunnableGroup(int runTimes, Runnable actionOrGroup){
+ this(ANONYMOUS_NAME, runTimes, actionOrGroup);
}
/**
@@ -74,7 +126,7 @@ public RunnableGroup(Runnable actionOrGroup, int runTimes){
* @param actionOrGroup a runnable action or group to be added.
*/
public RunnableGroup(String name, Runnable actionOrGroup){
- this(name, actionOrGroup, DEFAULT_RUN_TIMES);
+ this(name, DEFAULT_RUN_TIMES, actionOrGroup);
}
/**
@@ -83,7 +135,7 @@ public RunnableGroup(String name, Runnable actionOrGroup){
* @param actionOrGroup a runnable action or group to be added.
* @param runTimes the specified run times.
*/
- public RunnableGroup(String name, Runnable actionOrGroup, int runTimes){
+ public RunnableGroup(String name, int runTimes, Runnable actionOrGroup){
this.name = name;
this.runTimes = runTimes;
if(null != actionOrGroup){
@@ -101,12 +153,10 @@ public RunnableGroup(String name, Runnable actionOrGroup, int runTimes){
}
}
- private void initGroup(){
- runnableGroup = new ArrayList<Runnable>();
- }
+ /* public methods */
/**
- * add a anonymous runnable action to current runnable group with no need to new the collection.
+ * add a anonymous runnable action to current runnable group.
* @param action the runnable action to be added, can be another runnable group.
* @return the current runnable group in order to chain call.
*/
@@ -114,32 +164,24 @@ public RunnableGroup add(Runnable action){
return add(tryToGetName(action), action);
}
+ /**
+ * add a named runnable action to current runnable group.
+ * @param name name of the action.
+ * @param action action to be added.
+ * @return the current runnable group in order to chain call.
+ */
public RunnableGroup add(String name, Runnable action){
- return add(name, action, tryToGetRunTimes(action));
+ return add(name, tryToGetRunTimes(action), action);
}
+ /**
+ * add an anonymous action with run times to current runnable group.
+ * @param action action to be added.
+ * @param runTimes run times of this action.
+ * @return the current runnable group in order to chain call.
+ */
public RunnableGroup add(Runnable action, int runTimes){
- return add(tryToGetName(action), action, runTimes);
- }
-
- public static String tryToGetName(Runnable action){
- String name = ANONYMOUS_NAME;
- if(action instanceof RunnableGroup){
- String str = ((RunnableGroup)action).getName();
- if(!Strings.isEmpty(str)){
- name = str;
- }
- }
- return name;
- }
-
- public static int tryToGetRunTimes(Runnable action){
- int runTimes = DEFAULT_RUN_TIMES;
- if(action instanceof RunnableGroup){
- int i = ((RunnableGroup)action).getRunTimes();
- runTimes = i;
- }
- return runTimes;
+ return add(tryToGetName(action), runTimes, action);
}
/**
@@ -148,7 +190,7 @@ public static int tryToGetRunTimes(Runnable action){
* @param name the name of the action.
* @return the current runnable group in order to chain call.
*/
- public RunnableGroup add(String name, Runnable action, int runTimes){
+ public RunnableGroup add(String name, int runTimes, Runnable action){
/* if the element to be added is the first element */
if(null == runnableGroup){
initGroup();
@@ -170,6 +212,32 @@ public RunnableGroup add(String name, Runnable action, int runTimes){
return this;
}
+
+ /**
+ * try to get the name of a runnable action, therefore it's no need to use anonymous name.
+ */
+ public static String tryToGetName(Runnable action){
+ String name = ANONYMOUS_NAME;
+ if(action instanceof RunnableGroup){
+ String str = ((RunnableGroup)action).getName();
+ if(!Strings.isEmpty(str)){
+ name = str;
+ }
+ }
+ return name;
+ }
+
+ /**
+ * try to get the run times of a runnable action, therefore it's no need to use default run times.
+ */
+ public static int tryToGetRunTimes(Runnable action){
+ int runTimes = DEFAULT_RUN_TIMES;
+ if(action instanceof RunnableGroup){
+ int i = ((RunnableGroup)action).getRunTimes();
+ runTimes = i;
+ }
+ return runTimes;
+ }
/**
* run all the runnable actions in the runnable group.
@@ -189,27 +257,8 @@ public void run() {
perfResult.setRunTimes(runTimes);
perfResult.setElapsedMilliseconds(sw.getElapsedMilliseconds());
perfResult.setElapsedNanoseconds(sw.getElapsedNanoseconds());
- }
-
- private StopWatch runGroup(){
- StopWatch sw = StopWatch.startNew();
- for (int i = 0; i < getRunTimes(); i++) {
- for (Runnable action : runnableGroup) {
- action.run();
- perfResult.addChild(((RunnableGroup)action).getPerfResult());
- }
- }
- sw.stop();
- return sw;
- }
-
- private StopWatch runSingle(){
- StopWatch sw = StopWatch.startNew();
- for (int i = 0; i < getRunTimes(); i++) {
- singleAction.run();
- }
- sw.stop();
- return sw;
+
+ hasRun = true;
}
/**
@@ -220,6 +269,8 @@ public boolean isEmpty(){
return Collections.isEmpty(runnableGroup);
}
+ /* getter and setter */
+
/**
* get the name of the group.
* @return the name of the group.
@@ -256,16 +307,81 @@ public RunnableGroup setRunTimes(int runTimes) {
return this;
}
+ /**
+ * get the performance result of this runnable group.
+ * @return the performance result or null if the run method of this class haven't be invoked yet.
+ */
public PerfResult getPerfResult(){
- return this.perfResult;
+ if(false == hasRun){
+ throw new RuntimeException(
+ "RunnableGroup hasn't run yet! please get the performance result after run.");
+ }
+ return perfResult;
}
+ /**
+ * get the single action of this class.
+ * @return the single action of this class or null if this class contains a runnable group.
+ */
public Runnable getSingleAction() {
return singleAction;
}
+ /**
+ * set the single action of this class.
+ * @param singleAction the action which wish to be set.
+ */
public void setSingleAction(Runnable singleAction) {
+ if(singleAction instanceof RunnableGroup){
+ throw new RuntimeException("the single action member cannot be set to a RunnableGroup!");
+ }
this.singleAction = singleAction;
}
+
+ public boolean isHasRun() {
+ return hasRun;
+ }
+
+ public void setHasRun(boolean hasRun) {
+ this.hasRun = hasRun;
+ }
+
+ /* private methods */
+
+ /**
+ * init the group list for add elements.
+ */
+ private void initGroup(){
+ runnableGroup = new ArrayList<Runnable>();
+ }
+
+ /**
+ * run the runnable group contained in this class in recursion.
+ * @return the counted {@link StopWatch} as a run time result.
+ */
+ private StopWatch runGroup(){
+ StopWatch sw = StopWatch.startNew();
+ for (int i = 0; i < getRunTimes(); i++) {
+ for (Runnable action : runnableGroup) {
+ action.run();
+ perfResult.addChild(((RunnableGroup)action).getPerfResult());
+ }
+ }
+ sw.stop();
+ return sw;
+ }
+
+ /**
+ * run the single runnable action in this class.
+ * @return the counted {@link StopWatch} as a run time result.
+ */
+ private StopWatch runSingle(){
+ StopWatch sw = StopWatch.startNew();
+ for (int i = 0; i < getRunTimes(); i++) {
+ singleAction.run();
+ }
+ sw.stop();
+ return sw;
+ }
}
View
122 core-lang/src/main/java/bingo/lang/testing/RunnableMatrix.java
@@ -0,0 +1,122 @@
+package bingo.lang.testing;
+
+import java.util.LinkedList;
+import java.util.List;
+
+public class RunnableMatrix implements Runnable{
+ private List<Integer> runTimes;
+ private List<Runnable> actions;
+ private PerfResult[][] resultMatrix;
+
+ public RunnableMatrix(){
+ initMatrix();
+ }
+
+ public RunnableMatrix(List<Integer> runTimes, List<Runnable> actions){
+ if(runTimes == null || actions == null){
+ throw new RuntimeException(
+ "use this constructor to init a PerfMatrix " +
+ "must use not-null runTimes and actions");
+ }
+ this.runTimes = runTimes;
+ setActions(actions);
+ }
+
+ public RunnableMatrix addRunTime(int runTime){
+ runTimes.add(runTime);
+ return this;
+ }
+
+ public RunnableMatrix addRunTimes(int... runTimes){
+ for (int i = 0; i < runTimes.length; i++) {
+ this.runTimes.add(runTimes[i]);
+ }
+ return this;
+ }
+
+ public RunnableMatrix addAction(Runnable action){
+ actions.add(wrapGroupIfNot(action));
+ return this;
+ }
+
+ public RunnableMatrix addAction(String name, Runnable action){
+ actions.add(wrapGroupIfNot(name, action));
+ return this;
+ }
+
+ public RunnableMatrix addActions(Runnable... actions){
+ for (int i = 0; i < actions.length; i++) {
+ this.actions.add(wrapGroupIfNot(actions[i]));
+ }
+ return this;
+ }
+
+ public void run() {
+ initResultMatrix();
+ for (int i = 0; i < runTimes.size(); i++) {
+ int currentRunTimes = runTimes.get(i);
+ for (int j = 0; j < actions.size(); j++) {
+ RunnableGroup currentAction = (RunnableGroup) actions.get(j);
+ currentAction.setRunTimes(currentRunTimes);
+ currentAction.run();
+ resultMatrix[i][j] = currentAction.getPerfResult();
+ }
+ }
+ }
+
+ /* private methods */
+
+ private void initMatrix(){
+ runTimes = new LinkedList<Integer>();
+ actions = new LinkedList<Runnable>();
+ }
+
+ private void initResultMatrix(){
+ resultMatrix = new PerfResult[runTimes.size()][actions.size()];
+ }
+
+ /* getter and setter */
+
+ public List<Integer> getRunTimes() {
+ return runTimes;
+ }
+
+ public void setRunTimes(List<Integer> runTimes) {
+ this.runTimes = runTimes;
+ }
+
+ public List<Runnable> getActions() {
+ return actions;
+ }
+
+ public void setActions(List<Runnable> actions) {
+ for (int i = 0; i < actions.size(); i++) {
+ actions.set(i, wrapGroupIfNot(actions.get(i)));
+ }
+ this.actions = actions;
+ }
+
+ private Runnable wrapGroupIfNot(Runnable action){
+ RunnableGroup group = null;
+ if((action instanceof RunnableGroup) == false){
+ group = new RunnableGroup();
+ group.setSingleAction(action);
+ action = group;
+ }
+ return action;
+ }
+
+ private Runnable wrapGroupIfNot(String name, Runnable action){
+ RunnableGroup group = (RunnableGroup) wrapGroupIfNot(action);
+ group.setName(name);
+ return group;
+ }
+
+ public PerfResult[][] getResultMatrix() {
+ return resultMatrix;
+ }
+
+ public void setResultMatrix(PerfResult[][] resultMatrix) {
+ this.resultMatrix = resultMatrix;
+ }
+}
View
87 core-lang/src/test/java/bingo/lang/testing/PerfTest.java
@@ -1,3 +1,18 @@
+/*
+ * Copyright 2012 the original author or authors.
+ *
+ * Licensed 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 bingo.lang.testing;
import java.util.ArrayList;
@@ -8,34 +23,78 @@
public class PerfTest {
@Test
- public void testPerf(){
+ public void testPerfGroup(){
final List<String> list = new ArrayList<String>();
list.add("111");
list.add("22");
list.add("3");
- RunnableGroup rg = new RunnableGroup("group1", 2);
- rg.add("item1",new Runnable() {
+ RunnableGroup rg = new RunnableGroup("group100000")
+ .add("item1", 123,new Runnable() {
public void run() {
list.size();
}
- }, 123);
-
- Runnable run = new Runnable() {
+ })
+ .add("item2", 123,new Runnable() {
public void run() {
- list.getClass();
+ list.size();
}
- };
- RunnableGroup group = new RunnableGroup("group1.1", 5);
- group.add("123456",run, 1000000);
- rg.add(group);
+ })
+ .add("group1.1", 1, new RunnableGroup()
+ .add("123456", 1000000, new Runnable() {
+ public void run() {
+ list.getClass();
+ }
+ })
+ .add("group1.1.3", 1, new RunnableGroup()
+ .add("item3", 123, new Runnable() {
+ public void run() {
+ list.equals(list);
+ }
+ }))
+ )
+ .add("item2", 321, new Runnable() {
+ public void run() {
+ list.get(1);
+ }
+ });
- rg.add("hahah", new Runnable() {
+ Runnable r = new Runnable() {
public void run() {
list.get(1);
}
- }, 321);
+ };
- Perf.run("Array.get(native)", rg);
+ Perf perf = new Perf(rg);
+ perf.setProjectName("Array.get(native)");
+ perf.run();
+ }
+
+ @Test
+ public void testPerfMatrix(){
+ RunnableMatrix matrix = new RunnableMatrix();
+ matrix.addAction("this.getClass();", new Runnable(){
+ public void run() {
+ this.getClass();
+ }
+ }).addAction("this.equals(null);", new Runnable() {
+ public void run() {
+ this.equals(null);
+ }
+ }).addAction("this.equals(this);", new Runnable() {
+ public void run() {
+ this.equals(this);
+ }
+ }).addAction("this.hashCode();", new Runnable() {
+ public void run() {
+ this.hashCode();
+ }
+ });
+ matrix.addRunTimes(1, 10, 100, 1000, 10000, 100000, 1000000);
+ Perf perf = new Perf(matrix);
+ perf.setProjectName("this methods");
+ perf.setShowResultTo(Perf.TO_CONSOLE_AND_HTML);
+ perf.run();
}
+
}
Please sign in to comment.
Something went wrong with that request. Please try again.