Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Benchmark: Adds a workload to insert and read the inserted item. #4482

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------

namespace CosmosBenchmark
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Azure.Cosmos;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;

internal class InsertReadV3BenchmarkOperation : IBenchmarkOperation
{
private readonly CosmosClient client;
private readonly Container container;
private readonly string partitionKeyPath;
private readonly Dictionary<string, object> sampleJObject;
private readonly string databaseName;
private readonly string containerName;

public InsertReadV3BenchmarkOperation(
CosmosClient cosmosClient,
string dbName,
string containerName,
string partitionKeyPath,
string sampleJson)
{
this.databaseName = dbName;
this.containerName = containerName;
this.client = cosmosClient;

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 async Task<OperationResult> ExecuteOnceAsync()
{
PartitionKey partitionKey = new PartitionKey(this.sampleJObject[this.partitionKeyPath].ToString());
double ruCharges = 0;
CosmosDiagnostics writeDiagnostics = null;
using (MemoryStream input = JsonHelper.ToStream(this.sampleJObject))
{
ResponseMessage itemResponse = await this.container.CreateItemStreamAsync(
input,
partitionKey);

ruCharges = itemResponse.Headers.RequestCharge;
writeDiagnostics = itemResponse.Diagnostics;
System.Buffers.ArrayPool<byte>.Shared.Return(input.GetBuffer());
NaluTripician marked this conversation as resolved.
Show resolved Hide resolved

if (!itemResponse.IsSuccessStatusCode)
{
return new OperationResult()
NaluTripician marked this conversation as resolved.
Show resolved Hide resolved
{
DatabseName = this.databaseName,
ContainerName = this.containerName,
OperationType = this.OperationType,
RuCharges = ruCharges,
CosmosDiagnostics = itemResponse.Diagnostics,
LazyDiagnostics = () => itemResponse.Diagnostics.ToString(),
};
}
}

using ResponseMessage readResponse = await this.container.ReadItemStreamAsync(
this.sampleJObject["id"].ToString(),
partitionKey);
ruCharges += readResponse.Headers.RequestCharge;

return new OperationResult()
{
DatabseName = this.databaseName,
ContainerName = this.containerName,
OperationType = this.OperationType,
RuCharges = ruCharges,
CosmosDiagnostics = readResponse.Diagnostics,
LazyDiagnostics = () => $"read {readResponse.Diagnostics} write: {writeDiagnostics}",
};
}

public Task PrepareAsync()
{
string newPartitionKey = Guid.NewGuid().ToString();
this.sampleJObject["id"] = Guid.NewGuid().ToString();
this.sampleJObject[this.partitionKeyPath] = newPartitionKey;
return Task.CompletedTask;
}
}
}