private Func <IBenchmarkOperation> GetBenchmarkFactory( BenchmarkConfig config, string partitionKeyPath, CosmosClient cosmosClient, Microsoft.Azure.Documents.Client.DocumentClient documentClient) { string sampleItem = File.ReadAllText(config.ItemTemplateFile); Type[] availableBenchmarks = Program.AvailableBenchmarks(); IEnumerable <Type> res = availableBenchmarks .Where(e => e.Name.Equals(config.WorkloadType, StringComparison.OrdinalIgnoreCase) || e.Name.Equals(config.WorkloadType + "BenchmarkOperation", StringComparison.OrdinalIgnoreCase)); if (res.Count() != 1) { throw new NotImplementedException($"Unsupported workload type {config.WorkloadType}. Available ones are " + string.Join(", \r\n", availableBenchmarks.Select(e => e.Name))); } ConstructorInfo ci = null; object[] ctorArguments = null; Type benchmarkTypeName = res.Single(); if (benchmarkTypeName.Name.EndsWith("V3BenchmarkOperation")) { ci = benchmarkTypeName.GetConstructor(new Type[] { typeof(CosmosClient), typeof(string), typeof(string), typeof(string), typeof(string) }); ctorArguments = new object[] { cosmosClient, config.Database, config.Container, partitionKeyPath, sampleItem }; } else if (benchmarkTypeName.Name.EndsWith("V2BenchmarkOperation")) { ci = benchmarkTypeName.GetConstructor(new Type[] { typeof(Microsoft.Azure.Documents.Client.DocumentClient), typeof(string), typeof(string), typeof(string), typeof(string) }); ctorArguments = new object[] { documentClient, config.Database, config.Container, partitionKeyPath, sampleItem }; } if (ci == null) { throw new NotImplementedException($"Unsupported CTOR for workload type {config.WorkloadType} "); } return(() => (IBenchmarkOperation)ci.Invoke(ctorArguments)); }
static void Main(string[] args) { try { _documentDbClient = new Db.Client.DocumentClient( new Uri(ConfigurationManager.AppSettings["DocumentDbUri"]), ConfigurationManager.AppSettings["DocumentDbKey"]); _documentDatabase = _documentDbClient.CreateDatabaseQuery() .Where(db => db.Id == DatabaseStore).AsEnumerable().FirstOrDefault(); //Create database and collection if not present CreateDatabaseIfNotExistsAsync().Wait(); //Create Seed Data CreateDocumentsAsync(_customerCollection.SelfLink).Wait(); //Query inserted customers QueryAllDocuments(_customerCollection.SelfLink); //Query subdocuments(orders) QueryWithSubdocuments(_customerCollection.SelfLink); //Query all customer first and last name with order number with amount greate than $300.00 QueryWithTwoJoinsAndFilter(_customerCollection.SelfLink); //Create partitions InitializeRangeResolverAsync().Wait(); //Run queries agianst different partitions and see where the documents are stored QueryPartitionAsync().Wait(); //Update and delete the document UpdateAndDeleteDocumentAsync().Wait(); } catch (Db.DocumentClientException de) { var baseException = de.GetBaseException(); Console.WriteLine("{0} error occurred: {1}, Message: {2}", de.StatusCode, de.Message, baseException.Message); } catch (Exception e) { var baseException = e.GetBaseException(); Console.WriteLine("Error: {0}, Message: {1}", e.Message, baseException.Message); } finally { //Cleanup CleanUpAsync().Wait(); Console.WriteLine("End of demo, press any key to exit."); Console.ReadKey(); } }
private async Task CreateDocumentCollection(string databaseName, string collectionName) { // Example of creating a DocumentCollection (not used for this demo) using (var client = new Microsoft.Azure.Documents.Client.DocumentClient(new Uri(settings.URI), settings.Key)) { var db = await GetOrCreateDatabaseAsync(client, "MoviesDatabase"); var collection = new Microsoft.Azure.Documents.DocumentCollection { Id = collectionName }; collection.PartitionKey.Paths.Add("/id"); var c1 = await client.CreateDocumentCollectionAsync(db.SelfLink, collection); } }
/// <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 (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(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); } }
private async Task <Microsoft.Azure.Documents.Database> GetOrCreateDatabaseAsync(Microsoft.Azure.Documents.Client.DocumentClient client, string databaseId) { var database = client.CreateDatabaseQuery().Where(db => db.Id == databaseId).ToArray().FirstOrDefault(); if (database == null) { // Create the database. database = await client.CreateDatabaseAsync(new Microsoft.Azure.Documents.Database { Id = databaseId }); } return(database); }