55namespace CosmosBenchmark
66{
77 using System ;
8- using System . Net ;
8+ using System . Collections . Generic ;
9+ using System . IO ;
910 using System . Threading . Tasks ;
1011 using Microsoft . Azure . Cosmos ;
1112
1213 internal class CreateItemBenchmarkOperation : IBenchmarkOperation
1314 {
1415 private readonly Container container ;
15- private readonly string pk = "pk_benchmark" ;
16-
17- public CreateItemBenchmarkOperation ( CosmosClient client , string db , string containerName )
16+ private readonly string partitionKeyPath ;
17+ private readonly Dictionary < string , object > sampleJObject ;
18+ private readonly string databaseName ;
19+ private readonly string containerName ;
20+
21+ public CreateItemBenchmarkOperation (
22+ CosmosClient cosmosClient ,
23+ string dbName ,
24+ string containerName ,
25+ string partitionKeyPath ,
26+ string sampleJson )
1827 {
19- this . container = client . GetContainer ( db , containerName ) ;
28+ this . databaseName = dbName ;
29+ this . containerName = containerName ;
30+ this . container = cosmosClient . GetContainer ( this . databaseName , this . containerName ) ;
31+ this . partitionKeyPath = partitionKeyPath . Replace ( "/" , "" ) ;
32+ this . sampleJObject = JsonHelper . Deserialize < Dictionary < string , object > > ( sampleJson ) ;
2033 }
2134
22- public async Task PrepareAsync ( ) { }
35+ public BenchmarkOperationType OperationType => BenchmarkOperationType . Insert ;
2336
24- public async Task < OperationResult > ExecuteOnceAsync ( )
37+ public Task PrepareAsync ( )
2538 {
26- CosmosIntegrationTestObject item = new CosmosIntegrationTestObject
27- {
28- Id = Guid . NewGuid ( ) . ToString ( ) ,
29- Pk = this . pk ,
30- Other = "Create Test"
31- } ;
32-
33- ItemResponse < CosmosIntegrationTestObject > response = await this . container . CreateItemAsync ( item , new PartitionKey ( item . Pk ) ) ;
34- if ( response . StatusCode != HttpStatusCode . Created )
35- {
36- throw new Exception ( $ "Failed to create item: { item . Id } ") ;
37- }
38-
39- return new OperationResult { OperationType = BenchmarkOperationType . Insert } ;
39+ this . sampleJObject [ "id" ] = Guid . NewGuid ( ) . ToString ( ) ;
40+ this . sampleJObject [ this . partitionKeyPath ] = Guid . NewGuid ( ) . ToString ( ) ;
41+ return Task . CompletedTask ;
4042 }
4143
42- public BenchmarkOperationType OperationType => BenchmarkOperationType . Insert ;
43-
44- public class CosmosIntegrationTestObject
44+ public async Task < OperationResult > ExecuteOnceAsync ( )
4545 {
46- public string Id { get ; set ; }
47- public string Pk { get ; set ; }
48- public string Other { get ; set ; }
46+ PartitionKey partitionKey = new PartitionKey ( this . sampleJObject [ this . partitionKeyPath ] . ToString ( ) ) ;
47+ ItemResponse < Dictionary < string , object > > response = await this . container . CreateItemAsync ( this . sampleJObject , partitionKey : partitionKey ) ;
48+
49+ return new OperationResult
50+ {
51+ DatabseName = this . databaseName ,
52+ ContainerName = this . containerName ,
53+ OperationType = this . OperationType ,
54+ RuCharges = response . RequestCharge ,
55+ CosmosDiagnostics = response . Diagnostics ,
56+ LazyDiagnostics = ( ) => response . Diagnostics . ToString ( ) ,
57+ } ;
4958 }
5059 }
51- }
60+ }
0 commit comments