📄 directconnecttiming_t.java
字号:
/* $Id: DirectConnectTiming_T.java,v 1.8 2004/02/05 23:29:01 giuli Exp $*/
/*
* The contents of this file are subject to the OAA Community Research
* 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.ai.sri.com/~oaa/. Software distributed under the License
* is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either
* express or implied. See the License for the specific language governing
* rights and limitations under the License. Portions of the software are
* Copyright (c) SRI International, 1999. All rights reserved.
* "OAA" is a registered trademark, and "Open Agent Architecture" is a
* trademark, of SRI International, a California nonprofit public benefit
* corporation.
*/
package com.sri.oaa2.test.timing;
import com.sri.oaa2.com.LibCom;
import com.sri.oaa2.com.LibComTcpProtocol;
import com.sri.oaa2.icl.IclDataQ;
import com.sri.oaa2.icl.IclInt;
import com.sri.oaa2.icl.IclList;
import com.sri.oaa2.icl.IclStr;
import com.sri.oaa2.icl.IclStruct;
import com.sri.oaa2.icl.IclTerm;
import com.sri.oaa2.icl.IclVar;
import com.sri.oaa2.lib.LibOaa;
import com.sri.oaa2.lib.OAAEventListener;
import com.sri.oaa2.test.agent.OaaTest_T;
import com.sri.oaa2.agentlib.Agent;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Vector;
/**
* Before executing this test, the add agent should be run listening on the
* local host on port 4012. From the test directory, run:
* <pre><code>
* ./addAgent.sh -oaa_listen "tcp('localhost',4012)"
* </code></pre>
*/
public class DirectConnectTiming_T extends TestCase {
public final static int numRuns = 10;
boolean debugDot = false;
static boolean timeFac = true;
String testAgentAddr;
String dcParams;
OaaTest_T oaaAgent = new OaaTest_T() {
public String getAgentName() {
return "oaa_unitTest_agent_client";
}
};
String testAgentName = "TestAgent";
Map times = new LinkedHashMap();
Map tests = new LinkedHashMap();
public DirectConnectTiming_T(String name) {
super(name);
}
private void debug(String mess) {
System.out.println(mess);
}
public static void main(String[] args) {
if (args.length > 0) {
timeFac = args[0].equalsIgnoreCase("true");
}
if (timeFac) {
System.out.println("Timing facilitator and direct connect.");
} else {
System.out.println("Timing direct connect only; ommitting facilitator runs.");
}
junit.textui.TestRunner.run(suite());
}
public static Test suite() {
return new TestSuite(DirectConnectTiming_T.class);
}
public void generateReport(long startTime) {
debug("REPORT");
for (Iterator testsitr = tests.entrySet().iterator(); testsitr.hasNext();) {
Map.Entry testEntry = (Map.Entry) testsitr.next();
String testName = testEntry.getKey().toString();
Map testResults = (Map) testEntry.getValue();
debug("Test: " + testName);
for (Iterator itr = testResults.entrySet().iterator(); itr.hasNext();) {
Map.Entry entry = (Map.Entry) itr.next();
String key = entry.getKey().toString();
List times = (List) entry.getValue();
double total = 0;
double min = Double.MAX_VALUE, max = Double.MIN_VALUE;
for (Iterator titr = times.iterator(); titr.hasNext();) {
long time = ((Number) titr.next()).longValue();
total += time;
min = Math.min(min, time);
max = Math.max(max, time);
}
double avg = total / ((double) times.size());
debug(" Name: " + key);
debug(" Number of runs: " + times.size());
debug(" Minimum time = " + ((long) min) + " : " + reportTime((long) min));
debug(" Maximum time = " + ((long) max) + " : " + reportTime((long) max));
debug(" Average time = " + ((long) avg) + " : " + reportTime((long) avg));
}
}
long diff = System.currentTimeMillis() - startTime;
debug("Total time: " + diff);
debug(reportTime(diff));
System.gc();
try {
Thread.sleep(5000L);
} catch (InterruptedException ex) {
}
}
public String getTestAgentAddress() throws Exception {
IclList res = oaaAgent.interpret(IclTerm.fromString(true,
"oaa_Solve(agent_host(Addr,'" + testAgentName + "',Host), [])"), Agent.iclEmptyList);
if (res.size() == 0) {
throw new Exception("Unable to retrieve test agent address using agent_host");
}
String testAgentAddr = res.getTerm(0).getTerm(0).toString();
return testAgentAddr;
}
String reportTime(long diff) {
long sec = (long) Math.floor(((double) diff) / 1000.0);
long ms = diff - 1000 * sec;
long min = (long) Math.floor(((double) sec) / 60.0);
sec = sec - 60 * min;
long hour = (long) Math.floor(((double) min) / 60.0);
min = min - 60 * hour;
return hour + " hours, " + min + " min, " + sec + " sec, " +
ms + " milli";
}
public void testTime() throws Exception {
oaaAgent.facilitatorConnect(new String[]{});
long time = System.currentTimeMillis();
testAgentAddr = getTestAgentAddress();
debug("testAgentAddr = " + testAgentAddr);
dcParams = "[direct_connect(true), address(" + testAgentAddr + ")]";
// Throw first two tests away to initial setup overhead
timeDoNothing("Initial connect", (IclList) IclTerm.fromString(true, "[]"), 1);
timeDoNothing("Initial connect", (IclList) IclTerm.fromString(true, "[direct_connect(true)]"), 1);
times = new LinkedHashMap();
tests.put("doNothing()", times);
debug("Timing doNothing(10)");
timeDoNothing(10);
generateReport(time);
debug("Timing doNothing(50)");
timeDoNothing(50);
generateReport(time);
debug("Timing doNothing(200)");
timeDoNothing(200);
generateReport(time);
debug("Timing doNothing(500)");
timeDoNothing(500);
generateReport(time);
debug("Timing 1k makeString");
time1kMakeString();
generateReport(time);
debug("Timing 5k makeString");
time5kMakeString();
generateReport(time);
debug("Timing 10k makeString");
time10kMakeString();
generateReport(time);
debug("Timing 20k makeString");
time20kMakeString();
debug("Timing 60k makeString");
time60kMakeString();
generateReport(time);
debug("Timing 60k string copy");
time60kStringCopy();
generateReport(time);
debug("Timing 100k byte array");
time100kByteArray();
generateReport(time);
debug("Timing 500k byte array");
time500kByteArray();
generateReport(time);
debug("Timing 1M byte array");
time1MByteArray();
generateReport(time);
debug("Timing 1M data transfer in 100kb chunks");
timeDataTransfer(1, 100, true);
generateReport(time);
debug("Timing 1M data transfer in 10kb chunks");
timeDataTransfer(1, 10, true);
generateReport(time);
debug("Timing 10M data transfer in 500kb chunks");
timeDataTransfer(10, 500, true);
generateReport(time);
debug("Timing 10M data transfer in 100Kb chunks direct_connect only");
timeDataTransfer(10, 100, false);
generateReport(time);
debug("Timing 10M data transfer in 800Kb chunks direct_connect only");
timeDataTransfer(10, 900, false);
generateReport(time);
debug("Timing 10M data transfer in 1500Kb chunks direct_connect only");
timeDataTransfer(10, 1500, false);
generateReport(time);
oaaAgent.facilitatorDisconnect();
}
public void time60kStringCopy() throws Exception {
times = new LinkedHashMap();
tests.put("60k copyString", times);
// Time subsequent tests
for (int i = 0; i < numRuns; i++) {
if (timeFac) time60kStringCopy("[]");
time60kStringCopy(dcParams);
if (debugDot) System.out.print(".");
}
System.out.println();
}
public void time60kStringCopy(String params) throws Exception {
timeStringCopy("copyString(60kString) with " + params, (IclList) IclTerm.fromString(true, params), 60);
}
public void timeStringCopy(String name,
IclList solveArgs,
int nKilobytes) throws Exception {
int size = 1024 * nKilobytes;
StringBuffer s = new StringBuffer(size);
for (int indx = 0; indx < size; indx++) {
char c = (char) ('a' + Math.round(Math.random() * ('z' - 'a')));
s.append(c);
}
IclTerm fullGoal = new IclStruct("full_goal",
new IclStruct("copyString", new IclStr(s.toString()),
new IclVar("Ret")));
solveArgs.add(fullGoal);
IclTerm solveTerm = new IclStruct("oaa_Solve",
new IclStruct("copyString", new IclVar("In"),
new IclVar("Ret")), solveArgs);
long startTime = System.currentTimeMillis();
// Send the large string over and wait for it to come back
IclList res = oaaAgent.interpret(solveTerm, Agent.iclEmptyList);
long totalTime = System.currentTimeMillis() - startTime;
List timesList = (List) times.get(name);
boolean answer = false;
if (timesList == null) {
timesList = new Vector();
times.put(name, timesList);
}
for (Iterator itr = res.iterator(); itr.hasNext();) {
IclTerm term = (IclTerm) itr.next();
assertTrue(term.isStruct());
assertTrue("term.getTerm(0) = " + term.getTerm(0).getClass().getName(), term.getTerm(0).isStr());
assertTrue("term.getTerm(1) = " + term.getTerm(1).getClass().getName(), term.getTerm(1).isStr());
answer = true;
long time = totalTime;
timesList.add(new Long(time));
}
assertTrue("No Answer found", answer);
}
public void time60kMakeString() throws Exception {
times = new LinkedHashMap();
tests.put("60k makeString", times);
// Time subsequent tests
for (int i = 0; i < numRuns; i++) {
if (timeFac) time60kMakeString("[]");
time60kMakeString(dcParams);
}
}
public void time60kMakeString(String params) throws Exception {
timeMakeString("makeString(60kString) iterated 1 time with " + params, (IclList) IclTerm.fromString(true, params), 60, 1);
}
public void timeDoNothing(int numIt) throws Exception {
// Time subsequent tests
for (int i = 0; i < numRuns; i++) {
if (timeFac) timeDoNothing("[]", numIt);
timeDoNothing(dcParams, numIt);
}
}
public void timeDoNothing(String params, int numIt) throws Exception {
timeDoNothing("doNothing() iterated " + numIt + " times with " + params,
(IclList) IclTerm.fromString(true, params), numIt);
}
public void timeDoNothing(String name,
IclList solveArgs,
int numIt) throws Exception {
long startTime = System.currentTimeMillis();
for (int i = 0; i < numIt; i++) {
IclTerm solveTerm = new IclStruct("oaa_Solve",
new IclStruct("doNothing", new IclVar("Nothing")), solveArgs);
IclList res = oaaAgent.interpret(solveTerm, Agent.iclEmptyList);
assertTrue("No Answer found", res.size() > 0);
if (debugDot) System.out.print(".");
}
long totalTime = System.currentTimeMillis() - startTime;
System.out.println();
List timesList = (List) times.get(name);
if (timesList == null) {
timesList = new Vector();
times.put(name, timesList);
}
timesList.add(new Long(totalTime));
}
public void time1kMakeString() throws Exception {
times = new LinkedHashMap();
tests.put("1k makeString", times);
// Time subsequent tests
for (int i = 0; i < numRuns; i++) {
if (timeFac) time1kMakeString("[]");
time1kMakeString(dcParams);
}
}
public void time1kMakeString(String params) throws Exception {
timeMakeString("makeString(1kString) iterated 50 times with " + params,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -