public static void BenchmarkApplication <TAppLauncher>( BrowserTimeOptions browserTimeOptions, string resultDirectory ) where TAppLauncher : IApplicationLauncher, new() { resultDirectory = Path.GetFullPath(resultDirectory); var(builder, configuration) = DotvvmTestHost.InitializeBuilder <TAppLauncher>(DotvvmSamplesBenchmarker <TAppLauncher> .GetRootPath()); var host = builder .UseUrls("http://*:5004") .UseKestrel() .Build(); var hostCancel = new CancellationTokenSource(); var hostTask = host.RunAsync(hostCancel.Token); Debug.Assert(configuration.Value != null); var hostName = GetLocalHostname(); Console.WriteLine($"// hostname = {hostName}"); var urls = DotvvmSamplesBenchmarker <TAppLauncher> .GetTestRoutes(configuration.Value); var allResults = new List <(string, BrowserTimeResults)>(); foreach (var url in urls) { var absUrl = $"http://{hostName}:5004/{url.TrimStart('/')}"; Console.WriteLine($"// Benchmarking page load time - {url}"); try { // warm up the server Enumerable.Range(0, 40) .Select(_ => new HttpClient().GetStringAsync(absUrl)) .Apply(Task.WhenAll) .Wait(); var results = ParseResults(resultDirectory, RunBenchmark(browserTimeOptions, resultDirectory, absUrl), removeDir: true); allResults.Add((url, results)); Console.WriteLine($"// Done ({string.Join(", ", results.MeasuredValues.Select(k => k.Key + ": " + k.Value))})"); } catch (Exception ex) { Console.WriteLine($"Benchmarking failed: {ex}"); } } }
private static IEnumerable <string> RunBenchmark(BrowserTimeOptions options, string resultsDirectory, string url) { var args = options.GetArgs(url); Debug.Assert(!resultsDirectory.Contains(':')); var dockerCommand = new [] { "docker", "run", "--shm-size=1g", "--rm", "-v", $"{resultsDirectory}:/browsertime", browserTimeDockerImage, }.Concat(args).ToArray(); Console.WriteLine($"// running browsertime - {string.Join(" ", dockerCommand)}"); var command = Command.Run("sudo", dockerCommand); foreach (var line in command.StandardOutput.GetLines()) { yield return(line); } if (!command.Task.Wait(2_000)) { throw new Exception("BrowserTime: Process has not exited, but out stream is closed"); } if (command.Result.ExitCode != 0) { throw new Exception($"BrowserTime: Exit code = {command.Result.ExitCode}"); } }