Skip to content

Commit 8c966ad

Browse files
Add CacheKeyBaseline and results
1 parent fce77d6 commit 8c966ad

2 files changed

Lines changed: 58 additions & 1 deletion

File tree

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright (c) Six Labors.
2+
// Licensed under the Apache License, Version 2.0.
3+
4+
using System.Text;
5+
using Microsoft.AspNetCore.Http;
6+
using SixLabors.ImageSharp.Web.Caching;
7+
using SixLabors.ImageSharp.Web.Commands;
8+
9+
namespace SixLabors.ImageSharp.Web.Benchmarks.Caching
10+
{
11+
/// <summary>
12+
/// Original implementation of cache key creation.
13+
/// </summary>
14+
public class CacheKeyBaseline : ICacheKey
15+
{
16+
/// <inheritdoc/>
17+
public string Create(HttpContext context, CommandCollection commands)
18+
{
19+
var sb = new StringBuilder(context.Request.Host.ToString());
20+
21+
string pathBase = context.Request.PathBase.ToString();
22+
if (!string.IsNullOrWhiteSpace(pathBase))
23+
{
24+
sb.AppendFormat("{0}/", pathBase);
25+
}
26+
27+
string path = context.Request.Path.ToString();
28+
if (!string.IsNullOrWhiteSpace(path))
29+
{
30+
sb.Append(path);
31+
}
32+
33+
sb.Append(QueryString.Create(commands));
34+
35+
return sb.ToString().ToLowerInvariant();
36+
}
37+
}
38+
}

tests/ImageSharp.Web.Benchmarks/Caching/CacheKeyBenchmarks.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,34 @@ public class CacheKeyBenchmarks
1818
{ "width", "400" }
1919
};
2020

21+
private static readonly ICacheKey CacheKeyBaseline = new CacheKeyBaseline();
2122
private static readonly ICacheKey UriRelativeCacheKey = new UriRelativeCacheKey();
2223
private static readonly ICacheKey UriAbsoluteCacheKey = new UriAbsoluteCacheKey();
2324

24-
[Benchmark(Baseline = true, Description = "UriRelativeCacheKey")]
25+
[Benchmark(Baseline = true, Description = "Baseline")]
26+
public string CreateUsingBaseline() => CacheKeyBaseline.Create(Context, Commands);
27+
28+
[Benchmark(Description = "UriRelativeCacheKey")]
2529
public string CreateUsingUriRelativeCacheKey() => UriRelativeCacheKey.Create(Context, Commands);
2630

2731
[Benchmark(Description = "UriAbsoluteCacheKey")]
2832
public string CreateUsingUriAbsoluteCacheKey() => UriAbsoluteCacheKey.Create(Context, Commands);
2933

34+
/*
35+
BenchmarkDotNet=v0.13.1, OS=Windows 10.0.22000
36+
Intel Core i7-10750H CPU 2.60GHz, 1 CPU, 12 logical and 6 physical cores
37+
.NET SDK=6.0.101
38+
[Host] : .NET Core 3.1.22 (CoreCLR 4.700.21.56803, CoreFX 4.700.21.57101), X64 RyuJIT
39+
DefaultJob : .NET Core 3.1.22 (CoreCLR 4.700.21.56803, CoreFX 4.700.21.57101), X64 RyuJIT
40+
41+
42+
| Method | Mean | Error | StdDev | Ratio | RatioSD | Gen 0 | Allocated |
43+
|-------------------- |---------:|--------:|--------:|------:|--------:|-------:|----------:|
44+
| Baseline | 497.4 ns | 8.26 ns | 7.33 ns | 1.00 | 0.00 | 0.1106 | 696 B |
45+
| UriRelativeCacheKey | 270.9 ns | 5.47 ns | 7.11 ns | 0.55 | 0.02 | 0.0587 | 368 B |
46+
| UriAbsoluteCacheKey | 447.4 ns | 3.38 ns | 3.16 ns | 0.90 | 0.01 | 0.0939 | 592 B |
47+
*/
48+
3049
private static HttpContext CreateContext()
3150
{
3251
var httpContext = new DefaultHttpContext();

0 commit comments

Comments
 (0)