/// <summary> /// Create a partitioned container. /// </summary> /// <returns>The created container.</returns> private async Task <ContainerResponse> CreatePartitionedContainerAsync(BenchmarkOptions options) { Database database = await client.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)); } }
/// <summary> /// Run samples for Order By queries. /// </summary> /// <returns>a Task object.</returns> private async Task RunAsync(BenchmarkOptions options) { if (options.CleanupOnStart) { Database database = client.GetDatabase(options.Database); await database.DeleteStreamAsync(); } ContainerResponse containerResponse = await this.CreatePartitionedContainerAsync(options); Container container = containerResponse; int?currentContainerThroughput = await container.ReadThroughputAsync(); Console.WriteLine($"Using container {options.Container} with {currentContainerThroughput} RU/s"); int taskCount = options.DegreeOfParallelism; if (taskCount == -1) { // set TaskCount = 10 for each 10k RUs, minimum 1, maximum { #processor * 50 } taskCount = Math.Max(currentContainerThroughput.Value / 1000, 1); taskCount = Math.Min(taskCount, Environment.ProcessorCount * 50); } this.RequestUnitsConsumed = new double[taskCount]; Console.WriteLine("Starting Inserts with {0} tasks", taskCount); Console.WriteLine(); string sampleItem = File.ReadAllText(options.ItemTemplateFile); pendingTaskCount = taskCount; var tasks = new List <Task>(); tasks.Add(this.LogOutputStats()); string partitionKeyPath = containerResponse.Resource.PartitionKeyPath; long numberOfItemsToInsert = options.ItemCount / taskCount; for (var i = 0; i < taskCount; i++) { tasks.Add(this.InsertItem(i, container, partitionKeyPath, sampleItem, numberOfItemsToInsert)); } await Task.WhenAll(tasks); if (options.CleanupOnFinish) { Console.WriteLine($"Deleting Database {options.Database}"); Database database = client.GetDatabase(options.Database); await database.DeleteStreamAsync(); } }
/// <summary> /// Main method for the sample. /// </summary> /// <param name="args">command line arguments.</param> public static async Task Main(string[] args) { BenchmarkOptions options = null; Parser.Default.ParseArguments <BenchmarkOptions>(args) .WithParsed <BenchmarkOptions>(e => options = e) .WithNotParsed <BenchmarkOptions>(e => Program.HandleParseError(e)); ThreadPool.SetMinThreads(options.MinThreadPoolSize, options.MinThreadPoolSize); string accountKey = options.Key; options.Key = null; // Don't print using (var ct = new ConsoleColoeContext(ConsoleColor.Green)) { Console.WriteLine($"{nameof(CosmosBenchmark)} started with arguments"); Console.WriteLine("--------------------------------------------------------------------- "); Console.WriteLine(JsonConvert.SerializeObject(options, new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore, Formatting = Formatting.Indented, })); Console.WriteLine("--------------------------------------------------------------------- "); Console.WriteLine(); } CosmosClientOptions clientOptions = new CosmosClientOptions() { ApplicationName = "cosmosdbdotnetbenchmark", RequestTimeout = new TimeSpan(1, 0, 0), MaxRetryAttemptsOnRateLimitedRequests = 10, MaxRetryWaitTimeOnRateLimitedRequests = TimeSpan.FromSeconds(60), }; using (var client = new CosmosClient( options.EndPoint, accountKey, clientOptions)) { var program = new Program(client); await program.RunAsync(options); Console.WriteLine("CosmosBenchmark completed successfully."); } Console.WriteLine("Press any key to exit..."); Console.ReadLine(); }