-
Notifications
You must be signed in to change notification settings - Fork 533
Expand file tree
/
Copy pathCreateItemStreamV3BenchmarkOperation.cs
More file actions
63 lines (56 loc) · 2.52 KB
/
CreateItemStreamV3BenchmarkOperation.cs
File metadata and controls
63 lines (56 loc) · 2.52 KB
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
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------
namespace CosmosBenchmark
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using Microsoft.Azure.Cosmos;
internal class CreateItemStreamV3BenchmarkOperation : IBenchmarkOperation
{
private readonly Container container;
private readonly string partitionKeyPath;
private readonly Dictionary<string, object> sampleJObject;
private readonly string databaseName;
private readonly string containerName;
public CreateItemStreamV3BenchmarkOperation(
CosmosClient cosmosClient,
string dbName,
string containerName,
string partitionKeyPath,
string sampleJson)
{
this.databaseName = dbName;
this.containerName = containerName;
this.container = cosmosClient.GetContainer(this.databaseName, this.containerName);
this.partitionKeyPath = partitionKeyPath.Replace("/", "");
this.sampleJObject = JsonHelper.Deserialize<Dictionary<string, object>>(sampleJson);
}
public BenchmarkOperationType OperationType => BenchmarkOperationType.Insert;
public Task PrepareAsync()
{
this.sampleJObject["id"] = Guid.NewGuid().ToString();
this.sampleJObject[this.partitionKeyPath] = Guid.NewGuid().ToString();
return Task.CompletedTask;
}
public async Task<OperationResult> ExecuteOnceAsync()
{
PartitionKey partitionKey = new PartitionKey(this.sampleJObject[this.partitionKeyPath].ToString());
using (MemoryStream input = JsonHelper.ToStream(this.sampleJObject))
{
ResponseMessage itemResponse = await this.container.CreateItemStreamAsync(input, partitionKey);
return new OperationResult
{
DatabseName = this.databaseName,
ContainerName = this.containerName,
OperationType = this.OperationType,
RuCharges = itemResponse.Headers.RequestCharge,
CosmosDiagnostics = itemResponse.Diagnostics,
LazyDiagnostics = () => itemResponse.Diagnostics.ToString(),
};
}
}
}
}