litte step refactor

This commit is contained in:
coderfengyun 2013-12-18 17:19:03 +08:00
parent 4750a06c52
commit 51d0bab4d9
7 changed files with 103 additions and 52 deletions

View File

@ -39,12 +39,12 @@ public class ProxyServer implements Runnable {
} }
} }
public void processResponse(HttpRequestHeader header, byte[] response) public void processResponse(HttpRequestHeader header, byte[] requestBody,
throws Exception { byte[] response) throws Exception {
for (Enumeration<Observer> e = this.listeners.elements(); e for (Enumeration<Observer> e = this.listeners.elements(); e
.hasMoreElements();) { .hasMoreElements();) {
Observer pl = (Observer) e.nextElement(); Observer pl = (Observer) e.nextElement();
pl.processResponse(header, response); pl.processResponse(header, requestBody, response);
} }
} }
@ -81,7 +81,7 @@ public class ProxyServer implements Runnable {
byte[] paramArrayOfByte) throws Exception; byte[] paramArrayOfByte) throws Exception;
public abstract void processResponse( public abstract void processResponse(
HttpRequestHeader paramHttpRequestHeader, HttpRequestHeader paramHttpRequestHeader, byte[] requestBody,
byte[] paramArrayOfByte) throws Exception; byte[] paramArrayOfByte) throws Exception;
} }
} }

View File

@ -168,7 +168,7 @@ public class RequestHandler implements Runnable {
} }
log.trace("transferred response"); log.trace("transferred response");
this.proxyServer.processResponse(this.header, this.proxyServer.processResponse(this.header, requestBody,
this.buffer.toByteArray()); this.buffer.toByteArray());
log.trace("processed response"); log.trace("processed response");
} }

View File

@ -47,17 +47,15 @@ public abstract class AbstractCodeGenerator implements IScriptGenerator,
protected boolean headersExist = false; protected boolean headersExist = false;
private IScriptAdapter scriptAdapter; private IScriptAdapter scriptAdapter;
private String testName; private String testName;
@SuppressWarnings("unused")
private String testPath;
private boolean ignoreNextResponse; private boolean ignoreNextResponse;
private Pattern[] namePatterns; private Pattern[] namePatterns;
private LinkedList<BehaviorBaseModel> outstandingInserts; private LinkedList<BehaviorBaseModel> outstandingInserts;
private Thread insertThread; private Thread insertThread;
private String charset; private String charset;
private long timeElapsedSinceLastestRequest = 0L; private long timeElapsedSinceLastestRequest;
private Date latestRequestDate = new Date(); private Date latestRequestDate;
private boolean isFirstRequest = true; private int requestCount;
private int requestCount = 0; private boolean firstHtmlResponse;
static final boolean $assertionsDisabled = !(AbstractCodeGenerator.class static final boolean $assertionsDisabled = !(AbstractCodeGenerator.class
.desiredAssertionStatus()); .desiredAssertionStatus());
@ -74,7 +72,16 @@ public abstract class AbstractCodeGenerator implements IScriptGenerator,
return requestCount; return requestCount;
} }
private boolean isFirstHtmlResponse() {
return firstHtmlResponse;
}
private void setFirstHtmlResponse(boolean firstHtmlResponse) {
this.firstHtmlResponse = firstHtmlResponse;
}
public AbstractCodeGenerator(IScriptAdapter adapter, String[] nameRegExps) { public AbstractCodeGenerator(IScriptAdapter adapter, String[] nameRegExps) {
this.reset();
this.scriptAdapter = adapter; this.scriptAdapter = adapter;
this.namePatterns = new Pattern[nameRegExps.length]; this.namePatterns = new Pattern[nameRegExps.length];
for (int i = 0; i < nameRegExps.length; ++i) for (int i = 0; i < nameRegExps.length; ++i)
@ -86,6 +93,15 @@ public abstract class AbstractCodeGenerator implements IScriptGenerator,
this.insertThread.start(); this.insertThread.start();
} }
private void reset() {
this.latestRequestDate = new Date();
this.assertNumber = 0;
this.requestCount = 0;
this.latestRequestDate = new Date();
this.timeElapsedSinceLastestRequest = 0L;
this.setFirstHtmlResponse(true);
}
public IScriptAdapter getScriptAdapter() { public IScriptAdapter getScriptAdapter() {
return this.scriptAdapter; return this.scriptAdapter;
} }
@ -95,7 +111,6 @@ public abstract class AbstractCodeGenerator implements IScriptGenerator,
} }
private void setTestPath(String testPath) { private void setTestPath(String testPath) {
this.testPath = testPath;
} }
public long getTimeElapsedSinceLastestRequest() { public long getTimeElapsedSinceLastestRequest() {
@ -103,7 +118,7 @@ public abstract class AbstractCodeGenerator implements IScriptGenerator,
} }
public boolean isFirstRequest() { public boolean isFirstRequest() {
return this.isFirstRequest; return this.requestCount == 0;
} }
public String parseTestName() { public String parseTestName() {
@ -273,7 +288,6 @@ public abstract class AbstractCodeGenerator implements IScriptGenerator,
doSetData(action.getMultiPartData()); doSetData(action.getMultiPartData());
} }
this.requestCount++; this.requestCount++;
doCallUrl(action, method, data_str, cont_len_str); doCallUrl(action, method, data_str, cont_len_str);
} }
@ -308,8 +322,8 @@ public abstract class AbstractCodeGenerator implements IScriptGenerator,
return ((HeaderValue[]) (HeaderValue[]) v.toArray(hv)); return ((HeaderValue[]) (HeaderValue[]) v.toArray(hv));
} }
public void processResponse(HttpRequestHeader header, byte[] response) public void processResponse(HttpRequestHeader header, byte[] requestBody,
throws Exception { byte[] response) throws Exception {
if (this.ignoreNextResponse) { if (this.ignoreNextResponse) {
log.debug("Ignoring response"); log.debug("Ignoring response");
this.ignoreNextResponse = false; this.ignoreNextResponse = false;
@ -352,8 +366,7 @@ public abstract class AbstractCodeGenerator implements IScriptGenerator,
log.debug("Ignoring response because content type is not known: " log.debug("Ignoring response because content type is not known: "
+ contentType); + contentType);
} }
if (isFirstRequest())
this.isFirstRequest = false;
} }
// TODO: edit this type // TODO: edit this type

View File

@ -19,10 +19,6 @@ import org.bench4q.share.models.agent.scriptrecord.BehaviorBaseModel;
import org.bench4q.share.models.agent.scriptrecord.TimerBehaviorModel; import org.bench4q.share.models.agent.scriptrecord.TimerBehaviorModel;
import org.bench4q.share.models.agent.scriptrecord.UsePluginModel; import org.bench4q.share.models.agent.scriptrecord.UsePluginModel;
import org.bench4q.share.models.agent.scriptrecord.UserBehaviorModel; import org.bench4q.share.models.agent.scriptrecord.UserBehaviorModel;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class Bench4qCodeGenerator extends AbstractCodeGenerator { public class Bench4qCodeGenerator extends AbstractCodeGenerator {
static final String TIMER_PROP = "generator.isac.timer"; static final String TIMER_PROP = "generator.isac.timer";
@ -215,42 +211,29 @@ public class Bench4qCodeGenerator extends AbstractCodeGenerator {
if (htmlStart == -1 || htmlEnd == -1) { if (htmlStart == -1 || htmlEnd == -1) {
return; return;
} }
parseDocument(rootUrl, responseContent, htmlStart, htmlEnd);
}
private void parseDocument(String rootUrl, String responseContent,
int htmlStart, int htmlEnd) {
// TODO:reset the childrenUrls in scirptAdapter
String htmlContent = responseContent.substring(htmlStart, htmlEnd + 6); String htmlContent = responseContent.substring(htmlStart, htmlEnd + 6);
saveToFile(htmlContent); saveToFile(htmlContent);
Document document = Jsoup.parse(htmlContent); HtmlDocumentParser documentParser = new HtmlDocumentParser(htmlContent,
document.setBaseUri(rootUrl); rootUrl);
Elements allChildrenLinks = new Elements(); List<ChildrenUrl> childrenUrls = documentParser
allChildrenLinks.addAll(document.select("[src]")); .buildParentChildrenRelationship(this.getScriptAdapter()
allChildrenLinks.addAll(document.select("link[href]")); .getParentBatchIdWithParentUrl(rootUrl));
this.getScriptAdapter().getChildrenUrls().addAll(childrenUrls);
logger.info("Following url shuold in children urls: \n");
for (Element element : allChildrenLinks) {
String childAbsoluteUrl = element.absUrl("src");
if (childAbsoluteUrl.isEmpty()) {
childAbsoluteUrl = element.absUrl("href");
}
int parentBatchId = this.getScriptAdapter()
.getParentBatchIdWithParentUrl(rootUrl);
if (parentBatchId == -1) {
continue;
}
logger.info(childAbsoluteUrl);
this.getScriptAdapter()
.getChildrenUrls()
.add(ChildrenUrl.createChilldUrl(childAbsoluteUrl,
parentBatchId));
}
} }
private void saveToFile(String htmlContent) { private void saveToFile(String htmlContent) {
// For Test // TODO: For Test
try { try {
File storeFile = new File("RecordScriptTestCase/baidu.html"); File storeFile = new File("RecordScriptTestCase/baidu.html");
FileUtils.writeStringToFile(storeFile, htmlContent); FileUtils.writeStringToFile(storeFile, htmlContent);
} catch (IOException e) { } catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace(); e.printStackTrace();
} }
} }

View File

@ -0,0 +1,54 @@
package org.bench4q.master.scriptrecord.httpcapture.generator;
import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class HtmlDocumentParser {
private Document document;
private static Logger logger = Logger.getLogger(HtmlDocumentParser.class);
private Document getDocument() {
return document;
}
private void setDocument(Document document) {
this.document = document;
}
public HtmlDocumentParser(String htmlContent, String baseUrl) {
this.setDocument(Jsoup.parse(htmlContent, baseUrl));
}
public void beginParse() {
}
public List<ChildrenUrl> buildParentChildrenRelationship(int parentBatchId) {
List<ChildrenUrl> result = new ArrayList<ChildrenUrl>();
if (parentBatchId == -1) {
return result;
}
Elements allChildrenLinks = new Elements();
allChildrenLinks.addAll(this.getDocument().select("[src]"));
allChildrenLinks.addAll(this.getDocument().select("link[href]"));
logger.info("Following url shuold in children urls: \n");
for (Element element : allChildrenLinks) {
String childAbsoluteUrl = element.absUrl("src");
if (childAbsoluteUrl.isEmpty()) {
childAbsoluteUrl = element.absUrl("href");
}
logger.info(childAbsoluteUrl);
result.add(ChildrenUrl.createChilldUrl(childAbsoluteUrl,
parentBatchId));
}
return result;
}
}

View File

@ -10,7 +10,7 @@ import javax.xml.bind.annotation.XmlRootElement;
import org.bench4q.share.models.agent.scriptrecord.UsePluginModel; import org.bench4q.share.models.agent.scriptrecord.UsePluginModel;
@XmlRootElement(name = "runScenario") @XmlRootElement(name = "runScenario")
public class RunScenarioModelTest { public class RunScenarioModelNew {
private int poolSize; private int poolSize;
private List<UsePluginModel> usePlugins; private List<UsePluginModel> usePlugins;
private List<Page> pages; private List<Page> pages;
@ -44,7 +44,7 @@ public class RunScenarioModelTest {
this.pages = pages; this.pages = pages;
} }
public RunScenarioModelTest() { public RunScenarioModelNew() {
this.setUsePlugins(new ArrayList<UsePluginModel>()); this.setUsePlugins(new ArrayList<UsePluginModel>());
} }
} }

View File

@ -45,7 +45,8 @@ public class TestCodeGenerator extends TestRecordBase {
inputStream.close(); inputStream.close();
outputStream.close(); outputStream.close();
IScriptGenerator scriptGenerator = this.codeGenerator; IScriptGenerator scriptGenerator = this.codeGenerator;
scriptGenerator.processResponse(header, outputStream.toByteArray()); scriptGenerator.processResponse(header, new byte[0],
outputStream.toByteArray());
assertTrue(this.scriptAdapter.getChildrenUrls().size() > 0); assertTrue(this.scriptAdapter.getChildrenUrls().size() > 0);
} }