Skip to content

Commit fbf7ecf

Browse files
committed
bench(QueryParser): add sort benchmarks
1 parent 3af3b1a commit fbf7ecf

File tree

5 files changed

+69
-0
lines changed

5 files changed

+69
-0
lines changed

benchmarks/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
_data/
2+
*-report-default.md
3+
*-report.csv
4+
*-report.html
25

36
## Ignore Visual Studio temporary files, build results, and
47
## files generated by popular Visual Studio add-ons.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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.0.0
6+
[Host] : .NET Core 1.1.4 (Framework 4.6.25714.03), 64bit RyuJIT
7+
Job-HURVUO : .NET Core 1.1.4 (Framework 4.6.25714.03), 64bit RyuJIT
8+
9+
LaunchCount=3 TargetCount=20 WarmupCount=10
10+
11+
```
12+
| Method | Mean | Error | StdDev |
13+
|--------------- |---------:|----------:|----------:|
14+
| AscendingSort | 3.146 us | 0.0326 us | 0.0709 us |
15+
| DescendingSort | 3.372 us | 0.1228 us | 0.2618 us |

benchmarks/Benchmarks.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
55
<TargetFramework>$(NetCoreAppVersion)</TargetFramework>
6+
<AssemblyName>Benchmarks</AssemblyName>
67
</PropertyGroup>
78
<ItemGroup>
89
<PackageReference Include="BenchmarkDotNet" Version="0.10.10" />

benchmarks/Program.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
using BenchmarkDotNet.Running;
2+
using Benchmarks.Query;
23
using Benchmarks.Serialization;
34

45
namespace Benchmarks {
56
class Program {
67
static void Main(string[] args) {
78
BenchmarkRunner.Run<JsonApiDeserializer_Benchmarks>();
89
BenchmarkRunner.Run<JsonApiSerializer_Benchmarks>();
10+
BenchmarkRunner.Run<QueryParser_Benchmarks>();
911
}
1012
}
1113
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using System.Collections.Generic;
2+
using BenchmarkDotNet.Attributes;
3+
using BenchmarkDotNet.Attributes.Exporters;
4+
using BenchmarkDotNet.Attributes.Jobs;
5+
using JsonApiDotNetCore.Configuration;
6+
using JsonApiDotNetCore.Internal;
7+
using JsonApiDotNetCore.Models;
8+
using JsonApiDotNetCore.Services;
9+
using Moq;
10+
11+
namespace Benchmarks.Query {
12+
[MarkdownExporter, SimpleJob(launchCount : 3, warmupCount : 10, targetCount : 20)]
13+
public class QueryParser_Benchmarks {
14+
private readonly BenchmarkFacade _queryParser;
15+
16+
private const string ATTRIBUTE = "Attribute";
17+
private const string ASCENDING_SORT = ATTRIBUTE;
18+
private const string DESCENDING_SORT = "-" + ATTRIBUTE;
19+
20+
public QueryParser_Benchmarks() {
21+
var controllerContextMock = new Mock<IControllerContext>();
22+
controllerContextMock.Setup(m => m.RequestEntity).Returns(new ContextEntity {
23+
Attributes = new List<AttrAttribute> {
24+
new AttrAttribute(ATTRIBUTE) {
25+
InternalAttributeName = ATTRIBUTE
26+
}
27+
}
28+
});
29+
var options = new JsonApiOptions();
30+
_queryParser = new BenchmarkFacade(controllerContextMock.Object, options);
31+
}
32+
33+
[Benchmark]
34+
public void AscendingSort() => _queryParser._ParseSortParameters(ASCENDING_SORT);
35+
36+
[Benchmark]
37+
public void DescendingSort() => _queryParser._ParseSortParameters(DESCENDING_SORT);
38+
39+
// this facade allows us to expose and micro-benchmark protected methods
40+
private class BenchmarkFacade : QueryParser {
41+
public BenchmarkFacade(
42+
IControllerContext controllerContext,
43+
JsonApiOptions options) : base(controllerContext, options) { }
44+
45+
public void _ParseSortParameters(string value) => base.ParseSortParameters(value);
46+
}
47+
}
48+
}

0 commit comments

Comments
 (0)