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

106 lines
4.5 KiB
C#
Raw Normal View History

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")]
public class ArtifactsSize : SmokeTests
2023-08-17 17:58:40 +00:00
{
private readonly string baselineFilePath = BaselineHelper.GetBaselineFilePath($"ArtifactsSizes/{Config.TargetRid}.txt");
private readonly string[] baselineFileContent;
private readonly Regex buildVersionPattern = new(@"\b\d+\.\d+\.\d+[-@](alpha|preview|rc|rtm)\.\d(\.\d+\.\d+)?\b");
public ArtifactsSize(ITestOutputHelper outputHelper) : base(outputHelper)
2023-08-17 17:58:40 +00:00
{
baselineFileContent = File.Exists(baselineFilePath) ? File.ReadAllLines(baselineFilePath) : Array.Empty<string>();
}
2023-08-17 17:58:40 +00:00
[SkippableFact(new[] { Config.SourceBuiltArtifactsPathEnv, Config.SdkTarballPathEnv, Config.TargetRidEnv }, skipOnNullOrWhiteSpace: true)]
public void ArtifactsSizeTest()
{
Assert.True(Directory.Exists(BaselineHelper.GetBaselineFilePath("ArtifactsSizes/")));
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[] tarEntries = sdkTarEntries.Concat(artifactsTarEntries)
.Select(entry =>
{
string modifiedPath = buildVersionPattern.Replace(entry.Name, "VERSION");
string result = modifiedPath.Replace(Config.TargetRid, "TARGET_RID");
return $"{result}: {entry.Length} bytes";
})
.OrderBy(entry => entry)
.ToArray();
2023-08-17 17:58:40 +00:00
foreach (string entry in tarEntries)
{
string tarEntryFilename = entry.Substring(0, entry.IndexOf(":")).Trim();
string baselineEntry = baselineFileContent?.FirstOrDefault(baselineEntry => baselineEntry.StartsWith(tarEntryFilename)) ?? "";
2023-08-17 17:58:40 +00:00
if (string.IsNullOrEmpty(baselineEntry))
2023-08-17 17:58:40 +00:00
{
LogWarningMessage($"{tarEntryFilename} does not exist in baseline. Adding it to the baseline file");
File.AppendAllText(baselineFilePath, $"{entry}" + Environment.NewLine);
}
else
{
string tarEntrySize = entry.Substring(entry.IndexOf(":") + 1).Replace(" bytes", "").Trim();
string baselineEntrySize = baselineEntry.Substring(tarEntryFilename.Length + 1).Replace(" bytes", "").Trim() ?? "0";
2023-08-17 17:58:40 +00:00
string message = CompareFileSizes(tarEntryFilename, long.Parse(tarEntrySize), long.Parse(baselineEntrySize));
if (!string.IsNullOrEmpty(message))
2023-08-17 17:58:40 +00:00
{
LogWarningMessage(message);
2023-08-17 17:58:40 +00:00
}
}
}
CopyBaselineFile();
}
private string CompareFileSizes(string filePath, long fileSize, long baselineSize)
{
if (fileSize == 0 && baselineSize != 0)
return $"{filePath} is now 0 bytes. It was {baselineSize} bytes";
else if (fileSize != 0 && baselineSize == 0)
return $"{filePath} is no longer 0 bytes. It is now {fileSize} bytes";
else if (baselineSize != 0 && Math.Abs(((fileSize - baselineSize) / (double)baselineSize) * 100) >= 25)
return $"{filePath} increased in size by more than 25%. It was originally {baselineSize} bytes and is now {fileSize} bytes";
return "";
}
private void LogWarningMessage(string message)
{
string prefix = "##vso[task.logissue type=warning;]";
OutputHelper.WriteLine($"{Environment.NewLine}{prefix}{message}.{Environment.NewLine}");
OutputHelper.WriteLine("##vso[task.complete result=SucceededWithIssues;]");
}
2023-08-17 17:58:40 +00:00
private void CopyBaselineFile()
{
try
2023-08-17 17:58:40 +00:00
{
string actualFilePath = Path.Combine(DotNetHelper.LogsDirectory, $"Updated_ArtifactsSizes_{Config.TargetRid}.txt");
File.Copy(baselineFilePath, actualFilePath, true);
2023-08-17 17:58:40 +00:00
}
catch (IOException ex)
2023-08-17 17:58:40 +00:00
{
throw new InvalidOperationException($"An error occurred while copying the baselines file: {ex.Message}");
2023-08-17 17:58:40 +00:00
}
}
}