-
Notifications
You must be signed in to change notification settings - Fork 504
/
Copy pathQueryTSinglePkV2BenchmarkOperation.cs
118 lines (104 loc) · 4.56 KB
/
QueryTSinglePkV2BenchmarkOperation.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------
namespace CosmosBenchmark
{
using System;
using System.Collections.Generic;
using System.Net;
using System.Threading.Tasks;
using Microsoft.Azure.Documents;
using Microsoft.Azure.Documents.Client;
using Microsoft.Azure.Documents.Linq;
internal class QueryTSinglePkV2BenchmarkOperation : IBenchmarkOperation
{
private readonly DocumentClient documentClient;
private readonly string partitionKeyPath;
private readonly Dictionary<string, object> sampleJObject;
private readonly string databsaeName;
private readonly string containerName;
private readonly Uri containerUri;
private readonly string executionItemPartitionKey;
private readonly string executionItemId;
private bool initialized = false;
public QueryTSinglePkV2BenchmarkOperation(
DocumentClient documentClient,
string dbName,
string containerName,
string partitionKeyPath,
string sampleJson)
{
this.databsaeName = dbName;
this.containerName = containerName;
this.documentClient = documentClient;
this.partitionKeyPath = partitionKeyPath.Replace("/", "");
this.containerUri = UriFactory.CreateDocumentCollectionUri(this.databsaeName, this.containerName);
this.sampleJObject = JsonHelper.Deserialize<Dictionary<string, object>>(sampleJson);
this.executionItemPartitionKey = Guid.NewGuid().ToString();
this.executionItemId = Guid.NewGuid().ToString();
this.sampleJObject["id"] = this.executionItemId;
this.sampleJObject[this.partitionKeyPath] = this.executionItemPartitionKey;
}
public BenchmarkOperationType OperationType => BenchmarkOperationType.Query;
public async Task<OperationResult> ExecuteOnceAsync()
{
IDocumentQuery<Dictionary<string, object>> query = this.documentClient.CreateDocumentQuery<Dictionary<string, object>>(
this.containerUri,
new SqlQuerySpec("select * from T where T.id = @id")
{
Parameters = new SqlParameterCollection()
{
new SqlParameter()
{
Name = "@id",
Value = this.executionItemId
}
}
},
new FeedOptions()
{
PartitionKey = new PartitionKey(this.executionItemPartitionKey)
}).AsDocumentQuery();
double totalCharge = 0;
Func<string> lastDiagnostics = null;
while (query.HasMoreResults)
{
FeedResponse<Dictionary<string, object>> feedResponse = await query.ExecuteNextAsync<Dictionary<string, object>>();
foreach (Dictionary<string, object> item in feedResponse)
{
// No-op check that forces any lazy logic to be executed
if (item == null)
{
throw new Exception("Null item was returned");
}
}
totalCharge += feedResponse.RequestCharge;
lastDiagnostics = () => feedResponse.RequestDiagnosticsString;
}
return new OperationResult()
{
DatabseName = databsaeName,
ContainerName = containerName,
RuCharges = totalCharge,
LazyDiagnostics = lastDiagnostics,
};
}
public async Task PrepareAsync()
{
if (this.initialized)
{
return;
}
Uri containerUri = UriFactory.CreateDocumentCollectionUri(this.databsaeName, this.containerName);
ResourceResponse<Document> itemResponse = await this.documentClient.CreateDocumentAsync(
containerUri,
this.sampleJObject,
new RequestOptions() { PartitionKey = new PartitionKey(this.executionItemPartitionKey) });
if (itemResponse.StatusCode != HttpStatusCode.Created)
{
throw new Exception($"Create failed with status code: {itemResponse.StatusCode}");
}
this.initialized = true;
}
}
}