Esempio n. 1
0
        public void TestReadLatency()
        {
            //Insert a sample document to use for Read Latency Test
            SampleDoc document = new SampleDoc
            {
                Name          = "Scott Guthrie",
                City          = "Redmond",
                PostalCode    = "98052",
                UserDefinedId = 0, //not used in this demo
                Region        = "Southeast Asia"
            };

            //insert the document
            client.CreateDocumentAsync(collectionUri, document).GetAwaiter().GetResult();

            string region = client.ConnectionPolicy.PreferredLocations[0].ToString();

            Console.WriteLine();
            Console.WriteLine($"Test for 100 reads against region: {region}\r\nPress any key to continue\r\n...");
            Console.ReadKey(true);

            Stopwatch stopwatch = new Stopwatch();

            for (int i = 0; i < 100; i++)
            {
                stopwatch.Start();
                var sql  = "SELECT TOP 1 * FROM c WHERE c.postalcode = '98052'";
                var resp = client.CreateDocumentQuery(collectionUri, sql).ToList();
                stopwatch.Stop();

                Console.WriteLine($"Read operation {i} of 100. From region: {region}, elapsed time: {stopwatch.ElapsedMilliseconds} ms");
                stopwatch.Reset();
            }
        }
Esempio n. 2
0
        public async Task TestWriteLatency()
        {
            string region = client.ConnectionPolicy.PreferredLocations[0].ToString();

            Console.WriteLine();
            Console.WriteLine($"Test for 100 writes against region: {region}\r\nPress any key to continue\r\n...");
            Console.ReadKey(true);

            SampleDoc document = new SampleDoc
            {
                Name          = "Scott Guthrie",
                City          = "Redmond",
                PostalCode    = "98052",
                UserDefinedId = 9, //not used in this demo
                Region        = region
            };

            Stopwatch stopwatch = new Stopwatch();

            for (int i = 0; i < 100; i++)
            {
                stopwatch.Start();
                //insert the document
                await client.CreateDocumentAsync(collectionUri, document);

                stopwatch.Stop();

                Console.WriteLine($"Write {i} of 100. Insert to region: {region}, elapsed time: {stopwatch.ElapsedMilliseconds} ms");
                stopwatch.Reset();
            }
        }
        private async Task <Document> UpdateDocument(DocumentClient client, Uri collectionUri, Document document)
        {
            //The region property for the committed document replicated to all regions.
            //The update we will make will update that to be the region this document will be updated in.
            string region = client.ConnectionPolicy.PreferredLocations[0].ToString();

            //update the region property
            document.SetPropertyValue("region", region);

            SampleDoc doc = JsonConvert.DeserializeObject <SampleDoc>(document.ToString());

            Console.WriteLine($"Updating document, Id: {doc.Id} from Region: {doc.Region} to Region: {region}");

            try
            {
                return(await client.ReplaceDocumentAsync(document.SelfLink, document, new RequestOptions
                {
                    AccessCondition = new AccessCondition
                    {
                        Type = AccessConditionType.IfMatch,
                        Condition = document.ETag
                    }
                }));
            }
            catch (DocumentClientException ex)
            {
                if (ex.StatusCode == System.Net.HttpStatusCode.PreconditionFailed ||
                    ex.StatusCode == System.Net.HttpStatusCode.NotFound)
                {
                    Console.WriteLine();
                    Console.WriteLine($"Attempted Update on Doc Id: {doc.Id} from Region: {doc.Region} unsuccessful as previous update already committed and replicated.");
                    //Lost synchronously or not replicated yet. No conflict is induced.
                    return(null);
                }
                throw;
            }
        }
        private async Task <Document> InsertDocument(DocumentClient client, Uri collectionUri, int docId)
        {
            //Regional endpoint this document will be written to
            string region = client.ConnectionPolicy.PreferredLocations[0].ToString();

            //Create a random number between 0-10 for each document. This will be used by UDP stored procedure and LWW samples for conflict resolution.
            int userdefid = RandomGen.Next(0, 10);

            SampleDoc doc = new SampleDoc
            {
                Id            = docId.ToString(),
                Name          = "Scott Guthrie",
                City          = "Redmond",
                PostalCode    = "98052",
                UserDefinedId = userdefid,
                Region        = region
            };

            Console.WriteLine($"Id: {doc.Id}, Name: {doc.Name}, City: {doc.City}, PostalCode: {doc.PostalCode}, UserDefId: {doc.UserDefinedId}, Region: {doc.Region}");

            try
            {
                return(await client.CreateDocumentAsync(collectionUri, doc));
            }
            catch (DocumentClientException ex)
            {
                if (ex.StatusCode == System.Net.HttpStatusCode.Conflict)
                {
                    Console.WriteLine();
                    Console.WriteLine($"Attempted Insert for Doc Id: {doc.Id} from Region: {doc.Region} unsuccessful as previous insert already committed and replicated.");
                    //Conflict has already replicated so return null
                    return(null);
                }
                throw;
            }
        }