CapacityTestData ITableStorageWriterService.WriteToStorage() { var dataGuid = Guid.NewGuid(); try { var sw = new Stopwatch(); sw.Start(); var data = new CapacityTestData { PartitionKey = "Synchronous", RowKey = dataGuid.ToString().ToUpper(), RequestStart = DateTime.UtcNow, }; CloudTableClient cloudTableClient = GetCloudTableClient(); cloudTableClient.CreateTableIfNotExist(TableName); TableServiceContext context = cloudTableClient.GetDataServiceContext(); context.AddObject(TableName, data); DataServiceResponse response1 = context.SaveChangesWithRetries(); sw.Stop(); data.ElapsedMilliseconds = sw.ElapsedMilliseconds; data.RequestEnd = data.RequestStart + TimeSpan.FromMilliseconds(data.ElapsedMilliseconds); context.UpdateObject(data); DataServiceResponse response2 = context.SaveChangesWithRetries(); return data; } catch(Exception ex) { throw new Exception(string.Format("An error occurred synchronously saving the data '{0}'.", dataGuid), ex); } }
Task<CapacityTestData> ITableStorageWriterService.WriteToStorageAsyncTpl() { var dataGuid = Guid.NewGuid(); var sw = new Stopwatch(); sw.Start(); var data = new CapacityTestData { PartitionKey = "Asynchronous TPL", RowKey = dataGuid.ToString().ToUpper(), RequestStart = DateTime.UtcNow, }; CloudTableClient cloudTableClient = GetCloudTableClient(); TableServiceContext context = cloudTableClient.GetDataServiceContext(); var tcs = new TaskCompletionSource<CapacityTestData>(); cloudTableClient.CreateTableIfNotExistAsync(TableName) .ContinueWith(task1 => { if(task1.Exception != null) { var outerEx = new Exception(string.Format("An error occurred asynchronously (TPL) saving the data '{0}'.", dataGuid), task1.Exception.InnerException); tcs.SetException(outerEx); return; } context.AddObject(TableName, data); context.SaveChangesWithRetriesAsync() .ContinueWith(task2 => { if (task2.Exception != null) { var outerEx = new Exception(string.Format("An error occurred asynchronously (TPL) saving the data '{0}'.", dataGuid), task2.Exception.InnerException); tcs.SetException(outerEx); return; } sw.Stop(); data.ElapsedMilliseconds = sw.ElapsedMilliseconds; data.RequestEnd = data.RequestStart + TimeSpan.FromMilliseconds(data.ElapsedMilliseconds); context.UpdateObject(data); context .SaveChangesWithRetriesAsync() .ContinueWith(task3 => { if (task3.Exception != null) { var outerEx = new Exception(string.Format("An error occurred asynchronously (TPL) saving the data '{0}'.", dataGuid), task3.Exception.InnerException); tcs.SetException(outerEx); return; } tcs.SetResult(data); }); }); }); return tcs.Task; }
internal static void WriteResultsToResponse(HttpResponse httpResponse, CapacityTestData data) { httpResponse.ContentType = "text/plain"; httpResponse.Write(string.Format("{0} result {1}", data.PartitionKey, TimeSpan.FromMilliseconds(data.ElapsedMilliseconds))); }