コード例 #1
0
        private async Task GenerateInsertConflicts(Uri collectionUri, string test)
        {
            try
            {
                bool isConflicts = false;

                Console.WriteLine($"{test}\r\nPress any key to continue...");
                Console.ReadKey(true);

                while (!isConflicts)
                {
                    List <Task <SampleCustomer> > tasks = new List <Task <SampleCustomer> >();

                    SampleCustomer customer = customerGenerator.Generate();

                    foreach (DocumentClient client in clients)
                    {
                        tasks.Add(InsertItemAsync(client, collectionUri, customer));
                    }

                    SampleCustomer[] insertedItems = await Task.WhenAll(tasks);

                    isConflicts = IsConflicts(insertedItems);
                }
            }
            catch (DocumentClientException dcx)
            {
                Console.WriteLine(dcx.Message);
                Console.ReadKey();
            }
        }
コード例 #2
0
        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);
            }
        }
コード例 #3
0
        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);
        }
コード例 #4
0
        private async Task GenerateUpdateConflicts(Uri collectionUri, string test)
        {
            try
            {
                bool isConflicts = false;

                Console.WriteLine();
                Console.WriteLine($"{test}\r\nPress any key to continue...");
                Console.ReadKey(true);
                Console.WriteLine($"Inserting an item to create an update conflict on.");

                //Generate a new customer, set the region property
                SampleCustomer customer = customerGenerator.Generate();

                SampleCustomer insertedItem = await InsertItemAsync(clients[0], collectionUri, customer);

                Console.WriteLine($"Wait 2 seconds to allow item to replicate.");
                await Task.Delay(2000);

                RequestOptions requestOptions = new RequestOptions
                {
                    PartitionKey = new PartitionKey(PartitionKeyValue)
                };

                while (!isConflicts)
                {
                    IList <Task <SampleCustomer> > tasks = new List <Task <SampleCustomer> >();

                    SampleCustomer item = await clients[0].ReadDocumentAsync <SampleCustomer>(insertedItem.SelfLink, requestOptions);
                    Console.WriteLine($"Original - Name: {item.Name}, City: {item.City}, UserDefId: {item.UserDefinedId}, Region: {item.Region}");

                    foreach (DocumentClient client in clients)
                    {
                        tasks.Add(UpdateItemAsync(client, collectionUri, item));
                    }

                    SampleCustomer[] updatedItems = await Task.WhenAll(tasks);

                    //Delay to allow data to replicate
                    await Task.Delay(2000);

                    isConflicts = IsConflicts(updatedItems);
                }
            }
            catch (DocumentClientException dcx)
            {
                Console.WriteLine(dcx.Message);
                Console.ReadKey();
            }
        }
コード例 #5
0
        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;
            }
        }
コード例 #6
0
        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 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);
            }
        }
コード例 #8
0
        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);
        }
コード例 #9
0
 //Deep copy Document object
 public static SampleCustomer Clone(SampleCustomer source)
 {
     return(JsonConvert.DeserializeObject <SampleCustomer>(JsonConvert.SerializeObject(source)));
 }