local file storage added.

This commit is contained in:
Zhen Tang 2013-07-15 15:10:41 +08:00
parent 9f87dadc6a
commit e68215db04
6 changed files with 114 additions and 13 deletions

View File

@ -1,5 +1,6 @@
package org.bench4q.agent.scenario;
import java.io.File;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Collections;
@ -16,7 +17,7 @@ import javax.xml.bind.Marshaller;
import org.bench4q.agent.plugin.Plugin;
import org.bench4q.agent.plugin.PluginManager;
import org.bench4q.agent.storage.HdfsStorage;
import org.bench4q.agent.storage.StorageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@ -24,7 +25,7 @@ import org.springframework.stereotype.Component;
public class ScenarioEngine {
private PluginManager pluginManager;
private Map<UUID, ScenarioContext> runningTests;
private HdfsStorage hdfsStorage;
private StorageHelper storageHelper;
public ScenarioEngine() {
this.setRunningTests(new HashMap<UUID, ScenarioContext>());
@ -39,13 +40,13 @@ public class ScenarioEngine {
this.pluginManager = pluginManager;
}
private HdfsStorage getHdfsStorage() {
return hdfsStorage;
public StorageHelper getStorageHelper() {
return storageHelper;
}
@Autowired
private void setHdfsStorage(HdfsStorage hdfsStorage) {
this.hdfsStorage = hdfsStorage;
public void setStorageHelper(StorageHelper storageHelper) {
this.storageHelper = storageHelper;
}
public Map<UUID, ScenarioContext> getRunningTests() {
@ -156,10 +157,28 @@ public class ScenarioEngine {
}
}
public String getPath() {
public String getHdfsPath() {
return "hdfs://133.133.12.21:9000/home/bench4q/results";
}
public String getLocalPath() {
String directory = this.getClass().getProtectionDomain()
.getCodeSource().getLocation().getFile().replace("\\", "/");
File file = new File(directory);
if (!file.isDirectory()) {
directory = directory.substring(0, directory.lastIndexOf("/"));
}
if (!directory.endsWith("/")) {
directory += "/";
}
directory += "results";
File toCreate = new File(directory);
if (!toCreate.exists()) {
toCreate.mkdirs();
}
return directory;
}
public void saveTestResults(UUID runId) {
ScenarioContext scenarioContext = this.getRunningTests().get(runId);
if (scenarioContext == null) {
@ -201,10 +220,12 @@ public class ScenarioEngine {
testResult.getClass()).createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
stringWriter = new StringWriter();
String fileName = this.getPath() + "/" + runId.toString() + ".xml";
String fileName = this.getLocalPath() + "/" + runId.toString()
+ ".xml";
marshaller.marshal(testResult, stringWriter);
String content = stringWriter.toString();
this.getHdfsStorage().writeFile(content, fileName);
this.getStorageHelper().getLocalStorage()
.writeFile(content, fileName);
} catch (Exception e) {
e.printStackTrace();
} finally {

View File

@ -16,7 +16,7 @@ import org.apache.hadoop.io.IOUtils;
import org.springframework.stereotype.Component;
@Component
public class HdfsStorage {
public class HdfsStorage implements Storage {
private FileSystem getFileSystem() throws IOException {
Configuration conf = new Configuration();
conf.set("mapred.jop.tracker", "hdfs://133.133.12.21:9001");

View File

@ -0,0 +1,44 @@
package org.bench4q.agent.storage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import org.springframework.stereotype.Component;
@Component
public class LocalStorage implements Storage {
public String readFile(String path) {
try {
InputStreamReader inputStreamReader = new InputStreamReader(
new FileInputStream(new File(path)), "UTF-8");
StringBuffer ret = new StringBuffer();
int toRead;
while ((toRead = inputStreamReader.read()) != -1) {
ret.append((char) toRead);
}
inputStreamReader.close();
return ret.toString();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public boolean writeFile(String content, String path) {
try {
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(
new FileOutputStream(new File(path)), "UTF-8");
outputStreamWriter.write(content);
outputStreamWriter.flush();
outputStreamWriter.close();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
}

View File

@ -0,0 +1,7 @@
package org.bench4q.agent.storage;
public interface Storage {
public String readFile(String path);
public boolean writeFile(String content, String path);
}

View File

@ -0,0 +1,29 @@
package org.bench4q.agent.storage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class StorageHelper {
private HdfsStorage hdfsStorage;
private LocalStorage localStorage;
public HdfsStorage getHdfsStorage() {
return hdfsStorage;
}
@Autowired
public void setHdfsStorage(HdfsStorage hdfsStorage) {
this.hdfsStorage = hdfsStorage;
}
public LocalStorage getLocalStorage() {
return localStorage;
}
@Autowired
public void setLocalStorage(LocalStorage localStorage) {
this.localStorage = localStorage;
}
}

View File

@ -44,7 +44,7 @@ public class RunScenarioTest {
getUserBehaviorModel.setParameters(new ArrayList<ParameterModel>());
ParameterModel parameterModelOne = new ParameterModel();
parameterModelOne.setKey("url");
parameterModelOne.setValue("http://Bench4Q-Agent-1:6565");
parameterModelOne.setValue("http://localhost:6565");
getUserBehaviorModel.getParameters().add(parameterModelOne);
runScenarioModel.getUserBehaviors().add(getUserBehaviorModel);
UserBehaviorModel timerUserBehaviorModel = new UserBehaviorModel();
@ -64,7 +64,7 @@ public class RunScenarioTest {
.setParameters(new ArrayList<ParameterModel>());
ParameterModel parameterModelThree = new ParameterModel();
parameterModelThree.setKey("url");
parameterModelThree.setValue("http://Bench4Q-Agent-1:6565");
parameterModelThree.setValue("http://localhost:6565");
postUserBehaviorModel.getParameters().add(parameterModelThree);
ParameterModel parameterModelFour = new ParameterModel();
parameterModelFour.setKey("content");
@ -75,7 +75,7 @@ public class RunScenarioTest {
runScenarioModel.getClass()).createMarshaller();
StringWriter stringWriter = new StringWriter();
marshaller.marshal(runScenarioModel, stringWriter);
String url = "http://Bench4Q-Agent-1:6565/test/run";
String url = "http://localhost:6565/test/run";
String content = stringWriter.toString();
System.out.println(content);
URL target = new URL(url);