GetDocumentClient(
     SubmitToDocumentDb options)
 {
     return(new DocumentClient(
                new Uri(string.Format("https://{0}.documents.azure.com:443/", options.DocumentDbServiceName)),
                options.DocumentDbServiceKey
                ));
 }
        Index()
        {
            var defaultSettings = new SubmitToDocumentDb {
                DocumentDbServiceName    = "your-service-name-here",
                DocumentDbServiceKey     = "your-service-key-here",
                DatabaseId               = "Example-Db",
                CollectionId             = "Example-Collection",
                DocumentPayload          = "{'FirstName':'John','LastName':'Doe','DateOfBirth':'1980-01-01'}",
                NumberOfDocuments        = 1,
                MaxConcurrentSubmissions = 1
            };

            return(View(defaultSettings));
        }
        SubmitToDocumentDb(
            SubmitToDocumentDb options)
        {
            try
            {
                var document           = JObject.Parse(options.DocumentPayload);
                var client             = GetDocumentClient(options);
                var documentCollection = await GetDocumentCollectionAsync(
                    client :       client,
                    databaseId :   options.DatabaseId,
                    collectionId : options.CollectionId
                    );

                var totalStopWatch = Stopwatch.StartNew();

                Parallel.For(
                    fromInclusive:      0,
                    toExclusive:        options.NumberOfDocuments,
                    parallelOptions:    new ParallelOptions {
                    MaxDegreeOfParallelism = options.MaxConcurrentSubmissions
                },
                    body:               i => Task.WaitAll(
                        client.CreateDocumentAsync(
                            documentCollection.DocumentsLink,
                            document
                            )
                        )
                    );

                var totalElapsedMilliseconds   = totalStopWatch.ElapsedMilliseconds;
                var documentsPerSecond         = options.NumberOfDocuments / (totalElapsedMilliseconds / 1000.0);
                var averageLatencyMilliseconds = (double)totalElapsedMilliseconds / options.NumberOfDocuments;

                return(string.Format(
                           "Submitted {0} documents in {1}ms\r\nCalculated Rates: {2}/sec @ {3}ms/doc avg",
                           options.NumberOfDocuments,
                           totalElapsedMilliseconds,
                           documentsPerSecond.ToString("0.00"),
                           averageLatencyMilliseconds
                           ));
            }
            catch (Exception exception)
            {
                return(exception.ToString());
            }
        }