[英]Algorithm optimization - c#
我已經使用了一段時間了,但是我的問題是它有點慢。 我將不得不以某種方式對其進行優化,但是我發現有些困難。 當前速度為O(n ^ 4),在for循環中有一些額外的執行。
我想完成什么?
我在“運行部分”中存儲了很多測試用例,在“套件部分”中存儲了一些測試用例。 我想比較一下測試用例,以便能夠在“套件部分”中查看哪些測試用例失敗了。 但是,要獲取所有失敗的測試用例,我首先必須遍歷構建以進行測試運行,然后循環遍歷測試運行以獲取測試用例,然后將“運行部分測試用例”與“套件部分測試用例”進行比較。第4個循環。
有什么方法或方法可以用來簡化這項工作?
該算法在下面列出。
/// <summary>
/// Check if the sortet masterList matches any other testcases. If it does then return them.
/// </summary>
/// <algorithm>
/// The following soring algorithm is running O(n^4) which we have to optimize somehow.
/// </algorithm>
/// <param name="builds"></param>
/// <returns></returns>
/// <summary>
public IEnumerable<Entities.TestResult> RetrieveTestcasesFromSuite(string project, string buildNumber, int suiteId)
{
SuiteSorting aps = new SuiteSorting();
IBuilds build = TSBuilds.GetBuildByBuildNumber(project, buildNumber);
List<Entities.TestResult> failedTestcases = new List<Entities.TestResult>();
//Gets us a list of the testcase names from the suite number
List<string> dataen = new List<string>();
var testcaseSortingID = aps.GetTestcasesFromSuite(suiteId);
foreach (var element in testcaseSortingID)
{
dataen.Add(GetTitleFromTestcaseID(element));
}
//For the build we select, we want to see...
for (int i = 0; i < build.Count; i++)
{
ITestRuns testRuns = TS.Runs.TSRuns.GetTestRunByBuildUri(project, build.Value[i].Uri);
// Show only test runs that have completed
IEnumerable<TestRun> sortTestRuns = testRuns.Value.Where(p => p.State == TestState.Completed.ToString());
//Foreach testrun in the build we want to see..
foreach (ITestRun testRun in sortTestRuns)
{
ITestResults testResults = TS.Results.TSResults.GetListOfTestResultsByID(project, testRun.Id);
// Show only test results that have not passed
IEnumerable<TestResult> sortedTestResults = testResults.Value.Where(p => p.Outcome != TestOutcome.Passed.ToString());
//Foreach test result in each testrun we would like to...
foreach (ITestResult testResult in sortedTestResults)
{
//Foreach testcase found within suites, compare it with all testcases looped from above..
foreach (var element in dataen)
{
//Foreach testcase in suite, compare with testcases from run.
if (element.Equals(testResult.TestCaseTitle))
{
failedTestcases.Add(new Entities.TestResult()
{
RunId = testResult.TestRun.Id, // The test Run ID
RunTitle = testResult.TestRun.Name, // The test run Title
TestResultId = testResult.Id,
Area = testResult.Project.Name,
ComputerName = testResult.ComputerName,
FailureType = testResult.FailureType,
ErrorMessage = testResult.ErrorMessage,
TestCaseId = testResult.TestCase.Id,
TestCaseTitle = testResult.TestCaseTitle,
TestRunId = testResult.TestRun.Id,
Reason = ReasonHandler.GiveReasonFromErrorMessage(testResult.ErrorMessage), //Reason
Match = ReasonHandler.CompareReasonWithErrorMessageOne(testResult.ErrorMessage),
ReasonCategorie = GiveCategorieFromReason(testResult.ErrorMessage, ReasonHandler.GiveReasonFromErrorMessage(testResult.ErrorMessage)), //Retrurns Categorie of reason // numberInRow = dataToReturn.Count, do we use it?
JiraIssueUrl = JiraCommunication.CreatejiraUrlFromReason(ReasonHandler.GiveReasonFromErrorMessage(testResult.ErrorMessage)), //Creates the JiraIssueUrl
JiraKey = JiraCommunication.GetJiraKeyFromReason(ReasonHandler.GiveReasonFromErrorMessage(testResult.ErrorMessage)),
TestcaseTfsUrl = TfsHandler.GetTestcaseUrl(testResult.TestRun.Id.ToString(), testResult.Id.ToString()),
ResolutionStateId = testResult.ResolutionStateId
});
}
}
}
}
}
return failedTestcases;
}
我認為您可以對最里面的兩個循環執行以下操作並擺脫它們,因為您沒有提供完整的代碼,所以我沒有進行任何測試:
var failedTestCases = sortedTestResults.Where(x => dataen.Contains(x.TestCaseTitle)).Select(testResult => new Entities.TestResult
{
RunId = testResult.TestRun.Id, // The test Run ID
RunTitle = testResult.TestRun.Name,
TestResultId = testResult.Id,
Area = testResult.Project.Name,
// all other columns here ...
}).ToList();
正如我們在私人@KristoferMarEinarsson(我們是同事)中討論的那樣,您的性能下降來自外部調用,而不是算法/ CPU的性能問題。
GetBuildByBuildNumber
GetTestRunByBuildUri
GetListOfTestResultsByID
所有同步執行剩余查詢,因此大大降低了算法速度。
由於循環中的每次迭代都不依賴於先前的迭代,因此可以使循環中的所有邏輯異步運行。 從理論上講,這將使執行時間縮短到GetBuildByBuildNumber
+ GetTestRunByBuildUri
+ GetListOfTestResultsByID
的最壞情況。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.