Skip to content

Commit 6568c37

Browse files
committed
add benchmarks to RequestMiddleware
1 parent 2f0e481 commit 6568c37

File tree

4 files changed

+59
-7
lines changed

4 files changed

+59
-7
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
``` ini
2+
3+
BenchmarkDotNet=v0.10.10, OS=Mac OS X 10.12
4+
Processor=Intel Core i5-5257U CPU 2.70GHz (Broadwell), ProcessorCount=4
5+
.NET Core SDK=2.1.4
6+
[Host] : .NET Core 2.0.5 (Framework 4.6.0.0), 64bit RyuJIT
7+
DefaultJob : .NET Core 2.0.5 (Framework 4.6.0.0), 64bit RyuJIT
8+
9+
10+
```
11+
| Method | Mean | Error | StdDev | Gen 0 | Allocated |
12+
|----------- |----------:|----------:|----------:|-------:|----------:|
13+
| UsingSplit | 157.28 ns | 2.9689 ns | 5.8602 ns | 0.2134 | 336 B |
14+
| Current | 39.96 ns | 0.6489 ns | 0.6070 ns | - | 0 B |

benchmarks/Program.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using BenchmarkDotNet.Running;
22
using Benchmarks.LinkBuilder;
33
using Benchmarks.Query;
4+
using Benchmarks.RequestMiddleware;
45
using Benchmarks.Serialization;
56

67
namespace Benchmarks {
@@ -10,7 +11,8 @@ static void Main(string[] args) {
1011
typeof(JsonApiDeserializer_Benchmarks),
1112
typeof(JsonApiSerializer_Benchmarks),
1213
typeof(QueryParser_Benchmarks),
13-
typeof(LinkBuilder_GetNamespaceFromPath_Benchmarks)
14+
typeof(LinkBuilder_GetNamespaceFromPath_Benchmarks),
15+
typeof(ContainsMediaTypeParameters_Benchmarks)
1416
});
1517
switcher.Run(args);
1618
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System;
2+
using BenchmarkDotNet.Attributes;
3+
using BenchmarkDotNet.Attributes.Exporters;
4+
using JsonApiDotNetCore.Internal;
5+
6+
namespace Benchmarks.RequestMiddleware
7+
{
8+
[MarkdownExporter, MemoryDiagnoser]
9+
public class ContainsMediaTypeParameters_Benchmarks
10+
{
11+
private const string MEDIA_TYPE = "application/vnd.api+json; version=1";
12+
13+
[Benchmark]
14+
public void UsingSplit() => UsingSplitImpl(MEDIA_TYPE);
15+
16+
[Benchmark]
17+
public void Current()
18+
=> JsonApiDotNetCore.Middleware.RequestMiddleware.ContainsMediaTypeParameters(MEDIA_TYPE);
19+
20+
private bool UsingSplitImpl(string mediaType)
21+
{
22+
var mediaTypeArr = mediaType.Split(';');
23+
return (mediaTypeArr[0] == Constants.ContentType && mediaTypeArr.Length == 2);
24+
}
25+
}
26+
}

src/JsonApiDotNetCore/Middleware/RequestMiddleware.cs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System;
22
using System.Threading.Tasks;
3-
using JsonApiDotNetCore.Extensions;
43
using JsonApiDotNetCore.Internal;
54
using Microsoft.AspNetCore.Http;
65
using Microsoft.Extensions.Primitives;
@@ -54,12 +53,23 @@ private static bool IsValidAcceptHeader(HttpContext context)
5453
return true;
5554
}
5655

57-
private static bool ContainsMediaTypeParameters(string mediaType)
56+
internal static bool ContainsMediaTypeParameters(string mediaType)
5857
{
59-
const char delimeter = ';';
60-
var subSpans = mediaType.SpanSplit(delimeter);
61-
if (subSpans.Count == 0) return false;
62-
return subSpans.Count == 2 && subSpans[0].ToString() == Constants.ContentType;
58+
var incomingMediaTypeSpan = mediaType.AsSpan();
59+
60+
// if the content type is not application/vnd.api+json then continue on
61+
if(incomingMediaTypeSpan.Length < Constants.ContentType.Length)
62+
return false;
63+
64+
var incomingContentType = incomingMediaTypeSpan.Slice(0, Constants.ContentType.Length);
65+
if(incomingContentType.SequenceEqual(Constants.ContentType.AsSpan()) == false)
66+
return false;
67+
68+
// anything appended to "application/vnd.api+json;" will be considered a media type param
69+
return (
70+
incomingMediaTypeSpan.Length >= Constants.ContentType.Length + 2
71+
&& incomingMediaTypeSpan[Constants.ContentType.Length] == ';'
72+
);
6373
}
6474

6575
private static void FlushResponse(HttpContext context, int statusCode)

0 commit comments

Comments
 (0)