Skip to content

Commit e073203

Browse files
committed
More fixes
1 parent a2d61dd commit e073203

File tree

2 files changed

+50
-32
lines changed

2 files changed

+50
-32
lines changed

samples/documentdb-benchmark/App.config

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,18 @@
77
<gcServer enabled="true"/>
88
</runtime>
99
<appSettings>
10-
<add key="EndPointUrl" value="https://FILLIN.documents.azure.com:443/"/>
11-
<add key="AuthorizationKey" value="FILLIN"/>
10+
<add key="EndPointUrl" value="https://FILLME.azure.com:443/"/>
11+
<add key="AuthorizationKey" value="FILLME"/>
1212

1313
<add key="DatabaseName" value="db"/>
14-
<add key="CollectionName" value="testdata"/>
14+
<add key="CollectionName" value="data"/>
1515
<add key="MetricCollectionName" value="metrics"/>
1616

1717
<add key="CollectionThroughput" value="50000"/>
18-
<add key="ShouldDeleteAndRecreateDatabaseAndCollection" value="false"/>
18+
<add key="ShouldCleanupOnStart" value="false"/>
19+
<add key="ShouldCleanupOnFinish" value="false"/>
1920
<add key="DegreeOfParallelism" value="500"/>
20-
<add key="NumberOfDocumentsToInsert" value="100000"/>
21+
<add key="NumberOfDocumentsToInsert" value="10000"/>
2122

2223
<add key="CollectionPartitionKey" value="/partitionKey"/>
2324
<add key="DocumentTemplateFile" value="Player.json"/>

samples/documentdb-benchmark/Program.cs

Lines changed: 44 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public sealed class Program
2929
private static readonly string DatabaseName = ConfigurationManager.AppSettings["DatabaseName"];
3030
private static readonly string DataCollectionName = ConfigurationManager.AppSettings["CollectionName"];
3131
private static readonly string MetricCollectionName = ConfigurationManager.AppSettings["MetricCollectionName"];
32+
private static readonly int CollectionThroughput = int.Parse(ConfigurationManager.AppSettings["CollectionThroughput"]);
3233

3334
private static readonly ConnectionPolicy ConnectionPolicy = new ConnectionPolicy { ConnectionMode = ConnectionMode.Gateway, ConnectionProtocol = Protocol.Https, RequestTimeout = new TimeSpan(1, 0, 0) };
3435

@@ -37,7 +38,7 @@ public sealed class Program
3738
private static readonly string InstanceId = Dns.GetHostEntry("LocalHost").HostName + Process.GetCurrentProcess().Id;
3839
private const int MinThreadPoolSize = 100;
3940

40-
private volatile int pendingTaskCount;
41+
private int pendingTaskCount;
4142
private long documentsInserted;
4243
private ConcurrentDictionary<int, double> requestUnitsConsumed = new ConcurrentDictionary<int, double>();
4344
private DocumentClient client;
@@ -100,8 +101,6 @@ public static void Main(string[] args)
100101

101102
finally
102103
{
103-
Console.WriteLine("End of samples, press any key to exit.");
104-
Console.ReadKey();
105104
}
106105
}
107106

@@ -113,7 +112,7 @@ private async Task RunAsync()
113112
{
114113
DocumentCollection dataCollection = GetCollectionIfExists(DatabaseName, DataCollectionName);
115114

116-
if (bool.Parse(ConfigurationManager.AppSettings["ShouldDeleteAndRecreateDatabaseAndCollection"]) || dataCollection == null)
115+
if (bool.Parse(ConfigurationManager.AppSettings["ShouldCleanupOnStart"]) || dataCollection == null)
117116
{
118117
Database database = GetDatabaseIfExists(DatabaseName);
119118
if (database != null)
@@ -124,8 +123,14 @@ private async Task RunAsync()
124123
Console.WriteLine("Creating database {0}", DatabaseName);
125124
database = await client.CreateDatabaseAsync(new Database { Id = DatabaseName });
126125

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);
129134
}
130135

131136
DocumentCollection metricCollection = GetCollectionIfExists(DatabaseName, MetricCollectionName);
@@ -140,7 +145,7 @@ private async Task RunAsync()
140145
metricCollectionDefinition.Id = MetricCollectionName;
141146
metricCollectionDefinition.DefaultTimeToLive = defaultTimeToLive;
142147

143-
await ExecuteWithRetries<ResourceResponse<DocumentCollection>>(
148+
metricCollection = await ExecuteWithRetries<ResourceResponse<DocumentCollection>>(
144149
this.client,
145150
() => client.CreateDocumentCollectionAsync(
146151
UriFactory.CreateDatabaseUri(DatabaseName),
@@ -155,7 +160,7 @@ await ExecuteWithRetries<ResourceResponse<DocumentCollection>>(
155160
}
156161

157162
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"]);
159164

160165
pendingTaskCount = TaskCount;
161166
var tasks = new List<Task>();
@@ -168,14 +173,22 @@ await ExecuteWithRetries<ResourceResponse<DocumentCollection>>(
168173
}
169174

170175
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+
}
171185
}
172186

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)
174188
{
175189
requestUnitsConsumed[taskId] = 0;
176190
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);
179192

180193
for (var i = 0; i < numberOfDocumentsToInsert; i++)
181194
{
@@ -186,18 +199,12 @@ private async Task InsertDocument(int taskId, DocumentClient client, DocumentCol
186199
{
187200
ResourceResponse<Document> response = await ExecuteWithRetries<ResourceResponse<Document>>(
188201
client,
189-
() => client.UpsertDocumentAsync(
202+
() => client.CreateDocumentAsync(
190203
UriFactory.CreateDocumentCollectionUri(DatabaseName, DataCollectionName),
191204
newDictionary,
192205
new RequestOptions() { }));
193206

194207
string partition = response.SessionToken.Split(':')[0];
195-
if (!perPartitionCount.ContainsKey(partition))
196-
{
197-
perPartitionCount[partition] = 0;
198-
}
199-
200-
perPartitionCount[partition]++;
201208
requestUnitsConsumed[taskId] += response.RequestCharge;
202209
Interlocked.Increment(ref this.documentsInserted);
203210
}
@@ -297,22 +304,27 @@ await ExecuteWithRetries<ResourceResponse<Document>>(
297304
/// Create a partitioned collection.
298305
/// </summary>
299306
/// <returns>The created collection.</returns>
300-
private async Task<DocumentCollection> CreatePartitionedCollectionAsync(Database database)
307+
private async Task<DocumentCollection> CreatePartitionedCollectionAsync(string databaseName, string collectionName)
301308
{
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);
307310

308311
DocumentCollection collection = new DocumentCollection();
309-
collection.Id = DataCollectionName;
312+
collection.Id = collectionName;
310313
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();
312321

313322
return await ExecuteWithRetries<ResourceResponse<DocumentCollection>>(
314323
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 }));
316328
}
317329

318330
/// <summary>
@@ -330,6 +342,11 @@ private Database GetDatabaseIfExists(string databaseName)
330342
/// <returns>The requested collection</returns>
331343
private DocumentCollection GetCollectionIfExists(string databaseName, string collectionName)
332344
{
345+
if (GetDatabaseIfExists(databaseName) == null)
346+
{
347+
return null;
348+
}
349+
333350
return client.CreateDocumentCollectionQuery(UriFactory.CreateDatabaseUri(databaseName))
334351
.Where(c => c.Id == collectionName).AsEnumerable().FirstOrDefault();
335352
}

0 commit comments

Comments
 (0)