add code
This commit is contained in:
coderfengyun 2014-06-20 16:40:24 +08:00
parent 448d74f707
commit e613738fa8
4 changed files with 117 additions and 85 deletions

View File

@ -1,6 +1,6 @@
package org.bench4q.agent.plugin.basic.MongoDB;
import java.net.UnknownHostException;
import java.util.Arrays;
import org.bench4q.agent.plugin.Constructor;
import org.bench4q.agent.plugin.Parameter;
@ -8,6 +8,8 @@ import org.bench4q.agent.plugin.Plugin;
import org.bench4q.agent.plugin.behavior.Behavior;
import org.bench4q.agent.plugin.behavior.BehaviorType;
import org.bench4q.agent.utils.Type.SupportTypes;
import org.bench4q.agent.utils.types.Table;
import org.bench4q.agent.utils.types.Table.Row;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
@ -15,72 +17,82 @@ import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.Mongo;
import com.mongodb.util.JSON;
@Plugin("MongoDBPlugin")
public class MongoDBPlugin {
private final String hostName;
private final int port;
private final String dbName;
private final String tableUnderTest = "users";
@Constructor
public MongoDBPlugin() {
}
@Behavior(value = "Insert", type = BehaviorType.USER_BEHAVIOR)
public MongoDBReturn insert(
@Parameter(value = "key", type = SupportTypes.Field) String key,
@Parameter(value = "value", type = SupportTypes.Field) String value,
public MongoDBPlugin(
@Parameter(value = "hostName", type = SupportTypes.Field) String hostName,
@Parameter(value = "port", type = SupportTypes.Field) int port,
@Parameter(value = "dbName", type = SupportTypes.Field) String dbName) {
// TODO测试MongoDB的插入性能
int recordsum = 0;
try {
@SuppressWarnings("deprecation")
Mongo mongo = new Mongo(hostName, port);
DB db = mongo.getDB(dbName);
DBCollection users;
// 获取users DBCollection如果默认没有创建mongodb会自动创建
users = db.getCollection("users");
DBObject user = new BasicDBObject();
user.put(key, value);
users.save(user);
DBCollection test = db.getCollection("users");
// 查询所有的数据
DBCursor cur = test.find();
while (cur.hasNext()) {
System.out.println(cur.next());
this.hostName = hostName;
this.port = port;
this.dbName = dbName;
}
recordsum = test.find().count();
System.out.println("总数目: " + cur.count());
System.out.println("当前游标的id号: " + cur.getCursorId());
System.out.println(JSON.serialize(cur));
@SuppressWarnings("deprecation")
@Behavior(value = "Insert", type = BehaviorType.USER_BEHAVIOR)
public MongoDBReturn insert(
@Parameter(value = "id", type = SupportTypes.Field) String id,
@Parameter(value = "value", type = SupportTypes.Field) String value) {
// int recordsum = 0;
Mongo mongo = null;
try {
mongo = new Mongo(hostName, port);
DB db = mongo.getDB(dbName);
DBCollection table = db.getCollection(this.tableUnderTest);
DBObject valueToInsert = new BasicDBObject();
valueToInsert.put("_id", id);
valueToInsert.put("value", value);
table.save(valueToInsert);
return new MongoDBReturn(true);
} catch (Exception e) {
e.printStackTrace();
return new MongoDBReturn(false);
} finally {
if (mongo != null) {
mongo.close();
}
mongo = null;
db = null;
users = null;
}
} catch (UnknownHostException e) {
}
@SuppressWarnings("deprecation")
@Behavior(value = "Query", type = BehaviorType.USER_BEHAVIOR)
public MongoDBReturn query(
@Parameter(value = "properties", type = SupportTypes.Table) String properties) {
Mongo mongo = null;
try {
mongo = new Mongo(hostName, port);
DB db = mongo.getDB(dbName);
DBCollection table = db.getCollection(this.tableUnderTest);
Table propertiesTable = Table.buildTable(properties,
Arrays.asList("propertyName", "propertyValue"));
BasicDBObject query = new BasicDBObject();
for (Row row : propertiesTable.getRows()) {
String propertyName = row.getCell("propertyName");
String propertyValue = row.getCell("propertyValue");
query.append(propertyName, propertyValue);
}
DBCursor queryResult = table.find(query);
for (DBObject item : queryResult.toArray()) {
System.out.println(item);
}
return new MongoDBReturn(true);
} catch (Exception e) {
e.printStackTrace();
return new MongoDBReturn(false);
} finally {
if (mongo != null) {
mongo.close();
}
return new MongoDBReturn(recordsum > 0);
}
/*
* @Behavior(value = "Query", type = BehaviorType.USER_BEHAVIOR) public
* MongoDBReturn query(
*
* @Parameter(value = "queryString", type = SupportTypes.Field) String
* queryString,
*
* @Parameter(value = "attributes", type = SupportTypes.Field) List<Object>
* attributes){
*
*
* return null; }
*/
}
}

View File

@ -2,16 +2,7 @@
<!DOCTYPE ui SYSTEM "../../dtd/ui.dtd">
<ui>
<plugin name="MongoDBPlugin">
<params />
</plugin>
<behavior name="Insert">
<params>
<param name="key" label="key">
<field size="7" />
</param>
<param name="value" label="value">
<field size="7" />
</param>
<param name="hostName" label="hostName">
<field size="7" />
</param>
@ -22,6 +13,22 @@
<field size="7" />
</param>
</params>
</plugin>
<behavior name="Insert">
<params>
<param name="id" label="id">
<field size="7" />
</param>
<param name="value" label="value">
<field size="7" />
</param>
</params>
</behavior>
<behavior name="Query">
<params>
<param name="properties" label="properties">
<table cols="propertyName;propertyValue" />
</param>
</params>
</behavior>
</ui>

View File

@ -162,8 +162,10 @@ public class Table extends Type {
List<String> tempEntries = ParameterParser.getRealTokens(
Table.ESCAPE_ROW_SEPARATOR, value, false);
List<List<String>> allValues = new ArrayList<List<String>>();
for (int i = 0; i < tempEntries.size(); i++) {
String tempEntry = tempEntries.get(i);
for (String tempEntry : tempEntries) {
if (tempEntry.trim().isEmpty()) {
continue;
}
List<String> values = ParameterParser.getRealTokens(
Table.ESCAPE_COLUMN_SEPARATOR, tempEntry, true);
arrangeTableWithRowSeparatorTail(values);
@ -179,16 +181,14 @@ public class Table extends Type {
for (int i = 0; i < allValues.size(); i++) {
List<String> resultValues = new ArrayList<String>();
List<String> tempValues = allValues.get(i);
for (int j = 0; j < tempValues.size(); j++) {
String v = tempValues.get(j);
List<String> temp = ParameterParser
.getRealTokens("=", v, false);
String res;
if (temp.size() <= 1) {
for (String v : tempValues) {
// List<String> temp = ParameterParser
// .getRealTokens("=", v, false);
int index = v.indexOf('=');
if (index < 0) {
continue;
}
res = temp.get(1);
resultValues.add(res);
resultValues.add(v.substring(index + 1));
}
result.add(resultValues);
}

View File

@ -30,11 +30,26 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:application-context.xml" })
public class Test_MongoDBPlugin extends TestBase {
private UUID testId = UUID.randomUUID();
@Test
public void test_Insert() {
MongoDBPlugin mongoDBPlugin = new MongoDBPlugin();
MongoDBReturn result = mongoDBPlugin.insert("key1", "value1",
"localhost", 27017, "temp");
MongoDBPlugin mongoDBPlugin = new MongoDBPlugin("localhost", 27017,
"temp");
MongoDBReturn result = mongoDBPlugin.insert(this.testId.toString(),
"value1");
result = mongoDBPlugin.query("propertyName=_id|propertyValue="
+ this.testId.toString() + "|;");
assertTrue(result.getSuccessCount() > 0);
}
@Test
public void test_query() {
MongoDBPlugin mongoDBPlugin = new MongoDBPlugin("localhost", 27017,
"temp");
MongoDBReturn result = mongoDBPlugin
.query("propertyName=_id|propertyValue="
+ this.testId.toString() + "|;");
assertTrue(result.getSuccessCount() > 0);
}
@ -45,7 +60,10 @@ public class Test_MongoDBPlugin extends TestBase {
String pluginId = "mongo1";
mongoDB.setId(pluginId);
mongoDB.setName("MongoDBPlugin");
mongoDB.setParameters(new LinkedList<ParameterModel>());
mongoDB.setParameters(Arrays.asList(
ParameterModel.createParameter("hostName", "localhost"),
ParameterModel.createParameter("port", "27017"),
ParameterModel.createParameter("dbName", "temp")));
scenarioModel.getUsePlugins().add(mongoDB);
scenarioModel.setPages(new LinkedList<PageModel>());
@ -58,12 +76,7 @@ public class Test_MongoDBPlugin extends TestBase {
BehaviorModel.UserBehaviorBuilder(0, "Insert", pluginId, Arrays
.asList(ParameterModel.createParameter("key", "key2"),
ParameterModel.createParameter("value",
"value2"), ParameterModel
.createParameter("hostName",
"localhost"), ParameterModel
.createParameter("port", "27017"),
ParameterModel
.createParameter("dbName", "temp"))));
"value2"))));
String scriptContent = MarshalHelper.tryMarshal(scenarioModel);
System.out.println(scriptContent);
File targetFile = new File("Scripts"