Skip to content

Commit 50b8d0c

Browse files
committed
Update AssemblySizeTest.cs
1 parent 0d28d3e commit 50b8d0c

File tree

1 file changed

+64
-50
lines changed

1 file changed

+64
-50
lines changed

src/ApiBuilderTests/AssemblySizeTest.cs

Lines changed: 64 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,14 @@ public void MeasureAssemblySizes()
5757
}
5858
}
5959

60-
static (Dictionary<string, Dictionary<string, long>> assemblySizes, Dictionary<string, long> sourceSizes) MeasureAllVariants(string baseDir)
60+
static (Dictionary<string, Dictionary<string, long>> assemblySizes, Dictionary<string, Dictionary<string, long>> sourceSizes) MeasureAllVariants(string baseDir)
6161
{
6262
var projectDir = Path.Combine(baseDir, "build");
6363
Directory.CreateDirectory(projectDir);
6464

6565
// Build all variants and collect sizes
6666
var allSizes = new Dictionary<string, Dictionary<string, long>>();
67-
var sourceSizes = new Dictionary<string, long>();
67+
var sourceSizes = new Dictionary<string, Dictionary<string, long>>();
6868

6969
Console.WriteLine(" Building without polyfill...");
7070
(allSizes["without"], sourceSizes["without"]) = BuildAllFrameworksAndMeasure(projectDir, "without", polyfillImport: false, polyOptions: "");
@@ -87,60 +87,75 @@ public void MeasureAssemblySizes()
8787
return (allSizes, sourceSizes);
8888
}
8989

90-
static (Dictionary<string, long> assemblySizes, long sourceSize) BuildAllFrameworksAndMeasure(string projectDir, string variant, bool polyfillImport, string polyOptions)
90+
static (Dictionary<string, long> assemblySizes, Dictionary<string, long> sourceSizes) BuildAllFrameworksAndMeasure(string projectDir, string variant, bool polyfillImport, string polyOptions)
9191
{
9292
var variantDir = Path.Combine(projectDir, variant);
9393
Directory.CreateDirectory(variantDir);
9494

95-
// Navigate from assembly location to find Polyfill directory
95+
// Navigate from assembly location to find source directory
9696
var assemblyDir = Path.GetDirectoryName(typeof(AssemblySizeTest).Assembly.Location)!;
97-
// Go up from bin/Debug/net10.0 to src, then into Polyfill
98-
var polyfillDir = Path.GetFullPath(Path.Combine(assemblyDir, "..", "..", "..", "..", "Polyfill"));
99-
var polyfillTargetsPath = Path.Combine(polyfillDir, "Polyfill.targets");
97+
// Go up from bin/Debug/net10.0 to src
98+
var srcDir = Path.GetFullPath(Path.Combine(assemblyDir, "..", "..", "..", ".."));
99+
var testIncludesTargetsPath = Path.Combine(srcDir, "TestIncludes.targets");
100+
var polyfillTargetsPath = Path.Combine(srcDir, "Polyfill", "Polyfill.targets");
101+
var splitDir = Path.Combine(srcDir, "Split");
100102

101-
// Calculate source file size based on what's included
102-
long sourceSize = 0;
103+
// Calculate source file sizes per framework based on Split directory
104+
var sourceSizes = new Dictionary<string, long>();
103105
if (polyfillImport)
104106
{
105-
// Get all source files
106-
var allFiles = Directory.GetFiles(polyfillDir, "*.cs", SearchOption.AllDirectories)
107-
.Where(f => !f.Contains($"{Path.DirectorySeparatorChar}obj{Path.DirectorySeparatorChar}") &&
108-
!f.Contains($"{Path.DirectorySeparatorChar}bin{Path.DirectorySeparatorChar}"))
109-
.ToList();
110-
111-
// Exclude directories based on polyOptions (matching Polyfill.targets logic)
112-
if (!polyOptions.Contains("<PolyEnsure>true</PolyEnsure>"))
113-
allFiles = allFiles.Where(f => !f.Contains($"{Path.DirectorySeparatorChar}Ensure{Path.DirectorySeparatorChar}")).ToList();
114-
if (!polyOptions.Contains("<PolyArgumentExceptions>true</PolyArgumentExceptions>"))
115-
allFiles = allFiles.Where(f => !f.Contains($"{Path.DirectorySeparatorChar}ArgumentExceptions{Path.DirectorySeparatorChar}")).ToList();
116-
if (!polyOptions.Contains("<PolyStringInterpolation>true</PolyStringInterpolation>"))
117-
allFiles = allFiles.Where(f => !f.Contains($"{Path.DirectorySeparatorChar}StringInterpolation{Path.DirectorySeparatorChar}")).ToList();
118-
if (!polyOptions.Contains("<PolyNullability>true</PolyNullability>"))
119-
allFiles = allFiles.Where(f => !f.Contains($"{Path.DirectorySeparatorChar}Nullability{Path.DirectorySeparatorChar}")).ToList();
120-
121-
// Calculate compressed size (EmbedUntrackedSources uses deflate compression)
122-
sourceSize = allFiles.Sum(f =>
107+
foreach (var framework in TargetFrameworks)
123108
{
124-
var content = File.ReadAllBytes(f);
125-
using var output = new MemoryStream();
126-
using (var deflate = new DeflateStream(output, CompressionLevel.Optimal, leaveOpen: true))
109+
// Handle -windows targets by stripping the suffix (matching TestIncludes.targets logic)
110+
var splitFramework = framework.EndsWith("-windows")
111+
? framework[..framework.LastIndexOf("-windows", StringComparison.Ordinal)]
112+
: framework;
113+
114+
var frameworkDir = Path.Combine(splitDir, splitFramework);
115+
if (!Directory.Exists(frameworkDir))
127116
{
128-
deflate.Write(content);
117+
sourceSizes[framework] = 0;
118+
continue;
129119
}
130-
return output.Length;
131-
});
120+
121+
// Get all source files from the framework-specific Split directory
122+
var allFiles = Directory.GetFiles(frameworkDir, "*.cs", SearchOption.AllDirectories).ToList();
123+
124+
// Exclude directories based on polyOptions (matching Polyfill.targets logic)
125+
if (!polyOptions.Contains("<PolyEnsure>true</PolyEnsure>"))
126+
allFiles = allFiles.Where(f => !f.Contains($"{Path.DirectorySeparatorChar}Ensure{Path.DirectorySeparatorChar}")).ToList();
127+
if (!polyOptions.Contains("<PolyArgumentExceptions>true</PolyArgumentExceptions>"))
128+
allFiles = allFiles.Where(f => !f.Contains($"{Path.DirectorySeparatorChar}ArgumentExceptions{Path.DirectorySeparatorChar}")).ToList();
129+
if (!polyOptions.Contains("<PolyStringInterpolation>true</PolyStringInterpolation>"))
130+
allFiles = allFiles.Where(f => !f.Contains($"{Path.DirectorySeparatorChar}StringInterpolation{Path.DirectorySeparatorChar}")).ToList();
131+
if (!polyOptions.Contains("<PolyNullability>true</PolyNullability>"))
132+
allFiles = allFiles.Where(f => !f.Contains($"{Path.DirectorySeparatorChar}Nullability{Path.DirectorySeparatorChar}")).ToList();
133+
134+
// Calculate compressed size (EmbedUntrackedSources uses deflate compression)
135+
sourceSizes[framework] = allFiles.Sum(f =>
136+
{
137+
var content = File.ReadAllBytes(f);
138+
using var output = new MemoryStream();
139+
using (var deflate = new DeflateStream(output, CompressionLevel.Optimal, leaveOpen: true))
140+
{
141+
deflate.Write(content);
142+
}
143+
return output.Length;
144+
});
145+
}
146+
}
147+
else
148+
{
149+
foreach (var framework in TargetFrameworks)
150+
{
151+
sourceSizes[framework] = 0;
152+
}
132153
}
133154

134-
// Include Polyfill source files before the targets (which use Remove to exclude based on options)
135-
var polyfillSourceIncludes = polyfillImport
136-
? $"""
137-
<ItemGroup>
138-
<Compile Include="{polyfillDir}\**\*.cs" Exclude="{polyfillDir}\obj\**;{polyfillDir}\bin\**" />
139-
</ItemGroup>
140-
"""
141-
: "";
155+
// Import TestIncludes.targets (which includes Split files based on TargetFramework) and Polyfill.targets (for options handling)
142156
var polyfillImportLines = polyfillImport
143157
? $"""
158+
<Import Project="{testIncludesTargetsPath}" />
144159
<Import Project="{polyfillTargetsPath}" />
145160
"""
146161
: "";
@@ -173,7 +188,6 @@ public void MeasureAssemblySizes()
173188
{polyOptions}
174189
</PropertyGroup>
175190
{packageReferences}
176-
{polyfillSourceIncludes}
177191
{polyfillImportLines}
178192
<ItemGroup>
179193
<Compile Include="Class1.cs" />
@@ -238,7 +252,7 @@ public void Method1() { }
238252
}
239253
}
240254

241-
return (sizes, sourceSize);
255+
return (sizes, sourceSizes);
242256
}
243257

244258
static List<SizeResult> ConvertToSizeResults(Dictionary<string, Dictionary<string, long>> allSizes)
@@ -262,7 +276,7 @@ static List<SizeResult> ConvertToSizeResults(Dictionary<string, Dictionary<strin
262276
return results;
263277
}
264278

265-
static List<SizeResult> ConvertToSizeResultsWithEmbed(Dictionary<string, Dictionary<string, long>> allSizes, Dictionary<string, long> sourceSizes)
279+
static List<SizeResult> ConvertToSizeResultsWithEmbed(Dictionary<string, Dictionary<string, long>> allSizes, Dictionary<string, Dictionary<string, long>> sourceSizes)
266280
{
267281
var results = new List<SizeResult>();
268282

@@ -271,12 +285,12 @@ static List<SizeResult> ConvertToSizeResultsWithEmbed(Dictionary<string, Diction
271285
results.Add(new SizeResult
272286
{
273287
TargetFramework = framework,
274-
SizeWithoutPolyfill = allSizes["without"].GetValueOrDefault(framework, -1) + sourceSizes["without"],
275-
SizeWithPolyfill = allSizes["with"].GetValueOrDefault(framework, -1) + sourceSizes["with"],
276-
SizeWithEnsure = allSizes["ensure"].GetValueOrDefault(framework, -1) + sourceSizes["ensure"],
277-
SizeWithArgumentExceptions = allSizes["argex"].GetValueOrDefault(framework, -1) + sourceSizes["argex"],
278-
SizeWithStringInterpolation = allSizes["stringinterp"].GetValueOrDefault(framework, -1) + sourceSizes["stringinterp"],
279-
SizeWithNullability = allSizes["nullability"].GetValueOrDefault(framework, -1) + sourceSizes["nullability"]
288+
SizeWithoutPolyfill = allSizes["without"].GetValueOrDefault(framework, -1) + sourceSizes["without"].GetValueOrDefault(framework, 0),
289+
SizeWithPolyfill = allSizes["with"].GetValueOrDefault(framework, -1) + sourceSizes["with"].GetValueOrDefault(framework, 0),
290+
SizeWithEnsure = allSizes["ensure"].GetValueOrDefault(framework, -1) + sourceSizes["ensure"].GetValueOrDefault(framework, 0),
291+
SizeWithArgumentExceptions = allSizes["argex"].GetValueOrDefault(framework, -1) + sourceSizes["argex"].GetValueOrDefault(framework, 0),
292+
SizeWithStringInterpolation = allSizes["stringinterp"].GetValueOrDefault(framework, -1) + sourceSizes["stringinterp"].GetValueOrDefault(framework, 0),
293+
SizeWithNullability = allSizes["nullability"].GetValueOrDefault(framework, -1) + sourceSizes["nullability"].GetValueOrDefault(framework, 0)
280294
});
281295
}
282296

0 commit comments

Comments
 (0)