-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a867792
commit 4f2f6c0
Showing
8 changed files
with
137 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
using System; | ||
using System.Collections.Concurrent; | ||
using System.Collections.Generic; | ||
using System.Globalization; | ||
using System.Linq; | ||
using GoogleTestAdapter.Helpers; | ||
using GoogleTestAdapter.Model; | ||
|
||
namespace GoogleTestAdapter.Scheduling | ||
{ | ||
public class SchedulingAnalyzer | ||
{ | ||
private class Difference | ||
{ | ||
public TestCase TestCase; | ||
public int DifferenceInMs; | ||
} | ||
|
||
|
||
private readonly TestEnvironment _testEnvironment; | ||
|
||
public SchedulingAnalyzer(TestEnvironment testEnvironment) | ||
{ | ||
_testEnvironment = testEnvironment; | ||
} | ||
|
||
private ConcurrentDictionary<TestCase, int> ExpectedTestcaseDurations { get; } = new ConcurrentDictionary<TestCase, int>(); | ||
private ConcurrentDictionary<TestCase, int> ActualTestcaseDurations { get; } = new ConcurrentDictionary<TestCase, int>(); | ||
|
||
public bool AddExpectedDuration(TestCase testCase, int duration) | ||
{ | ||
return ExpectedTestcaseDurations.TryAdd(testCase, duration); | ||
} | ||
|
||
public bool AddActualDuration(TestCase testCase, int duration) | ||
{ | ||
return ActualTestcaseDurations.TryAdd(testCase, duration); | ||
} | ||
|
||
public void PrintStatisticsToDebugOutput() | ||
{ | ||
_testEnvironment.DebugInfo(">>> Scheduling statistics <<<"); | ||
_testEnvironment.DebugInfo($"# of expected test case durations: {ExpectedTestcaseDurations.Count}"); | ||
_testEnvironment.DebugInfo($"# of actual test case durations: {ActualTestcaseDurations.Count}"); | ||
if (ExpectedTestcaseDurations.Count == 0 || ActualTestcaseDurations.Count == 0) | ||
{ | ||
_testEnvironment.DebugInfo("Nothing to report."); | ||
return; | ||
} | ||
|
||
var differences = new List<Difference>(); | ||
differences.AddRange(ExpectedTestcaseDurations | ||
.Where(ed => ActualTestcaseDurations.ContainsKey(ed.Key)) | ||
.Select(ed => new Difference | ||
{ | ||
TestCase = ed.Key, | ||
DifferenceInMs = ed.Value - ActualTestcaseDurations[ed.Key] | ||
})); | ||
differences.Sort((d1, d2) => Math.Abs(d2.DifferenceInMs) - Math.Abs(d1.DifferenceInMs)); | ||
|
||
int sumOfAllDifferences = differences.Select(d => d.DifferenceInMs).Sum(); | ||
double avgDifference = (double) sumOfAllDifferences / differences.Count; | ||
double sumOfSquaresOfDifferences = differences.Select(d => (d.DifferenceInMs - avgDifference) * (d.DifferenceInMs - avgDifference)).Sum(); | ||
double standardDeviation = Math.Sqrt(sumOfSquaresOfDifferences / differences.Count); | ||
|
||
_testEnvironment.DebugInfo($"{differences.Count} expected durations have been found in actual durations"); | ||
_testEnvironment.DebugInfo($"Avg difference between expected and actual duration: {avgDifference.ToString("F1", CultureInfo.InvariantCulture)}ms"); | ||
_testEnvironment.DebugInfo($"Standard deviation: {standardDeviation.ToString("F1", CultureInfo.InvariantCulture)}ms"); | ||
|
||
int nrOfWorstDifferences = Math.Min(10, differences.Count); | ||
_testEnvironment.DebugInfo($"{nrOfWorstDifferences} worst differences:"); | ||
for (int i = 0; i < nrOfWorstDifferences; i++) | ||
{ | ||
_testEnvironment.DebugInfo($"Test {differences[i].TestCase.FullyQualifiedName}: Expected {ExpectedTestcaseDurations[differences[i].TestCase]}ms, actual {ActualTestcaseDurations[differences[i].TestCase]}ms"); | ||
} | ||
} | ||
|
||
} | ||
|
||
} |