private async Task <SampleCustomer> UpdateItemAsync(DocumentClient client, Uri collectionUri, SampleCustomer item) { //DeepCopy the item item = Helpers.Clone(item); //Make a change to the item to update. item.Region = Helpers.ParseEndpoint(client.WriteEndpoint); item.UserDefinedId = Helpers.RandomNext(0, 1000); Console.WriteLine($"Update - Name: {item.Name}, City: {item.City}, UserDefId: {item.UserDefinedId}, Region: {item.Region}"); try { var response = await client.ReplaceDocumentAsync(item.SelfLink, item, new RequestOptions { AccessCondition = new AccessCondition { Type = AccessConditionType.IfMatch, Condition = item.ETag } }); return((SampleCustomer)(dynamic)response.Resource); } catch (DocumentClientException ex) { if (ex.StatusCode == HttpStatusCode.PreconditionFailed || ex.StatusCode == HttpStatusCode.NotFound) { //No conflict is induced. return(null); } throw; } }
private async Task <SampleCustomer> InsertItemAsync(DocumentClient client, Uri collectionUri, SampleCustomer item) { //DeepCopy the item item = Helpers.Clone(item); //Update UserDefinedId for each item to random number for Conflict Resolution item.UserDefinedId = Helpers.RandomNext(0, 1000); //Update the write region to the client regions so we know which client wrote the item item.Region = Helpers.ParseEndpoint(client.WriteEndpoint); Console.WriteLine($"Attempting insert - Name: {item.Name}, City: {item.City}, UserDefId: {item.UserDefinedId}, Region: {item.Region}"); try { var response = await client.CreateDocumentAsync(collectionUri, item); return((SampleCustomer)(dynamic)response.Resource); } catch (DocumentClientException ex) { if (ex.StatusCode == System.Net.HttpStatusCode.Conflict) { //Item has already replicated so return null return(null); } throw; } }
private async Task WriteBenchmark(DocumentClient client, string accountType, bool final = false) { string region = Helpers.ParseEndpoint(client.WriteEndpoint); Stopwatch stopwatch = new Stopwatch(); int i = 0; int total = 100; long lt = 0; double ru = 0; Console.WriteLine(); Console.WriteLine($"Test {total} writes against {accountType} account in {region} from West US 2\r\nPress any key to continue\r\n..."); Console.ReadKey(true); for (i = 0; i < total; i++) { SampleCustomer customer = customerGenerator.Generate(); stopwatch.Start(); ResourceResponse <Document> response = await client.CreateDocumentAsync(containerUri, customer); stopwatch.Stop(); Console.WriteLine($"Write {i} of {total}, to region: {region}, Latency: {stopwatch.ElapsedMilliseconds} ms, Request Charge: {response.RequestCharge} RUs"); lt += stopwatch.ElapsedMilliseconds; ru += response.RequestCharge; stopwatch.Reset(); } results.Add(new ResultData { Test = $"Test writes against {accountType} account in {region}", AvgLatency = (lt / total).ToString(), AvgRU = Math.Round(ru / total).ToString() }); Console.WriteLine(); Console.WriteLine("Summary"); Console.WriteLine("-----------------------------------------------------------------------------------------------------"); Console.WriteLine($"Test {total} writes against {accountType} account in {region}"); Console.WriteLine(); Console.WriteLine($"Average Latency:\t{(lt / total)} ms"); Console.WriteLine($"Average Request Units:\t{Math.Round(ru / total)} RUs"); Console.WriteLine(); Console.WriteLine("Press any key to continue..."); Console.ReadKey(true); if (final) { Console.WriteLine(); Console.WriteLine("Summary"); Console.WriteLine("-----------------------------------------------------------------------------------------------------"); foreach (ResultData r in results) { Console.WriteLine($"{r.Test}\tAvg Latency: {r.AvgLatency} ms\tAverage RU: {r.AvgRU}"); } Console.WriteLine(); Console.WriteLine($"Test concluded. Press any key to continue\r\n..."); Console.ReadKey(true); } }
private async Task WriteBenchmarkStrong(DocumentClient client) { Stopwatch stopwatch = new Stopwatch(); int i = 0; int total = 100; long lt = 0; double ru = 0; List <Result> result = new List <Result>(); string region = Helpers.ParseEndpoint(client.WriteEndpoint); string consistency = client.ConsistencyLevel.ToString(); Console.WriteLine($"Test {total} writes account in {region} with {consistency} consistency between all replicas. \r\nPress any key to continue\r\n..."); Console.ReadKey(true); Console.WriteLine(); for (i = 0; i < total; i++) { SampleCustomer customer = customerGenerator.Generate(); stopwatch.Start(); ResourceResponse <Document> response = await client.CreateDocumentAsync(containerUri, customer); stopwatch.Stop(); Console.WriteLine($"Write: Item {i} of {total}, Region: {region}, Latency: {stopwatch.ElapsedMilliseconds} ms, Request Charge: {response.RequestCharge} RUs"); result.Add(new Result(stopwatch.ElapsedMilliseconds, response.RequestCharge)); lt += stopwatch.ElapsedMilliseconds; ru += response.RequestCharge; stopwatch.Reset(); } //Average at 99th Percentile string _latency = Math.Round(result.OrderBy(o => o.Latency).Take(99).Average(o => o.Latency), 0).ToString(); string _ru = Math.Round(result.OrderBy(o => o.Latency).Take(99).Average(o => o.RU)).ToString(); results.Add(new ResultData { Test = $"Test with all {consistency} Consistency", AvgLatency = _latency, AvgRU = _ru }); Console.WriteLine(); Console.WriteLine(); Console.WriteLine("Test Summary"); Console.WriteLine("-----------------------------------------------------------------------------------------------------"); Console.WriteLine($"Test {total} writes account in {region} with {consistency} consistency between all replicas"); Console.WriteLine(); Console.WriteLine($"Average Latency:\t{_latency} ms"); Console.WriteLine($"Average Request Units:\t{_ru} RUs"); Console.WriteLine(); Console.WriteLine("Press any key to continue..."); Console.ReadKey(true); }
private async Task ReadBenchmark(DocumentClient client, string accountType, bool final = false) { string region = Helpers.ParseEndpoint(client.ReadEndpoint); Stopwatch stopwatch = new Stopwatch(); FeedOptions feedOptions = new FeedOptions { PartitionKey = new PartitionKey(PartitionKeyValue) }; string sql = "SELECT * FROM c"; var items = client.CreateDocumentQuery(containerUri, sql, feedOptions).ToList(); int i = 0; int total = items.Count; List <Result> result = new List <Result>(); Console.WriteLine(); Console.WriteLine($"Test {total} reads against {accountType} account in {region} from West US 2\r\nPress any key to continue\r\n..."); Console.ReadKey(true); RequestOptions requestOptions = new RequestOptions { PartitionKey = new PartitionKey(PartitionKeyValue) }; foreach (Document item in items) { stopwatch.Start(); ResourceResponse <Document> response = await client.ReadDocumentAsync(item.SelfLink, requestOptions); stopwatch.Stop(); Console.WriteLine($"Read {i} of {total}, region: {region}, Latency: {stopwatch.ElapsedMilliseconds} ms, Request Charge: {response.RequestCharge} RUs"); result.Add(new Result(stopwatch.ElapsedMilliseconds, response.RequestCharge)); i++; stopwatch.Reset(); } //Average at 99th Percentile string _latency = Math.Round(result.OrderBy(o => o.Latency).Take(99).Average(o => o.Latency), 0).ToString(); string _ru = Math.Round(result.OrderBy(o => o.Latency).Take(99).Average(o => o.RU)).ToString(); results.Add(new ResultData { Test = $"Test reads against {accountType} account in {region}", AvgLatency = _latency, AvgRU = _ru }); Console.WriteLine(); Console.WriteLine("Summary"); Console.WriteLine("-----------------------------------------------------------------------------------------------------"); Console.WriteLine($"Test {total} reads against {accountType} account in {region}"); Console.WriteLine(); Console.WriteLine($"Average Latency:\t{_latency} ms"); Console.WriteLine($"Average Request Units:\t{_ru} RUs"); Console.WriteLine(); Console.WriteLine("Press any key to continue..."); Console.ReadKey(true); if (final) { Console.WriteLine(); Console.WriteLine("Summary"); Console.WriteLine("-----------------------------------------------------------------------------------------------------"); foreach (ResultData r in results) { Console.WriteLine($"{r.Test}\tAvg Latency: {r.AvgLatency} ms\tAverage RU: {r.AvgRU}"); } Console.WriteLine(); Console.WriteLine($"Test concluded. Press any key to continue\r\n..."); Console.ReadKey(true); } }
private async Task WriteBenchmark(DocumentClient client, string distance, bool final = false) { Stopwatch stopwatch = new Stopwatch(); int i = 0; int total = 100; List <Result> result = new List <Result>(); //Write tests for account with Eventual consistency string region = Helpers.ParseEndpoint(client.WriteEndpoint); string consistency = client.ConsistencyLevel.ToString(); Console.WriteLine(); Console.WriteLine($"Test {total} writes account in {region} with {consistency} consistency level, and replica {distance} away. \r\nPress any key to continue\r\n..."); Console.ReadKey(true); Console.WriteLine(); for (i = 0; i < total; i++) { SampleCustomer customer = customerGenerator.Generate(); stopwatch.Start(); ResourceResponse <Document> response = await client.CreateDocumentAsync(containerUri, customer); stopwatch.Stop(); Console.WriteLine($"Write: Item {i} of {total}, Region: {region}, Latency: {stopwatch.ElapsedMilliseconds} ms, Request Charge: {response.RequestCharge} RUs"); result.Add(new Result(stopwatch.ElapsedMilliseconds, response.RequestCharge)); stopwatch.Reset(); } //Average at 99th Percentile string _latency = Math.Round(result.OrderBy(o => o.Latency).Take(99).Average(o => o.Latency), 0).ToString(); string _ru = Math.Round(result.OrderBy(o => o.Latency).Take(99).Average(o => o.RU)).ToString(); results.Add(new ResultData { Test = $"Test writes with {consistency} Consistency", AvgLatency = _latency, AvgRU = _ru }); Console.WriteLine(); Console.WriteLine(); Console.WriteLine("Summary"); Console.WriteLine("-----------------------------------------------------------------------------------------------------"); Console.WriteLine($"Test 100 writes against account in {region} with {consistency} consistency level, with replica {distance} away"); Console.WriteLine(); Console.WriteLine($"Average Latency:\t{_latency} ms"); Console.WriteLine($"Average Request Units:\t{_ru} RUs"); Console.WriteLine(); Console.WriteLine("Press any key to continue..."); Console.ReadKey(true); if (final) { Console.WriteLine(); Console.WriteLine("Summary"); Console.WriteLine("-----------------------------------------------------------------------------------------------------"); foreach (ResultData r in results) { Console.WriteLine($"{r.Test}\tAvg Latency: {r.AvgLatency} ms\tAverage RU: {r.AvgRU}"); } Console.WriteLine(); Console.WriteLine($"Test concluded. Press any key to continue\r\n..."); Console.ReadKey(true); } }
private async Task WriteBenchmarkCustomSync(DocumentClient writeClient, DocumentClient readClient) { Stopwatch stopwatch = new Stopwatch(); int i = 0; int total = 100; long lt = 0; double ru = 0; List <Result> result = new List <Result>(); string writeRegion = Helpers.ParseEndpoint(writeClient.WriteEndpoint); string readRegion = Helpers.ParseEndpoint(readClient.ReadEndpoint); string consistency = writeClient.ConsistencyLevel.ToString(); Console.WriteLine(); Console.WriteLine($"Test {total} writes in {writeRegion} with {consistency} consistency between all replicas except {readRegion} with Strong consistency. \r\nPress any key to continue\r\n..."); Console.ReadKey(true); PartitionKey partitionKeyValue = new PartitionKey(PartitionKeyValue); Console.WriteLine(); for (i = 0; i < total; i++) { SampleCustomer customer = customerGenerator.Generate(); stopwatch.Start(); //Write ResourceResponse <Document> writeResponse = await writeClient.CreateDocumentAsync(containerUri, customer); //Read ResourceResponse <Document> readResponse = await readClient.ReadDocumentAsync(writeResponse.Resource.SelfLink, new RequestOptions { PartitionKey = partitionKeyValue, SessionToken = writeResponse.SessionToken }); stopwatch.Stop(); lt = stopwatch.ElapsedMilliseconds; ru = writeResponse.RequestCharge + readResponse.RequestCharge; result.Add(new Result(lt, ru)); stopwatch.Reset(); Console.WriteLine($"Write/Read: Item {i} of {total}, Region: {writeRegion}, Latency: {lt} ms, Request Charge: {ru} RUs"); } //Average at 99th Percentile string _latency = Math.Round(result.OrderBy(o => o.Latency).Take(99).Average(o => o.Latency), 0).ToString(); string _ru = Math.Round(result.OrderBy(o => o.Latency).Take(99).Average(o => o.RU)).ToString(); results.Add(new ResultData { Test = $"Test with Custom Synchronization", AvgLatency = _latency, AvgRU = _ru }); Console.WriteLine(); Console.WriteLine(); Console.WriteLine("Test Summary"); Console.WriteLine("-------------------------------------------------------------------------------------------------------------------------------------"); Console.WriteLine($"Test {total} writes in {writeRegion} with {consistency} consistency between all replicas except {readRegion} with Strong consistency"); Console.WriteLine(); Console.WriteLine($"Average Latency:\t{_latency} ms"); Console.WriteLine($"Average Request Units:\t{_ru} RUs"); Console.WriteLine(); Console.WriteLine("Press any key to continue..."); Console.ReadKey(true); Console.WriteLine(); Console.WriteLine("All Tests Summary"); Console.WriteLine("-----------------------------------------------------------------------------------------------------"); foreach (ResultData r in results) { Console.WriteLine($"{r.Test}\tAvg Latency: {r.AvgLatency} ms\tAverage RU: {r.AvgRU}"); } Console.WriteLine(); Console.WriteLine($"Test concluded. Press any key to continue\r\n..."); Console.ReadKey(true); }
private async Task ReadBenchmark(DocumentClient client, string accountType, bool final = false) { string region = Helpers.ParseEndpoint(client.ReadEndpoint); Stopwatch stopwatch = new Stopwatch(); FeedOptions feedOptions = new FeedOptions { PartitionKey = new PartitionKey(PartitionKeyValue) }; string sql = "SELECT * FROM c"; var items = client.CreateDocumentQuery(containerUri, sql, feedOptions).ToList(); int i = 0; int total = items.Count; long lt = 0; double ru = 0; Console.WriteLine(); Console.WriteLine($"Test {total} reads against {accountType} account in {region}\r\nPress any key to continue\r\n..."); Console.ReadKey(true); RequestOptions requestOptions = new RequestOptions { PartitionKey = new PartitionKey(PartitionKeyValue) }; foreach (Document item in items) { stopwatch.Start(); ResourceResponse <Document> response = await client.ReadDocumentAsync(item.SelfLink, requestOptions); stopwatch.Stop(); Console.WriteLine($"Read {i} of {total}, region: {region}, Latency: {stopwatch.ElapsedMilliseconds} ms, Request Charge: {response.RequestCharge} RUs"); lt += stopwatch.ElapsedMilliseconds; ru += response.RequestCharge; i++; stopwatch.Reset(); } results.Add(new ResultData { Test = $"Test reads against {accountType} account in {region}", AvgLatency = (lt / total).ToString(), AvgRU = Math.Round(ru / total).ToString() }); Console.WriteLine(); Console.WriteLine("Summary"); Console.WriteLine("-----------------------------------------------------------------------------------------------------"); Console.WriteLine($"Test {total} reads against {accountType} account in {region}"); Console.WriteLine(); Console.WriteLine($"Average Latency:\t{(lt / total)} ms"); Console.WriteLine($"Average Request Units:\t{Math.Round(ru / total)} RUs"); Console.WriteLine(); Console.WriteLine("Press any key to continue..."); Console.ReadKey(true); if (final) { Console.WriteLine(); Console.WriteLine("Summary"); Console.WriteLine("-----------------------------------------------------------------------------------------------------"); foreach (ResultData r in results) { Console.WriteLine($"{r.Test}\tAvg Latency: {r.AvgLatency} ms\tAverage RU: {r.AvgRU}"); } Console.WriteLine(); Console.WriteLine($"Test concluded. Press any key to continue\r\n..."); Console.ReadKey(true); } }