4
4
using System ;
5
5
using System . Collections . Generic ;
6
6
using System . IO ;
7
+ using System . IO . Compression ;
7
8
using System . Linq ;
8
9
using System . Runtime . ExceptionServices ;
9
10
using System . Text . Encodings . Web ;
@@ -55,8 +56,9 @@ public static async Task<int> Main(string[] args)
55
56
browser . Url = launchUrl ;
56
57
browser . Navigate ( ) ;
57
58
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 ) ;
60
62
61
63
Console . WriteLine ( "Done executing benchmark" ) ;
62
64
return 0 ;
@@ -67,7 +69,7 @@ internal static void SetBenchmarkResult(List<BenchmarkResult> result)
67
69
benchmarkResult . TrySetResult ( result ) ;
68
70
}
69
71
70
- private static void FormatAsBenchmarksOutput ( List < BenchmarkResult > results )
72
+ private static void FormatAsBenchmarksOutput ( List < BenchmarkResult > results , ( long publishSize , long compressedSize ) sizes )
71
73
{
72
74
// Sample of the the format: https://github.com/aspnet/Benchmarks/blob/e55f9e0312a7dd019d1268c1a547d1863f0c7237/src/Benchmarks/Program.cs#L51-L67
73
75
var output = new BenchmarkOutput ( ) ;
@@ -100,17 +102,11 @@ private static void FormatAsBenchmarksOutput(List<BenchmarkResult> results)
100
102
Format = "n2" ,
101
103
} ) ;
102
104
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
-
109
105
output . Measurements . Add ( new BenchmarkMeasurement
110
106
{
111
107
Timestamp = DateTime . UtcNow ,
112
- Name = "Publish size" ,
113
- Value = GetDirectorySize ( testApp ) / 1024 ,
108
+ Name = "blazorwasm/publish- size" ,
109
+ Value = sizes . publishSize / 1024 ,
114
110
} ) ;
115
111
116
112
output . Metadata . Add ( new BenchmarkMetadata
@@ -122,15 +118,11 @@ private static void FormatAsBenchmarksOutput(List<BenchmarkResult> results)
122
118
Format = "n2" ,
123
119
} ) ;
124
120
125
- var gzip = new FileInfo ( Path . Combine (
126
- testAssemblyLocation . Directory . FullName ,
127
- $ "{ testAssembly . GetName ( ) . Name } .gzip") ) ;
128
-
129
121
output . Measurements . Add ( new BenchmarkMeasurement
130
122
{
131
123
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 ,
134
126
} ) ;
135
127
136
128
Console . WriteLine ( "#StartJobStatistics" ) ;
@@ -205,6 +197,17 @@ static string GetListeningUrl(IHost testApp)
205
197
. First ( ) ;
206
198
}
207
199
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
+
208
211
static long GetDirectorySize ( DirectoryInfo directory )
209
212
{
210
213
// This can happen if you run the app without publishing it.
@@ -228,5 +231,41 @@ static long GetDirectorySize(DirectoryInfo directory)
228
231
229
232
return size ;
230
233
}
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
+ }
231
270
}
232
271
}
0 commit comments