mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-01-18 17:23:52 -05:00
fixed perf tests and added more scenarios (#683)
* fixed perf tests and added more scenarios
This commit is contained in:
@@ -3,9 +3,12 @@
|
||||
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||
//
|
||||
|
||||
using Microsoft.SqlTools.ServiceLayer.TestDriver.Utility;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace Microsoft.SqlTools.ServiceLayer.Test.Common
|
||||
@@ -17,10 +20,16 @@ namespace Microsoft.SqlTools.ServiceLayer.Test.Common
|
||||
{
|
||||
private static string resultFolder = InitResultFolder();
|
||||
|
||||
private List<double> iterations = new List<double>();
|
||||
|
||||
private static string InitResultFolder()
|
||||
{
|
||||
string resultFodler = Environment.GetEnvironmentVariable("ResultFolder");
|
||||
if (string.IsNullOrEmpty(resultFodler))
|
||||
{
|
||||
resultFodler = TestRunner.Instance.ResultFolder;
|
||||
}
|
||||
if (string.IsNullOrEmpty(resultFodler))
|
||||
{
|
||||
string assemblyLocation = System.Reflection.Assembly.GetEntryAssembly().Location;
|
||||
resultFodler = Path.GetDirectoryName(assemblyLocation);
|
||||
@@ -43,18 +52,39 @@ namespace Microsoft.SqlTools.ServiceLayer.Test.Common
|
||||
public void End()
|
||||
{
|
||||
EndDateTime = DateTime.UtcNow;
|
||||
iterations.Add(TotalMilliSeconds);
|
||||
if (PrintResult)
|
||||
{
|
||||
Console.WriteLine("Result: " + TotalMilliSeconds);
|
||||
}
|
||||
}
|
||||
|
||||
public void EndAndPrint([CallerMemberName] string testName = "")
|
||||
{
|
||||
End();
|
||||
Print(testName);
|
||||
}
|
||||
|
||||
public void Print([CallerMemberName] string testName = "")
|
||||
{
|
||||
if (PrintResult)
|
||||
{
|
||||
var iterationArray = iterations.ToArray();
|
||||
double elapsed = Percentile(iterationArray, 0.5);
|
||||
var currentColor = Console.ForegroundColor;
|
||||
Console.ForegroundColor = ConsoleColor.Green;
|
||||
Console.WriteLine(string.Format(CultureInfo.InvariantCulture, "Test Name: {0} Run time in milliSeconds: {1}", testName, TotalMilliSeconds));
|
||||
Console.ForegroundColor = currentColor;
|
||||
string resultContent = Newtonsoft.Json.JsonConvert.SerializeObject(new TestResult { ElapsedTime = TotalMilliSeconds });
|
||||
string resultContent = Newtonsoft.Json.JsonConvert.SerializeObject(new TestResult
|
||||
{
|
||||
ElapsedTime = TotalMilliSeconds,
|
||||
MetricValue = elapsed,
|
||||
PrimaryMetric = "ElapsedTimeMetric",
|
||||
Iterations = iterationArray,
|
||||
FiftiethPercentile = Percentile(iterationArray, 0.5),
|
||||
NinetiethPercentile = Percentile(iterationArray, 0.9),
|
||||
Average = iterations.Where(x => x > 0).Average()
|
||||
});
|
||||
string fileName = testName + ".json";
|
||||
string resultFilePath = string.IsNullOrEmpty(resultFolder) ? fileName : Path.Combine(resultFolder, fileName);
|
||||
File.WriteAllText(resultFilePath, resultContent);
|
||||
@@ -62,6 +92,22 @@ namespace Microsoft.SqlTools.ServiceLayer.Test.Common
|
||||
}
|
||||
}
|
||||
|
||||
private static double Percentile(double[] sequence, double excelPercentile)
|
||||
{
|
||||
Array.Sort(sequence);
|
||||
int N = sequence.Length;
|
||||
double n = (N - 1) * excelPercentile + 1;
|
||||
// Another method: double n = (N + 1) * excelPercentile;
|
||||
if (n == 1d) return sequence[0];
|
||||
else if (n == N) return sequence[N - 1];
|
||||
else
|
||||
{
|
||||
int k = (int)n;
|
||||
double d = n - k;
|
||||
return sequence[k - 1] + d * (sequence[k] - sequence[k - 1]);
|
||||
}
|
||||
}
|
||||
|
||||
public double TotalMilliSeconds
|
||||
{
|
||||
get
|
||||
|
||||
Reference in New Issue
Block a user