因此,我有一个使用JMS将消息发送到队列的程序,某些第三方系统接收该请求,对其进行处理,然后将其返回到另一个队列,我从该队列中出队并记录其所花费的往返时间要发送,处理和接收的消息。我正在使用System.nanoTime()和许多for循环来完成此操作,但是我感觉返回的时间总是不正确,这意味着我或者使用了错误的方法来获取时间,或者我对如何使用nanoTime()有缺陷或实施不佳。 (作为一个旁注,我在遇到问题时出现了一个错误)。关于获得往返时间的更好方法的任何想法吗?代码如下所示:
此处的目的是对第三方服务进行负载测试,并让我们对何时开始获得较慢的响应有所了解。这是我对软件开发以及性能测试的第一次冒险。任何帮助将不胜感激!
count=0;
int multiple = 0; //Integer to keep track of how many times the thread was put to sleep because response from queue was null
//Essentially give third party service Some breathing room
//System.out.println("SENT: " + fileBasedResponse.toString());
ArrayList<SendingThread> pool = new ArrayList<SendingThread>(arraySize); //Keeps track of SendingThread Objects (Class that extends Thread
for(int x = 0;x<arraySize;x++) //create all threadObjs
{
String item = Integer.toString(x); //Might use the namign relation between sets, might not, we'll see
pool.add(x, new SendingThread(rfids[x],rfids,x)); //Adds the objects to the ArrayList
//100% sure I don't need all three of these parameters, but ah well
(pool.get(x)).start(); //Fires off each thread
}
//Send Every Message and start timing?
Long[] startTimes= new Long[arraySize]; //The StartTime (In nanoseconds) for each thread is stored here, for when we send teh message to the queue
//Normal Bijection mapping
int trigger = 0;
for(int x = 0;x<arraySize;x++) //This for loop sends out each of the responses from each of the threads
{
// clientSend.sendTextMessage(responses[x]);
clientSend.sendTextMessage(pool.get(x).getFileBasedResponse());
startTimes[x] = System.nanoTime(); //Start time is recoreded as soon as we fire off the message, in nanoseconds, again, bijective mapping
}
while(count != arraySize-1) //Pretty much trying to assure we terminate when we have all the requests we sent, but seem to be losing a request somewhere...
{
System.out.println(trigger++);
String rec = clientRec.receiveTextMessage(); //Pull from receipt queue
//System.out.println(rec);
while(rec == null) //if we a pull a null, we're either done or third party is slowing down, so we give it some time
{
Thread.sleep(10); //10 MS to be exact
rec = clientRec.receiveTextMessage(); //Then we try recieving a message again, hopefully GES processed by now
multiple++; //We use this multiple value to account for the time lost sleeping when determining roundtrup time
System.out.println(multiple);
if(multiple > 100) //TIMEOUT statement, ensures we don't get stuck ina loop here or are waiting for nothing
{
System.out.println(output.toString());
System.out.println("TIMEOUT, RESPONSE NULL FOR OVER 10 SECONDS");
System.exit(0);
}
}
for(int x = 0;x<arraySize;x++)
{
if(rec.contains(pool.get(x).getRFID())) //Very resource intensive, but iterates through the arrayList to see which thread has the RFID associated with
// the received message
{
long end = System.nanoTime(); //recording endtime
output.append('\n' + pool.get(x).toString() + '\n'); //buffered output
double difference = ((end - startTimes[x]) / 1e6) - (multiple * 10); //calculated roundtrip time (something is def wrong with the formula here)
output.append(difference + '\n'); //More output
count++; //Means we got a hit, one less request to expect
multiple = 0; //reset the multiple offset
break;
}
}
}
clientSend.close(); //close connections
clientRec.close();
System.out.println(output.toString()); //output the results
} }