/
QueryEntityComparisonBenchmark.cs
96 lines (83 loc) · 3.82 KB
/
QueryEntityComparisonBenchmark.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
using System;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Amazon.DynamoDBv2;
using Amazon.DynamoDBv2.DataModel;
using Amazon.Runtime;
using BenchmarkDotNet.Attributes;
using Benchmarks.Entities;
using Benchmarks.Mocks;
using Benchmarks.Mocks.Http;
using EfficientDynamoDb;
using EfficientDynamoDb.Configs;
using EfficientDynamoDb.Configs.Http;
using EfficientDynamoDb.DocumentModel;
using EfficientDynamoDb.Internal.Crc;
using RegionEndpoint = EfficientDynamoDb.Configs.RegionEndpoint;
namespace Benchmarks.Query
{
[MemoryDiagnoser]
public class QueryEntityComparisonBenchmark
{
[Params(10, 100, 1000)]
public int EntitiesCount;
private byte[] _responseContentBytes;
private byte[] _describeTableBytes;
private readonly DynamoDBContext _awsDbContext;
private readonly DynamoDbContext _efficientDbContext;
public QueryEntityComparisonBenchmark()
{
var ddbConfig = new AmazonDynamoDBConfig {RegionEndpoint = Amazon.RegionEndpoint.USEast1, HttpClientFactory = new MockHttpClientFactory(CreateResponse)};
var dbClient = new AmazonDynamoDBClient(
new BasicAWSCredentials(Environment.GetEnvironmentVariable("DEV_AWS_PUBLIC_KEY"), Environment.GetEnvironmentVariable("DEV_AWS_PRIVATE_KEY")),
ddbConfig);
var contextConfig = new DynamoDBContextConfig
{
TableNamePrefix = "production_",
Conversion = DynamoDBEntryConversion.V2
};
_awsDbContext = new DynamoDBContext(dbClient, contextConfig);
_efficientDbContext = new DynamoDbContext(new DynamoDbContextConfig(RegionEndpoint.USEast1, new AwsCredentials("test", "test"))
{
HttpClientFactory = new DefaultHttpClientFactory(new HttpClient(new MockHttpClientHandler(CreateResponse)))
});
}
[GlobalSetup]
public void SetupMixedBenchmark() => SetupBenchmark<MixedEntity>(x => EntitiesFactory.CreateMixedEntity(x).ToDocument());
[Benchmark(Description = "EfficientDynamoDb")]
public async Task<int> EfficientDynamoDbBenchmark()
{
var result = await _efficientDbContext.Query<MixedEntity>()
.WithKeyExpression(Condition<MixedEntity>.On(x => x.Pk).EqualTo("test"))
.ToListAsync().ConfigureAwait(false);
return result.Count;
}
[Benchmark(Description = "aws-sdk-net")]
public async Task<int> AwsSdkNetBenchmark()
{
var result = await _awsDbContext.QueryAsync<MixedEntity>("test").GetRemainingAsync().ConfigureAwait(false);
return result.Count;
}
private void SetupBenchmark<T>(Func<int, Document> entityFactory) where T: KeysOnlyEntity, new()
{
_responseContentBytes = QueryResponseFactory.CreateResponse(entityFactory, EntitiesCount);
_describeTableBytes = DescribeTableResponseFactory.CreateResponse();
}
private HttpResponseMessage CreateResponse(HttpRequestMessage request)
{
if(request.Headers.Contains("X-AMZ-Target") && request.Headers.GetValues("X-AMZ-Target").First().Contains("DescribeTable"))
return new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new ByteArrayContent(_describeTableBytes),
Headers = {{"x-amz-crc32", Crc32Algorithm.Compute(_describeTableBytes).ToString()}}
};
return new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new ByteArrayContent(_responseContentBytes),
Headers = {{"x-amz-crc32", Crc32Algorithm.Compute(_responseContentBytes).ToString()}}
};
}
}
}