Skip to content

Commit 8d2691b

Browse files
authored
[Internal] Category: Refactors Cosmos benchmark operations (#3961)
* Refactoring: base classes for operations. * Updating comments. * Adding new line at the end of the file. * Fixing code review points. * Restore PrepareAsync to be virtual.
1 parent d7fc282 commit 8d2691b

17 files changed

+73
-17
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//------------------------------------------------------------
2+
// Copyright (c) Microsoft Corporation. All rights reserved.
3+
//------------------------------------------------------------
4+
5+
namespace CosmosBenchmark
6+
{
7+
/// <summary>
8+
/// Benchmark operation type.
9+
/// </summary>
10+
public enum BenchmarkOperationType
11+
{
12+
Read,
13+
Insert,
14+
Query
15+
}
16+
}

Microsoft.Azure.Cosmos.Samples/Tools/Benchmark/Fx/CosmosDiagnosticsLogger.cs

-3
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,8 @@ namespace CosmosBenchmark
66
{
77
using System;
88
using System.Collections.Concurrent;
9-
using System.Collections.Generic;
109
using System.Diagnostics;
1110
using System.Linq;
12-
using System.Runtime.CompilerServices;
13-
using System.Text;
1411
using Microsoft.Azure.Cosmos;
1512
using Newtonsoft.Json.Linq;
1613

Microsoft.Azure.Cosmos.Samples/Tools/Benchmark/IBenchmarkOperation.cs

+17-1
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,26 @@ namespace CosmosBenchmark
66
{
77
using System.Threading.Tasks;
88

9+
/// <summary>
10+
/// Represents the Benchmark operation.
11+
/// </summary>
912
internal interface IBenchmarkOperation
1013
{
11-
Task PrepareAsync();
14+
/// <summary>
15+
/// Benchmark operation type.
16+
/// </summary>
17+
BenchmarkOperationType OperationType { get; }
1218

19+
/// <summary>
20+
/// Executes Benchmark operation once asynchronously.
21+
/// </summary>
22+
/// <returns>The operation result wrapped by task.</returns>
1323
Task<OperationResult> ExecuteOnceAsync();
24+
25+
/// <summary>
26+
/// Prepares Benchmark operation asynchronously.
27+
/// </summary>
28+
/// <returns>The task related to method's work.</returns>
29+
Task PrepareAsync();
1430
}
1531
}

Microsoft.Azure.Cosmos.Samples/Tools/Benchmark/v2/InsertV2BenchmarkOperation.cs

+2
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ public InsertV2BenchmarkOperation(
3838
this.sampleJObject = JsonHelper.Deserialize<Dictionary<string, object>>(sampleJson);
3939
}
4040

41+
public BenchmarkOperationType OperationType => BenchmarkOperationType.Insert;
42+
4143
public async Task<OperationResult> ExecuteOnceAsync()
4244
{
4345
ResourceResponse<Document> itemResponse = await this.documentClient.CreateDocumentAsync(

Microsoft.Azure.Cosmos.Samples/Tools/Benchmark/v2/QueryStreamSinglePkV2BenchmarkOperation.cs

+2
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ public QueryStreamSinglePkV2BenchmarkOperation(
4747
this.containerUri = UriFactory.CreateDocumentCollectionUri(this.databsaeName, this.containerName);
4848
}
4949

50+
public BenchmarkOperationType OperationType => BenchmarkOperationType.Query;
51+
5052
public async Task<OperationResult> ExecuteOnceAsync()
5153
{
5254
IDocumentQuery<dynamic> query = this.documentClient.CreateDocumentQuery<dynamic>(

Microsoft.Azure.Cosmos.Samples/Tools/Benchmark/v2/QueryTSinglePkV2BenchmarkOperation.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ public QueryTSinglePkV2BenchmarkOperation(
4646
this.sampleJObject[this.partitionKeyPath] = this.executionItemPartitionKey;
4747
}
4848

49+
public BenchmarkOperationType OperationType => BenchmarkOperationType.Query;
50+
4951
public async Task<OperationResult> ExecuteOnceAsync()
5052
{
5153
IDocumentQuery<Dictionary<string, object>> query = this.documentClient.CreateDocumentQuery<Dictionary<string, object>>(
@@ -107,7 +109,7 @@ public async Task PrepareAsync()
107109
new RequestOptions() { PartitionKey = new PartitionKey(this.executionItemPartitionKey) });
108110
if (itemResponse.StatusCode != HttpStatusCode.Created)
109111
{
110-
throw new Exception($"Create failed with statuscode: {itemResponse.StatusCode}");
112+
throw new Exception($"Create failed with status code: {itemResponse.StatusCode}");
111113
}
112114

113115
this.initialized = true;

Microsoft.Azure.Cosmos.Samples/Tools/Benchmark/v2/ReadFeedStreamV2BenchmarkOperation.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ public ReadFeedStreamV2BenchmarkOperation(
4040
this.sampleJObject = JsonHelper.Deserialize<Dictionary<string, object>>(sampleJson);
4141
}
4242

43+
public BenchmarkOperationType OperationType => BenchmarkOperationType.Read;
44+
4345
public async Task<OperationResult> ExecuteOnceAsync()
4446
{
4547
Uri containerUri = UriFactory.CreateDocumentCollectionUri(this.databsaeName, this.containerName);
@@ -75,7 +77,7 @@ public async Task PrepareAsync()
7577
new RequestOptions() { PartitionKey = new PartitionKey(this.nextExecutionItemPartitionKey) });
7678
if (itemResponse.StatusCode != HttpStatusCode.Created)
7779
{
78-
throw new Exception($"Create failed with statuscode: {itemResponse.StatusCode}");
80+
throw new Exception($"Create failed with status code: {itemResponse.StatusCode}");
7981
}
8082
}
8183
}

Microsoft.Azure.Cosmos.Samples/Tools/Benchmark/v2/ReadNotExistsV2BenchmarkOperation.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ internal class ReadNotExistsV2BenchmarkOperation : IBenchmarkOperation
2222

2323
private readonly DocumentClient documentClient;
2424

25+
public BenchmarkOperationType OperationType => BenchmarkOperationType.Read;
26+
2527
public ReadNotExistsV2BenchmarkOperation(
2628
DocumentClient documentClient,
2729
string dbName,
@@ -52,7 +54,7 @@ public async Task<OperationResult> ExecuteOnceAsync()
5254
{
5355
if (dce.StatusCode != HttpStatusCode.NotFound)
5456
{
55-
throw new Exception($"ReadItem failed wth {dce?.StatusCode} {dce?.ToString()}");
57+
throw new Exception($"ReadItem failed with {dce?.StatusCode} {dce?.ToString()}");
5658
}
5759

5860
return new OperationResult()

Microsoft.Azure.Cosmos.Samples/Tools/Benchmark/v2/ReadStreamExistsV2BenchmarkOperation.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ internal class ReadStreamExistsV2BenchmarkOperation : IBenchmarkOperation
2525

2626
private readonly DocumentClient documentClient;
2727

28+
public BenchmarkOperationType OperationType => BenchmarkOperationType.Read;
29+
2830
public ReadStreamExistsV2BenchmarkOperation(
2931
DocumentClient documentClient,
3032
string dbName,
@@ -84,7 +86,7 @@ public async Task PrepareAsync()
8486

8587
if (itemResponse.StatusCode != HttpStatusCode.Created)
8688
{
87-
throw new Exception($"Create failed with statuscode: {itemResponse.StatusCode}");
89+
throw new Exception($"Create failed with status code: {itemResponse.StatusCode}");
8890
}
8991
}
9092
}

Microsoft.Azure.Cosmos.Samples/Tools/Benchmark/v2/ReadTExistsV2BenchmarkOperation.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ internal class ReadTExistsV2BenchmarkOperation : IBenchmarkOperation
2626

2727
private readonly DocumentClient documentClient;
2828

29+
public BenchmarkOperationType OperationType => BenchmarkOperationType.Read;
30+
2931
public ReadTExistsV2BenchmarkOperation(
3032
DocumentClient documentClient,
3133
string dbName,
@@ -85,7 +87,7 @@ public async Task PrepareAsync()
8587

8688
if (itemResponse.StatusCode != HttpStatusCode.Created)
8789
{
88-
throw new Exception($"Create failed with statuscode: {itemResponse.StatusCode}");
90+
throw new Exception($"Create failed with status code: {itemResponse.StatusCode}");
8991
}
9092
}
9193
}

Microsoft.Azure.Cosmos.Samples/Tools/Benchmark/v3/InsertV3BenchmarkOperation.cs

+2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ public InsertV3BenchmarkOperation(
3737
this.sampleJObject = JsonHelper.Deserialize<Dictionary<string, object>>(sampleJson);
3838
}
3939

40+
public BenchmarkOperationType OperationType => BenchmarkOperationType.Insert;
41+
4042
public async Task<OperationResult> ExecuteOnceAsync()
4143
{
4244
using (MemoryStream input = JsonHelper.ToStream(this.sampleJObject))

Microsoft.Azure.Cosmos.Samples/Tools/Benchmark/v3/QueryTV3BenchmarkOperation.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ internal abstract class QueryTV3BenchmarkOperation : IBenchmarkOperation
3131
public abstract bool IsPaginationEnabled { get; }
3232
public abstract bool IsQueryStream { get; }
3333

34+
public BenchmarkOperationType OperationType => BenchmarkOperationType.Query;
35+
3436
protected string executionItemId = null;
3537
protected string executionPartitionKey = null;
3638

@@ -291,7 +293,7 @@ public virtual async Task PrepareAsync()
291293

292294
if (itemResponse.StatusCode != HttpStatusCode.Created)
293295
{
294-
throw new Exception($"Create failed with statuscode: {itemResponse.StatusCode}");
296+
throw new Exception($"Create failed with status code: {itemResponse.StatusCode}");
295297
}
296298
}
297299
}

Microsoft.Azure.Cosmos.Samples/Tools/Benchmark/v3/ReadFeedStreamV3BenchmarkOperation.cs

+4-2
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ public ReadFeedStreamV3BenchmarkOperation(
3939
this.sampleJObject = JsonHelper.Deserialize<Dictionary<string, object>>(sampleJson);
4040
}
4141

42+
public BenchmarkOperationType OperationType => BenchmarkOperationType.Read;
43+
4244
public async Task<OperationResult> ExecuteOnceAsync()
4345
{
4446
FeedIterator feedIterator = this.container
@@ -50,7 +52,7 @@ public async Task<OperationResult> ExecuteOnceAsync()
5052
ResponseMessage feedResponse = await feedIterator.ReadNextAsync();
5153
if (feedResponse.StatusCode != HttpStatusCode.OK)
5254
{
53-
throw new Exception($"ReadItem failed wth {feedResponse.StatusCode}");
55+
throw new Exception($"ReadItem failed with {feedResponse.StatusCode}");
5456
}
5557

5658
return new OperationResult()
@@ -84,7 +86,7 @@ public async Task PrepareAsync()
8486

8587
if (itemResponse.StatusCode != HttpStatusCode.Created)
8688
{
87-
throw new Exception($"Create failed with statuscode: {itemResponse.StatusCode}");
89+
throw new Exception($"Create failed with status code: {itemResponse.StatusCode}");
8890
}
8991
}
9092
}

Microsoft.Azure.Cosmos.Samples/Tools/Benchmark/v3/ReadNotExistsV3BenchmarkOperation.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ public ReadNotExistsV3BenchmarkOperation(
3232
this.container = cosmosClient.GetContainer(this.databsaeName, this.containerName);
3333
}
3434

35+
public BenchmarkOperationType OperationType => BenchmarkOperationType.Read;
36+
3537
public async Task<OperationResult> ExecuteOnceAsync()
3638
{
3739
using (ResponseMessage itemResponse = await this.container.ReadItemStreamAsync(
@@ -40,7 +42,7 @@ public async Task<OperationResult> ExecuteOnceAsync()
4042
{
4143
if (itemResponse.StatusCode != HttpStatusCode.NotFound)
4244
{
43-
throw new Exception($"ReadItem failed wth {itemResponse.StatusCode}");
45+
throw new Exception($"ReadItem failed with {itemResponse.StatusCode}");
4446
}
4547

4648
return new OperationResult()

Microsoft.Azure.Cosmos.Samples/Tools/Benchmark/v3/ReadStreamExistsV3BenchmarkOperation.cs

+2
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ public ReadStreamExistsV3BenchmarkOperation(
3939
this.sampleJObject = JsonHelper.Deserialize<Dictionary<string, object>>(sampleJson);
4040
}
4141

42+
public BenchmarkOperationType OperationType => BenchmarkOperationType.Read;
43+
4244
public async Task<OperationResult> ExecuteOnceAsync()
4345
{
4446
using (ResponseMessage itemResponse = await this.container.ReadItemStreamAsync(

Microsoft.Azure.Cosmos.Samples/Tools/Benchmark/v3/ReadStreamExistsWithDiagnosticsV3BenchmarkOperation.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ public ReadStreamExistsWithDiagnosticsV3BenchmarkOperation(
3939
this.sampleJObject = JsonHelper.Deserialize<Dictionary<string, object>>(sampleJson);
4040
}
4141

42+
public BenchmarkOperationType OperationType => BenchmarkOperationType.Read;
43+
4244
public async Task<OperationResult> ExecuteOnceAsync()
4345
{
4446
using (ResponseMessage itemResponse = await this.container.ReadItemStreamAsync(
@@ -47,7 +49,7 @@ public async Task<OperationResult> ExecuteOnceAsync()
4749
{
4850
if (itemResponse.StatusCode != HttpStatusCode.OK)
4951
{
50-
throw new Exception($"ReadItem failed wth {itemResponse.StatusCode}");
52+
throw new Exception($"ReadItem failed with {itemResponse.StatusCode}");
5153
}
5254

5355
string diagnostics = itemResponse.Diagnostics.ToString();

Microsoft.Azure.Cosmos.Samples/Tools/Benchmark/v3/ReadTExistsV3BenchmarkOperation.cs

+4-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ namespace CosmosBenchmark
1010
using System.Net;
1111
using System.Threading.Tasks;
1212
using Microsoft.Azure.Cosmos;
13-
using Newtonsoft.Json.Linq;
1413

1514
internal class ReadTExistsV3BenchmarkOperation : IBenchmarkOperation
1615
{
@@ -40,14 +39,16 @@ public ReadTExistsV3BenchmarkOperation(
4039
this.sampleJObject = JsonHelper.Deserialize<Dictionary<string, object>>(sampleJson);
4140
}
4241

42+
public BenchmarkOperationType OperationType => BenchmarkOperationType.Read;
43+
4344
public async Task<OperationResult> ExecuteOnceAsync()
4445
{
4546
ItemResponse<Dictionary<string, object>> itemResponse = await this.container.ReadItemAsync<Dictionary<string, object>>(
4647
this.nextExecutionItemId,
4748
new PartitionKey(this.nextExecutionItemPartitionKey));
4849
if (itemResponse.StatusCode != HttpStatusCode.OK)
4950
{
50-
throw new Exception($"ReadItem failed wth {itemResponse.StatusCode}");
51+
throw new Exception($"ReadItem failed with {itemResponse.StatusCode}");
5152
}
5253

5354
return new OperationResult()
@@ -81,7 +82,7 @@ public async Task PrepareAsync()
8182

8283
if (itemResponse.StatusCode != HttpStatusCode.Created)
8384
{
84-
throw new Exception($"Create failed with statuscode: {itemResponse.StatusCode}");
85+
throw new Exception($"Create failed with status code: {itemResponse.StatusCode}");
8586
}
8687
}
8788
}

0 commit comments

Comments
 (0)