dotnet-installer/src/SourceBuild/content/test/Microsoft.DotNet.SourceBuild.SmokeTests/ArtifactsSizeTest.cs

101 lines
4.4 KiB
C#
Raw Normal View History

2023-08-22 22:42:41 +00:00
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
2023-08-17 17:58:40 +00:00
using System;
using System.IO;
using System.Linq;
2023-08-17 18:02:37 +00:00
using System.Collections;
using System.Collections.Generic;
using System.Text.RegularExpressions;
2023-08-17 18:02:37 +00:00
using System.Formats.Tar;
using System.Threading.Tasks;
2023-08-17 17:58:40 +00:00
using Xunit;
using Xunit.Abstractions;
namespace Microsoft.DotNet.SourceBuild.SmokeTests;
[Trait("Category", "SdkContent")]
2023-08-22 22:42:41 +00:00
public class ArtifactsSizeTest : SmokeTests
2023-08-17 17:58:40 +00:00
{
private static readonly string BaselineFilePath = BaselineHelper.GetBaselineFilePath($"ArtifactsSizes/{Config.TargetRid}.txt");
private static readonly Dictionary<string, long> BaselineFileContent = new Dictionary<string, long>();
private static readonly Regex BuildVersionPattern = new(@"\b\d+\.\d+\.\d+[-@](alpha|preview|rc|rtm)\.\d(\.\d+\.\d+)?\b");
2023-08-22 22:42:41 +00:00
private const int SizeThreshold = 25;
2023-08-22 22:42:41 +00:00
public ArtifactsSizeTest(ITestOutputHelper outputHelper) : base(outputHelper)
2023-08-17 17:58:40 +00:00
{
if(File.Exists(BaselineFilePath))
{
string[] baselineFileContent = File.ReadAllLines(BaselineFilePath);
foreach(string entry in baselineFileContent)
{
string[] splitEntry = entry.Split(':');
BaselineFileContent[splitEntry[0].Trim()] = long.Parse(splitEntry[1].Trim());
}
}
2023-08-22 22:42:41 +00:00
else
{
Assert.True(Directory.Exists(BaselineHelper.GetBaselineFilePath("ArtifactsSizes/")));
Assert.False(true, $"Baseline file `{BaselineFilePath}' does not exist. Please create the baseline file then rerun the test.");
}
}
2023-08-17 17:58:40 +00:00
[SkippableFact(new[] { Config.SourceBuiltArtifactsPathEnv, Config.SdkTarballPathEnv, Config.TargetRidEnv }, skipOnNullOrWhiteSpace: true)]
2023-08-22 22:42:41 +00:00
public void CompareArtifactsToBaseline()
{
Assert.NotNull(Config.SourceBuiltArtifactsPath);
Assert.NotNull(Config.SdkTarballPath);
Assert.NotNull(Config.TargetRid);
2023-08-17 17:58:40 +00:00
IEnumerable<TarEntry> artifactsTarEntries = Utilities.GetTarballContent(Config.SourceBuiltArtifactsPath).Where(entry => entry.EntryType == TarEntryType.RegularFile);
IEnumerable<TarEntry> sdkTarEntries = Utilities.GetTarballContent(Config.SdkTarballPath).Where(entry => entry.EntryType == TarEntryType.RegularFile);
2023-08-17 17:58:40 +00:00
(string FilePath, long Bytes)[] tarEntries = sdkTarEntries.Concat(artifactsTarEntries)
.Select(entry =>
{
2023-08-22 22:42:41 +00:00
string result = BaselineHelper.RemoveVersions(entry.Name);
result = BaselineHelper.RemoveRids(result);
result = BaselineHelper.RemoveNetTfmPaths(result);
return (FilePath: result, Bytes: entry.Length);
})
.OrderBy(entry => entry.FilePath)
.ToArray();
2023-08-17 17:58:40 +00:00
2023-08-22 22:42:41 +00:00
foreach (var entry in tarEntries)
{
2023-08-22 22:42:41 +00:00
if (!BaselineFileContent.ContainsKey(entry.FilePath))
2023-08-17 17:58:40 +00:00
{
2023-08-22 22:42:41 +00:00
OutputHelper.LogWarningMessage($"{entry.FilePath} does not exist in baseline. Adding it to the baseline file");
}
else
{
2023-08-22 22:42:41 +00:00
CompareFileSizes(entry.FilePath, entry.Bytes, BaselineFileContent[entry.FilePath]);
2023-08-17 17:58:40 +00:00
}
}
try
{
string actualFilePath = Path.Combine(DotNetHelper.LogsDirectory, $"Updated_ArtifactsSizes_{Config.TargetRid}.txt");
File.WriteAllLines(actualFilePath, tarEntries.Select(entry => $"{entry.FilePath}: {entry.Bytes}"));
}
catch (IOException ex)
{
throw new InvalidOperationException($"An error occurred while copying the baselines file: {BaselineFilePath}", ex);
}
}
private void CompareFileSizes(string filePath, long fileSize, long baselineSize)
{
if (fileSize == 0 && baselineSize != 0)
2023-08-22 22:42:41 +00:00
OutputHelper.LogWarningMessage($"'{filePath}' is now 0 bytes. It was {baselineSize} bytes");
else if (fileSize != 0 && baselineSize == 0)
2023-08-22 22:42:41 +00:00
OutputHelper.LogWarningMessage($"'{filePath}' is no longer 0 bytes. It is now {fileSize} bytes");
else if (baselineSize != 0 && Math.Abs(((fileSize - baselineSize) / (double)baselineSize) * 100) >= SizeThreshold)
OutputHelper.LogWarningMessage($"'{filePath}' increased in size by more than {SizeThreshold}%. It was originally {baselineSize} bytes and is now {fileSize} bytes");
}
2023-08-17 17:58:40 +00:00
}