-
Notifications
You must be signed in to change notification settings - Fork 504
/
Copy pathReadStreamExistsWithDiagnosticsV3BenchmarkOperation.cs
101 lines (85 loc) · 3.87 KB
/
ReadStreamExistsWithDiagnosticsV3BenchmarkOperation.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
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------
namespace CosmosBenchmark
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Threading.Tasks;
using Microsoft.Azure.Cosmos;
internal class ReadStreamExistsWithDiagnosticsV3BenchmarkOperation : IBenchmarkOperation
{
private readonly Container container;
private readonly string partitionKeyPath;
private readonly Dictionary<string, object> sampleJObject;
private readonly string databsaeName;
private readonly string containerName;
private string nextExecutionItemPartitionKey;
private string nextExecutionItemId;
public ReadStreamExistsWithDiagnosticsV3BenchmarkOperation(
CosmosClient cosmosClient,
string dbName,
string containerName,
string partitionKeyPath,
string sampleJson)
{
this.databsaeName = dbName;
this.containerName = containerName;
this.container = cosmosClient.GetContainer(this.databsaeName, this.containerName);
this.partitionKeyPath = partitionKeyPath.Replace("/", "");
this.sampleJObject = JsonHelper.Deserialize<Dictionary<string, object>>(sampleJson);
}
public BenchmarkOperationType OperationType => BenchmarkOperationType.Read;
public async Task<OperationResult> ExecuteOnceAsync()
{
using (ResponseMessage itemResponse = await this.container.ReadItemStreamAsync(
this.nextExecutionItemId,
new PartitionKey(this.nextExecutionItemPartitionKey)))
{
if (itemResponse.StatusCode != HttpStatusCode.OK)
{
throw new Exception($"ReadItem failed with {itemResponse.StatusCode}");
}
string diagnostics = itemResponse.Diagnostics.ToString();
if (string.IsNullOrEmpty(diagnostics))
{
throw new Exception();
}
return new OperationResult()
{
DatabseName = databsaeName,
ContainerName = containerName,
RuCharges = itemResponse.Headers.RequestCharge,
CosmosDiagnostics = itemResponse.Diagnostics,
LazyDiagnostics = () => itemResponse.Diagnostics.ToString(),
};
}
}
public async Task PrepareAsync()
{
if (string.IsNullOrEmpty(this.nextExecutionItemId) ||
string.IsNullOrEmpty(this.nextExecutionItemPartitionKey))
{
this.nextExecutionItemId = Guid.NewGuid().ToString();
this.nextExecutionItemPartitionKey = Guid.NewGuid().ToString();
this.sampleJObject["id"] = this.nextExecutionItemId;
this.sampleJObject[this.partitionKeyPath] = this.nextExecutionItemPartitionKey;
using (MemoryStream inputStream = JsonHelper.ToStream(this.sampleJObject))
{
using (ResponseMessage itemResponse = await this.container.CreateItemStreamAsync(
inputStream,
new PartitionKey(this.nextExecutionItemPartitionKey)))
{
System.Buffers.ArrayPool<byte>.Shared.Return(inputStream.GetBuffer());
if (itemResponse.StatusCode != HttpStatusCode.Created)
{
throw new Exception($"Create failed with statuscode: {itemResponse.StatusCode}");
}
}
}
}
}
}
}