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-22 22:26:10 +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-21 21:34:59 +00:00
|
|
|
|
2023-08-17 23:30:26 +00:00
|
|
|
|
|
|
|
public ArtifactsSize(ITestOutputHelper outputHelper) : base(outputHelper)
|
2023-08-17 17:58:40 +00:00
|
|
|
{
|
2023-08-22 22:26:10 +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-17 23:30:26 +00:00
|
|
|
}
|
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-22 22:26:10 +00:00
|
|
|
(string FilePath, long Bytes)[] tarEntries = sdkTarEntries.Concat(artifactsTarEntries)
|
2023-08-21 21:34:59 +00:00
|
|
|
.Select(entry =>
|
|
|
|
{
|
|
|
|
string modifiedPath = buildVersionPattern.Replace(entry.Name, "VERSION");
|
2023-08-22 22:26:10 +00:00
|
|
|
string result = BaselineHelper.RemoveRids(modifiedPath);
|
|
|
|
return (result, entry.Length);
|
2023-08-21 21:34:59 +00:00
|
|
|
})
|
2023-08-22 22:26:10 +00:00
|
|
|
.OrderBy(entry => entry.FilePath)
|
2023-08-17 23:30:26 +00:00
|
|
|
.ToArray();
|
2023-08-17 17:58:40 +00:00
|
|
|
|
2023-08-17 23:30:26 +00:00
|
|
|
foreach (string entry in tarEntries)
|
|
|
|
{
|
2023-08-22 22:26:10 +00:00
|
|
|
if (BaselineFileContent.ContainsKey(entry.Filename))
|
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-22 22:26:10 +00:00
|
|
|
File.AppendAllText(baselineFilePath, $"{entry}" + Environment.NewLine); // save writes to the end
|
2023-08-17 23:30:26 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-08-22 22:26:10 +00:00
|
|
|
CompareFileSizes(tarEntryFilename, long.Parse(tarEntrySize), long.Parse(baselineEntrySize));
|
2023-08-17 17:58:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-17 23:30:26 +00:00
|
|
|
CopyBaselineFile();
|
|
|
|
}
|
|
|
|
|
2023-08-22 22:26:10 +00:00
|
|
|
private void CompareFileSizes(string filePath, long fileSize, long baselineSize)
|
2023-08-17 23:30:26 +00:00
|
|
|
{
|
|
|
|
if (fileSize == 0 && baselineSize != 0)
|
2023-08-22 22:26:10 +00:00
|
|
|
LogWarningMessage($"'{filePath}' is now 0 bytes. It was {baselineSize} bytes");
|
2023-08-17 23:30:26 +00:00
|
|
|
else if (fileSize != 0 && baselineSize == 0)
|
2023-08-22 22:26:10 +00:00
|
|
|
LogWarningMessage($"'{filePath}' is no longer 0 bytes. It is now {fileSize} bytes");
|
2023-08-17 23:30:26 +00:00
|
|
|
else if (baselineSize != 0 && Math.Abs(((fileSize - baselineSize) / (double)baselineSize) * 100) >= 25)
|
2023-08-22 22:26:10 +00:00
|
|
|
LogWarningMessage($"'{filePath}' increased in size by more than 25%. It was originally {baselineSize} bytes and is now {fileSize} bytes");
|
|
|
|
return;
|
2023-08-17 23:30:26 +00:00
|
|
|
}
|
|
|
|
|
2023-08-22 22:26:10 +00:00
|
|
|
// make an exdtension method in ITestOutputHelper
|
2023-08-17 23:30:26 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|