Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Benchmarks and more unit tests, plus resulting bug fixes #26

Closed
wants to merge 10 commits into from
15 changes: 7 additions & 8 deletions SimpleCDN.Benchmarks/Benchmarks.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Jobs;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SimpleCDN.Benchmarks
{
[MemoryDiagnoser(false)]
//[SimpleJob(RuntimeMoniker.NativeAot90)]
[SimpleJob(RuntimeMoniker.Net90)]
[MemoryDiagnoser(false)]
public class Benchmarks
{
public static IEnumerable<char[]> Paths =>
Expand All @@ -25,14 +19,19 @@ public class Benchmarks

[Benchmark(OperationsPerInvoke = NormalizationBenchmarkIterationsPerInvoke)]
[ArgumentsSource(nameof(Paths))]
public void Normalize(char[] path)
public long Normalize(char[] path)
{
long res = 0;

for (var i = 0; i < NormalizationBenchmarkIterationsPerInvoke; i++)
{
var copy = path.ToArray();
var span = copy.AsSpan();
Helpers.Extensions.Normalize(ref span);
res += span.BinarySearch('/');
}

return res;
}
}
}
4 changes: 1 addition & 3 deletions SimpleCDN.Tests.Integration/EndpointTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using Microsoft.AspNetCore.Mvc.Testing;

namespace SimpleCDN.Tests.Integration
namespace SimpleCDN.Tests.Integration
{
public class EndpointTests : IClassFixture<CustomWebApplicationFactory>
{
Expand Down
4 changes: 0 additions & 4 deletions SimpleCDN.Tests/CacheManagerTests.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
using SimpleCDN.Cache;
using SimpleCDN.Services;
using SimpleCDN.Tests.Mocks;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SimpleCDN.Tests
{
Expand Down
5 changes: 0 additions & 5 deletions SimpleCDN.Tests/Mocks/DistributedCacheMock.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
using Microsoft.Extensions.Caching.Distributed;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SimpleCDN.Tests.Mocks
{
Expand Down
8 changes: 1 addition & 7 deletions SimpleCDN.Tests/NormalizeTests.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SimpleCDN.Tests
namespace SimpleCDN.Tests
{
[TestFixture]
public class NormalizeTests
Expand Down
44 changes: 21 additions & 23 deletions SimpleCDN.sln
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,44 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.13.35507.96
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SimpleCDN", "SimpleCDN\SimpleCDN.csproj", "{E15F4BC2-E820-48D6-857B-E2DC9E35D370}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SimpleCDN.Tests", "SimpleCDN.Tests\SimpleCDN.Tests.csproj", "{6D8833B9-7EC8-4212-83DD-ABCCD677309E}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SimpleCDN", "SimpleCDN\SimpleCDN.csproj", "{9C7F11BA-ECF0-8CD1-D1F5-DB31D692724E}"
EndProject
Project("{E53339B2-1760-4266-BCC7-CA923CBCF16C}") = "docker-compose", "docker-compose.dcproj", "{81DDED9D-158B-E303-5F62-77A2896D2A5A}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SimpleCDN.Tests.Integration", "SimpleCDN.Tests.Integration\SimpleCDN.Tests.Integration.csproj", "{F79E71E8-89D8-46F7-802C-CFDF3A77447D}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SimpleCDN.Tests", "SimpleCDN.Tests\SimpleCDN.Tests.csproj", "{9F41AC4B-73A5-AED7-2FAD-13532040B2E1}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SimpleCDN.Benchmarks", "SimpleCDN.Benchmarks\SimpleCDN.Benchmarks.csproj", "{01B35836-CD1D-0631-A280-04996E3E2058}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SimpleCDN.Benchmarks", "SimpleCDN.Benchmarks\SimpleCDN.Benchmarks.csproj", "{73BC9966-46FF-40D8-89F4-C990B15370A2}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SimpleCDN.Tests.Integration", "SimpleCDN.Tests.Integration\SimpleCDN.Tests.Integration.csproj", "{748DF05A-61E9-3214-6751-2FB8D9C75A1C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{E15F4BC2-E820-48D6-857B-E2DC9E35D370}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E15F4BC2-E820-48D6-857B-E2DC9E35D370}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E15F4BC2-E820-48D6-857B-E2DC9E35D370}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E15F4BC2-E820-48D6-857B-E2DC9E35D370}.Release|Any CPU.Build.0 = Release|Any CPU
{6D8833B9-7EC8-4212-83DD-ABCCD677309E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6D8833B9-7EC8-4212-83DD-ABCCD677309E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6D8833B9-7EC8-4212-83DD-ABCCD677309E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6D8833B9-7EC8-4212-83DD-ABCCD677309E}.Release|Any CPU.Build.0 = Release|Any CPU
{9C7F11BA-ECF0-8CD1-D1F5-DB31D692724E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9C7F11BA-ECF0-8CD1-D1F5-DB31D692724E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9C7F11BA-ECF0-8CD1-D1F5-DB31D692724E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9C7F11BA-ECF0-8CD1-D1F5-DB31D692724E}.Release|Any CPU.Build.0 = Release|Any CPU
{81DDED9D-158B-E303-5F62-77A2896D2A5A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{81DDED9D-158B-E303-5F62-77A2896D2A5A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{81DDED9D-158B-E303-5F62-77A2896D2A5A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{81DDED9D-158B-E303-5F62-77A2896D2A5A}.Release|Any CPU.Build.0 = Release|Any CPU
{F79E71E8-89D8-46F7-802C-CFDF3A77447D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F79E71E8-89D8-46F7-802C-CFDF3A77447D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F79E71E8-89D8-46F7-802C-CFDF3A77447D}.Release|Any CPU.Build.0 = Release|Any CPU
{73BC9966-46FF-40D8-89F4-C990B15370A2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{73BC9966-46FF-40D8-89F4-C990B15370A2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{73BC9966-46FF-40D8-89F4-C990B15370A2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{73BC9966-46FF-40D8-89F4-C990B15370A2}.Release|Any CPU.Build.0 = Release|Any CPU
{9F41AC4B-73A5-AED7-2FAD-13532040B2E1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9F41AC4B-73A5-AED7-2FAD-13532040B2E1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9F41AC4B-73A5-AED7-2FAD-13532040B2E1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9F41AC4B-73A5-AED7-2FAD-13532040B2E1}.Release|Any CPU.Build.0 = Release|Any CPU
{01B35836-CD1D-0631-A280-04996E3E2058}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{01B35836-CD1D-0631-A280-04996E3E2058}.Debug|Any CPU.Build.0 = Debug|Any CPU
{01B35836-CD1D-0631-A280-04996E3E2058}.Release|Any CPU.ActiveCfg = Release|Any CPU
{01B35836-CD1D-0631-A280-04996E3E2058}.Release|Any CPU.Build.0 = Release|Any CPU
{748DF05A-61E9-3214-6751-2FB8D9C75A1C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{748DF05A-61E9-3214-6751-2FB8D9C75A1C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{748DF05A-61E9-3214-6751-2FB8D9C75A1C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{748DF05A-61E9-3214-6751-2FB8D9C75A1C}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {D252A113-93EA-49FE-9080-E3A41CD60F25}
EndGlobalSection
EndGlobal
2 changes: 1 addition & 1 deletion SimpleCDN/Endpoints/CDN.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ public static IEndpointRouteBuilder RegisterCDNEndpoints(this IEndpointRouteBuil
{
try
{

if (loader.GetFile(route) is CDNFile file)
{
var typedAccept = ctx.Request.GetTypedHeaders().Accept;
Expand Down Expand Up @@ -57,6 +56,7 @@ public static IEndpointRouteBuilder RegisterCDNEndpoints(this IEndpointRouteBuil
}).CacheOutput(policy =>
{
// cache the response for 1 minute to reduce load on the server
// this is another layer of caching, on top of the CDNLoader cache.
policy.Cache()
.Expire(TimeSpan.FromMinutes(1))
.SetVaryByRouteValue("route")
Expand Down
10 changes: 10 additions & 0 deletions SimpleCDN/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ private static void Main(string[] args)
.AddJsonFile($"appsettings.{builder.Environment.EnvironmentName}.json", optional: true, reloadOnChange: true)
.AddCommandLine(args);

builder.Services.AddOutputCache(options =>
{
var configuration = options.ApplicationServices.GetRequiredService<CDNConfiguration>();

// output cache is 1/10th of memory cache
// in bytes in kilobytes
options.MaximumBodySize = configuration.MaxMemoryCacheSize * 100;
});

builder.Services.MapConfiguration();

// for now, we use a simple size-limited in-memory cache.
Expand All @@ -35,6 +44,7 @@ private static void Main(string[] args)
var app = builder.Build();

app.RegisterCDNEndpoints();
app.UseOutputCache();

app.Run();
}
Expand Down
Loading