Skip to content
Draft

Test #45

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,10 @@
<Version>3.5.119</Version>
</PackageReference>
</ItemGroup>

<PropertyGroup>
<LangVersion>preview</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>Benchmarks</RootNamespace>
</PropertyGroup>

Expand All @@ -14,7 +12,7 @@

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.13.5"/>
<PackageReference Include="Microsoft.Build.Tasks.Core" Version="17.3.2"/>
<PackageReference Include="Microsoft.Build.Tasks.Core" Version="17.8.3" />
</ItemGroup>

</Project>
5 changes: 3 additions & 2 deletions MSBuild.CompilerCache.Benchmarks/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,13 @@ public void HashCalculationPerfTest()
var decomposed = TargetsExtractionUtils.DecomposeCompilerProps(inputs.AllProps);
var memCache = new DictionaryBasedCache<CacheKey, RefDataWithOriginalExtract>();
var refCacheFileBased = new RefCache("c:/projekty/.refcache");
var refCache = new CacheCombiner<CacheKey, RefDataWithOriginalExtract>(memCache, refCacheFileBased);
var refCache = CacheCombiner.Combine(memCache, refCacheFileBased);
var refTrimmingConfig = new RefTrimmingConfig();

void Act()
{
var inputs = LocatorAndPopulator.CalculateLocalInputs(decomposed, refCache, "assembly", refTrimmingConfig, new DictionaryBasedCache<FileHashCacheKey, string>(), Utils.DefaultHasher);
var hasher = HasherFactory.CreateHash(HasherType.XxHash64);
var inputs = LocatorAndPopulator.CalculateLocalInputs(decomposed, refCache, "assembly", refTrimmingConfig, new DictionaryBasedCache<FileHashCacheKey, string>(), hasher, null);
if (inputs.Files.Length == 0) throw new Exception();
}

Expand Down
50 changes: 25 additions & 25 deletions MSBuild.CompilerCache.Tests/EndToEndTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,21 +47,21 @@ private static string NugetConfig(string sourcePath) =>
""";

private static readonly string PropsFile =
$"""
<Project>

<PropertyGroup>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<DebugType>Embedded</DebugType>
<Deterministic>true</Deterministic>
</PropertyGroup>

<PropertyGroup>
<CompilationCacheConfigPath Condition="'$(CompilationCacheConfigPath)' == ''">$(MSBuildThisFileDirectory).cache/</CompilationCacheConfigPath>
</PropertyGroup>

</Project>
""";
"""
<Project>
<PropertyGroup>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<DebugType>Embedded</DebugType>
<Deterministic>true</Deterministic>
</PropertyGroup>
<PropertyGroup>
<CompilerCacheConfigPath Condition="'$(CompilerCacheConfigPath)' == ''">$(MSBuildThisFileDirectory).cache/</CompilerCacheConfigPath>
</PropertyGroup>

</Project>
""";

public void Dispose()
{
Expand Down Expand Up @@ -133,9 +133,9 @@ public record ProjectFileBuilder
public bool GenerateDocumentationFile { get; init; } = true;
public bool ProduceReferenceAssembly { get; init; } = true;
public string? AssemblyName { get; init; } = null;
public string? CompilationCacheConfigPath { get; init; } = null;
public string? CompilerCacheConfigPath { get; init; }
public string TargetFramework { get; init; } = "net6.0";
public string Name { get; init; } = null;
public string Name { get; init; }

public ProjectFileBuilder(string name) => Name = name;

Expand Down Expand Up @@ -176,7 +176,7 @@ void AddIfNotNull(string name, string? value)
}

AddIfNotNull("AssemblyName", AssemblyName);
AddIfNotNull("CompilationCacheConfigPath", CompilationCacheConfigPath);
AddIfNotNull("CompilerCacheConfigPath", CompilerCacheConfigPath);

return properties;
}
Expand Down Expand Up @@ -249,9 +249,9 @@ FileInfo DllFile(DirectoryInfo projDir, ProjectFileBuilder proj) =>
var dll2 = DllFile(projDir2, proj);
var dll3 = DllFile(projDir3, proj);

var hash1 = Utils.FileBytesToHashHex(dll1.FullName, Utils.DefaultHasher);
var hash2 = Utils.FileBytesToHashHex(dll2.FullName, Utils.DefaultHasher);
var hash3 = Utils.FileBytesToHashHex(dll3.FullName, Utils.DefaultHasher);
var hash1 = TestUtils.FileBytesToHash(dll1.FullName, TestUtils.DefaultHasher);
var hash2 = TestUtils.FileBytesToHash(dll2.FullName, TestUtils.DefaultHasher);
var hash3 = TestUtils.FileBytesToHash(dll3.FullName, TestUtils.DefaultHasher);

Assert.That(hash2, Is.EqualTo(hash1));
Assert.That(hash3, Is.Not.EqualTo(hash2));
Expand All @@ -269,7 +269,7 @@ public class Class { }
var proj =
new ProjectFileBuilder("C.csproj")
{
CompilationCacheConfigPath = configFile.FullName,
CompilerCacheConfigPath = configFile.FullName,
ProduceReferenceAssembly = produceRefAssembly
}
.WithSource(source);
Expand All @@ -285,7 +285,7 @@ namespace CSharp
var proj =
new ProjectFileBuilder("F.fsproj")
{
CompilationCacheConfigPath = configFile.FullName,
CompilerCacheConfigPath = configFile.FullName,
ProduceReferenceAssembly = produceRefAssembly
}
.WithSource(source);
Expand Down Expand Up @@ -313,7 +313,7 @@ private static string[] BuildProject(DirectoryInfo dir, ProjectFileBuilder proje
{
Environment.SetEnvironmentVariable("MSBuildSDKsPath", null);
Environment.SetEnvironmentVariable("MSBuildExtensionsPath", null);
TestUtils.RunProcess("dotnet", $"add package MSBuild.CompilerCache --prerelease", dir);
return TestUtils.RunProcess("dotnet", $"build -verbosity:normal", dir);
TestUtils.RunProcess("dotnet", "add package MSBuild.CompilerCache --prerelease", dir);
return TestUtils.RunProcess("dotnet", "build -verbosity:normal", dir);
}
}
4 changes: 3 additions & 1 deletion MSBuild.CompilerCache.Tests/Extensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using Newtonsoft.Json;

namespace Tests;

public static class Extensions
{
public static string ToJson(this object x) => Newtonsoft.Json.JsonConvert.SerializeObject(x);
public static string ToJson(this object x) => JsonConvert.SerializeObject(x);
}
48 changes: 22 additions & 26 deletions MSBuild.CompilerCache.Tests/InMemoryTaskBasedTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
using MSBuild.CompilerCache;
using Newtonsoft.Json;
using NUnit.Framework;
using IRefCache =
MSBuild.CompilerCache.ICacheBase<MSBuild.CompilerCache.CacheKey, MSBuild.CompilerCache.RefDataWithOriginalExtract>;
using IRefCache = MSBuild.CompilerCache.ICacheBase<MSBuild.CompilerCache.CacheKey, MSBuild.CompilerCache.RefDataWithOriginalExtract>;
using JsonSerializer = System.Text.Json.JsonSerializer;

namespace Tests;

Expand Down Expand Up @@ -35,7 +35,7 @@ public void SetUp()
dict[(key, life)] = value);

_buildEngine
.Setup(x => x.GetRegisteredTaskObject(It.IsAny<object>(), It.IsAny<RegisteredTaskObjectLifetime>()))
.Setup(x => x.UnregisterTaskObject(It.IsAny<object>(), It.IsAny<RegisteredTaskObjectLifetime>()))
.Returns((object key, RegisteredTaskObjectLifetime life) =>
{
var k = (key, life);
Expand All @@ -60,25 +60,20 @@ public void TearDown()
tmpDir.Dispose();
}

public record All(LocateInputs LocateInputs, LocalInputs LocalInputs, CacheKey CacheKey,
string LocalInputsHash, FullExtract FullExtract);
public record All(LocalInputs LocalInputs, CacheKey CacheKey, FullExtract FullExtract);

public static All AllFromInputs(LocateInputs inputs, IRefCache refCache)
{
var decomposed = TargetsExtractionUtils.DecomposeCompilerProps(inputs.AllProps);
var localInputs = LocatorAndPopulator.CalculateLocalInputs(decomposed, refCache, assemblyName: "",
trimmingConfig: new RefTrimmingConfig(), fileHashCache: new DictionaryBasedCache<FileHashCacheKey, string>(),
hasher: Utils.DefaultHasher);
var extract = localInputs.ToFullExtract();
var hashString = Utils.ObjectToHash(extract, Utils.DefaultHasher);
var localInputs = LocatorAndPopulator.CalculateLocalInputs(decomposed, refCache, compilingAssemblyName: "",
trimmingConfig: new RefTrimmingConfig(), fileHashCache: new DictionaryBasedCache<FileHashCacheKey, string>(), hasher: TestUtils.DefaultHasher, counters: null);
var extract = localInputs.ToSlim().ToFullExtract();
var jsonBytes = JsonSerializerExt.SerializeToUtf8Bytes(extract, null, FullExtractJsonContext.Default.FullExtract);
var hashString = Utils.BytesToHash(jsonBytes, TestUtils.DefaultHasher);
var cacheKey = LocatorAndPopulator.GenerateKey(inputs, hashString);
var localInputsHash = Utils.ObjectToHash(localInputs, Utils.DefaultHasher);

return new All(
LocateInputs: inputs,
LocalInputs: localInputs,
return new All(LocalInputs: localInputs,
CacheKey: cacheKey,
LocalInputsHash: localInputsHash,
FullExtract: extract
);
}
Expand All @@ -90,7 +85,7 @@ public string SaveConfig(Config config)
}

[Test]
public void SimpleCacheHitTest()
public async Task SimpleCacheHitTest()
{
var outputItems = new[]
{
Expand All @@ -116,15 +111,17 @@ public void SimpleCacheHitTest()
);
var refCache = new RefCache(tmpDir.Dir.CombineAsDir(".refcache").FullName);
var all = AllFromInputs(inputs, refCache);
var zip = LocatorAndPopulator.BuildOutputsZip(tmpDir.Dir, outputItems,
new AllCompilationMetadata(null, all.LocalInputs), Utils.DefaultHasher);
var hasher = TestUtils.DefaultHasher;
var outputData = await Task.WhenAll(outputItems.Select(i => LocatorAndPopulator.GatherSingleOutputData(i, hasher, null)).ToArray());
var zip = await LocatorAndPopulator.BuildOutputsZip(tmpDir.Dir, outputData,
new AllCompilationMetadata(null, all.LocalInputs.ToSlim()), hasher);

foreach (var outputItem in outputItems)
{
File.Move(outputItem.LocalPath, outputItem.LocalPath + ".copy");
}

_compilationResultsCache.Set(all.CacheKey, all.FullExtract, zip);
await _compilationResultsCache.SetAsync(all.CacheKey, all.FullExtract, zip);

locate.SetInputs(inputs);
var locateSuccess = locate.Execute();
Expand All @@ -135,7 +132,6 @@ public void SimpleCacheHitTest()
{
Assert.That(locateResult.CacheHit, Is.True);
Assert.That(locateResult.CacheKey, Is.EqualTo(all.CacheKey));
Assert.That(locateResult.LocalInputsHash, Is.EqualTo(all.LocalInputsHash));
Assert.That(locateResult.CacheSupported, Is.True);
Assert.That(locateResult.RunCompilation, Is.False);
});
Expand All @@ -146,13 +142,13 @@ public void SimpleCacheHitTest()
foreach (var outputItem in outputItems)
{
Assert.That(File.Exists(outputItem.LocalPath));
Assert.That(File.ReadAllText(outputItem.LocalPath),
Is.EqualTo(File.ReadAllText(outputItem.LocalPath + ".copy")));
Assert.That(await File.ReadAllTextAsync(outputItem.LocalPath),
Is.EqualTo(await File.ReadAllTextAsync(outputItem.LocalPath + ".copy")));
}
}

[Test]
public void SimpleCacheMissTest()
public async Task SimpleCacheMissTest()
{
var outputItems = new[]
{
Expand Down Expand Up @@ -189,18 +185,18 @@ public void SimpleCacheMissTest()
{
Assert.That(locateResult.CacheHit, Is.False);
Assert.That(locateResult.CacheKey, Is.EqualTo(all.CacheKey));
Assert.That(locateResult.LocalInputsHash, Is.EqualTo(all.LocalInputsHash));
Assert.That(locateResult.CacheSupported, Is.True);
Assert.That(locateResult.RunCompilation, Is.True);
});

use.Guid = locate.Guid;
use.CompilationSucceeded = true;
Assert.That(use.Execute(), Is.True);

var allKeys = _compilationResultsCache.GetAllExistingKeys();
Assert.That(allKeys, Is.EquivalentTo(new[] { all.CacheKey }));

var zip = _compilationResultsCache.Get(all.CacheKey);
var zip = await _compilationResultsCache.GetAsync(all.CacheKey);
Assert.That(zip, Is.Not.Null);
var fromCacheDir = tmpDir.Dir.CreateSubdirectory("from_cache");
ZipFile.ExtractToDirectory(zip, fromCacheDir.FullName);
Expand All @@ -209,7 +205,7 @@ public void SimpleCacheMissTest()
{
var cachedFile = fromCacheDir.CombineAsFile(outputItem.CacheFileName);
Assert.That(File.Exists(cachedFile.FullName));
Assert.That(File.ReadAllText(cachedFile.FullName), Is.EqualTo(File.ReadAllText(outputItem.LocalPath)));
Assert.That(await File.ReadAllTextAsync(cachedFile.FullName), Is.EqualTo(await File.ReadAllTextAsync(outputItem.LocalPath)));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,15 @@

<PropertyGroup>
<OutputType>Library</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
<PackageReference Include="Moq" Version="4.18.4" />
<PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.4.2" />
<PackageReference Include="Microsoft.Build.Tasks.Core" Version="17.3.2" />
<PackageReference Include="Microsoft.Build.Tasks.Core" Version="17.8.3" />
<PackageReference Include="JetBrains.Refasmer" Version="1.0.33" PrivateAssets="all" />
<PackageReference Include="FastHashes" Version="3.5.0" PrivateAssets="all" />
</ItemGroup>
Expand Down
15 changes: 8 additions & 7 deletions MSBuild.CompilerCache.Tests/PerfTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ public class PerfTests
public void HashCalculationPerfTest()
{
var sw = Stopwatch.StartNew();
var fileHashCache = new FileHashCache(".filehashcache");
var fileHashCache = new FileHashCache(".filehashcache", TestUtils.DefaultHasher, null);
var inMemoryFileHashCache = new DictionaryBasedCache<FileHashCacheKey, string>();
var combinedFileHashCache = new CacheCombiner<FileHashCacheKey, string>(inMemoryFileHashCache, fileHashCache);
var combinedFileHashCache = CacheCombiner.Combine(inMemoryFileHashCache, fileHashCache);
var inMemoryRefCache = new InMemoryRefCache();
var refCache = new RefCache(".refcache");
var combinedRefCache = new CacheCombiner<CacheKey, RefDataWithOriginalExtract>(inMemoryRefCache, refCache);
var combinedRefCache = CacheCombiner.Combine(inMemoryRefCache, refCache);

// var refCacheDir = new DisposableDir();
for (int i = 0; i < 20; i++)
Expand All @@ -38,13 +38,14 @@ public void HashCalculationPerfTest()
);
var decomposed = TargetsExtractionUtils.DecomposeCompilerProps(inputs.AllProps);
var refTrimmingConfig = new RefTrimmingConfig();
var hasher = TestUtils.DefaultHasher;
var localInputs =
LocatorAndPopulator.CalculateLocalInputs(decomposed, combinedRefCache, "assembly", refTrimmingConfig,
combinedFileHashCache, Utils.DefaultHasher);
var extract = localInputs.ToFullExtract();
var hashString = Utils.ObjectToHash(extract);
combinedFileHashCache, hasher, null);
var extract = localInputs.ToSlim().ToFullExtract();
var hashString = Utils.ObjectToHash(extract, hasher);
var cacheKey = LocatorAndPopulator.GenerateKey(inputs, hashString);
var localInputsHash = Utils.ObjectToHash(localInputs);
var localInputsHash = Utils.ObjectToHash(localInputs, hasher);
}
Console.WriteLine($"[{i}] {sw.ElapsedMilliseconds}ms");
}
Expand Down
17 changes: 9 additions & 8 deletions MSBuild.CompilerCache.Tests/RefCacheTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ public void EmptyCacheMisses()
using var dir = new DisposableDir();
var cache = new RefCache(dir.FullName);
var key = new CacheKey("a");
Assert.Multiple(() =>
Assert.Multiple(async () =>
{
Assert.That(cache.Exists(key), Is.False);
Assert.That(cache.Get(key), Is.Null);
Assert.That(await cache.GetAsync(key), Is.Null);
});
}

[Test]
public void AfterSetCacheHits()
public async Task AfterSetCacheHits()
{
using var dir = new DisposableDir();
var cache = new RefCache(dir.FullName);
Expand All @@ -37,17 +37,18 @@ public void AfterSetCacheHits()
Length: 1212,
LastWriteTimeUtc: new DateTime(2023, 7, 1, 0, 0, 0, kind: DateTimeKind.Utc)
),
Hash: null
Hash: "hash"
)
);
cache.Set(key, data);

Assert.Multiple(() =>
await cache.SetAsync(key, data);

Assert.Multiple(async () =>
{
Assert.That(cache.Exists(key), Is.True);
var cached = cache.Get(key);
var cached = await cache.GetAsync(key);
Assert.That(cached, Is.Not.Null);
Assert.That(cached.ToJson(), Is.EqualTo(data.ToJson()));
Assert.That(cached!.ToJson(), Is.EqualTo(data.ToJson()));
Assert.That(cache.Exists(new CacheKey("b")), Is.False);
});
}
Expand Down
2 changes: 1 addition & 1 deletion MSBuild.CompilerCache.Tests/Samples/Program.cs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
System.Console.WriteLine("Hello");
Console.WriteLine("Hello");
Loading