-
-
Notifications
You must be signed in to change notification settings - Fork 158
/
Copy pathJsonApiDeserializer_Benchmarks.cs
61 lines (52 loc) · 2.34 KB
/
JsonApiDeserializer_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
using System;
using System.Collections.Generic;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Attributes.Exporters;
using JsonApiDotNetCore.Builders;
using JsonApiDotNetCore.Configuration;
using JsonApiDotNetCore.Internal.Generics;
using JsonApiDotNetCore.Models;
using JsonApiDotNetCore.Serialization;
using JsonApiDotNetCore.Services;
using Moq;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
namespace Benchmarks.Serialization {
[MarkdownExporter]
public class JsonApiDeserializer_Benchmarks {
private const string TYPE_NAME = "simple-types";
private static readonly string Content = JsonConvert.SerializeObject(new Document {
Data = new ResourceObject {
Type = TYPE_NAME,
Id = "1",
Attributes = new Dictionary<string, object> {
{
"name",
Guid.NewGuid().ToString()
}
}
}
});
private readonly JsonApiDeSerializer _jsonApiDeSerializer;
public JsonApiDeserializer_Benchmarks() {
var contextGraphBuilder = new ContextGraphBuilder();
contextGraphBuilder.AddResource<SimpleType>(TYPE_NAME);
var contextGraph = contextGraphBuilder.Build();
var jsonApiContextMock = new Mock<IJsonApiContext>();
jsonApiContextMock.SetupAllProperties();
jsonApiContextMock.Setup(m => m.ContextGraph).Returns(contextGraph);
jsonApiContextMock.Setup(m => m.AttributesToUpdate).Returns(new Dictionary<AttrAttribute, object>());
var jsonApiOptions = new JsonApiOptions();
jsonApiOptions.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
jsonApiContextMock.Setup(m => m.Options).Returns(jsonApiOptions);
var genericProcessorFactoryMock = new Mock<IGenericProcessorFactory>();
_jsonApiDeSerializer = new JsonApiDeSerializer(jsonApiContextMock.Object, genericProcessorFactoryMock.Object);
}
[Benchmark]
public object DeserializeSimpleObject() => _jsonApiDeSerializer.Deserialize<SimpleType>(Content);
private class SimpleType : Identifiable {
[Attr("name")]
public string Name { get; set; }
}
}
}