Skip to content

Commit c7e6bee

Browse files
authored
Include compressed size in output (#18485)
1 parent 3111032 commit c7e6bee

File tree

1 file changed

+56
-17
lines changed
  • src/Components/benchmarkapps/Wasm.Performance/Driver

1 file changed

+56
-17
lines changed

src/Components/benchmarkapps/Wasm.Performance/Driver/Program.cs

Lines changed: 56 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System;
55
using System.Collections.Generic;
66
using System.IO;
7+
using System.IO.Compression;
78
using System.Linq;
89
using System.Runtime.ExceptionServices;
910
using System.Text.Encodings.Web;
@@ -55,8 +56,9 @@ public static async Task<int> Main(string[] args)
5556
browser.Url = launchUrl;
5657
browser.Navigate();
5758

58-
var results = await benchmarkResult.Task;
59-
FormatAsBenchmarksOutput(results);
59+
var appSize = GetBlazorAppSize();
60+
await Task.WhenAll(benchmarkResult.Task, appSize);
61+
FormatAsBenchmarksOutput(benchmarkResult.Task.Result, appSize.Result);
6062

6163
Console.WriteLine("Done executing benchmark");
6264
return 0;
@@ -67,7 +69,7 @@ internal static void SetBenchmarkResult(List<BenchmarkResult> result)
6769
benchmarkResult.TrySetResult(result);
6870
}
6971

70-
private static void FormatAsBenchmarksOutput(List<BenchmarkResult> results)
72+
private static void FormatAsBenchmarksOutput(List<BenchmarkResult> results, (long publishSize, long compressedSize) sizes)
7173
{
7274
// Sample of the the format: https://github.com/aspnet/Benchmarks/blob/e55f9e0312a7dd019d1268c1a547d1863f0c7237/src/Benchmarks/Program.cs#L51-L67
7375
var output = new BenchmarkOutput();
@@ -100,17 +102,11 @@ private static void FormatAsBenchmarksOutput(List<BenchmarkResult> results)
100102
Format = "n2",
101103
});
102104

103-
var testAssembly = typeof(TestApp.Program).Assembly;
104-
var testAssemblyLocation = new FileInfo(testAssembly.Location);
105-
var testApp = new DirectoryInfo(Path.Combine(
106-
testAssemblyLocation.Directory.FullName,
107-
testAssembly.GetName().Name));
108-
109105
output.Measurements.Add(new BenchmarkMeasurement
110106
{
111107
Timestamp = DateTime.UtcNow,
112-
Name = "Publish size",
113-
Value = GetDirectorySize(testApp) / 1024,
108+
Name = "blazorwasm/publish-size",
109+
Value = sizes.publishSize / 1024,
114110
});
115111

116112
output.Metadata.Add(new BenchmarkMetadata
@@ -122,15 +118,11 @@ private static void FormatAsBenchmarksOutput(List<BenchmarkResult> results)
122118
Format = "n2",
123119
});
124120

125-
var gzip = new FileInfo(Path.Combine(
126-
testAssemblyLocation.Directory.FullName,
127-
$"{testAssembly.GetName().Name}.gzip"));
128-
129121
output.Measurements.Add(new BenchmarkMeasurement
130122
{
131123
Timestamp = DateTime.UtcNow,
132-
Name = "Publish size (compressed)",
133-
Value = (gzip.Exists ? gzip.Length : 0) / 1024,
124+
Name = "blazorwasm/compressed-publish-size",
125+
Value = sizes.compressedSize / 1024,
134126
});
135127

136128
Console.WriteLine("#StartJobStatistics");
@@ -205,6 +197,17 @@ static string GetListeningUrl(IHost testApp)
205197
.First();
206198
}
207199

200+
static async Task<(long size, long compressedSize)> GetBlazorAppSize()
201+
{
202+
var testAssembly = typeof(TestApp.Startup).Assembly;
203+
var testAssemblyLocation = new FileInfo(testAssembly.Location);
204+
var testApp = new DirectoryInfo(Path.Combine(
205+
testAssemblyLocation.Directory.FullName,
206+
testAssembly.GetName().Name));
207+
208+
return (GetDirectorySize(testApp), await GetBrotliCompressedSize(testApp));
209+
}
210+
208211
static long GetDirectorySize(DirectoryInfo directory)
209212
{
210213
// This can happen if you run the app without publishing it.
@@ -228,5 +231,41 @@ static long GetDirectorySize(DirectoryInfo directory)
228231

229232
return size;
230233
}
234+
235+
static async Task<long> GetBrotliCompressedSize(DirectoryInfo directory)
236+
{
237+
if (!directory.Exists)
238+
{
239+
return 0;
240+
}
241+
242+
var tasks = new List<Task<long>>();
243+
foreach (var item in directory.EnumerateFileSystemInfos())
244+
{
245+
if (item is FileInfo fileInfo)
246+
{
247+
tasks.Add(GetCompressedFileSize(fileInfo));
248+
}
249+
else if (item is DirectoryInfo directoryInfo)
250+
{
251+
tasks.Add(GetBrotliCompressedSize(directoryInfo));
252+
}
253+
}
254+
255+
return (await Task.WhenAll(tasks)).Sum(s => s);
256+
257+
async Task<long> GetCompressedFileSize(FileInfo fileInfo)
258+
{
259+
using var inputStream = new FileStream(fileInfo.FullName, FileMode.Open, FileAccess.Read, FileShare.Read, 1, useAsync: true);
260+
using var outputStream = new MemoryStream();
261+
262+
using (var brotliStream = new BrotliStream(outputStream, CompressionLevel.Optimal, leaveOpen: true))
263+
{
264+
await inputStream.CopyToAsync(brotliStream);
265+
}
266+
267+
return outputStream.Length;
268+
}
269+
}
231270
}
232271
}

0 commit comments

Comments
 (0)