-
-
Notifications
You must be signed in to change notification settings - Fork 158
/
Copy pathQueryParser_Benchmarks.cs
68 lines (59 loc) · 2.57 KB
/
QueryParser_Benchmarks.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
using System;
using System.Collections.Generic;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Attributes.Exporters;
using BenchmarkDotNet.Attributes.Jobs;
using JsonApiDotNetCore.Configuration;
using JsonApiDotNetCore.Internal;
using JsonApiDotNetCore.Managers.Contracts;
using JsonApiDotNetCore.Models;
using JsonApiDotNetCore.Services;
using Microsoft.AspNetCore.Http.Internal;
using Microsoft.Extensions.Primitives;
using Moq;
namespace Benchmarks.Query {
[MarkdownExporter, SimpleJob(launchCount : 3, warmupCount : 10, targetCount : 20), MemoryDiagnoser]
public class QueryParser_Benchmarks {
private readonly BenchmarkFacade _queryParser;
private const string ATTRIBUTE = "Attribute";
private const string ASCENDING_SORT = ATTRIBUTE;
private const string DESCENDING_SORT = "-" + ATTRIBUTE;
public QueryParser_Benchmarks() {
var requestMock = new Mock<IRequestContext>();
requestMock.Setup(m => m.GetRequestResource()).Returns(new ResourceContext {
Attributes = new List<AttrAttribute> {
new AttrAttribute(ATTRIBUTE, ATTRIBUTE)
}
});
var options = new JsonApiOptions();
_queryParser = new BenchmarkFacade(requestMock.Object, options);
}
[Benchmark]
public void AscendingSort() => _queryParser._ParseSortParameters(ASCENDING_SORT);
[Benchmark]
public void DescendingSort() => _queryParser._ParseSortParameters(DESCENDING_SORT);
[Benchmark]
public void ComplexQuery() => Run(100, () => _queryParser.Parse(
new QueryCollection(
new Dictionary<string, StringValues> {
{ $"filter[{ATTRIBUTE}]", new StringValues(new [] { "abc", "eq:abc" }) },
{ $"sort", $"-{ATTRIBUTE}" },
{ $"include", "relationship" },
{ $"page[size]", "1" },
{ $"fields[resource]", ATTRIBUTE },
}
)
));
private void Run(int iterations, Action action) {
for (int i = 0; i < iterations; i++)
action();
}
// this facade allows us to expose and micro-benchmark protected methods
private class BenchmarkFacade : QueryParameterDiscovery {
public BenchmarkFacade(
IRequestContext currentRequest,
JsonApiOptions options) : base(currentRequest, options) { }
public void _ParseSortParameters(string value) => base.ParseSortParameters(value);
}
}
}