From ea976a2ee43edd456492c77b1e446a5ff46a8c5b Mon Sep 17 00:00:00 2001 From: coderfengyun Date: Fri, 28 Feb 2014 14:58:54 +0800 Subject: [PATCH] refactor, and add --- RecordScriptTestCase/baidu.html | 106 +++--------------- .../master/domain/entity/TestPlan.java | 21 ++-- .../domain/factory/TestPlanFactory.java | 11 +- .../master/domain/service/TestPlanEngine.java | 44 +++++--- .../domain/service/TestPlanService.java | 21 +++- .../domain/testplan/LoadDistribute.java | 25 +++-- .../master/exception/ExceptionUtils.java | 15 +++ .../master/test/domain/Test_TestPlan.java | 4 +- .../test/service/Test_TestPlanEngine.java | 14 +-- .../test/testplan/Test_LoadDistribute.java | 8 +- .../transaction/Test_ScriptLoadCommand.java | 1 + 11 files changed, 123 insertions(+), 147 deletions(-) diff --git a/RecordScriptTestCase/baidu.html b/RecordScriptTestCase/baidu.html index c63c48f8..373ced8a 100644 --- a/RecordScriptTestCase/baidu.html +++ b/RecordScriptTestCase/baidu.html @@ -1,93 +1,17 @@ -Baidu | Business Overview
Corporate Information
Business Overview
Products
Management
Board of Directors
Corporate Governance
Investor Relations
Fact Sheet
Press Releases
Financial Reports
Webcasts
Stock Information
SEC Filings
Analyst Coverage
IR Calendar
Investor FAQs
Contact Baidu
IR Contacts
Request Information
Email Alert
back to baidu

The Baidu Story



Our name was inspired by a poem written more than 800 years ago during the Song Dynasty. The poem compares the search for a retreating beauty amid chaotic glamour with the search for one's dream while confronted by life's many obstacles. "…hundreds and thousands of times, for her I searched in chaos, suddenly, I turned by chance, to where the lights were waning, and there she stood." Baidu, whose literal meaning is “hundreds of times”, represents a persistent search for the ideal.

-Baidu was founded in 2000 by Internet pioneer Robin Li, creator of visionary search technology Hyperlink Analysis, with the mission of providing the best way for people to find what they’re looking for online. Over the past decade we have strived to fulfill this mission by listening carefully to our users’ needs and wants. To provide intelligent, relevant search results for the tens of billions of queries that are entered into our search platform every day, we focus on powering the best technology optimized for up-to-date local tastes and preferences. Our deep understanding of Chinese language and culture is central to our success and this kind of knowledge allows us to tailor our search technology for our users’ needs. Just to cite one example, we believe there are at least 38 ways of saying "I" in the Chinese language. It is important that we recognize these nuances to effectively address our users’ requests. -

-We provide our users with many channels to find and share information. In addition to our core web search product, we power many popular community-based products, such as Baidu PostBar, the world’s first and largest Chinese-language query-based searchable online community platform, Baidu Knows, the world’s largest Chinese-language interactive knowledge-sharing platform, and Baidu Encyclopedia, the world’s largest user-generated Chinese-language encyclopedia, to name but a few. Beyond these marquee products we also offer dozens of helpful vertical search-based products, such as Maps, Image Search, Video Search, News Search, and many more. We power these through our cutting-edge technology, continually innovating to enhance these services. Our new Box Computing Open Platform brings users deep-linked content and even applications they can use directly through their search box. We believe that Box Computing will dramatically improve people’s search experience and become ubiquitous across all Internet devices including computers and mobile platforms. -

-In addition to serving individual users, we also serve as a media platform for online marketing customers. Our business model is mainly based on a performance-oriented marketing platform for businesses to cost effectively reach relevant Internet users. We offer performance-based online marketing services and display advertisements through both Baidu organic websites and our affiliated websites (our Union business). Our affiliated websites lead traffic to us through integrating a Baidu search box into their sites and/or by displaying relevant contextual promotional links for our customers. The majority of our revenue is derived from performance-based online marketing services and our customers pay on a cost per click basis – that is, our customers only pay when their paid-link is clicked through and they get the “lead”. Our goal is to give our customers an online marketing platform that has a wide range of functions which they can use to meet their marketing needs and an extensive selection of tools for managing their accounts as well as data for analyzing and optimizing ROI. -

-To best serve our customers, our sales efforts consist of direct sales teams in first tier cities and third-party distributors in lower tier cities. This allows us to better penetrate each market and tailor our support and personal interaction based on customers’ needs. Today, our online marketing platform serves hundreds of thousands of small- and medium-sized enterprises (SMEs) and many branded multinational customers. The measurable ROI offered by our online marketing platform has made it one of the most effective marketing platforms for companies targeting the Chinese market. We will continue to strive to provide an extra level of value-added sales and customer service to address a wide range of customer needs. Our focus is to help the market continue to develop and educate the many companies who don’t understand the benefits of search engine marketing so that we can help them grow their businesses. -

-You don’t need us to tell you that China’s Internet space is booming. With the world’s largest Internet user population – 564 million as of end of 2012 – and a long way to go to reach internet penetration levels of developed countries, China’s internet is growing in both influence and sophistication. And as more and more Chinese come online, Baidu continues to innovate to meet their increasingly diverse tastes. With our goal of best serving the needs of our users and customers with intelligent and relevant solutions, we look forward to a robust future.

-
Copyright © Baidu, Inc. All Rights Reserved.
testPlanScripts; private Set monitors; private LoadDistribute loadDistribute; @@ -119,6 +121,15 @@ public class TestPlan implements IAggregate { this.failTimes = failTimes; } + @Column(name = "requiredLoad", nullable = false) + public int getRequiredLoad() { + return requiredLoad; + } + + public void setRequiredLoad(int requiredLoad) { + this.requiredLoad = requiredLoad; + } + @OneToMany(mappedBy = "testPlan", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER) public Set getTestPlanScripts() { return testPlanScripts; @@ -155,7 +166,7 @@ public class TestPlan implements IAggregate { return null; } - public boolean run() { + public TestPlanStatus run() { return this.getLoadDistribute().generateLoadForTestPlan(this); } @@ -178,12 +189,4 @@ public class TestPlan implements IAggregate { return true; } - @Transient - public int getTotalRequireLoad() { - int result = 0; - for (TestPlanScript testPlanScript : this.getTestPlanScripts()) { - result += testPlanScript.getRequireLoad(); - } - return result; - } } diff --git a/src/main/java/org/bench4q/master/domain/factory/TestPlanFactory.java b/src/main/java/org/bench4q/master/domain/factory/TestPlanFactory.java index 75ab0f6d..cd5bb6e3 100644 --- a/src/main/java/org/bench4q/master/domain/factory/TestPlanFactory.java +++ b/src/main/java/org/bench4q/master/domain/factory/TestPlanFactory.java @@ -26,6 +26,7 @@ import org.bench4q.share.models.master.MonitorModel; import org.bench4q.share.models.master.RunningScriptModel; import org.bench4q.share.models.master.TestPlanModel; import org.bench4q.share.models.master.TestScriptConfig; +import org.bench4q.master.exception.ExceptionUtils.IllegalParameterException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @@ -54,7 +55,7 @@ public class TestPlanFactory { } public TestPlan createATestPlanWithoutIdentity(TestPlanModel testPlanModel, - User user, UUID runId) { + User user, UUID runId) throws IllegalParameterException { TestPlan result = new TestPlan(); result.setCreateDateTime(new Date()); result.setCurrentStatus(TestPlanStatus.NotStart.name()); @@ -65,13 +66,21 @@ public class TestPlanFactory { result.setTestPlanRunId(runId.toString()); result.setUser(user); Set testPlanScripts = new HashSet(); + int requiredLoad = 0; for (RunningScriptModel runningScriptModel : testPlanModel .getRunningScriptModels()) { + if (runningScriptModel.getRequireLoad() <= 0) { + throw new IllegalParameterException( + "runningScriptModel's requireLoad is L.T. zero where scriptId is " + + runningScriptModel.getScriptId()); + } + requiredLoad += runningScriptModel.getRequireLoad(); testPlanScripts.add(createATestPlanScriptWithoutId( runningScriptModel.getRequireLoad(), runningScriptModel.getScriptId(), runningScriptModel.getConfig(), result)); } + result.setRequiredLoad(requiredLoad); result.setTestPlanScripts(testPlanScripts); Set monitors = new HashSet(); diff --git a/src/main/java/org/bench4q/master/domain/service/TestPlanEngine.java b/src/main/java/org/bench4q/master/domain/service/TestPlanEngine.java index 5e7b93f3..2d8fbebe 100644 --- a/src/main/java/org/bench4q/master/domain/service/TestPlanEngine.java +++ b/src/main/java/org/bench4q/master/domain/service/TestPlanEngine.java @@ -73,13 +73,36 @@ public class TestPlanEngine implements TaskCompleteCallback, public void doRunTestPlan(final UUID testPlanId) { TestPlan testPlan = getTestPlanRepository().getTestPlanBy(testPlanId); - if (!testPlan.run()) { - commitError(testPlan); - testPlan = null; - return; + // if (!testPlan.run()) { + // commitError(testPlan); + // testPlan = null; + // return; + // } + // commitInRunning(testPlan); + // getTestPlanRepository().attachRunningTestPlan(testPlan); + TestPlanStatus returnStatus = testPlan.run(); + commitTestPlanStaus(returnStatus, testPlan); + } + + private void commitTestPlanStaus(TestPlanStatus returnStatus, + TestPlan testPlan) { + if (returnStatus == TestPlanStatus.InRunning) { + commitInRunning(testPlan); + } else { + commitError(testPlan, returnStatus); } - commitInRunning(testPlan); + } + + private void commitError(TestPlan testPlan, TestPlanStatus errorStatus) { + testPlan.setCurrentStatus(errorStatus.name()); + getTestPlanRepository().updateEntity(testPlan); + } + + private void commitInRunning(TestPlan testPlan) { + testPlan.setCurrentStatus(TestPlanStatus.InRunning.name()); + testPlan.setLatestRunningTime(new Date()); getTestPlanRepository().attachRunningTestPlan(testPlan); + getTestPlanRepository().updateEntity(testPlan); } public void doTaskComplete(UUID testPlanID) { @@ -99,17 +122,6 @@ public class TestPlanEngine implements TaskCompleteCallback, this.getTestPlanRepository().updateEntity(testPlan); } - private void commitError(TestPlan testPlan) { - testPlan.setCurrentStatus(TestPlanStatus.Error.name()); - getTestPlanRepository().updateEntity(testPlan); - } - - private void commitInRunning(TestPlan testPlan) { - testPlan.setCurrentStatus(TestPlanStatus.InRunning.name()); - testPlan.setLatestRunningTime(new Date()); - getTestPlanRepository().updateEntity(testPlan); - } - public void executePendingTestPlan() { } diff --git a/src/main/java/org/bench4q/master/domain/service/TestPlanService.java b/src/main/java/org/bench4q/master/domain/service/TestPlanService.java index a08b01e2..c8596a8e 100644 --- a/src/main/java/org/bench4q/master/domain/service/TestPlanService.java +++ b/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,8 @@ import org.bench4q.master.domain.entity.User; import org.bench4q.master.domain.factory.BusinessModelMapFactory; import org.bench4q.master.domain.factory.TestPlanFactory; import org.bench4q.master.domain.repository.TestPlanRepository; +import org.bench4q.master.exception.ExceptionLog; +import org.bench4q.master.exception.ExceptionUtils.IllegalParameterException; import org.bench4q.share.enums.master.TestPlanStatus; import org.bench4q.share.models.master.MonitorModel; import org.bench4q.share.models.master.TestPlanModel; @@ -54,12 +57,18 @@ public class TestPlanService { this.testPlanFactory = testPlanFactory; } - public boolean submitTestPlan( - final TestPlanModel testPlanBusinessModel, final User user, - final UUID testPlanRunId) { - return this.getTestPlanRepository().attach( - this.getTestPlanFactory().createATestPlanWithoutIdentity( - testPlanBusinessModel, user, testPlanRunId)); + public boolean submitTestPlan(final TestPlanModel testPlanBusinessModel, + final User user, final UUID testPlanRunId) { + try { + return this.getTestPlanRepository().attach( + this.getTestPlanFactory().createATestPlanWithoutIdentity( + testPlanBusinessModel, user, testPlanRunId)); + } catch (IllegalParameterException e) { + // TODO: give back the message + Logger.getLogger(TestPlanService.class).error( + ExceptionLog.getStackTrace(e) + e.getMessage()); + return false; + } } public List loadTestPlans(User user) { diff --git a/src/main/java/org/bench4q/master/domain/testplan/LoadDistribute.java b/src/main/java/org/bench4q/master/domain/testplan/LoadDistribute.java index 4ac6fea1..ad130a2c 100644 --- a/src/main/java/org/bench4q/master/domain/testplan/LoadDistribute.java +++ b/src/main/java/org/bench4q/master/domain/testplan/LoadDistribute.java @@ -10,6 +10,7 @@ import org.bench4q.master.domain.interfaces.RunningScriptInterface; import org.bench4q.master.domain.repository.TestPlanRepository; import org.bench4q.master.testplan.highavailable.HighAvailablePool; import org.bench4q.master.transaction.script.ScriptLoadCommand; +import org.bench4q.share.enums.master.TestPlanStatus; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @@ -47,30 +48,32 @@ public class LoadDistribute { return this.getHighAvailableAgentPool().getMaxAvailableLoad() >= totalRequireLoad; } - public boolean generateLoadForTestPlan(TestPlan testPlanInDomain) { + public TestPlanStatus generateLoadForTestPlan(TestPlan testPlanInDomain) { synchronized (this.highAvailableAgentPool.getPool()) { - if (!hasEnoughMaxLoad(testPlanInDomain.getTotalRequireLoad())) { + if (!hasEnoughMaxLoad(testPlanInDomain.getRequiredLoad())) { logger.error("There is no enough max load in the pool!"); - return false; + return TestPlanStatus.PendingNoEnoughMaxLoad; } - if (!hasEnoughCurrentLoad(testPlanInDomain.getTotalRequireLoad())) { + if (!hasEnoughCurrentLoad(testPlanInDomain.getRequiredLoad())) { logger.info("There is no enough current load in the pool!"); - return false; + return TestPlanStatus.PendingNoEnoughCurrentLoad; } - // TODO: add the algorithm of ditribute load for (RunningScriptInterface testPlanScript : testPlanInDomain .getTestPlanScripts()) { if (!distributeLoadForScript(testPlanScript)) { - return false; + return TestPlanStatus.ErrorInDistributeLoadForScript; } - // testPlanScript.doAfterRun(); } - this.getHighAvailableAgentPool().timerTask(); - testPlanInDomain.doAfterRun(); - return true; + doAfterDistributeLoadForTestPlan(testPlanInDomain); + return TestPlanStatus.InRunning; } } + private void doAfterDistributeLoadForTestPlan(TestPlan testPlanInDomain) { + this.getHighAvailableAgentPool().timerTask(); + testPlanInDomain.doAfterRun(); + } + public boolean distributeLoadForScript(RunningScriptInterface testPlanScript) { ScriptLoadCommand scriptLoadCommand = new ScriptLoadCommand( testPlanScript, testPlanScript.getTestPlanID()); diff --git a/src/main/java/org/bench4q/master/exception/ExceptionUtils.java b/src/main/java/org/bench4q/master/exception/ExceptionUtils.java index cc1d795d..34ef0cdd 100644 --- a/src/main/java/org/bench4q/master/exception/ExceptionUtils.java +++ b/src/main/java/org/bench4q/master/exception/ExceptionUtils.java @@ -55,4 +55,19 @@ public class ExceptionUtils { return "Transaction commit fails!"; } } + + public static class IllegalParameterException extends Exception { + private static final long serialVersionUID = -4999157899142651582L; + private String msg; + + public IllegalParameterException(String msg) { + this.msg = msg; + } + + @Override + public String getMessage() { + return this.msg; + } + + } } diff --git a/src/test/java/org/bench4q/master/test/domain/Test_TestPlan.java b/src/test/java/org/bench4q/master/test/domain/Test_TestPlan.java index 010b97f0..42a76314 100644 --- a/src/test/java/org/bench4q/master/test/domain/Test_TestPlan.java +++ b/src/test/java/org/bench4q/master/test/domain/Test_TestPlan.java @@ -47,11 +47,13 @@ public class Test_TestPlan extends TestBase_MakeUpTestPlan { @Test public void testRun() throws InterruptedException { + assertTrue(this.getHaPool().getCurrentAvailableLoad() >= 500); TestPlan testPlanFromRepo = this.getTestPlanRepository().getTestPlanBy( getTestPlanRunIdUuid()); TestPlan testPlanInDomain = this.getTestPlanFactory().convertToDomain( testPlanFromRepo); - testPlanInDomain.run(); + TestPlanStatus returnStatus = testPlanInDomain.run(); + assertEquals(TestPlanStatus.InRunning, returnStatus); assertTrue(this.getTestPlanRepository().updateEntity(testPlanInDomain)); TestPlan testPlanAfterRun = this.getTestPlanRepository().getTestPlanBy( getTestPlanRunIdUuid()); diff --git a/src/test/java/org/bench4q/master/test/service/Test_TestPlanEngine.java b/src/test/java/org/bench4q/master/test/service/Test_TestPlanEngine.java index 3bc48d14..dbe4bf67 100644 --- a/src/test/java/org/bench4q/master/test/service/Test_TestPlanEngine.java +++ b/src/test/java/org/bench4q/master/test/service/Test_TestPlanEngine.java @@ -37,11 +37,6 @@ public class Test_TestPlanEngine extends TestBase_MakeUpTestPlan { testForStatus(status); } - @Test - public void testCommitError() throws Exception { - testForStatus(TestPlanStatus.Error); - } - @Test public void testInRunning() throws Exception { testForStatus(TestPlanStatus.InRunning); @@ -65,9 +60,8 @@ public class Test_TestPlanEngine extends TestBase_MakeUpTestPlan { assertEquals(TestPlanStatus.InRunning, TestPlanStatus.valueOf(testPlan.getCurrentStatus())); assertTrue(testPlan.getLatestRunningTime().after(dateBeforeRun)); - assertEquals( - TestPlanStatus.InRunning, - TestPlanStatus.valueOf(this.getTestPlanRepository() + assertEquals(TestPlanStatus.InRunning, + TestPlanStatus.valueOf(getTestPlanRepository() .getRunningTestPlanBy(getTestPlanRunIdUuid()) .getCurrentStatus())); cleanUpForTestPlanRunning(); @@ -82,8 +76,8 @@ public class Test_TestPlanEngine extends TestBase_MakeUpTestPlan { this.setTestPlanRunIdUuid(this.getTestPlanEngine().runWith( createATestPlanWithOneScript(scriptId), user)); Thread.sleep(10000); - assertEquals( - TestPlanStatus.Error, + assertNotEquals( + TestPlanStatus.InRunning, TestPlanStatus.valueOf(this.getTestPlanRepository() .getTestPlanBy(getTestPlanRunIdUuid()) .getCurrentStatus())); diff --git a/src/test/java/org/bench4q/master/test/testplan/Test_LoadDistribute.java b/src/test/java/org/bench4q/master/test/testplan/Test_LoadDistribute.java index 618ccfe1..f6cc3482 100644 --- a/src/test/java/org/bench4q/master/test/testplan/Test_LoadDistribute.java +++ b/src/test/java/org/bench4q/master/test/testplan/Test_LoadDistribute.java @@ -9,6 +9,7 @@ import org.bench4q.master.domain.entity.TestPlan; import org.bench4q.master.domain.factory.TestPlanFactory; import org.bench4q.master.domain.testplan.LoadDistribute; import org.bench4q.master.test.TestBase_MakeUpTestPlan; +import org.bench4q.share.enums.master.TestPlanStatus; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -67,10 +68,13 @@ public class Test_LoadDistribute extends TestBase_MakeUpTestPlan { public void testGenerateLoadForTestPlan() { TestPlan testPlanInDomain = this.getTestPlanRepository().getTestPlanBy( getTestPlanRunIdUuid()); + testPlanInDomain = this.getTestPlanFactory().convertToDomain( testPlanInDomain); - assertTrue(this.getLoadDistribute().generateLoadForTestPlan( - testPlanInDomain)); + assertEquals(TestPlanStatus.InRunning, this.getLoadDistribute() + .generateLoadForTestPlan(testPlanInDomain)); + assertEquals(EACH_SCRIPT_LOAD_SMALLSCALE, + testPlanInDomain.getRequiredLoad()); testPlanInDomain = this.getTestPlanRepository().getTestPlanBy( getTestPlanRunIdUuid()); Set runningAgentsAfterRefetch = testPlanInDomain diff --git a/src/test/java/org/bench4q/master/test/transaction/Test_ScriptLoadCommand.java b/src/test/java/org/bench4q/master/test/transaction/Test_ScriptLoadCommand.java index c80e1533..91944da3 100644 --- a/src/test/java/org/bench4q/master/test/transaction/Test_ScriptLoadCommand.java +++ b/src/test/java/org/bench4q/master/test/transaction/Test_ScriptLoadCommand.java @@ -51,6 +51,7 @@ public class Test_ScriptLoadCommand extends TestBase_MakeUpTestPlan { @Test public void testExecuteWithTestPlanScript() { submitATestPlanWithOneScript(); + this.getHaPool().timerTask(); TestPlanScript testPlanScript = this.getTestPlanRepository() .getTestPlanBy(getTestPlanRunIdUuid()) .extracSpecifiedScript(this.getScriptId());