@@ -29,6 +29,7 @@ public sealed class Program
29
29
private static readonly string DatabaseName = ConfigurationManager . AppSettings [ "DatabaseName" ] ;
30
30
private static readonly string DataCollectionName = ConfigurationManager . AppSettings [ "CollectionName" ] ;
31
31
private static readonly string MetricCollectionName = ConfigurationManager . AppSettings [ "MetricCollectionName" ] ;
32
+ private static readonly int CollectionThroughput = int . Parse ( ConfigurationManager . AppSettings [ "CollectionThroughput" ] ) ;
32
33
33
34
private static readonly ConnectionPolicy ConnectionPolicy = new ConnectionPolicy { ConnectionMode = ConnectionMode . Gateway , ConnectionProtocol = Protocol . Https , RequestTimeout = new TimeSpan ( 1 , 0 , 0 ) } ;
34
35
@@ -37,7 +38,7 @@ public sealed class Program
37
38
private static readonly string InstanceId = Dns . GetHostEntry ( "LocalHost" ) . HostName + Process . GetCurrentProcess ( ) . Id ;
38
39
private const int MinThreadPoolSize = 100 ;
39
40
40
- private volatile int pendingTaskCount ;
41
+ private int pendingTaskCount ;
41
42
private long documentsInserted ;
42
43
private ConcurrentDictionary < int , double > requestUnitsConsumed = new ConcurrentDictionary < int , double > ( ) ;
43
44
private DocumentClient client ;
@@ -100,8 +101,6 @@ public static void Main(string[] args)
100
101
101
102
finally
102
103
{
103
- Console . WriteLine ( "End of samples, press any key to exit." ) ;
104
- Console . ReadKey ( ) ;
105
104
}
106
105
}
107
106
@@ -113,7 +112,7 @@ private async Task RunAsync()
113
112
{
114
113
DocumentCollection dataCollection = GetCollectionIfExists ( DatabaseName , DataCollectionName ) ;
115
114
116
- if ( bool . Parse ( ConfigurationManager . AppSettings [ "ShouldDeleteAndRecreateDatabaseAndCollection " ] ) || dataCollection == null )
115
+ if ( bool . Parse ( ConfigurationManager . AppSettings [ "ShouldCleanupOnStart " ] ) || dataCollection == null )
117
116
{
118
117
Database database = GetDatabaseIfExists ( DatabaseName ) ;
119
118
if ( database != null )
@@ -124,8 +123,14 @@ private async Task RunAsync()
124
123
Console . WriteLine ( "Creating database {0}" , DatabaseName ) ;
125
124
database = await client . CreateDatabaseAsync ( new Database { Id = DatabaseName } ) ;
126
125
127
- Console . WriteLine ( "Creating collection {0}" , DataCollectionName ) ;
128
- dataCollection = await this . CreatePartitionedCollectionAsync ( database ) ;
126
+ Console . WriteLine ( "Creating collection {0} with {1} RU/s" , DataCollectionName , CollectionThroughput ) ;
127
+ dataCollection = await this . CreatePartitionedCollectionAsync ( DatabaseName , DataCollectionName ) ;
128
+ }
129
+ else
130
+ {
131
+ OfferV2 offer = ( OfferV2 ) client . CreateOfferQuery ( ) . Where ( o => o . ResourceLink == dataCollection . SelfLink ) . AsEnumerable ( ) . FirstOrDefault ( ) ;
132
+ int throughput = offer . Content . OfferThroughput ;
133
+ Console . WriteLine ( "Found collection {0} with {1} RU/s" , DataCollectionName , CollectionThroughput ) ;
129
134
}
130
135
131
136
DocumentCollection metricCollection = GetCollectionIfExists ( DatabaseName , MetricCollectionName ) ;
@@ -140,7 +145,7 @@ private async Task RunAsync()
140
145
metricCollectionDefinition . Id = MetricCollectionName ;
141
146
metricCollectionDefinition . DefaultTimeToLive = defaultTimeToLive ;
142
147
143
- await ExecuteWithRetries < ResourceResponse < DocumentCollection > > (
148
+ metricCollection = await ExecuteWithRetries < ResourceResponse < DocumentCollection > > (
144
149
this . client ,
145
150
( ) => client . CreateDocumentCollectionAsync (
146
151
UriFactory . CreateDatabaseUri ( DatabaseName ) ,
@@ -155,7 +160,7 @@ await ExecuteWithRetries<ResourceResponse<DocumentCollection>>(
155
160
}
156
161
157
162
Console . WriteLine ( "Starting Inserts with {0} tasks" , TaskCount ) ;
158
- Dictionary < string , object > sampleDocument = JsonConvert . DeserializeObject < Dictionary < string , object > > ( File . ReadAllText ( ConfigurationManager . AppSettings [ "DocumentTemplateFile" ] ) ) ;
163
+ string sampleDocument = File . ReadAllText ( ConfigurationManager . AppSettings [ "DocumentTemplateFile" ] ) ;
159
164
160
165
pendingTaskCount = TaskCount ;
161
166
var tasks = new List < Task > ( ) ;
@@ -168,14 +173,22 @@ await ExecuteWithRetries<ResourceResponse<DocumentCollection>>(
168
173
}
169
174
170
175
await Task . WhenAll ( tasks ) ;
176
+
177
+ if ( bool . Parse ( ConfigurationManager . AppSettings [ "ShouldCleanupOnFinish" ] ) )
178
+ {
179
+ Console . WriteLine ( "Deleting Database {0}" , DatabaseName ) ;
180
+ await ExecuteWithRetries < ResourceResponse < Database > > (
181
+ this . client ,
182
+ ( ) => client . DeleteDatabaseAsync ( UriFactory . CreateDatabaseUri ( DatabaseName ) ) ,
183
+ true ) ;
184
+ }
171
185
}
172
186
173
- private async Task InsertDocument ( int taskId , DocumentClient client , DocumentCollection collection , IDictionary < string , object > sampleDocument , long numberOfDocumentsToInsert )
187
+ private async Task InsertDocument ( int taskId , DocumentClient client , DocumentCollection collection , string sampleJson , long numberOfDocumentsToInsert )
174
188
{
175
189
requestUnitsConsumed [ taskId ] = 0 ;
176
190
string partitionKeyProperty = collection . PartitionKey . Paths [ 0 ] . Replace ( "/" , "" ) ;
177
- Dictionary < string , int > perPartitionCount = new Dictionary < string , int > ( ) ;
178
- Dictionary < string , object > newDictionary = new Dictionary < string , object > ( sampleDocument ) ;
191
+ Dictionary < string , object > newDictionary = JsonConvert . DeserializeObject < Dictionary < string , object > > ( sampleJson ) ;
179
192
180
193
for ( var i = 0 ; i < numberOfDocumentsToInsert ; i ++ )
181
194
{
@@ -186,18 +199,12 @@ private async Task InsertDocument(int taskId, DocumentClient client, DocumentCol
186
199
{
187
200
ResourceResponse < Document > response = await ExecuteWithRetries < ResourceResponse < Document > > (
188
201
client ,
189
- ( ) => client . UpsertDocumentAsync (
202
+ ( ) => client . CreateDocumentAsync (
190
203
UriFactory . CreateDocumentCollectionUri ( DatabaseName , DataCollectionName ) ,
191
204
newDictionary ,
192
205
new RequestOptions ( ) { } ) ) ;
193
206
194
207
string partition = response . SessionToken . Split ( ':' ) [ 0 ] ;
195
- if ( ! perPartitionCount . ContainsKey ( partition ) )
196
- {
197
- perPartitionCount [ partition ] = 0 ;
198
- }
199
-
200
- perPartitionCount [ partition ] ++ ;
201
208
requestUnitsConsumed [ taskId ] += response . RequestCharge ;
202
209
Interlocked . Increment ( ref this . documentsInserted ) ;
203
210
}
@@ -297,22 +304,27 @@ await ExecuteWithRetries<ResourceResponse<Document>>(
297
304
/// Create a partitioned collection.
298
305
/// </summary>
299
306
/// <returns>The created collection.</returns>
300
- private async Task < DocumentCollection > CreatePartitionedCollectionAsync ( Database database )
307
+ private async Task < DocumentCollection > CreatePartitionedCollectionAsync ( string databaseName , string collectionName )
301
308
{
302
- DocumentCollection existingCollection = client . CreateDocumentCollectionQuery ( database . SelfLink ) . Where ( c => c . Id == DataCollectionName ) . AsEnumerable ( ) . FirstOrDefault ( ) ;
303
- if ( existingCollection != null )
304
- {
305
- return existingCollection ;
306
- }
309
+ DocumentCollection existingCollection = GetCollectionIfExists ( databaseName , collectionName ) ;
307
310
308
311
DocumentCollection collection = new DocumentCollection ( ) ;
309
- collection . Id = DataCollectionName ;
312
+ collection . Id = collectionName ;
310
313
collection . PartitionKey . Paths . Add ( ConfigurationManager . AppSettings [ "CollectionPartitionKey" ] ) ;
311
- int collectionThroughput = int . Parse ( ConfigurationManager . AppSettings [ "CollectionThroughput" ] ) ;
314
+
315
+ // Show user cost of running this test
316
+ double estimatedCostPerMonth = 0.06 * CollectionThroughput ;
317
+ double estimatedCostPerHour = estimatedCostPerMonth / ( 24 * 30 ) ;
318
+ Console . WriteLine ( "The collection will cost an estimated ${0} per hour (${1} per month)" , estimatedCostPerHour , estimatedCostPerMonth ) ;
319
+ Console . WriteLine ( "Press enter to continue ..." ) ;
320
+ Console . ReadLine ( ) ;
312
321
313
322
return await ExecuteWithRetries < ResourceResponse < DocumentCollection > > (
314
323
this . client ,
315
- ( ) => client . CreateDocumentCollectionAsync ( database . SelfLink , collection , new RequestOptions { OfferThroughput = collectionThroughput } ) ) ;
324
+ ( ) => client . CreateDocumentCollectionAsync (
325
+ UriFactory . CreateDatabaseUri ( databaseName ) ,
326
+ collection ,
327
+ new RequestOptions { OfferThroughput = CollectionThroughput } ) ) ;
316
328
}
317
329
318
330
/// <summary>
@@ -330,6 +342,11 @@ private Database GetDatabaseIfExists(string databaseName)
330
342
/// <returns>The requested collection</returns>
331
343
private DocumentCollection GetCollectionIfExists ( string databaseName , string collectionName )
332
344
{
345
+ if ( GetDatabaseIfExists ( databaseName ) == null )
346
+ {
347
+ return null ;
348
+ }
349
+
333
350
return client . CreateDocumentCollectionQuery ( UriFactory . CreateDatabaseUri ( databaseName ) )
334
351
. Where ( c => c . Id == collectionName ) . AsEnumerable ( ) . FirstOrDefault ( ) ;
335
352
}
0 commit comments