Exemplo n.º 1
0
        private async Task <Container> CreateContainerAsync(Database db)
        {
            var containerId       = RandomName();
            var containerResponse = await db.CreateContainerAsync(containerId, "/PartitionKey");

            return(containerResponse.Container);
        }
Exemplo n.º 2
0
        private static async Task <Container> CreateDemoEnvironment(CosmosClient client)
        {
            ContainerResponse hotelContainer = null;

            try
            {
                // Set up a database
                Microsoft.Azure.Cosmos.Database database = await client.CreateDatabaseIfNotExistsAsync(databaseId);

                // Container and Throughput Properties
                ContainerProperties  containerProperties  = new ContainerProperties(readHeavyId, "/CityName");
                ThroughputProperties throughputProperties = ThroughputProperties.CreateAutoscaleThroughput(20000);

                // Create a read heavy environment
                hotelContainer = await database.CreateContainerAsync(
                    containerProperties,
                    throughputProperties);
            }
            catch (CosmosException ce)
            {
                Console.WriteLine($"Exception thrown by Cosmos DB: {ce.StatusCode}, {ce.Message}");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"General Exception thrown: {ex.Message}");
                throw;
            }

            return(hotelContainer);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Create a partitioned container.
        /// </summary>
        /// <returns>The created container.</returns>
        private static async Task <ContainerResponse> CreatePartitionedContainerAsync(BenchmarkConfig options, CosmosClient cosmosClient)
        {
            Microsoft.Azure.Cosmos.Database database = await cosmosClient.CreateDatabaseIfNotExistsAsync(options.Database);

            Container container = database.GetContainer(options.Container);

            try
            {
                return(await container.ReadContainerAsync());
            }
            catch (CosmosException ex) when(ex.StatusCode == HttpStatusCode.NotFound)
            {
                // Show user cost of running this test
                double estimatedCostPerMonth = 0.06 * options.Throughput;
                double estimatedCostPerHour  = estimatedCostPerMonth / (24 * 30);

                Console.WriteLine($"The container will cost an estimated ${Math.Round(estimatedCostPerHour, 2)} per hour (${Math.Round(estimatedCostPerMonth, 2)} per month)");
                Console.WriteLine("Press enter to continue ...");
                Console.ReadLine();

                string partitionKeyPath = options.PartitionKeyPath;

                return(await database.CreateContainerAsync(options.Container, partitionKeyPath, options.Throughput));
            }
        }
Exemplo n.º 4
0
        private static Microsoft.Azure.Cosmos.Database GetDatabase()
        {
            CosmosClient client = new CosmosClient(_accountEndpoint, _accountEndpointToken);

            Microsoft.Azure.Cosmos.Database database = client.GetDatabase("OrdersDB");

            return(database);
        }
        public void connectionSetup()
        {
            var cosmosClientOptions = new CosmosClientOptions()
            {
                ApplicationRegion = "Central US",
            };

            // Create a new instance of the Cosmos Client
            this.cosmosClient = new CosmosClient(EndpointUri, PrimaryKey, cosmosClientOptions);
            this.database     = this.cosmosClient.GetDatabase(databaseId);
            //this.container = this.database.GetContainer(containerId);
        }
Exemplo n.º 6
0
        static async Task Main(string[] args)
        {
            CosmosClient cosmosClient = new CosmosClient(EndpointUrl, AuthorizationKey, new CosmosClientOptions()
            {
                AllowBulkExecution = true
            });

            Microsoft.Azure.Cosmos.Database database = await cosmosClient.CreateDatabaseIfNotExistsAsync(Program.DatabaseName);

            await database.DefineContainer(Program.ContainerName, "/eyeColor")
            .WithIndexingPolicy()
            .WithIndexingMode(IndexingMode.Consistent)
            .WithIncludedPaths()
            .Attach()
            .WithExcludedPaths()
            .Path("/*")
            .Attach()
            .Attach()
            .CreateAsync(50000);

            List <KeyValuePair <PartitionKey, Stream> > itemsToInsert = new List <KeyValuePair <PartitionKey, Stream> >(ItemsToInsert);

            foreach (Model.Person item in Program.GetItemsToInsert())
            {
                MemoryStream stream = new MemoryStream();
                await JsonSerializer.SerializeAsync(stream, item);

                itemsToInsert.Add(new KeyValuePair <PartitionKey, Stream>(new PartitionKey(item.eyeColor), stream));
            }

            Container   container = database.GetContainer(ContainerName);
            List <Task> tasks     = new List <Task>(ItemsToInsert);

            foreach (KeyValuePair <PartitionKey, Stream> item in itemsToInsert)
            {
                tasks.Add(container.CreateItemStreamAsync(item.Value, item.Key)
                          .ContinueWith((Task <ResponseMessage> task) =>
                {
                    using (ResponseMessage response = task.Result)
                    {
                        if (!response.IsSuccessStatusCode)
                        {
                            Console.WriteLine($"Received {response.StatusCode} ({response.ErrorMessage}).");
                        }
                    }
                }));
            }

            // Wait until all are done
            await Task.WhenAll(tasks);
        }
Exemplo n.º 7
0
        private async Task InitAsync()
        {
            if (CosmosClient == null || Database == null || Containers == null || Containers.Count == 0)
            {
                CosmosClient = new CosmosClient(Endpoint, PrimaryKey);
                Database     = await CosmosClient.CreateDatabaseIfNotExistsAsync(DatabaseId);

                foreach (KeyValuePair <string, string> entry in ContainerData)
                {
                    Container c = await Database.CreateContainerIfNotExistsAsync(new ContainerProperties(entry.Key, entry.Value));

                    Containers.Add(entry.Key, c);
                }
            }
        }
        public static async void Run([CosmosDBTrigger(
                                          databaseName: "contoso",
                                          collectionName: "category",
                                          ConnectionStringSetting = "CosmosConnectionString",
                                          LeaseCollectionName = "leases")] IReadOnlyList <Document> input, ILogger log)
        {
            if (input != null && input.Count > 0)
            {
                log.LogInformation("Documents modified " + input.Count);
                log.LogInformation("First document Id " + input[0].Id);

                var       connectionString = "contoso-meo.redis.cache.windows.net:6380,password=EIhC70PPwkHZKSY5cc48SAy6YfM4FmahxchOEHKJjwo=,ssl=True,abortConnect=False";
                var       redisConnection  = ConnectionMultiplexer.Connect(connectionString);
                IDatabase db = redisConnection.GetDatabase();
                db.Execute("flushall");

                string       connStr      = Environment.GetEnvironmentVariable("CosmosConnectionString");
                CosmosClient cosmosClient = new CosmosClient(connStr);
                Microsoft.Azure.Cosmos.Database cosmosDb = cosmosClient.GetDatabase("contoso");
                Container container = cosmosDb.GetContainer("category");

                var sqlQueryText = $"SELECT c.CategoryId, c.CategoryName, c.Decription, c.Products, c.id FROM c";

                log.LogInformation("Running query: {0}\n", sqlQueryText);

                QueryDefinition         queryDefinition        = new QueryDefinition(sqlQueryText);
                FeedIterator <Category> queryResultSetIterator = container.GetItemQueryIterator <Category>(queryDefinition);

                List <Category> categories = new List <Category>();

                while (queryResultSetIterator.HasMoreResults)
                {
                    log.LogInformation("In loop");
                    FeedResponse <Category> currentResultSet = await queryResultSetIterator.ReadNextAsync();

                    foreach (Category cat in currentResultSet)
                    {
                        categories.Add(cat);
                        log.LogInformation("\tRead {0}\n", cat);

                        db.StringSet($"category:{cat.CategoryId}", cat.CategoryName);
                    }
                }

                redisConnection.Dispose();
                redisConnection = null;
            }
        }
        public DatabaseContainer(IDatabaseService databaseService)
        {
            _props = DatabaseHelpers.GetProperties <T>();
            _name  = _props.ContainerName;

            if (databaseService.Client == null)
            {
                _database = null;
            }
            else
            {
                _database = databaseService.GetDatabase();
            }

            _container = null;
        }
Exemplo n.º 10
0
        /// <summary>
        /// Run samples for Order By queries.
        /// </summary>
        /// <returns>a Task object.</returns>
        private async Task ExecuteAsync(BenchmarkConfig config, string accountKey)
        {
            using (CosmosClient cosmosClient = config.CreateCosmosClient(accountKey))
            {
                if (config.CleanupOnStart)
                {
                    Microsoft.Azure.Cosmos.Database database = cosmosClient.GetDatabase(config.Database);
                    await database.DeleteStreamAsync();
                }

                ContainerResponse containerResponse = await Program.CreatePartitionedContainerAsync(config, cosmosClient);

                Container container = containerResponse;

                int?currentContainerThroughput = await container.ReadThroughputAsync();

                Console.WriteLine($"Using container {config.Container} with {currentContainerThroughput} RU/s");

                int taskCount = config.GetTaskCount(currentContainerThroughput.Value);

                Console.WriteLine("Starting Inserts with {0} tasks", taskCount);
                Console.WriteLine();

                string partitionKeyPath      = containerResponse.Resource.PartitionKeyPath;
                int    numberOfItemsToInsert = config.ItemCount / taskCount;

                // TBD: 2 clients SxS some overhead
                using (DocumentClient documentClient = config.CreateDocumentClient(accountKey))
                {
                    Func <IBenchmarkOperatrion> benchmarkOperationFactory = this.GetBenchmarkFactory(
                        config,
                        partitionKeyPath,
                        cosmosClient,
                        documentClient);

                    IExecutionStrategy execution = IExecutionStrategy.StartNew(config, benchmarkOperationFactory);
                    await execution.ExecuteAsync(taskCount, numberOfItemsToInsert, config.TraceFailures, 0.01);
                }

                if (config.CleanupOnFinish)
                {
                    Console.WriteLine($"Deleting Database {config.Database}");
                    Microsoft.Azure.Cosmos.Database database = cosmosClient.GetDatabase(config.Database);
                    await database.DeleteStreamAsync();
                }
            }
        }
Exemplo n.º 11
0
        public static async void GenerateData(
            string key,
            string dbName,
            string endpoint,
            string containerName,
            string partitionKey,
            int documentsCount,
            int batchSize)
        {
            var options = new CosmosClientOptions()
            {
                AllowBulkExecution = true
            };

            var cosmosClient = new CosmosClient(endpoint, key, options);

            Microsoft.Azure.Cosmos.Database database = await cosmosClient.CreateDatabaseIfNotExistsAsync(dbName);

            Container idContainer = await database.CreateContainerIfNotExistsAsync(containerName, partitionKey);

            var invoices = GenerateInvoices();

            List <Task> concurrentTasks = new List <Task>();

            for (int i = 0; i < documentsCount / batchSize; i++)
            {
                List <Task> indexedTasks = new List <Task>();
                foreach (var invoice in invoices.Take(batchSize))
                {
                    indexedTasks.Add(idContainer.CreateItemAsync(invoice));
                }

                Console.WriteLine($"Starting insertion of {batchSize} documents into {containerName} in {dbName}");
                await Task.WhenAll(indexedTasks);

                Console.WriteLine($"Completed insertion of {batchSize} documents into {containerName} in {dbName}");
            }
        }
Exemplo n.º 12
0
        public static async Task Main(string[] args)
        {
            using (CosmosClient client = new CosmosClient(_endpointUri, _primaryKey))
            {
                Microsoft.Azure.Cosmos.Database database = client.GetDatabase(_databaseId);
                Container container = database.GetContainer(_containerId);

                List <Food> foods = new Faker <Food>()
                                    .RuleFor(p => p.Id, f => (-1 - f.IndexGlobal).ToString())
                                    .RuleFor(p => p.Description, f => f.Commerce.ProductName())
                                    .RuleFor(p => p.ManufacturerName, f => f.Company.CompanyName())
                                    .RuleFor(p => p.FoodGroup, f => "Energy Bars")
                                    .Generate(10000);

                int pointer = 0;
                while (pointer < foods.Count)
                {
                    StoredProcedureExecuteResponse <int> result = await container.Scripts.ExecuteStoredProcedureAsync <IEnumerable <Food>, int>(new PartitionKey("Energy Bars"), "bulkUpload", foods.Skip(pointer));

                    pointer += result.Resource;
                    await Console.Out.WriteLineAsync($"{pointer} Total Items\t{result.Resource} Items Uploaded in this Iteration");
                }

                Console.WriteLine("Execution paused for verification. Press any key to continue to delete.");
                Console.ReadKey();

                bool resume = true;
                do
                {
                    string query = "SELECT * FROM foods f WHERE f.foodGroup = 'Energy Bars'";
                    StoredProcedureExecuteResponse <DeleteStatus> result = await container.Scripts.ExecuteStoredProcedureAsync <string, DeleteStatus>(new PartitionKey("Energy Bars"), "bulkDelete", query);

                    await Console.Out.WriteLineAsync($"Batch Delete Completed.\tDeleted: {result.Resource.Deleted}\tContinue: {result.Resource.Continuation}");

                    resume = result.Resource.Continuation;
                }while (resume);
            }
        }
        public async Task CreateCollectionIfNotExistsAsync(string endPointUri, string secretKey, string databaseName, string collectionName, int throughput, string partitionKey)
        {
            Microsoft.Azure.Cosmos.Database db = await destinationCollectionClient.CreateDatabaseIfNotExistsAsync(databaseName);

            containerToStoreDocuments = await db.CreateContainerIfNotExistsAsync(collectionName, "/" + partitionKey);
        }
Exemplo n.º 14
0
        /// <summary>
        /// Run samples for Order By queries.
        /// </summary>
        /// <returns>a Task object.</returns>
        private async Task <RunSummary> ExecuteAsync(BenchmarkConfig config)
        {
            using (CosmosClient cosmosClient = config.CreateCosmosClient(config.Key))
            {
                Microsoft.Azure.Cosmos.Database database = cosmosClient.GetDatabase(config.Database);
                if (config.CleanupOnStart)
                {
                    await database.DeleteStreamAsync();
                }

                ContainerResponse containerResponse = await Program.CreatePartitionedContainerAsync(config, cosmosClient);

                Container container = containerResponse;

                int?currentContainerThroughput = await container.ReadThroughputAsync();

                if (!currentContainerThroughput.HasValue)
                {
                    // Container throughput is not configured. It is shared database throughput
                    ThroughputResponse throughputResponse = await database.ReadThroughputAsync(requestOptions : null);

                    throw new InvalidOperationException($"Using database {config.Database} with {throughputResponse.Resource.Throughput} RU/s. " +
                                                        $"Container {config.Container} must have a configured throughput.");
                }

                Console.WriteLine($"Using container {config.Container} with {currentContainerThroughput} RU/s");
                int taskCount = config.GetTaskCount(currentContainerThroughput.Value);

                Console.WriteLine("Starting Inserts with {0} tasks", taskCount);
                Console.WriteLine();

                string partitionKeyPath = containerResponse.Resource.PartitionKeyPath;
                int    opsPerTask       = config.ItemCount / taskCount;

                // TBD: 2 clients SxS some overhead
                RunSummary runSummary;
                using (DocumentClient documentClient = config.CreateDocumentClient(config.Key))
                {
                    Func <IBenchmarkOperation> benchmarkOperationFactory = this.GetBenchmarkFactory(
                        config,
                        partitionKeyPath,
                        cosmosClient,
                        documentClient);

                    if (config.DisableCoreSdkLogging)
                    {
                        // Do it after client initialization (HACK)
                        Program.ClearCoreSdkListeners();
                    }

                    IExecutionStrategy execution = IExecutionStrategy.StartNew(config, benchmarkOperationFactory);
                    runSummary = await execution.ExecuteAsync(taskCount, opsPerTask, config.TraceFailures, 0.01);
                }

                if (config.CleanupOnFinish)
                {
                    Console.WriteLine($"Deleting Database {config.Database}");
                    await database.DeleteStreamAsync();
                }

                runSummary.WorkloadType = config.WorkloadType;
                runSummary.id           = $"{DateTime.UtcNow:yyyy-MM-dd:HH-mm}-{config.CommitId}";
                runSummary.Commit       = config.CommitId;
                runSummary.CommitDate   = config.CommitDate;
                runSummary.CommitTime   = config.CommitTime;

                runSummary.Date        = DateTime.UtcNow.ToString("yyyy-MM-dd");
                runSummary.Time        = DateTime.UtcNow.ToString("HH-mm");
                runSummary.BranchName  = config.BranchName;
                runSummary.TotalOps    = config.ItemCount;
                runSummary.Concurrency = taskCount;
                runSummary.Database    = config.Database;
                runSummary.Container   = config.Container;
                runSummary.AccountName = config.EndPoint;
                runSummary.pk          = config.ResultsPartitionKeyValue;
                runSummary.MaxTcpConnectionsPerEndpoint = config.MaxTcpConnectionsPerEndpoint;
                runSummary.MaxRequestsPerTcpConnection  = config.MaxRequestsPerTcpConnection;

                string consistencyLevel = config.ConsistencyLevel;
                if (string.IsNullOrWhiteSpace(consistencyLevel))
                {
                    AccountProperties accountProperties = await cosmosClient.ReadAccountAsync();

                    consistencyLevel = accountProperties.Consistency.DefaultConsistencyLevel.ToString();
                }
                runSummary.ConsistencyLevel = consistencyLevel;


                if (config.PublishResults)
                {
                    runSummary.Diagnostics = CosmosDiagnosticsLogger.GetDiagnostics();
                    await this.PublishResults(
                        config,
                        runSummary,
                        cosmosClient);
                }

                return(runSummary);
            }
        }
Exemplo n.º 15
0
 private void GetDatabase()
 {
     this.database = this.cosmosClient.GetDatabase(databaseId);
 }
Exemplo n.º 16
0
        /// <summary>
        /// Executing benchmarks for V2/V3 cosmosdb SDK.
        /// </summary>
        /// <returns>a Task object.</returns>
        private async Task <RunSummary> ExecuteAsync(BenchmarkConfig config)
        {
            // V3 SDK client initialization
            using (CosmosClient cosmosClient = config.CreateCosmosClient(config.Key))
            {
                Microsoft.Azure.Cosmos.Database database = cosmosClient.GetDatabase(config.Database);
                if (config.CleanupOnStart)
                {
                    await database.DeleteStreamAsync();
                }

                ContainerResponse containerResponse = await Program.CreatePartitionedContainerAsync(config, cosmosClient);

                Container container = containerResponse;

                int?currentContainerThroughput = await container.ReadThroughputAsync();

                if (!currentContainerThroughput.HasValue)
                {
                    // Container throughput is not configured. It is shared database throughput
                    ThroughputResponse throughputResponse = await database.ReadThroughputAsync(requestOptions : null);

                    throw new InvalidOperationException($"Using database {config.Database} with {throughputResponse.Resource.Throughput} RU/s. " +
                                                        $"Container {config.Container} must have a configured throughput.");
                }

                Console.WriteLine($"Using container {config.Container} with {currentContainerThroughput} RU/s");
                int taskCount = config.GetTaskCount(currentContainerThroughput.Value);

                Console.WriteLine("Starting Inserts with {0} tasks", taskCount);
                Console.WriteLine();

                string partitionKeyPath = containerResponse.Resource.PartitionKeyPath;
                int    opsPerTask       = config.ItemCount / taskCount;

                // TBD: 2 clients SxS some overhead
                RunSummary runSummary;

                // V2 SDK client initialization
                using (Microsoft.Azure.Documents.Client.DocumentClient documentClient = config.CreateDocumentClient(config.Key))
                {
                    Func <IBenchmarkOperation> benchmarkOperationFactory = this.GetBenchmarkFactory(
                        config,
                        partitionKeyPath,
                        cosmosClient,
                        documentClient);

                    if (config.DisableCoreSdkLogging)
                    {
                        // Do it after client initialization (HACK)
                        Program.ClearCoreSdkListeners();
                    }

                    IExecutionStrategy execution = IExecutionStrategy.StartNew(benchmarkOperationFactory);
                    runSummary = await execution.ExecuteAsync(config, taskCount, opsPerTask, 0.01);
                }

                if (config.CleanupOnFinish)
                {
                    Console.WriteLine($"Deleting Database {config.Database}");
                    await database.DeleteStreamAsync();
                }

                string consistencyLevel = config.ConsistencyLevel;
                if (string.IsNullOrWhiteSpace(consistencyLevel))
                {
                    AccountProperties accountProperties = await cosmosClient.ReadAccountAsync();

                    consistencyLevel = accountProperties.Consistency.DefaultConsistencyLevel.ToString();
                }
                runSummary.ConsistencyLevel = consistencyLevel;


                if (config.PublishResults)
                {
                    runSummary.Diagnostics = CosmosDiagnosticsLogger.GetDiagnostics();
                    await this.PublishResults(
                        config,
                        runSummary,
                        cosmosClient);
                }

                return(runSummary);
            }
        }
        public async Task Setup_CosmosResources()
        {
            // Create resources for Scenario 0 - setup
            Console.BackgroundColor = ConsoleColor.Blue;
            Console.ForegroundColor = ConsoleColor.Yellow;

            Console.WriteLine("Running setup...\n");

            Console.ResetColor();

            // Create database for partitioning demo. We set the RU/s high to simulate having a large workload with many partitions, to demo the impact of cross-partition queries on larger datasets. If you do not wish to demo Scenario 2, replace the throughput with the minimum of 400 RU/s.
            var partitionDemoDatabaseName = "PartitioningDemo";

            Microsoft.Azure.Cosmos.Database partitioningDemoDB = await cosmosClient.CreateDatabaseIfNotExistsAsync(id : partitionDemoDatabaseName, throughput : 40000);

            Console.WriteLine("\tCreated database {0}", partitioningDemoDB.Id);

            // Create 2 containers with different partition keys for Scenarios
            UserReviewsV1Container = await partitioningDemoDB.CreateContainerIfNotExistsAsync(id : "UserReviews_v1", partitionKeyPath : "/id");

            Console.WriteLine("\tCreated container {0} with partition key 'id'", UserReviewsV1Container.Id);

            UserReviewsV2Container = await partitioningDemoDB.CreateContainerIfNotExistsAsync(id : "UserReviews_v2", partitionKeyPath : "/username");

            Console.WriteLine("\tCreated container {0} with partition key 'username'", UserReviewsV2Container.Id);

            // Create database Indexing policy demo
            var indexingDemoDatabaseName = "IndexingDemo";

            Microsoft.Azure.Cosmos.Database indexingDemoDB = await cosmosClient.CreateDatabaseIfNotExistsAsync(id : indexingDemoDatabaseName, throughput : 400);

            // Create containers for Indexing policy demo
            var defaultIndexingPolicyContatinerName = "DefaultIndexingPolicy";
            var tunedIndexingPolicyContainerName    = "TunedIndexingPolicy";

            ContainerResponse defaultIndexingPolicyContainer = await indexingDemoDB.DefineContainer(name : defaultIndexingPolicyContatinerName, partitionKeyPath : "/partitionKey")
                                                               .CreateIfNotExistsAsync();

            Console.WriteLine("\tCreated container {0} with default indexing policy", defaultIndexingPolicyContainer.Resource.Id);

            ContainerResponse tunedIndexingPolicyContainer = await indexingDemoDB.DefineContainer(name : tunedIndexingPolicyContainerName, partitionKeyPath : "/partitionKey")
                                                             // Define indexing policy with included and excluded paths
                                                             .WithIndexingPolicy()
                                                             .WithIncludedPaths()
                                                             .Path("/username/*")
                                                             .Path("/productName/*")
                                                             .Path("/verifiedPurchase/*")
                                                             .Attach()
                                                             .WithExcludedPaths()
                                                             .Path("/*")
                                                             .Attach()
                                                             .Attach()
                                                             .CreateIfNotExistsAsync();

            Console.WriteLine("\tCreated container {0} with tuned indexing policy", tunedIndexingPolicyContainer.Resource.Id);

            // Import data into the containers

            Console.BackgroundColor = ConsoleColor.Blue;
            Console.ForegroundColor = ConsoleColor.Yellow;

            Console.WriteLine("\nStarting data import: this will take a few minutes...");

            Console.ResetColor();

            using (WebClient wc = new WebClient())
            {
                var            json = wc.DownloadString("https://cosmosnotebooksdata.blob.core.windows.net/notebookdata/ProductReviewsData.json");
                List <dynamic> userProductReviews = JsonConvert.DeserializeObject <List <dynamic> >(json);

                Console.WriteLine("\tFinished downloading data from 'https://cosmosnotebooksdata.blob.core.windows.net/notebookdata/ProductReviewsData.json'");
                Console.WriteLine("\tWriting data into Cosmos containers...");

                Stopwatch stopWatch = new Stopwatch();
                stopWatch.Start();

                // Enable bulk execution mode for faster import
                cosmosClient.ClientOptions.AllowBulkExecution = true;
                List <Task <int> > tasks = new List <Task <int> >();

                for (var i = 0; i < userProductReviews.Count; i++)
                {
                    var documentType = userProductReviews[i].documentType;

                    if (documentType == "user")
                    {
                        User user = JsonConvert.DeserializeObject <User>(userProductReviews[i].ToString());
                        tasks.Add(InsertUserItemsAsync(user));
                    }
                    else if (documentType == "review")
                    {
                        ProductReview productReview = JsonConvert.DeserializeObject <ProductReview>(userProductReviews[i].ToString());
                        tasks.Add(InsertProductReviewItemsAsync(productReview));
                    }
                }

                await Task.WhenAll(tasks);

                stopWatch.Stop();
                TimeSpan ts = stopWatch.Elapsed;
                //Print results
                string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
                                                   ts.Hours, ts.Minutes, ts.Seconds,
                                                   ts.Milliseconds / 10);

                Console.WriteLine("\tImport of {0} items into {1} and {2} containers completed. Total time: {3}", tasks.Sum(task => task.Result) / 2, UserReviewsV1Container.Id, UserReviewsV2Container.Id, elapsedTime);
            }

            // Scale down the database to the minimum RU/s.
            var currentThroughput = await partitioningDemoDB.ReadThroughputAsync(requestOptions : new RequestOptions());

            int minThroughput      = (int)currentThroughput.MinThroughput;
            var throughputResponse = await partitioningDemoDB.ReplaceThroughputAsync(minThroughput);

            Console.WriteLine("\tScaled PartitioningDemo database down to minimum {0} RU/s", throughputResponse.Resource.Throughput);

            // Reset bulk execution mode to false for rest of demo
            cosmosClient.ClientOptions.AllowBulkExecution = false;
        }
Exemplo n.º 18
0
        /// <summary>
        /// Run samples for Order By queries.
        /// </summary>
        /// <returns>a Task object.</returns>
        private async Task <RunSummary> ExecuteAsync(BenchmarkConfig config, string accountKey)
        {
            using (CosmosClient cosmosClient = config.CreateCosmosClient(accountKey))
            {
                if (config.CleanupOnStart)
                {
                    Microsoft.Azure.Cosmos.Database database = cosmosClient.GetDatabase(config.Database);
                    await database.DeleteStreamAsync();
                }

                ContainerResponse containerResponse = await Program.CreatePartitionedContainerAsync(config, cosmosClient);

                Container container = containerResponse;

                int?currentContainerThroughput = await container.ReadThroughputAsync();

                Console.WriteLine($"Using container {config.Container} with {currentContainerThroughput} RU/s");

                int taskCount = config.GetTaskCount(currentContainerThroughput.Value);

                Console.WriteLine("Starting Inserts with {0} tasks", taskCount);
                Console.WriteLine();

                string partitionKeyPath = containerResponse.Resource.PartitionKeyPath;
                int    opsPerTask       = config.ItemCount / taskCount;

                // TBD: 2 clients SxS some overhead
                RunSummary runSummary;
                using (DocumentClient documentClient = config.CreateDocumentClient(accountKey))
                {
                    Func <IBenchmarkOperatrion> benchmarkOperationFactory = this.GetBenchmarkFactory(
                        config,
                        partitionKeyPath,
                        cosmosClient,
                        documentClient);

                    IExecutionStrategy execution = IExecutionStrategy.StartNew(config, benchmarkOperationFactory);
                    runSummary = await execution.ExecuteAsync(taskCount, opsPerTask, config.TraceFailures, 0.01);
                }

                if (config.CleanupOnFinish)
                {
                    Console.WriteLine($"Deleting Database {config.Database}");
                    Microsoft.Azure.Cosmos.Database database = cosmosClient.GetDatabase(config.Database);
                    await database.DeleteStreamAsync();
                }

                runSummary.WorkloadType = config.WorkloadType;
                runSummary.id           = $"{DateTime.UtcNow.ToString("yyyy-MM-dd:HH-mm")}-{config.CommitId}";
                runSummary.Commit       = config.CommitId;
                runSummary.CommitDate   = config.CommitDate;
                runSummary.CommitTime   = config.CommitTime;

                runSummary.Date        = DateTime.UtcNow.ToString("yyyy-MM-dd");
                runSummary.Time        = DateTime.UtcNow.ToString("HH-mm");
                runSummary.BranchName  = config.BranchName;
                runSummary.TotalOps    = config.ItemCount;
                runSummary.Concurrency = taskCount;
                runSummary.Database    = config.Database;
                runSummary.Container   = config.Container;
                runSummary.AccountName = config.EndPoint;
                runSummary.pk          = config.ResultsPartitionKeyValue;

                string consistencyLevel = config.ConsistencyLevel;
                if (string.IsNullOrWhiteSpace(consistencyLevel))
                {
                    AccountProperties accountProperties = await cosmosClient.ReadAccountAsync();

                    consistencyLevel = accountProperties.Consistency.DefaultConsistencyLevel.ToString();
                }
                runSummary.ConsistencyLevel = consistencyLevel;


                if (config.PublishResults)
                {
                    Container resultsContainer = cosmosClient.GetContainer(config.Database, config.ResultsContainer);
                    await resultsContainer.CreateItemAsync(runSummary, new PartitionKey(runSummary.pk));
                }

                return(runSummary);
            }
        }
Exemplo n.º 19
0
        public static async void Run([CosmosDBTrigger(
                                          databaseName: "contoso",
                                          collectionName: "orders-aggregate",
                                          ConnectionStringSetting = "CosmosConnectionString",
                                          LeaseCollectionName = "leases")] IReadOnlyList <Microsoft.Azure.Documents.Document> input, ILogger log)
        {
            if (input != null && input.Count > 0)
            {
                log.LogInformation("Documents modified " + input.Count);
                log.LogInformation("First document Id " + input[0].Id);

                string       connStr               = Environment.GetEnvironmentVariable("CosmosConnectionString");
                CosmosClient cosmosClient          = new CosmosClient(connStr);
                Microsoft.Azure.Cosmos.Database db = cosmosClient.GetDatabase("contoso");
                Container container = db.GetContainer("orders-for-hour");

                // custQuery is an IEnumerable<IGrouping<string, Customer>>
                var dateQuery =
                    from o in input
                    group o by o.GetPropertyValue <string>("OrderDateHour") into orderDateGroup
                    select orderDateGroup;

                log.LogInformation(dateQuery.Count().ToString());

                foreach (var dq in dateQuery)
                {
                    log.LogInformation("DateQuery: " + dq.Key);

                    var sqlQueryText = $"SELECT * FROM c WHERE c.id = '{dq.Key}'";

                    log.LogInformation("Running query: {0}\n", sqlQueryText);

                    QueryDefinition           queryDefinition        = new QueryDefinition(sqlQueryText);
                    FeedIterator <OrderEvent> queryResultSetIterator = container.GetItemQueryIterator <OrderEvent>(queryDefinition);

                    List <OrderEvent> orders = new List <OrderEvent>();

                    while (queryResultSetIterator.HasMoreResults)
                    {
                        log.LogInformation("In loop");
                        FeedResponse <OrderEvent> currentResultSet = await queryResultSetIterator.ReadNextAsync();

                        foreach (OrderEvent order in currentResultSet)
                        {
                            orders.Add(order);
                            log.LogInformation("\tRead {0}\n", order);
                        }
                    }

                    log.LogInformation("Orders Count: " + orders.Count);

                    OrderEvent currentOrder = new OrderEvent()
                    {
                        id = dq.Key, Count = dq.Count(), Total = dq.Sum(x => x.GetPropertyValue <decimal>("Total"))
                    };

                    if (orders.Count == 0)
                    {
                        log.LogInformation("Addin entry");
                        try
                        {
                            ItemResponse <OrderEvent> orderResponse = await container.CreateItemAsync <OrderEvent>(currentOrder);
                        }
                        catch (Exception ex)
                        {
                            log.LogInformation("Exception: " + ex.Message);
                        }
                    }
                    else
                    {
                        ItemResponse <OrderEvent> orderResponse = await container.ReadItemAsync <OrderEvent>(dq.Key, new PartitionKey(dq.Key));

                        int     currentCount = orderResponse.Resource.Count;
                        decimal currentTotal = orderResponse.Resource.Total;

                        OrderEvent newOrder = new OrderEvent()
                        {
                            id = dq.Key, Count = currentCount + currentOrder.Count, Total = currentTotal + currentOrder.Total
                        };
                        orderResponse = await container.ReplaceItemAsync <OrderEvent>(newOrder, newOrder.id);
                    }
                }
            }
        }