Changeset 12146


Ignore:
Timestamp:
03/05/15 17:09:10 (3 years ago)
Author:
ascheibe
Message:

#2353

  • improved performance of status page
  • fixed code formatting and usings
  • simplified some expressions
Location:
trunk/sources
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Services.Hive.Web/Hive-3.3/Status.aspx.cs

    r12012 r12146  
    2323using System.Collections.Generic;
    2424using System.Linq;
    25 using System.Web;
    26 using System.Web.UI;
    2725using System.Web.UI.WebControls;
    28 using HeuristicLab.Services.Hive.DataAccess;
    2926using HeuristicLab.Services.Hive;
    30 using HeuristicLab.Services.Hive.DataTransfer;
    31 using System.Text;
    3227using System.Web.UI.DataVisualization.Charting;
    3328using DA = HeuristicLab.Services.Hive.DataAccess;
     
    3732  protected void Page_Load(object sender, EventArgs e) {
    3833    var dao = ServiceLocator.Instance.HiveDao;
     34    var optDao = ServiceLocator.Instance.OptimizedHiveDao;
    3935    var transactionManager = ServiceLocator.Instance.TransactionManager;
    4036    var resourceName = Request.QueryString["resource"];
    41     IEnumerable<Guid> resourceIds = new List<Guid>();
    42     IEnumerable<DT.Slave> onlineSlaves = new List<DT.Slave>();
     37    List<Guid> resourceIds = new List<Guid>();
     38    List<DT.Slave> onlineSlaves = new List<DT.Slave>();
    4339    int currentlyJobsWaiting = 0;
    44     Dictionary<Guid, int> calculatingTasksByUser = new Dictionary<Guid,int>();
     40    Dictionary<Guid, int> calculatingTasksByUser = new Dictionary<Guid, int>();
    4541    Dictionary<Guid, int> waitingTasksByUser = new Dictionary<Guid, int>();
    4642    List<DT.Resource> groups = new List<DT.Resource>();
    4743
    4844    transactionManager.UseTransaction(() => {
    49        groups = dao.GetResources(x => x.ResourceType == "GROUP").ToList();
    50     }, false, false);   
     45      groups = dao.GetResources(x => x.ResourceType == "GROUP").ToList();
     46    }, false, false);
    5147
    5248    if (!string.IsNullOrEmpty(resourceName)) {
    53         transactionManager.UseTransaction(() =>
    54         {
    55             var resId = dao.GetResources(x => x.Name == resourceName).Single().Id;
    56             resourceIds = dao.GetChildResources(resId).Select(x => x.Id).Union(new List<Guid> { resId });
    57             calculatingTasksByUser = dao.GetCalculatingTasksByUserForResources(resourceIds.ToList());
    58             waitingTasksByUser = dao.GetWaitingTasksByUserForResources(resourceIds.ToList());
    59         }, false, false);       
     49      transactionManager.UseTransaction(() => {
     50        var resId = dao.GetResources(x => x.Name == resourceName).Single().Id;
     51        resourceIds = dao.GetChildResources(resId).Select(x => x.Id).Union(new List<Guid> { resId }).ToList();
     52        calculatingTasksByUser = dao.GetCalculatingTasksByUserForResources(resourceIds);
     53        waitingTasksByUser = dao.GetWaitingTasksByUserForResources(resourceIds);
     54      }, false, false);
    6055    } else {
    61         transactionManager.UseTransaction(() =>
    62         {
    63              resourceIds = dao.GetResources(x => true).Select(y => y.Id);
    64              calculatingTasksByUser = dao.GetCalculatingTasksByUser();
    65              waitingTasksByUser = dao.GetWaitingTasksByUser();           
    66         }, false, false);
    67     }   
     56      transactionManager.UseTransaction(() => {
     57        resourceIds = optDao.GetAllResourceIds().ToList();
     58        calculatingTasksByUser = optDao.GetCalculatingTasksByUser();
     59        waitingTasksByUser = optDao.GetWaitingTasksByUser();
     60      }, false, false);
     61    }
    6862
    69     transactionManager.UseTransaction(() =>
    70     {                     
    71         onlineSlaves = dao.GetSlaves(x => (x.SlaveState == DA.SlaveState.Calculating || x.SlaveState == DA.SlaveState.Idle) && resourceIds.Contains(x.ResourceId));
    72         currentlyJobsWaiting = dao.GetLightweightTasks(x => x.State == DA.TaskState.Waiting).Count();           
     63    transactionManager.UseTransaction(() => {
     64      if (string.IsNullOrEmpty(resourceName)) {
     65        onlineSlaves = dao.GetSlaves(x => x.SlaveState == DA.SlaveState.Calculating || x.SlaveState == DA.SlaveState.Idle).ToList();
     66      } else {
     67        onlineSlaves = dao.GetSlaves(x => (x.SlaveState == DA.SlaveState.Calculating || x.SlaveState == DA.SlaveState.Idle) && resourceIds.Contains(x.ResourceId)).ToList();
     68      }
     69      currentlyJobsWaiting = optDao.GetNumberOfWaitingTasks();
    7370    }, false, false);
    7471
     
    7673    int currentlyAvailableCores = onlineSlaves.Where(s => s.Cores.HasValue && s.IsAllowedToCalculate).Sum(s => s.Cores.Value);
    7774    int currentlyUsedCores = overallCurrentlyAvailableCores - onlineSlaves.Where(s => s.FreeCores.HasValue).Sum(s => s.FreeCores.Value);
    78    
     75
    7976    this.overallAvailableCoresLabel.Text = overallCurrentlyAvailableCores.ToString();
    8077    this.availableCoresLabel.Text = currentlyAvailableCores.ToString();
     
    8683    groupsLabel.Text += string.Join(", ", groups.Select(x => string.Format("<a href=\"?resource={0}\">{0}</a>", x.Name)));
    8784
    88     overallCpuUtilizationLabel.Text = (onlineSlaves.Count() > 0 ? Math.Round(onlineSlaves.Average(s => s.CpuUtilization), 2).ToString() : "0.0") + " %";
    89     cpuUtilizationLabel.Text = (onlineSlaves.Count() > 0 && onlineSlaves.Where(x => x.IsAllowedToCalculate).Count() > 0 ? Math.Round(onlineSlaves.Where(x => x.IsAllowedToCalculate).Average(s => s.CpuUtilization), 2).ToString() : "0.0") + " %";
     85    overallCpuUtilizationLabel.Text = (onlineSlaves.Any() ? Math.Round(onlineSlaves.Average(s => s.CpuUtilization), 2).ToString() : "0.0") + " %";
     86    cpuUtilizationLabel.Text = (onlineSlaves.Any() && onlineSlaves.Any(x => x.IsAllowedToCalculate) ? Math.Round(onlineSlaves.Where(x => x.IsAllowedToCalculate).Average(s => s.CpuUtilization), 2).ToString() : "0.0") + " %";
    9087
    91     DT.Statistics[] stats = new DT.Statistics[0];   
    92     transactionManager.UseTransaction(() =>
    93     {   
    94         if (daysDropDownList.SelectedValue == "All") {
    95           stats = dao.GetStatistics(x => true).OrderBy(x => x.TimeStamp).ToArray();
    96         } else {
    97           stats = dao.GetStatistics(x => x.Timestamp >= DateTime.Now.Subtract(TimeSpan.FromDays(int.Parse(daysDropDownList.SelectedValue)))).OrderBy(x => x.TimeStamp).ToArray();
    98         }
     88    DT.Statistics[] stats = new DT.Statistics[0];
     89    transactionManager.UseTransaction(() => {
     90      if (daysDropDownList.SelectedValue == "All") {
     91        stats = dao.GetStatistics(x => true).OrderBy(x => x.TimeStamp).ToArray();
     92      } else {
     93        stats = dao.GetStatistics(x => x.Timestamp >= DateTime.Now.Subtract(TimeSpan.FromDays(int.Parse(daysDropDownList.SelectedValue)))).OrderBy(x => x.TimeStamp).ToArray();
     94      }
    9995    }, false, false);
    100    
     96
    10197    for (int i = 0; i < stats.Length; i++) {
    10298      var s = stats[i];
    103       var slaveStats = s.SlaveStatistics.Where(x => resourceIds.Contains(x.SlaveId));
     99      var slaveStats = s.SlaveStatistics.Where(x => resourceIds.Contains(x.SlaveId)).ToArray();
    104100
    105       var averageCpuUtilization = slaveStats.Count() > 0 ? slaveStats.Average(x => x.CpuUtilization) : 0.0;
     101      var averageCpuUtilization = slaveStats.Any() ? slaveStats.Average(x => x.CpuUtilization) : 0.0;
    106102      cpuUtilizationChart.Series[0].Points.Add(new DataPoint(s.TimeStamp.ToOADate(), averageCpuUtilization));
    107103
     
    118114    }
    119115
    120     GenerateTasksByUserTable(waitingTasksByUser, waitingTasksByUserTable); 
    121     GenerateTasksByUserTable(calculatingTasksByUser, calculatingTasksByUserTable); 
     116    GenerateTasksByUserTable(waitingTasksByUser, waitingTasksByUserTable);
     117    GenerateTasksByUserTable(calculatingTasksByUser, calculatingTasksByUserTable);
    122118  }
    123119
     
    134130      table.Rows.Add(curRow);
    135131    }
    136     if (tasksByUser.Count() > 0) {
    137       TableRow sumRow = new TableRow();       
     132    if (tasksByUser.Any()) {
     133      TableRow sumRow = new TableRow();
    138134      TableCell sumCell = new TableCell();
    139135      sumCell.BorderWidth = Unit.Pixel(3);
     
    143139      table.Rows.Add(sumRow);
    144140    }
    145   } 
     141  }
    146142}
  • trunk/sources/HeuristicLab.Services.Hive/3.3/HiveDao.cs

    r12012 r12146  
    869869    }
    870870
    871     public Dictionary<Guid, int> GetWaitingTasksByUser() {
    872       using (var db = CreateContext()) {
    873         var waitingTasksByUser = from task in db.Tasks
    874                                  where task.State == TaskState.Waiting
    875                                  group task by task.Job.OwnerUserId into g
    876                                  select new { UserId = g.Key, UsedCores = g.Count() };
    877         return waitingTasksByUser.ToDictionary(x => x.UserId, x => x.UsedCores);
    878       }
    879     }
    880 
    881871    public Dictionary<Guid, int> GetWaitingTasksByUserForResources(List<Guid> resourceIds) {
    882872      using (var db = CreateContext()) {
     
    886876                                 select new { UserId = g.Key, UsedCores = g.Count() };
    887877        return waitingTasksByUser.ToDictionary(x => x.UserId, x => x.UsedCores);
    888       }
    889     }
    890 
    891     public Dictionary<Guid, int> GetCalculatingTasksByUser() {
    892       using (var db = CreateContext()) {
    893         var calculatingTasksByUser = from task in db.Tasks
    894                                      where task.State == TaskState.Calculating
    895                                      group task by task.Job.OwnerUserId into g
    896                                      select new { UserId = g.Key, UsedCores = g.Count() };
    897         return calculatingTasksByUser.ToDictionary(x => x.UserId, x => x.UsedCores);
    898878      }
    899879    }
  • trunk/sources/HeuristicLab.Services.Hive/3.3/Interfaces/IHiveDao.cs

    r12012 r12146  
    150150
    151151    #region Statistics Methods
    152     Dictionary<Guid, int> GetWaitingTasksByUser();
    153152    Dictionary<Guid, int> GetWaitingTasksByUserForResources(List<Guid> resourceIds);
    154     Dictionary<Guid, int> GetCalculatingTasksByUser();
    155153    Dictionary<Guid, int> GetCalculatingTasksByUserForResources(List<Guid> resourceIds);
    156154    DT.Statistics GetStatistic(Guid id);
  • trunk/sources/HeuristicLab.Services.Hive/3.3/Interfaces/IOptimizedHiveDao.cs

    r12012 r12146  
    6565    IEnumerable<Guid> GetAssignedResourceIds(Guid jobId);
    6666    #endregion
     67
     68    #region Website Methods
     69
     70    IEnumerable<Guid> GetAllResourceIds();
     71
     72    int GetNumberOfWaitingTasks();
     73
     74    Dictionary<Guid, int> GetCalculatingTasksByUser();
     75
     76    Dictionary<Guid, int> GetWaitingTasksByUser();
     77
     78    #endregion
    6779  }
    6880}
  • trunk/sources/HeuristicLab.Services.Hive/3.3/OptimizedHiveDao.cs

    r12012 r12146  
    245245    );
    246246    #endregion
     247
     248
     249    #region Website Methods
     250    private const string GetAllResourceIdsString = @"SELECT ResourceId FROM [Resource]";
     251    public IEnumerable<Guid> GetAllResourceIds() {
     252      return Db.ExecuteQuery<Guid>(GetAllResourceIdsString);
     253    }
     254
     255    private const string GetNumberOfWaitingTasksString = @"SELECT COUNT(TaskId)
     256                                                           FROM [Task]
     257                                                           WHERE TaskState LIKE 'Waiting'";
     258    public int GetNumberOfWaitingTasks() {
     259      return Db.ExecuteQuery<int>(GetNumberOfWaitingTasksString).Single();
     260    }
     261
     262    private class UserTasks {
     263      public Guid OwnerUserId;
     264      public int Count;
     265    }
     266
     267    private const string GetCalculatingTasksByUserString = @"SELECT Job.OwnerUserId, COUNT(Task.TaskId) as Count
     268                                                             FROM Task, Job
     269                                                             WHERE TaskState LIKE 'Calculating' AND Task.JobId = Job.JobId
     270                                                             GROUP BY Job.OwnerUserId";
     271
     272    public Dictionary<Guid, int> GetCalculatingTasksByUser() {
     273      var result = Db.ExecuteQuery<UserTasks>(GetCalculatingTasksByUserString);
     274      Dictionary<Guid, int> lst = new Dictionary<Guid, int>();
     275
     276      foreach (var userTask in result) {
     277        lst.Add(userTask.OwnerUserId, userTask.Count);
     278      }
     279      return lst;
     280    }
     281
     282    private const string GetWaitingTasksByUserString = @"SELECT Job.OwnerUserId, COUNT(Task.TaskId) as Count
     283                                                         FROM Task, Job
     284                                                         WHERE TaskState LIKE 'Waiting' AND Task.JobId = Job.JobId
     285                                                         GROUP BY Job.OwnerUserId";
     286
     287    public Dictionary<Guid, int> GetWaitingTasksByUser() {
     288      var result = Db.ExecuteQuery<UserTasks>(GetWaitingTasksByUserString);
     289      Dictionary<Guid, int> lst = new Dictionary<Guid, int>();
     290
     291      foreach (var userTask in result) {
     292        lst.Add(userTask.OwnerUserId, userTask.Count);
     293      }
     294      return lst;
     295    }
     296    #endregion
    247297  }
    248298}
Note: See TracChangeset for help on using the changeset viewer.