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
Lines changed: 16 additions & 0 deletions
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

Lines changed: 0 additions & 3 deletions
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

Lines changed: 17 additions & 1 deletion
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

Lines changed: 2 additions & 0 deletions
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

Lines changed: 2 additions & 0 deletions
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

Lines changed: 3 additions & 1 deletion
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

Lines changed: 3 additions & 1 deletion
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

Lines changed: 3 additions & 1 deletion
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

Lines changed: 3 additions & 1 deletion
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

Lines changed: 3 additions & 1 deletion
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
}

0 commit comments

Comments
 (0)