public ContentionController(PerformanceDataContext context) { _MetricContext = context ?? throw new ArgumentNullException(nameof(context)); }
public void TestPerfData() { // Trace.TraceInformation("Starting TestPerfData"); Trace.TraceInformation("Starting TestPerfData", "Information"); try { var account = Microsoft.WindowsAzure.CloudStorageAccount.FromConfigurationSetting("DataConnectionString"); var context = new PerformanceDataContext(account.TableEndpoint.ToString(), account.Credentials); var data = context.PerfData; DateTime dtnow = DateTime.UtcNow.AddMinutes(-30); DateTime dtnow2 = DateTime.UtcNow.AddMinutes(-20); System.Collections.Generic.List <PerformanceData> selectedData = (from d in data where d.CounterName == @"\Processor(_Total)\% Processor Time" && d.EventTickCount >= dtnow.Ticks && d.EventTickCount <= dtnow2.Ticks && d.Role == "ProjectCS218" select d).ToList <PerformanceData>(); Trace.TraceInformation(selectedData.Count.ToString() + " " + dtnow.Ticks + " " + dtnow2.Ticks);; double AvgCPU = selectedData.Where(p => p.CounterName == @"\Processor(_Total)\% Processor Time").DefaultIfEmpty().Average(p => p == null ? 0 : p.CounterValue); /* double AvgCPU = (from d in selectedData * where d.CounterName == @"\Processor(_Total)\% Processor Time" * select d.CounterValue).Average();*/ //Trace.WriteLine("Average CPU == " + AvgCPU.ToString()); Trace.TraceInformation("Average CPU == " + AvgCPU.ToString(), "Information Role Name"); Trace.TraceInformation("Max Time Stamp CPU == " + selectedData.Max(item => item.Timestamp).ToString(), "Information"); Trace.TraceInformation("Min Time Stamp CPU == " + selectedData.Min(item => item.Timestamp).ToString(), "Information"); //test threshold if (AvgCPU >= 1) { Trace.TraceInformation("Increase the instance by adding one more role" + AvgCPU.ToString(), "Information"); // Trace.TraceInformation(selectedData.Max(item => item.Timestamp).ToString(), "Information"); //Trace.TraceInformation(selectedData.Min(item => item.Timestamp).ToString(), "Information"); //increase instances string deploymentInfo = AzureRESTMgmtHelper.GetDeploymentInfo(); string svcconfig = AzureRESTMgmtHelper.GetServiceConfig(deploymentInfo); int InstanceCount = System.Convert.ToInt32(AzureRESTMgmtHelper.GetInstanceCount(svcconfig, "ProjectCS218")); if (InstanceCount == 1) { InstanceCount++; Trace.TraceInformation("Average CPU == " + AvgCPU.ToString(), "Information" + " Instance Count increased by 1"); string UpdatedSvcConfig = AzureRESTMgmtHelper.ChangeInstanceCount(svcconfig, "ProjectCS218", InstanceCount.ToString()); AzureRESTMgmtHelper.ChangeConfigFile(UpdatedSvcConfig); } } else if (AvgCPU < 1) //50 { Trace.TraceInformation("in the AvgCPU < 5 test."); string deploymentInfo = AzureRESTMgmtHelper.GetDeploymentInfo(); string svcconfig = AzureRESTMgmtHelper.GetServiceConfig(deploymentInfo); int InstanceCount = System.Convert.ToInt32(AzureRESTMgmtHelper.GetInstanceCount(svcconfig, "ProjectCS218")); if (InstanceCount > 1) { InstanceCount--; string UpdatedSvcConfig = AzureRESTMgmtHelper.ChangeInstanceCount(svcconfig, "ProjectCS218", InstanceCount.ToString()); AzureRESTMgmtHelper.ChangeConfigFile(UpdatedSvcConfig); } } } catch (System.Exception ex) { Trace.TraceInformation(ex.StackTrace, "Information"); Trace.WriteLine("Worker Role Error: " + ex.Message); } }