|
| 1 | +// Copyright (c) Six Labors. |
| 2 | +// Licensed under the Apache License, Version 2.0. |
| 3 | + |
| 4 | +using System; |
| 5 | +using BenchmarkDotNet.Attributes; |
| 6 | +using Microsoft.AspNetCore.Http; |
| 7 | +using SixLabors.ImageSharp.Web.Caching; |
| 8 | +using SixLabors.ImageSharp.Web.Commands; |
| 9 | + |
| 10 | +namespace SixLabors.ImageSharp.Web.Benchmarks.Caching |
| 11 | +{ |
| 12 | + [Config(typeof(MemoryConfig))] |
| 13 | + public class CacheKeyBenchmarks |
| 14 | + { |
| 15 | + private static readonly HttpContext Context = CreateContext(); |
| 16 | + private static readonly CommandCollection Commands = new() |
| 17 | + { |
| 18 | + { "width", "400" } |
| 19 | + }; |
| 20 | + |
| 21 | + private static readonly ICacheKey UriRelativeCacheKey = new UriRelativeCacheKey(); |
| 22 | + private static readonly ICacheKey UriAbsoluteCacheKey = new UriAbsoluteCacheKey(); |
| 23 | + |
| 24 | + [Benchmark(Baseline = true, Description = "UriRelativeCacheKey")] |
| 25 | + public string CreateUsingUriRelativeCacheKey() => UriRelativeCacheKey.Create(Context, Commands); |
| 26 | + |
| 27 | + [Benchmark(Description = "UriAbsoluteCacheKey")] |
| 28 | + public string CreateUsingUriAbsoluteCacheKey() => UriAbsoluteCacheKey.Create(Context, Commands); |
| 29 | + |
| 30 | + private static HttpContext CreateContext() |
| 31 | + { |
| 32 | + var httpContext = new DefaultHttpContext(); |
| 33 | + httpContext.Request.Scheme = Uri.UriSchemeHttp; |
| 34 | + httpContext.Request.Host = new HostString("testwebsite.com"); |
| 35 | + httpContext.Request.PathBase = "/images"; |
| 36 | + httpContext.Request.Path = "/image-12345.jpeg"; |
| 37 | + httpContext.Request.QueryString = new QueryString("?should-be=ignored"); |
| 38 | + |
| 39 | + return httpContext; |
| 40 | + } |
| 41 | + } |
| 42 | +} |
0 commit comments