From f6bb5384cdb3295b21877b02589c4f5a84fa813a Mon Sep 17 00:00:00 2001 From: coderfengyun Date: Mon, 7 Jul 2014 09:08:02 +0800 Subject: [PATCH] refactor refactor --- .../impl/ScenarioResultCollector.java | 4 +- .../plugin/basic/MongoDB/MongoDBPlugin.java | 10 +- .../java/org/bench4q/agent/test/Solution.java | 195 ++++++++++++++---- .../agent/test/TestWithScriptFile.java | 2 +- .../agent/test/scenario/Test_Scenario.java | 20 +- .../domain/service/TestPlanService.java | 4 + .../datastatistics/ScriptBriefStatistics.java | 4 +- .../unitTest/service/Test_TestPlanEngine.java | 19 +- 8 files changed, 200 insertions(+), 58 deletions(-) diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/datacollector/impl/ScenarioResultCollector.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/datacollector/impl/ScenarioResultCollector.java index 57451662..53b44994 100644 --- a/Bench4Q-Agent/src/main/java/org/bench4q/agent/datacollector/impl/ScenarioResultCollector.java +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/datacollector/impl/ScenarioResultCollector.java @@ -245,12 +245,12 @@ public class ScenarioResultCollector extends AbstractDataCollector { + new SimpleDateFormat("hhmm") + ".txt"; } - public void add(PageResult pageResult) { + public synchronized void add(PageResult pageResult) { this.getPageResultCollector().add(pageResult); } @Override - public void add(BehaviorResult behaviorResult) { + public synchronized void add(BehaviorResult behaviorResult) { super.add(behaviorResult); statisticScenarioBriefResult(behaviorResult); statisticBehaviorBriefResult(behaviorResult); diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/plugin/basic/MongoDB/MongoDBPlugin.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/plugin/basic/MongoDB/MongoDBPlugin.java index 05c3576a..62a01a7f 100644 --- a/Bench4Q-Agent/src/main/java/org/bench4q/agent/plugin/basic/MongoDB/MongoDBPlugin.java +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/plugin/basic/MongoDB/MongoDBPlugin.java @@ -18,7 +18,11 @@ import com.mongodb.DBCollection; import com.mongodb.DBCursor; import com.mongodb.DBObject; import com.mongodb.Mongo; +import com.mongodb.MongoClientOptions; +import com.mongodb.MongoOptions; +import com.mongodb.ServerAddress; +@SuppressWarnings("deprecation") @Plugin("MongoDBPlugin") public class MongoDBPlugin { private final String hostName; @@ -36,14 +40,15 @@ public class MongoDBPlugin { this.dbName = dbName; } - @SuppressWarnings("deprecation") @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) { Mongo mongo = null; try { - mongo = new Mongo(hostName, port); + mongo = new Mongo(new ServerAddress(hostName, port), + new MongoOptions(new MongoClientOptions.Builder() + .maxWaitTime(1000 * 40).build())); DB db = mongo.getDB(dbName); DBCollection table = db.getCollection(this.tableUnderTest); DBObject valueToInsert = new BasicDBObject(); @@ -62,7 +67,6 @@ public class MongoDBPlugin { } - @SuppressWarnings("deprecation") @Behavior(value = "Query", type = BehaviorType.USER_BEHAVIOR) public MongoDBReturn query( @Parameter(value = "properties", type = SupportTypes.Table) String properties) { diff --git a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/Solution.java b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/Solution.java index 69756d4e..b2c52059 100644 --- a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/Solution.java +++ b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/Solution.java @@ -2,63 +2,182 @@ package org.bench4q.agent.test; import static org.junit.Assert.*; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.HashSet; import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.Queue; +import java.util.Set; import org.junit.Test; public class Solution { + private Set dict; - public int candy(int[] ratings) { - if (ratings == null || ratings.length == 0) { - return 0; + public List> findLadders(String start, String end, + Set dict) { + if (start == null || end == null || start.length() != end.length()) { + return new ArrayList>(); } - int result = ratings.length, minIndex = findMinIndex(ratings); - // Left part - for (int i = minIndex - 1; i >= 0; i--) { - } - // Right Part - for (int i = minIndex + 1; i <= ratings.length; i++) { - } - return result; + this.dict = dict; + return broadFirstSearch(start, end); } - private int findMinIndex(int[] target) { - int i = 0, maxValue = Integer.MIN_VALUE, result = -1; - while (i < target.length) { - if (target[i] > maxValue) { - maxValue = target[i]; - result = i; + private List> broadFirstSearch(String start, String end) { + List> result = new ArrayList>(); + Queue queue1 = new LinkedList(), queue2 = new LinkedList(), currentQueue = queue1, nextQueue = queue2; + Map footPrints = new HashMap(); + currentQueue.offer(new WordInPath(start, null)); + footPrints.put(start, 0); + boolean toCheckNextLevel = true; + while (currentQueue.peek() != null) { + WordInPath current = currentQueue.poll(); + if (current.reach(end)) { + result.add(current.buildPath()); + toCheckNextLevel = false; + continue; + } + if (!toCheckNextLevel) { + continue; + } + for (String neighbor : current.getNeighbors()) { + if ((!footPrints.containsKey(neighbor)) + || (current.length() <= footPrints.get(neighbor))) { + footPrints.put(neighbor, current.length()); + nextQueue.offer(new WordInPath(neighbor, current)); + } + } + if (currentQueue.peek() == null) { + Queue temp = currentQueue; + currentQueue = nextQueue; + nextQueue = temp; } } return result; } - private int sumOfZeroTo(int target) { - return target * (target + 1) / 2; + private class WordInPath { + private final String word; + private final WordInPath previous; + private final int size; + + public WordInPath(String word, WordInPath previous) { + this.word = word; + this.previous = previous; + this.size = previous == null ? 1 : previous.length() + 1; + } + + public List buildPath() { + LinkedList result = new LinkedList(); + WordInPath current = this; + while (current != null) { + result.push(current.word); + current = current.previous; + } + return result; + } + + public int length() { + return this.size; + } + + public boolean reach(String target) { + return this.word.equals(target); + } + + private List getNeighbors() { + List result = new LinkedList(); + StringBuilder changed = new StringBuilder(this.word); + for (int i = 0; i < this.word.length(); i++) { + char ch = this.word.charAt(i); + for (char c = 'a'; c < 'z'; c++) { + if (c == ch) { + continue; + } + changed.setCharAt(i, c); + String temp = changed.toString(); + if (Solution.this.dict.contains(temp)) { + result.add(temp); + } + changed.setCharAt(i, ch); + } + } + return result; + } + } @Test - public void testAscending() { - assertEquals(15, this.candy(new int[] { 1, 2, 3, 4, 5 })); + public void test() { + List> result = this.findLadders( + "nape", + "mild", + new HashSet(Arrays.asList("dose", "ends", "dine", + "jars", "prow", "soap", "guns", "hops", "cray", "hove", + "ella", "hour", "lens", "jive", "wiry", "earl", "mara", + "part", "flue", "putt", "rory", "bull", "york", "ruts", + "lily", "vamp", "bask", "peer", "boat", "dens", "lyre", + "jets", "wide", "rile", "boos", "down", "path", "onyx", + "mows", "toke", "soto", "dork", "nape", "mans", "loin", + "jots", "male", "sits", "minn", "sale", "pets", "hugo", + "woke", "suds", "rugs", "vole", "warp", "mite", "pews", + "lips", "pals", "nigh", "sulk", "vice", "clod", "iowa", + "gibe", "shad", "carl", "huns", "coot", "sera", "mils", + "rose", "orly", "ford", "void", "time", "eloy", "risk", + "veep", "reps", "dolt", "hens", "tray", "melt", "rung", + "rich", "saga", "lust", "yews", "rode", "many", "cods", + "rape", "last", "tile", "nosy", "take", "nope", "toni", + "bank", "jock", "jody", "diss", "nips", "bake", "lima", + "wore", "kins", "cult", "hart", "wuss", "tale", "sing", + "lake", "bogy", "wigs", "kari", "magi", "bass", "pent", + "tost", "fops", "bags", "duns", "will", "tart", "drug", + "gale", "mold", "disk", "spay", "hows", "naps", "puss", + "gina", "kara", "zorn", "boll", "cams", "boas", "rave", + "sets", "lego", "hays", "judy", "chap", "live", "bahs", + "ohio", "nibs", "cuts", "pups", "data", "kate", "rump", + "hews", "mary", "stow", "fang", "bolt", "rues", "mesh", + "mice", "rise", "rant", "dune", "jell", "laws", "jove", + "bode", "sung", "nils", "vila", "mode", "hued", "cell", + "fies", "swat", "wags", "nate", "wist", "honk", "goth", + "told", "oise", "wail", "tels", "sore", "hunk", "mate", + "luke", "tore", "bond", "bast", "vows", "ripe", "fond", + "benz", "firs", "zeds", "wary", "baas", "wins", "pair", + "tags", "cost", "woes", "buns", "lend", "bops", "code", + "eddy", "siva", "oops", "toed", "bale", "hutu", "jolt", + "rife", "darn", "tape", "bold", "cope", "cake", "wisp", + "vats", "wave", "hems", "bill", "cord", "pert", "type", + "kroc", "ucla", "albs", "yoko", "silt", "pock", "drub", + "puny", "fads", "mull", "pray", "mole", "talc", "east", + "slay", "jamb", "mill", "dung", "jack", "lynx", "nome", + "leos", "lade", "sana", "tike", "cali", "toge", "pled", + "mile", "mass", "leon", "sloe", "lube", "kans", "cory", + "burs", "race", "toss", "mild", "tops", "maze", "city", + "sadr", "bays", "poet", "volt", "laze", "gold", "zuni", + "shea", "gags", "fist", "ping", "pope", "cora", "yaks", + "cosy", "foci", "plan", "colo", "hume", "yowl", "craw", + "pied", "toga", "lobs", "love", "lode", "duds", "bled", + "juts", "gabs", "fink", "rock", "pant", "wipe", "pele", + "suez", "nina", "ring", "okra", "warm", "lyle", "gape", + "bead", "lead", "jane", "oink", "ware", "zibo", "inns", + "mope", "hang", "made", "fobs", "gamy", "fort", "peak", + "gill", "dino", "dina", "tier"))); + + for (List item : result) { + for (String word : item) { + System.out.print(word + " -> "); + } + System.out.println(); + } } @Test - public void testAllEquals() { - assertEquals(5, this.candy(new int[] { 2, 2, 2, 2, 2 })); - } - - @Test - public void testAscendingAndEquals() { - assertEquals(18, this.candy(new int[] { 1, 2, 3, 4, 4, 4 })); - } - - @Test - public void testEqualsAndAscending() { - assertEquals(12, this.candy(new int[] { 1, 1, 1, 3, 4, 5 })); - } - - @Test - public void testAscendingAndDescending() { - assertEquals(16, this.candy(new int[] { 1, 2, 3, 4, 3, 2, 1 })); + public void test2() { + String test1 = "abc"; + StringBuilder builder = new StringBuilder(test1); + builder.setCharAt(1, 'a'); + System.out.println(builder.toString()); } } \ No newline at end of file diff --git a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/TestWithScriptFile.java b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/TestWithScriptFile.java index 494f444e..82a41dcc 100644 --- a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/TestWithScriptFile.java +++ b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/TestWithScriptFile.java @@ -62,7 +62,7 @@ public class TestWithScriptFile { public TestWithScriptFile() { this.setFilePath("Scripts" + System.getProperty("file.separator") - + "testForBrief.xml"); + + "mongo9To1.xml"); this.setHttpRequester(new HttpRequester()); } diff --git a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/scenario/Test_Scenario.java b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/scenario/Test_Scenario.java index b2b9d827..98942cad 100644 --- a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/scenario/Test_Scenario.java +++ b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/scenario/Test_Scenario.java @@ -35,11 +35,13 @@ public class Test_Scenario extends TestBase { @Test public void test_RealScenario() throws IOException { - BehaviorModel[] behaviors = new BehaviorModel[29]; + BehaviorModel[] behaviors = new BehaviorModel[28]; for (int i = 0; i < 9; i++) { - behaviors[3 * i] = BehaviorModel.ControlBehaviorBuilder(3 * i, - "Next", "UUID_0", Collections. emptyList()); - behaviors[3 * i + 1] = BehaviorModel.ControlBehaviorBuilder( + behaviors[3 * i] = BehaviorModel + .ControlBehaviorBuilder(3 * i, "Sleep", "Timer_0", Arrays + .asList(ParameterModel + .createParameter("time", "10"))); + behaviors[3 * i + 1] = BehaviorModel.TimerBehaviorBuilder( 3 * i + 1, "Set", "Context_0", Arrays.asList(ParameterModel .createParameter("toSave", "${UUID_0:var}"))); behaviors[3 * i + 2] = BehaviorModel @@ -55,13 +57,9 @@ public class Test_Scenario extends TestBase { "value", "828731929090399688663513844419156517791913487136162075054396888608979390802698967398774976538053071265086303536432973027547398485897859650637839199967916419278859657562410371550970990533755543895317958490879810955359859803793616914752497927102101107393602107374624265703167902037562601756290624400217677905796841586506540830996528039667809341170671731917392828543425909887469350859806312812589702773082295018742634842159778009485901428286815715514440370336211621120027294867866046842579395402354849290925687231979896816952130161564546780855710443002842992917011602246580128072986737704758180050405202976240171827695507627799312781883033823819663102843159141022325482803101497005554269652897365593399081219143594918151989844361363454642099858085072470136971359171934346214072016627940215525657780835264287095014748952849011967941296078931973149033644269161629346531549579584063826798825958153903463232155174301997795504543310963155667642944030315802981522872371957377604512253881055347955240594154853114826089693318722619283530276507503"))); } - behaviors[27] = BehaviorModel.UserBehaviorBuilder(27, "Query", - "MongoDBPlugin_0", - Arrays.asList(ParameterModel.createParameter("properties", - "propertyName=key|propertyValue=${Context_0:var}"))); - behaviors[28] = BehaviorModel.TimerBehaviorBuilder(28, "Sleep", + behaviors[27] = BehaviorModel.TimerBehaviorBuilder(28, "Sleep", "Timer_0", - Arrays.asList(ParameterModel.createParameter("time", "10"))); + Arrays.asList(ParameterModel.createParameter("time", "1000"))); RunScenarioModel runScenarioModel = buildRunScenarioModelWith( Arrays.asList( new UsePluginModel("Timer_0", "ConstantTimer", @@ -70,7 +68,7 @@ public class Test_Scenario extends TestBase { new UsePluginModel("Context_0", "Context", null), new UsePluginModel("MongoDBPlugin_0", "MongoDBPlugin", Arrays.asList(ParameterModel.createParameter( - "hostName", "localhost"), + "hostName", "133.133.13.154"), ParameterModel.createParameter("port", "27017"), ParameterModel .createParameter("dbName", diff --git a/Bench4Q-Master/src/main/java/org/bench4q/master/domain/service/TestPlanService.java b/Bench4Q-Master/src/main/java/org/bench4q/master/domain/service/TestPlanService.java index 52f8641f..15c8fb3b 100644 --- a/Bench4Q-Master/src/main/java/org/bench4q/master/domain/service/TestPlanService.java +++ b/Bench4Q-Master/src/main/java/org/bench4q/master/domain/service/TestPlanService.java @@ -4,6 +4,7 @@ import java.util.LinkedList; import java.util.List; import java.util.UUID; +import org.apache.log4j.Logger; import org.bench4q.master.domain.entity.Monitor; import org.bench4q.master.domain.entity.TestPlan; import org.bench4q.master.domain.entity.TestPlanScript; @@ -11,6 +12,7 @@ import org.bench4q.master.domain.entity.User; import org.bench4q.master.domain.factory.BusinessModelMapFactory; import org.bench4q.master.domain.repository.TestPlanRepository; import org.bench4q.share.enums.master.TestPlanStatus; +import org.bench4q.share.helper.MarshalHelper; import org.bench4q.share.models.master.MonitorModel; import org.bench4q.share.models.master.ScriptHandleModel; import org.bench4q.share.models.master.TestPlanDBModel; @@ -80,6 +82,8 @@ public class TestPlanService { monitorModels.add(BusinessModelMapFactory.toModel(monitor)); } resultModel.setMonitorModels(monitorModels); + Logger.getLogger(this.getClass()).info( + "runningInfo : " + MarshalHelper.tryMarshal(resultModel)); return resultModel; } } diff --git a/Bench4Q-Master/src/main/java/org/bench4q/master/domain/valueobject/datastatistics/ScriptBriefStatistics.java b/Bench4Q-Master/src/main/java/org/bench4q/master/domain/valueobject/datastatistics/ScriptBriefStatistics.java index f5a06cc7..9e562c4f 100644 --- a/Bench4Q-Master/src/main/java/org/bench4q/master/domain/valueobject/datastatistics/ScriptBriefStatistics.java +++ b/Bench4Q-Master/src/main/java/org/bench4q/master/domain/valueobject/datastatistics/ScriptBriefStatistics.java @@ -204,8 +204,8 @@ public class ScriptBriefStatistics extends ScriptStatistics { private ScriptBriefResultModel dealWithInvalidSamplePoint( ScriptBriefResultModel inputModel) { - if (this.totalFailCountThisTime == 0 - && this.totalSuccessCountThisTime == 0) { + if (this.totalSuccessCountFromBegin == 0 + && this.totalFailCountFromBegin == 0) { return this.getLastValidBriefResultModel(); } this.setLastValidBriefResultModel(inputModel); diff --git a/Bench4Q-Master/src/test/java/org/bench4q/master/unitTest/service/Test_TestPlanEngine.java b/Bench4Q-Master/src/test/java/org/bench4q/master/unitTest/service/Test_TestPlanEngine.java index 519b8bf0..354b6149 100644 --- a/Bench4Q-Master/src/test/java/org/bench4q/master/unitTest/service/Test_TestPlanEngine.java +++ b/Bench4Q-Master/src/test/java/org/bench4q/master/unitTest/service/Test_TestPlanEngine.java @@ -1,11 +1,18 @@ package org.bench4q.master.unitTest.service; +import java.io.File; +import java.io.IOException; +import java.util.Collections; import java.util.Date; + +import org.apache.commons.io.FileUtils; import org.bench4q.master.Main; import org.bench4q.master.domain.entity.Agent; +import org.bench4q.master.domain.entity.Script; import org.bench4q.master.domain.entity.TestPlan; import org.bench4q.master.domain.entity.TestPlanScript; import org.bench4q.master.domain.entity.User; +import org.bench4q.master.domain.repository.ScriptRepositoty; import org.bench4q.master.domain.service.AgentService; import org.bench4q.master.unitTest.TestBase_MakeUpTestPlan; import org.bench4q.share.enums.master.TestPlanStatus; @@ -19,6 +26,7 @@ import org.junit.runners.MethodSorters; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.web.multipart.MultipartFile; import static org.junit.Assert.*; @@ -28,10 +36,17 @@ import static org.junit.Assert.*; public class Test_TestPlanEngine extends TestBase_MakeUpTestPlan { @Autowired private AgentService agentService; + @Autowired + private ScriptRepositoty scriptRepository; @Before - public void prepare() { + public void prepare() throws IOException { synchronized (Test_TestPlanEngine.class) { + this.scriptRepository.attach(Script.createScriptWithoutId("test1", + FileUtils.readFileToString(new File( + "Scripts/forGoodRecord.xml")), this + .getUserRepository().getUser("admin"), Collections + . emptyList())); this.getAgentRepository().detach("147.0.0.1"); this.agentService.addAgentToPool(Agent.createAgentWithoutId( "147.0.0.1", 6565, 500)); @@ -48,6 +63,8 @@ public class Test_TestPlanEngine extends TestBase_MakeUpTestPlan { if ((agent = this.getAgentRepository().getAgentBy("147.0.01")) != null) { this.getAgentRepository().detach(agent.getId()); } + this.scriptRepository.detach(this.scriptRepository.getScriptByName( + "test1").getId()); } }