Skip to content

Commit

Permalink
[Internal] Category: Refactors Cosmos benchmark operations (#3961)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
M-Lipin authored Jul 14, 2023
1 parent d7fc282 commit 8d2691b
Show file tree
Hide file tree
Showing 17 changed files with 73 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------

namespace CosmosBenchmark
{
/// <summary>
/// Benchmark operation type.
/// </summary>
public enum BenchmarkOperationType
{
Read,
Insert,
Query
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,8 @@ namespace CosmosBenchmark
{
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using Microsoft.Azure.Cosmos;
using Newtonsoft.Json.Linq;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,26 @@ namespace CosmosBenchmark
{
using System.Threading.Tasks;

/// <summary>
/// Represents the Benchmark operation.
/// </summary>
internal interface IBenchmarkOperation
{
Task PrepareAsync();
/// <summary>
/// Benchmark operation type.
/// </summary>
BenchmarkOperationType OperationType { get; }

/// <summary>
/// Executes Benchmark operation once asynchronously.
/// </summary>
/// <returns>The operation result wrapped by task.</returns>
Task<OperationResult> ExecuteOnceAsync();

/// <summary>
/// Prepares Benchmark operation asynchronously.
/// </summary>
/// <returns>The task related to method's work.</returns>
Task PrepareAsync();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public InsertV2BenchmarkOperation(
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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ public QueryStreamSinglePkV2BenchmarkOperation(
this.containerUri = UriFactory.CreateDocumentCollectionUri(this.databsaeName, this.containerName);
}

public BenchmarkOperationType OperationType => BenchmarkOperationType.Query;

public async Task<OperationResult> ExecuteOnceAsync()
{
IDocumentQuery<dynamic> query = this.documentClient.CreateDocumentQuery<dynamic>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ public QueryTSinglePkV2BenchmarkOperation(
this.sampleJObject[this.partitionKeyPath] = this.executionItemPartitionKey;
}

public BenchmarkOperationType OperationType => BenchmarkOperationType.Query;

public async Task<OperationResult> ExecuteOnceAsync()
{
IDocumentQuery<Dictionary<string, object>> query = this.documentClient.CreateDocumentQuery<Dictionary<string, object>>(
Expand Down Expand Up @@ -107,7 +109,7 @@ public async Task PrepareAsync()
new RequestOptions() { PartitionKey = new PartitionKey(this.executionItemPartitionKey) });
if (itemResponse.StatusCode != HttpStatusCode.Created)
{
throw new Exception($"Create failed with statuscode: {itemResponse.StatusCode}");
throw new Exception($"Create failed with status code: {itemResponse.StatusCode}");
}

this.initialized = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ public ReadFeedStreamV2BenchmarkOperation(
this.sampleJObject = JsonHelper.Deserialize<Dictionary<string, object>>(sampleJson);
}

public BenchmarkOperationType OperationType => BenchmarkOperationType.Read;

public async Task<OperationResult> ExecuteOnceAsync()
{
Uri containerUri = UriFactory.CreateDocumentCollectionUri(this.databsaeName, this.containerName);
Expand Down Expand Up @@ -75,7 +77,7 @@ public async Task PrepareAsync()
new RequestOptions() { PartitionKey = new PartitionKey(this.nextExecutionItemPartitionKey) });
if (itemResponse.StatusCode != HttpStatusCode.Created)
{
throw new Exception($"Create failed with statuscode: {itemResponse.StatusCode}");
throw new Exception($"Create failed with status code: {itemResponse.StatusCode}");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ internal class ReadNotExistsV2BenchmarkOperation : IBenchmarkOperation

private readonly DocumentClient documentClient;

public BenchmarkOperationType OperationType => BenchmarkOperationType.Read;

public ReadNotExistsV2BenchmarkOperation(
DocumentClient documentClient,
string dbName,
Expand Down Expand Up @@ -52,7 +54,7 @@ public async Task<OperationResult> ExecuteOnceAsync()
{
if (dce.StatusCode != HttpStatusCode.NotFound)
{
throw new Exception($"ReadItem failed wth {dce?.StatusCode} {dce?.ToString()}");
throw new Exception($"ReadItem failed with {dce?.StatusCode} {dce?.ToString()}");
}

return new OperationResult()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ internal class ReadStreamExistsV2BenchmarkOperation : IBenchmarkOperation

private readonly DocumentClient documentClient;

public BenchmarkOperationType OperationType => BenchmarkOperationType.Read;

public ReadStreamExistsV2BenchmarkOperation(
DocumentClient documentClient,
string dbName,
Expand Down Expand Up @@ -84,7 +86,7 @@ public async Task PrepareAsync()

if (itemResponse.StatusCode != HttpStatusCode.Created)
{
throw new Exception($"Create failed with statuscode: {itemResponse.StatusCode}");
throw new Exception($"Create failed with status code: {itemResponse.StatusCode}");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ internal class ReadTExistsV2BenchmarkOperation : IBenchmarkOperation

private readonly DocumentClient documentClient;

public BenchmarkOperationType OperationType => BenchmarkOperationType.Read;

public ReadTExistsV2BenchmarkOperation(
DocumentClient documentClient,
string dbName,
Expand Down Expand Up @@ -85,7 +87,7 @@ public async Task PrepareAsync()

if (itemResponse.StatusCode != HttpStatusCode.Created)
{
throw new Exception($"Create failed with statuscode: {itemResponse.StatusCode}");
throw new Exception($"Create failed with status code: {itemResponse.StatusCode}");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public InsertV3BenchmarkOperation(
this.sampleJObject = JsonHelper.Deserialize<Dictionary<string, object>>(sampleJson);
}

public BenchmarkOperationType OperationType => BenchmarkOperationType.Insert;

public async Task<OperationResult> ExecuteOnceAsync()
{
using (MemoryStream input = JsonHelper.ToStream(this.sampleJObject))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ internal abstract class QueryTV3BenchmarkOperation : IBenchmarkOperation
public abstract bool IsPaginationEnabled { get; }
public abstract bool IsQueryStream { get; }

public BenchmarkOperationType OperationType => BenchmarkOperationType.Query;

protected string executionItemId = null;
protected string executionPartitionKey = null;

Expand Down Expand Up @@ -291,7 +293,7 @@ public virtual async Task PrepareAsync()

if (itemResponse.StatusCode != HttpStatusCode.Created)
{
throw new Exception($"Create failed with statuscode: {itemResponse.StatusCode}");
throw new Exception($"Create failed with status code: {itemResponse.StatusCode}");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public ReadFeedStreamV3BenchmarkOperation(
this.sampleJObject = JsonHelper.Deserialize<Dictionary<string, object>>(sampleJson);
}

public BenchmarkOperationType OperationType => BenchmarkOperationType.Read;

public async Task<OperationResult> ExecuteOnceAsync()
{
FeedIterator feedIterator = this.container
Expand All @@ -50,7 +52,7 @@ public async Task<OperationResult> ExecuteOnceAsync()
ResponseMessage feedResponse = await feedIterator.ReadNextAsync();
if (feedResponse.StatusCode != HttpStatusCode.OK)
{
throw new Exception($"ReadItem failed wth {feedResponse.StatusCode}");
throw new Exception($"ReadItem failed with {feedResponse.StatusCode}");
}

return new OperationResult()
Expand Down Expand Up @@ -84,7 +86,7 @@ public async Task PrepareAsync()

if (itemResponse.StatusCode != HttpStatusCode.Created)
{
throw new Exception($"Create failed with statuscode: {itemResponse.StatusCode}");
throw new Exception($"Create failed with status code: {itemResponse.StatusCode}");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ public ReadNotExistsV3BenchmarkOperation(
this.container = cosmosClient.GetContainer(this.databsaeName, this.containerName);
}

public BenchmarkOperationType OperationType => BenchmarkOperationType.Read;

public async Task<OperationResult> ExecuteOnceAsync()
{
using (ResponseMessage itemResponse = await this.container.ReadItemStreamAsync(
Expand All @@ -40,7 +42,7 @@ public async Task<OperationResult> ExecuteOnceAsync()
{
if (itemResponse.StatusCode != HttpStatusCode.NotFound)
{
throw new Exception($"ReadItem failed wth {itemResponse.StatusCode}");
throw new Exception($"ReadItem failed with {itemResponse.StatusCode}");
}

return new OperationResult()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public ReadStreamExistsV3BenchmarkOperation(
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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public ReadStreamExistsWithDiagnosticsV3BenchmarkOperation(
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(
Expand All @@ -47,7 +49,7 @@ public async Task<OperationResult> ExecuteOnceAsync()
{
if (itemResponse.StatusCode != HttpStatusCode.OK)
{
throw new Exception($"ReadItem failed wth {itemResponse.StatusCode}");
throw new Exception($"ReadItem failed with {itemResponse.StatusCode}");
}

string diagnostics = itemResponse.Diagnostics.ToString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ namespace CosmosBenchmark
using System.Net;
using System.Threading.Tasks;
using Microsoft.Azure.Cosmos;
using Newtonsoft.Json.Linq;

internal class ReadTExistsV3BenchmarkOperation : IBenchmarkOperation
{
Expand Down Expand Up @@ -40,14 +39,16 @@ public ReadTExistsV3BenchmarkOperation(
this.sampleJObject = JsonHelper.Deserialize<Dictionary<string, object>>(sampleJson);
}

public BenchmarkOperationType OperationType => BenchmarkOperationType.Read;

public async Task<OperationResult> ExecuteOnceAsync()
{
ItemResponse<Dictionary<string, object>> itemResponse = await this.container.ReadItemAsync<Dictionary<string, object>>(
this.nextExecutionItemId,
new PartitionKey(this.nextExecutionItemPartitionKey));
if (itemResponse.StatusCode != HttpStatusCode.OK)
{
throw new Exception($"ReadItem failed wth {itemResponse.StatusCode}");
throw new Exception($"ReadItem failed with {itemResponse.StatusCode}");
}

return new OperationResult()
Expand Down Expand Up @@ -81,7 +82,7 @@ public async Task PrepareAsync()

if (itemResponse.StatusCode != HttpStatusCode.Created)
{
throw new Exception($"Create failed with statuscode: {itemResponse.StatusCode}");
throw new Exception($"Create failed with status code: {itemResponse.StatusCode}");
}
}
}
Expand Down

0 comments on commit 8d2691b

Please sign in to comment.