deal with the invalid terminate line when record, will do it

continuously
This commit is contained in:
coderfengyun 2013-12-12 16:17:25 +08:00
parent 30c658adb8
commit 9eeb32a32b
8 changed files with 48 additions and 40 deletions

View File

@ -3,12 +3,15 @@ package org.bench4q.master.scriptrecord.httpcapture;
import java.io.IOException;
import java.io.InputStream;
import java.util.StringTokenizer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.log4j.Logger;
public class HttpRequestHeader {
private static final Log log = LogFactory.getLog(HttpRequestHeader.class);
private static final int NEW_LINE_BYTE_VALUE = 10;
private static final Logger log = Logger.getLogger(HttpRequestHeader.class);
public String method = new String();
@ -32,8 +35,6 @@ public class HttpRequestHeader {
public int oldContentLength = -1;
public long timeDifference;
public String unrecognized = new String();
public boolean pragmaNoCache = false;
@ -50,6 +51,7 @@ public class HttpRequestHeader {
this.version = getToken(tz);
while (true) {
String line = readLine();
System.out.println(dealWithNewLineAndReturn(line));
log.trace(line);
tz = new StringTokenizer(line);
String Token = getToken(tz);
@ -92,10 +94,6 @@ public class HttpRequestHeader {
this.contentLength = Integer.parseInt(Token);
} else if (Token.equalsIgnoreCase("CONTENT-TYPE:")) {
this.contentType = getRemainder(tz);
} else if (Token.equalsIgnoreCase("TEST-CONNECTION-SEND-TIME:")) {
// TODO:get the time difference between browser an proxy
this.timeDifference = System.currentTimeMillis()
- Long.parseLong(getToken(tz));
} else {
this.unrecognized = (this.unrecognized + Token + " "
+ getRemainder(tz) + "\r\n");
@ -103,10 +101,21 @@ public class HttpRequestHeader {
}
}
private String dealWithNewLineAndReturn(String s) {
String result = "";
Pattern returnPattern = Pattern.compile("\r");
Pattern newLinePattern = Pattern.compile("\n");
Matcher retMatcher = returnPattern.matcher(s);
result = retMatcher.replaceAll("/r");
Matcher newLineMatcher = newLinePattern.matcher(result);
result = newLineMatcher.replaceAll("/n");
return result;
}
String readLine() throws IOException {
StringBuffer sb = new StringBuffer();
int c;
while ((c = this.input.read()) != 10) {
while ((c = this.input.read()) != NEW_LINE_BYTE_VALUE) {
if (c == -1)
throw new IOException("unterminated line in request header");
sb.append((char) c);

View File

@ -11,14 +11,11 @@ import java.net.MalformedURLException;
import java.net.Socket;
import java.net.SocketException;
import java.net.URL;
import org.apache.log4j.Logger;
import org.bench4q.master.exception.ExceptionLog;
public class RequestHandler implements Runnable {
private static final Logger log = Logger.getLogger(RequestHandler.class);
private Config config = Config.getConfig();
private InputStream clientIn;
private InputStream serverIn;
@ -44,7 +41,8 @@ public class RequestHandler implements Runnable {
try {
this.header = new HttpRequestHeader(this.clientIn);
} catch (IOException e) {
log.error("truncated request from browser: " + e.getMessage()
log.error("truncated request from browser: "
+ ExceptionLog.getExceptionStackTrace(e).toString()
+ this.header.url);
throw new Utils.SilentException();
}

View File

@ -11,7 +11,7 @@ import org.bench4q.master.testplan.highavailable.faultolerence.BriefAgentFault;
import org.bench4q.share.communication.HttpRequester;
import org.bench4q.share.communication.HttpRequester.HttpResponse;
import org.bench4q.share.models.agent.AgentBriefStatusModel;
import org.bench4q.share.models.agent.RunScenarioModel;
import org.bench4q.share.models.agent.RunScenarioModelNew;
import org.bench4q.share.models.agent.RunScenarioResultModel;
import org.bench4q.share.models.agent.StopTestModel;
import org.bench4q.share.models.agent.TestBehaviorsBriefModel;
@ -33,11 +33,10 @@ public class RunningAgentService {
}
public RunScenarioResultModel run(Agent agent,
RunScenarioModel runScenarioModel) throws IOException {
HttpResponse httpResponse = this.httpRequester
.sendPostXml(buildBaseUrl(agent) + "/test/run", MarshalHelper
.marshal(RunScenarioModel.class, runScenarioModel),
null);
RunScenarioModelNew runScenarioModel) throws IOException {
HttpResponse httpResponse = this.httpRequester.sendPostXml(
buildBaseUrl(agent) + "/test/run", MarshalHelper.marshal(
RunScenarioModelNew.class, runScenarioModel), null);
if (HttpRequester.isInvalidResponse(httpResponse)) {
return null;
}

View File

@ -11,7 +11,7 @@ import org.bench4q.master.entity.db.User;
import org.bench4q.master.exception.ExceptionLog;
import org.bench4q.master.helper.MarshalHelper;
import org.bench4q.master.helper.SessionHelper;
import org.bench4q.share.models.agent.RunScenarioModel;
import org.bench4q.share.models.agent.RunScenarioModelNew;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;
@ -162,13 +162,13 @@ public class ScriptService {
}
}
public RunScenarioModel getRunSceniroModelByScriptId(int scriptId) {
public RunScenarioModelNew getRunSceniroModelByScriptId(int scriptId) {
String content = this.getScriptContentByScriptId(scriptId);
if (content == null) {
return null;
}
return (RunScenarioModel) MarshalHelper.unmarshal(
RunScenarioModel.class, content);
return (RunScenarioModelNew) MarshalHelper.unmarshal(
RunScenarioModelNew.class, content);
}
public String getScriptContentByScriptId(int scriptId) {

View File

@ -9,14 +9,14 @@ import org.bench4q.master.helper.ApplicationContextHelper;
import org.bench4q.master.service.communication.RunningAgentService;
import org.bench4q.master.service.db.AgentService;
import org.bench4q.master.testplan.transaction.Transaction;
import org.bench4q.share.models.agent.RunScenarioModel;
import org.bench4q.share.models.agent.RunScenarioModelNew;
import org.bench4q.share.models.agent.RunScenarioResultModel;
public class AgentTransaction implements Transaction {
private String agentHostName;
private int agentPort;
private RunScenarioModel runScenarioModel;
private RunScenarioModelNew runScenarioModel;
private AgentService agentService;
private RunningAgentService runningAgentService;
private Logger logger = Logger.getLogger(AgentTransaction.class);
@ -37,11 +37,11 @@ public class AgentTransaction implements Transaction {
this.agentPort = agentPort;
}
private RunScenarioModel getRunScenarioModel() {
private RunScenarioModelNew getRunScenarioModel() {
return runScenarioModel;
}
private void setRunScenarioModel(RunScenarioModel runScenarioModel) {
private void setRunScenarioModel(RunScenarioModelNew runScenarioModel) {
this.runScenarioModel = runScenarioModel;
}
@ -62,7 +62,7 @@ public class AgentTransaction implements Transaction {
}
public AgentTransaction(String agentHostName, int port,
RunScenarioModel runScenarioModel) {
RunScenarioModelNew runScenarioModel) {
this.setAgentHostName(agentHostName);
this.setAgentPort(port);
this.setRunScenarioModel(runScenarioModel);
@ -90,7 +90,8 @@ public class AgentTransaction implements Transaction {
}
return ret;
} catch (IOException e) {
this.logger.error(ExceptionLog.getExceptionStackTrace(e).toString());
this.logger
.error(ExceptionLog.getExceptionStackTrace(e).toString());
throw new AgentRunException();
}
}
@ -102,7 +103,7 @@ public class AgentTransaction implements Transaction {
return agent;
}
private boolean isValidRunScenarioModel(RunScenarioModel runScenarioModel) {
private boolean isValidRunScenarioModel(RunScenarioModelNew runScenarioModel) {
return runScenarioModel == null ? false : true;
}

View File

@ -20,7 +20,7 @@ import org.bench4q.master.testplan.highavailable.HighAvailablePool;
import org.bench4q.master.testplan.transaction.Transaction;
import org.bench4q.master.testplan.transaction.agent.AgentRunException;
import org.bench4q.master.testplan.transaction.agent.AgentTransaction;
import org.bench4q.share.models.agent.RunScenarioModel;
import org.bench4q.share.models.agent.RunScenarioModelNew;
import org.bench4q.share.models.agent.RunScenarioResultModel;
import org.bench4q.share.models.agent.StopTestModel;
@ -104,7 +104,7 @@ public abstract class ScriptLoadBase implements Transaction {
public Object execute() {
logger.info(this.getRunningScript().getScriptId());
RunScenarioModel runScenarioModel = this.getScriptService()
RunScenarioModelNew runScenarioModel = this.getScriptService()
.getRunSceniroModelByScriptId(
this.getRunningScript().getScriptId());
if (runScenarioModel == null) {
@ -140,7 +140,7 @@ public abstract class ScriptLoadBase implements Transaction {
protected abstract int getRequireLoad();
private void runAgentsWithScenario(RunScenarioModel runScenarioModel,
private void runAgentsWithScenario(RunScenarioModelNew runScenarioModel,
int totalRequireLoad, int scriptId, UUID testPlanId)
throws JAXBException, ScriptLoadDistributeException {
RunningAgent runningAgent = new RunningAgent();

View File

@ -25,7 +25,7 @@ log4j.appender.FALTAL.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%
log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
log4j.appender.D.File = logs/log.log
log4j.appender.D.Append = true
log4j.appender.D.Threshold = INFO
log4j.appender.D.Threshold = TRACE
log4j.appender.D.layout = org.apache.log4j.PatternLayout
log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n

View File

@ -11,12 +11,13 @@ import static org.junit.Assert.*;
import org.bench4q.master.helper.MarshalHelper;
import org.bench4q.master.test.TestBase;
import org.bench4q.share.communication.HttpRequester.HttpResponse;
import org.bench4q.share.models.agent.RunScenarioModel;
import org.bench4q.share.models.agent.RunScenarioModelNew;
import org.bench4q.share.models.master.OperateScriptServerResponseModel;
import org.bench4q.share.models.master.ScriptModel;
import org.junit.Test;
public class RecordScriptControllerTest extends TestBase {
private static final int RECORD_TIME = 60000;
private final String SCRIPT_URL = TestBase.BASE_URL + "/RecordScript";
public OperateScriptServerResponseModel startRecord() throws IOException,
@ -75,15 +76,15 @@ public class RecordScriptControllerTest extends TestBase {
InterruptedException {
RecordScriptControllerTest test = new RecordScriptControllerTest();
test.accessTocken = test.login();
Thread.sleep(10000);
Thread.sleep(1000);
OperateScriptServerResponseModel model = test.startRecord();
Thread.sleep(600000);
Thread.sleep(RECORD_TIME);
System.out.println("Thread has waken up");
test.stopRecord(model);
ScriptModel scriptModel = test.saveScriptToDB(model, "chen2");
assertNotNull(scriptModel.getScriptContent());
RunScenarioModel runScenarioModel = (RunScenarioModel) MarshalHelper
.unmarshal(RunScenarioModel.class,
RunScenarioModelNew runScenarioModel = (RunScenarioModelNew) MarshalHelper
.unmarshal(RunScenarioModelNew.class,
scriptModel.getScriptContent());
assertNotNull(runScenarioModel);