-
Notifications
You must be signed in to change notification settings - Fork 504
/
Copy pathInsertV2BenchmarkOperation.cs
73 lines (59 loc) · 2.55 KB
/
InsertV2BenchmarkOperation.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
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------
namespace CosmosBenchmark
{
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Azure.Documents;
using Microsoft.Azure.Documents.Client;
internal class InsertV2BenchmarkOperation : IBenchmarkOperation
{
private readonly DocumentClient documentClient;
private readonly Uri containerUri;
private readonly string partitionKeyPath;
private readonly Dictionary<string, object> sampleJObject;
private readonly string databsaeName;
private readonly string containerName;
public InsertV2BenchmarkOperation(
DocumentClient documentClient,
string dbName,
string containerName,
string partitionKeyPath,
string sampleJson)
{
this.documentClient = documentClient;
this.containerUri = UriFactory.CreateDocumentCollectionUri(dbName, containerName);
this.partitionKeyPath = partitionKeyPath.Replace("/", "");
this.databsaeName = dbName;
this.containerName = containerName;
this.sampleJObject = JsonHelper.Deserialize<Dictionary<string, object>>(sampleJson);
}
public BenchmarkOperationType OperationType => BenchmarkOperationType.Insert;
public async Task<OperationResult> ExecuteOnceAsync()
{
ResourceResponse<Document> itemResponse = await this.documentClient.CreateDocumentAsync(
this.containerUri,
this.sampleJObject,
new RequestOptions() { PartitionKey = new PartitionKey(this.sampleJObject[this.partitionKeyPath]) }
);
Document value = itemResponse.Resource;
double ruCharges = itemResponse.RequestCharge;
return new OperationResult()
{
DatabseName = databsaeName,
ContainerName = containerName,
RuCharges = ruCharges,
LazyDiagnostics = () => itemResponse.RequestDiagnosticsString,
};
}
public Task PrepareAsync()
{
string newPartitionKey = Guid.NewGuid().ToString();
this.sampleJObject["id"] = Guid.NewGuid().ToString();
this.sampleJObject[this.partitionKeyPath] = newPartitionKey;
return Task.CompletedTask;
}
}
}