adding all of botlist, initial add
[botlist.git] / openbotlist / tests / system / loadhttptest / src / org / spirit / loadtest / LoadTestManagerThread.java
blob1a023301fa01411256f7ec3a7387ca549928f188
1 /*-----------------------------------------------
2 * Berlin Brown
3 * Created on Apr 13, 2007
4 * LoadTestManagerThread.java
5 *-----------------------------------------------
6 */
7 package org.spirit.loadtest;
9 /**
10 * Thread launched to send out HTTP requests and if enabled, write response and status
11 * information to file.
13 public class LoadTestManagerThread implements Runnable {
15 private LoadTestManager client;
17 public LoadTestManagerThread(LoadTestManager client) {
18 this.client = client;
20 public LoadTestManager getTestClient() {
21 return this.client;
24 /**
25 * Add additional message content to result tuple.
26 * Length = 4
27 * 4/18/2008
29 private String [] additionalHeadersResTuple(final String [] responseTuple, final String additional_msg, final boolean valid_xhtml) {
30 final String [] new_copy = new String [LoadTestManager.MAX_LOG_RESULT_TUPLE];
31 final Object obj_src = responseTuple;
32 final Object obj_dest = new_copy;
33 // Only copy 3 elements
34 System.arraycopy(obj_src, 0, obj_dest, 0, 3);
35 new_copy[3] = additional_msg;
36 new_copy[4] = "" + valid_xhtml;
37 return new_copy;
39 private void loadSingleURL(String url) {
40 try {
41 final long allStart = System.currentTimeMillis();
42 for (int i = 0; i < getTestClient().getLinesWrite(); i++) {
43 final long tStart = System.currentTimeMillis();
44 System.out.println("attempting request to=" + url);
45 String [] responseTuple = LoadTestManager.connectURL(url, false);
46 final String http_data = responseTuple[1];
47 String additional_msg = "";
48 boolean is_valid = false;
49 if (this.getTestClient().isValidateXHTMLEnabled()) {
50 final Object [] validate_res = LoadTestXMLValidate.validateXML(url, http_data);
51 is_valid = ((Boolean) validate_res[0]).booleanValue();
52 if (!is_valid) {
53 // Append the XML validate text to the response tuple, for printing to the HTML document.
54 additional_msg = (String) validate_res[1];
57 responseTuple = additionalHeadersResTuple(responseTuple, additional_msg, is_valid);
58 long tEnd = System.currentTimeMillis();
59 long diff = tEnd - tStart;
60 System.out.println("single request time=" + diff + " ms -- from " + Thread.currentThread().getName());
61 //*********************
62 // Log the file to the simple text document, also save for HTML output
63 //*********************
64 LoadTestManager.log(diff, responseTuple, url);
65 // Move to next iteration.
66 this.getTestClient().incNumberOfRequests();
67 this.getTestClient().incTotalTime(diff);
68 Thread.sleep(getTestClient().getThreadSleepTime());
71 final long allEnd = System.currentTimeMillis();
72 final long perThreadDiff = allEnd - allStart;
73 System.out.println("All requests time=" + perThreadDiff + " ms");
75 // The following code buildRequestSection, clearRequest are needed after writing the HTML content
76 this.getTestClient().getHtmlOutput().buildRequestSection(perThreadDiff + " ms, requests=" + getTestClient().getLinesWrite());
77 this.getTestClient().getHtmlOutput().clearRequest();
79 } catch (Exception e) {
80 e.printStackTrace();
84 private void loadSequenceFile(final String script) {
85 try {
86 System.out.println("INFO: loading sequence script=" + script);
87 LoadTestSequenceParser parser = new LoadTestSequenceParser();
88 String realFilename = script.substring("script://".length());
89 parser.parse(realFilename);
90 parser.printSummary();
91 parser.handleSequence();
92 } catch (Exception e) {
93 e.printStackTrace();
97 public void run() {
98 if (this.getTestClient().isUseDataFile()) {
99 Object [] data = LoadTestManager.loadDataFile(this.getTestClient().getDataFile());
100 for (int i = 0; i < data.length; i++) {
101 String url = (String) data[i];
102 if (url.startsWith("script://")) {
103 loadSequenceFile(url);
104 } else {
105 loadSingleURL(url);
109 } else {
110 loadSingleURL(getTestClient().getTestURL());
114 // End of File