I have this algorithm which I have been working on for some time, but my problem is that it is kinda slow. I will have to optimize it somehow but I find it a little hard. The current speed is O(n^4) with some extra execution in for loops.
What am I trying to accomplish?
I have a lot of testcases stored in a "run section" and some testcases stored in a "suite section". I would like to compare the testcases to be able to see which testcases has failed in the "suite section". But to get all the failen testcases I first have to loop through the build to get to the test runs then loop through the testruns to get to the testcases and then I compare the "run section testcases" with the "suite section testcases" in the 4'th loop.
Is there any way or methods i could use to simplify this job?
The algorithm is listed below.
/// <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;
}
dataenaDictionaryor even better aHashsetto useContains()instead of aforloop to compare titles. Secondly i would remove redundant calls toReasonHandler.GiveReasonFromErrorMessage(), save the result to a variable and use it instead.GetTestRunByBuildUriandGetListOfTestResultsByIDAre these querying remote data?