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;
|
2023-08-21 21:34:59 +00:00
|
|
|
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;
|
|
|
|
|
2023-08-17 23:30:26 +00:00
|
|
|
namespace Microsoft.DotNet.SourceBuild.SmokeTests;
|
|
|
|
|
|
|
|
[Trait("Category", "SdkContent")]
|
|
|
|
public class ArtifactsSize : SmokeTests
|
2023-08-17 17:58:40 +00:00
|
|
|
{
|
2023-08-21 21:34:59 +00:00
|
|
|
private readonly string baselineFilePath = BaselineHelper.GetBaselineFilePath($"ArtifactsSizes/{Config.TargetRid}.txt");
|
2023-08-17 23:30:26 +00:00
|
|
|
private readonly string[] baselineFileContent;
|
2023-08-21 21:34:59 +00:00
|
|
|
private readonly Regex buildVersionPattern = new(@"\b\d+\.\d+\.\d+[-@](alpha|preview|rc|rtm)\.\d(\.\d+\.\d+)?\b");
|
|
|
|
|
2023-08-17 23:30:26 +00:00
|
|
|
|
|
|
|
public ArtifactsSize(ITestOutputHelper outputHelper) : base(outputHelper)
|
2023-08-17 17:58:40 +00:00
|
|
|
{
|
2023-08-17 23:30:26 +00:00
|
|
|
baselineFileContent = File.Exists(baselineFilePath) ? File.ReadAllLines(baselineFilePath) : Array.Empty<string>();
|
|
|
|
}
|
2023-08-17 17:58:40 +00:00
|
|
|
|
2023-08-21 21:34:59 +00:00
|
|
|
[SkippableFact(new[] { Config.SourceBuiltArtifactsPathEnv, Config.SdkTarballPathEnv, Config.TargetRidEnv }, skipOnNullOrWhiteSpace: true)]
|
2023-08-17 23:30:26 +00:00
|
|
|
public void ArtifactsSizeTest()
|
|
|
|
{
|
2023-08-21 21:34:59 +00:00
|
|
|
Assert.True(Directory.Exists(BaselineHelper.GetBaselineFilePath("ArtifactsSizes/")));
|
2023-08-17 23:30:26 +00:00
|
|
|
Assert.NotNull(Config.SourceBuiltArtifactsPath);
|
|
|
|
Assert.NotNull(Config.SdkTarballPath);
|
2023-08-21 21:34:59 +00:00
|
|
|
Assert.NotNull(Config.TargetRid);
|
2023-08-17 17:58:40 +00:00
|
|
|
|
2023-08-17 23:30:26 +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
|
|
|
|
2023-08-17 23:30:26 +00:00
|
|
|
string[] tarEntries = sdkTarEntries.Concat(artifactsTarEntries)
|
2023-08-21 21:34:59 +00:00
|
|
|
.Select(entry =>
|
|
|
|
{
|
|
|
|
string modifiedPath = buildVersionPattern.Replace(entry.Name, "VERSION");
|
|
|
|
string result = modifiedPath.Replace(Config.TargetRid, "TARGET_RID");
|
|
|
|
return $"{result}: {entry.Length} bytes";
|
|
|
|
})
|
2023-08-17 23:30:26 +00:00
|
|
|
.OrderBy(entry => entry)
|
|
|
|
.ToArray();
|
2023-08-17 17:58:40 +00:00
|
|
|
|
2023-08-17 23:30:26 +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
|
|
|
|
2023-08-17 23:30:26 +00:00
|
|
|
if (string.IsNullOrEmpty(baselineEntry))
|
2023-08-17 17:58:40 +00:00
|
|
|
{
|
2023-08-21 21:34:59 +00:00
|
|
|
LogWarningMessage($"{tarEntryFilename} does not exist in baseline. Adding it to the baseline file");
|
2023-08-17 23:30:26 +00:00
|
|
|
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
|
|
|
|
2023-08-17 23:30:26 +00:00
|
|
|
string message = CompareFileSizes(tarEntryFilename, long.Parse(tarEntrySize), long.Parse(baselineEntrySize));
|
|
|
|
if (!string.IsNullOrEmpty(message))
|
2023-08-17 17:58:40 +00:00
|
|
|
{
|
2023-08-17 23:30:26 +00:00
|
|
|
LogWarningMessage(message);
|
2023-08-17 17:58:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-17 23:30:26 +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;]";
|
2023-08-21 23:19:03 +00:00
|
|
|
|
2023-08-17 23:30:26 +00:00
|
|
|
OutputHelper.WriteLine($"{Environment.NewLine}{prefix}{message}.{Environment.NewLine}");
|
|
|
|
OutputHelper.WriteLine("##vso[task.complete result=SucceededWithIssues;]");
|
|
|
|
}
|
2023-08-17 17:58:40 +00:00
|
|
|
|
2023-08-17 23:30:26 +00:00
|
|
|
private void CopyBaselineFile()
|
|
|
|
{
|
|
|
|
try
|
2023-08-17 17:58:40 +00:00
|
|
|
{
|
2023-08-21 23:19:03 +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
|
|
|
}
|
2023-08-17 23:30:26 +00:00
|
|
|
catch (IOException ex)
|
2023-08-17 17:58:40 +00:00
|
|
|
{
|
2023-08-17 23:30:26 +00:00
|
|
|
throw new InvalidOperationException($"An error occurred while copying the baselines file: {ex.Message}");
|
2023-08-17 17:58:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|