public static async Task ReadBenchmark(Benchmark benchmark) { //Verify the benchmark is setup await Benchmark.InitializeBenchmark(benchmark); //Fetch the id values to do point reads on List <string> ids = await GetIds(benchmark); //Console.WriteLine($"\nTest {ids.Count} {GetBenchmarkType(benchmark)} against {benchmark.testName} account in {benchmark.readRegion} from {benchmark.testRegion}\nPress any key to continue\n..."); Console.WriteLine($"\n{benchmark.testDescription}\nPress any key to continue\n..."); Console.ReadKey(true); int test = 0; List <Result> results = new List <Result>(); //Individual Benchmark results Stopwatch stopwatch = new Stopwatch(); foreach (string id in ids) { stopwatch.Start(); ItemResponse <SampleCustomer> response = await benchmark.container.ReadItemAsync <SampleCustomer>(id : id, partitionKey : new PartitionKey(benchmark.partitionKeyValue)); stopwatch.Stop(); Console.WriteLine($"Read {test++} of {ids.Count}, region: {benchmark.readRegion}, Latency: {stopwatch.ElapsedMilliseconds} ms, Request Charge: {response.RequestCharge} RUs"); results.Add(new Result(stopwatch.ElapsedMilliseconds, response.RequestCharge)); stopwatch.Reset(); } OutputResults(benchmark, results); }
public async Task Initialize() { try { Console.WriteLine("Single/Multi Region Initialize"); foreach (Benchmark benchmark in benchmarks) { await Benchmark.InitializeBenchmark(benchmark); } } catch (Exception e) { Console.WriteLine(e.Message + "\nPress any key to continue"); Console.ReadKey(); } }
public static async Task CustomSyncBenchmark(Benchmark benchmark) { //Verify the benchmark is setup await Benchmark.InitializeBenchmark(benchmark); //Customers to insert List <SampleCustomer> customers = SampleCustomer.GenerateCustomers(benchmark.partitionKeyValue, 100); //Create Read client for custom sync CosmosClient syncClient = new CosmosClient(benchmark.endpoint, benchmark.key, new CosmosClientOptions { ApplicationRegion = benchmark.readRegion }); Container syncContainer = syncClient.GetContainer(benchmark.databaseId, benchmark.containerId); //Console.WriteLine($"\nTest {customers.Count} {GetBenchmarkType(benchmark)} against {benchmark.testName} account in {benchmark.writeRegion} from {benchmark.testName}\nPress any key to continue\n..."); Console.WriteLine($"\n{benchmark.testDescription}\nPress any key to continue\n..."); Console.ReadKey(true); int test = 0; List <Result> results = new List <Result>(); //Individual Benchmark results Stopwatch stopwatch = new Stopwatch(); foreach (SampleCustomer customer in customers) { stopwatch.Start(); //write item in one region ItemResponse <SampleCustomer> response = await benchmark.container.CreateItemAsync <SampleCustomer>(customer, new PartitionKey(benchmark.partitionKeyValue)); //read back in a second passing the session token with the LSN for the write ItemResponse <SampleCustomer> syncResponse = await syncContainer.ReadItemAsync <SampleCustomer>( customer.Id, new PartitionKey(benchmark.partitionKeyValue), new ItemRequestOptions { SessionToken = response.Headers.Session }); stopwatch.Stop(); Console.WriteLine($"Write {test++} of {customers.Count}, region: {benchmark.writeRegion}, Latency: {stopwatch.ElapsedMilliseconds} ms, Request Charge: {response.RequestCharge} RUs"); results.Add(new Result(stopwatch.ElapsedMilliseconds, response.RequestCharge)); stopwatch.Reset(); } OutputResults(benchmark, results); }
public async Task RunBenchmarks() { try { Console.WriteLine($"Test reads from Multi-Master account during failover\n{Helpers.Line}\nPlease Wait..."); //Run benchmarks, collect results Benchmark benchmark = benchmarks[0]; //Verify the benchmark is setup await Benchmark.InitializeBenchmark(benchmark); List <string> ids = await Benchmark.GetIds(benchmark); string id = ids[0]; bool exit = false; int seconds = 0; string initialRegion = ""; string failoverRegion; ItemResponse <SampleCustomer> response = await benchmark.container.ReadItemAsync <SampleCustomer>(id : id, partitionKey : new PartitionKey(benchmark.partitionKeyValue)); initialRegion = Helpers.ParseRegionFromDiag(response.Diagnostics.ToString()); failoverRegion = initialRegion; string regionalEndpoint = Helpers.ParseEndpointFromDiag(response.Diagnostics.ToString()); string ipAddress = Dns.GetHostAddresses(regionalEndpoint)[0].ToString(); Console.WriteLine($"Initiate Reads from {initialRegion}"); Console.WriteLine($"Simulating regional failure by creating a Firewall Rule blocking the ip address for {initialRegion}"); Firewall.AddFirewallRule("Failover Test", ipAddress); while (!exit) { response = await benchmark.container.ReadItemAsync <SampleCustomer>(id : id, partitionKey : new PartitionKey(benchmark.partitionKeyValue)); failoverRegion = Helpers.ParseRegionFromDiag(response.Diagnostics.ToString()); Console.WriteLine($"Read Region: {failoverRegion}, IpAddress: {ipAddress}"); //We need to do a create operation to force the diagnostic strings to flip in the SDK client await benchmark.client.CreateDatabaseIfNotExistsAsync("newDB"); if (failoverRegion != initialRegion) { Console.WriteLine($"Region failover has occurred. Complete in {seconds} seconds\nPress any key to continue\n..."); Console.ReadKey(true); Firewall.RemoveFirewallRule("Failover Test"); exit = true; } await Task.Delay(1000); seconds++; } } catch (Exception e) { Console.WriteLine(e.Message + "\nPress any key to continue"); Console.ReadKey(); } }