Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Services.Hive.Tests/DaoTests.cs @ 6229

Last change on this file since 6229 was 6229, checked in by cneumuel, 14 years ago

#1233

  • added basic statistics recording for (once per minute)
    • executiontime per user
    • usedcores, usedmemory per slave
File size: 12.3 KB
Line 
1using System;
2using System.Linq;
3using HeuristicLab.Services.Hive.Common.DataTransfer;
4using HeuristicLab.Services.Hive.Common.ServiceContracts;
5using HeuristicLab.Services.Hive.DataAccess;
6using Microsoft.VisualStudio.TestTools.UnitTesting;
7
8namespace HeuristicLab.Services.Hive.Tests {
9  using System.Collections.Generic;
10  using System.Threading;
11  using DT = HeuristicLab.Services.Hive.Common.DataTransfer;
12
13  [TestClass]
14  public class DaoTests {
15
16    [ClassInitialize]
17    public static void MyClassInitialize(TestContext testContext) {
18      ServiceLocator.Instance = new MockServiceLocator(ServiceLocator.Instance);
19    }
20
21    private IHiveService GetLocalService() {
22      return new HiveService();
23    }
24
25    [TestMethod]
26    public void TestJobDao() {
27      IHiveDao dao = ServiceLocator.Instance.HiveDao;
28
29      DT.HiveExperiment he = new DT.HiveExperiment();
30      he.DateCreated = DateTime.Now;
31      he.Name = "TestExperiment";
32      he.OwnerUserId = Guid.NewGuid();
33      he.ResourceNames = "HEAL";
34      he.Id = dao.AddHiveExperiment(he);
35
36      DT.Job job1 = new DT.Job();
37      job1.State = JobState.Offline;
38      job1.StateLog.Add(new DT.StateLog { State = JobState.Offline, DateTime = DateTime.Now });
39      job1.Command = Command.Pause;
40      job1.HiveExperimentId = he.Id;
41
42      DT.JobData jobData1 = new DT.JobData();
43      jobData1.Data = new byte[] { 0, 1, 2, 3, 4, 5 };
44      jobData1.LastUpdate = DateTime.Now;
45
46      DT.Plugin plugin1 = new DT.Plugin();
47      plugin1.Name = "Tests.MyPlugin";
48      plugin1.Version = new Version("1.0.0.0");
49      plugin1.UserId = Guid.Empty;
50      plugin1.IsLocal = true;
51      plugin1.DateCreated = DateTime.Now;
52
53      DT.PluginData pluginData1 = new DT.PluginData();
54      pluginData1.PluginId = plugin1.Id;
55      pluginData1.FileName = "Tests.MyPlugin-1.0.dll";
56      pluginData1.Data = new byte[] { 0, 1, 2, 3, 4, 5 };
57
58      plugin1.Id = dao.AddPlugin(plugin1);
59      pluginData1.PluginId = plugin1.Id;
60      pluginData1.Id = dao.AddPluginData(pluginData1);
61
62      job1.PluginsNeededIds.Add(plugin1.Id);
63
64      job1.Id = dao.AddJob(job1);
65      jobData1.JobId = job1.Id;
66      dao.AddJobData(jobData1);
67
68      DT.Job job1loaded = dao.GetJob(job1.Id);
69      Assert.AreEqual(job1.Id, job1loaded.Id);
70      Assert.AreEqual(job1.CoresNeeded, job1loaded.CoresNeeded);
71      Assert.AreEqual(job1.DateCreated.ToString(), job1loaded.DateCreated.ToString());
72      Assert.AreEqual(null, job1loaded.DateFinished);
73      Assert.IsTrue(job1.PluginsNeededIds.SequenceEqual(job1loaded.PluginsNeededIds));
74      Assert.AreEqual(job1.StateLog.Count, job1loaded.StateLog.Count);
75      Assert.AreEqual(job1.Command, job1loaded.Command);
76      Assert.AreEqual(job1.HiveExperimentId, job1loaded.HiveExperimentId);
77      Assert.IsTrue(Math.Abs((job1loaded.LastJobDataUpdate - jobData1.LastUpdate).TotalSeconds) < 1);
78      for (int i = 0; i < job1.StateLog.Count; i++) {
79        Assert.AreEqual(job1.Id, job1loaded.StateLog[i].JobId);
80        Assert.AreEqual(job1.StateLog[i].State, job1loaded.StateLog[i].State);
81        Assert.AreEqual(job1.StateLog[i].SlaveId, job1loaded.StateLog[i].SlaveId);
82        Assert.AreEqual(job1.StateLog[i].UserId, job1loaded.StateLog[i].UserId);
83        Assert.AreEqual(job1.StateLog[i].Exception, job1loaded.StateLog[i].Exception);
84        Assert.IsTrue(Math.Abs((job1.StateLog[i].DateTime - job1loaded.StateLog[i].DateTime).TotalSeconds) < 1);
85      }
86
87      job1 = job1loaded;
88
89      // test jobstates
90      job1.StateLog.Add(new DT.StateLog { State = JobState.Transferring, DateTime = DateTime.Now }); Thread.Sleep(10);
91      job1.StateLog.Add(new DT.StateLog { State = JobState.Calculating, DateTime = DateTime.Now }); Thread.Sleep(10);
92      job1.StateLog.Add(new DT.StateLog { State = JobState.Transferring, DateTime = DateTime.Now }); Thread.Sleep(10);
93      job1.StateLog.Add(new DT.StateLog { State = JobState.Finished, DateTime = DateTime.Now }); Thread.Sleep(10);
94      dao.UpdateJob(job1);
95
96      job1loaded = dao.GetJob(job1.Id);
97      for (int i = 0; i < job1.StateLog.Count; i++) {
98        Assert.AreEqual(job1.Id, job1loaded.StateLog[i].JobId);
99        Assert.AreEqual(job1.StateLog[i].State, job1loaded.StateLog[i].State);
100        Assert.AreEqual(job1.StateLog[i].SlaveId, job1loaded.StateLog[i].SlaveId);
101        Assert.AreEqual(job1.StateLog[i].UserId, job1loaded.StateLog[i].UserId);
102        Assert.AreEqual(job1.StateLog[i].Exception, job1loaded.StateLog[i].Exception);
103        Assert.IsTrue(Math.Abs((job1.StateLog[i].DateTime - job1loaded.StateLog[i].DateTime).TotalSeconds) < 1);
104      }
105
106      DT.JobData jobData1Loaded = dao.GetJobData(job1.Id);
107      Assert.AreEqual(jobData1.JobId, jobData1Loaded.JobId);
108      Assert.IsTrue(Math.Abs((jobData1.LastUpdate - jobData1Loaded.LastUpdate).TotalSeconds) < 1);
109      Assert.IsTrue(jobData1.Data.SequenceEqual(jobData1Loaded.Data));
110
111      dao.DeleteHiveExperiment(he.Id);
112
113      Assert.AreEqual(null, dao.GetJob(job1.Id));
114      Assert.AreEqual(null, dao.GetJobData(job1.Id));
115      Assert.AreEqual(null, dao.GetHiveExperiment(he.Id));
116    }
117
118    [TestMethod]
119    public void TestSlaveDao() {
120      IHiveDao dao = ServiceLocator.Instance.HiveDao;
121      DT.SlaveGroup slaveGroup = new DT.SlaveGroup() {
122        Name = "Test"
123      };
124      slaveGroup.Id = dao.AddSlaveGroup(slaveGroup);
125
126      DT.Slave slave = new DT.Slave() {
127        Id = Guid.NewGuid(),
128        Name = "Test",
129        OperatingSystem = null, //"Windows 3.11",
130        Cores = 2,
131        Memory = 1024,
132        FreeMemory = 512
133      };
134
135      Assert.AreEqual(slave.Id, dao.AddSlave(slave));
136
137      // update
138      slave.FreeMemory = 255;
139      slave.OperatingSystem = Environment.OSVersion.VersionString;
140      dao.UpdateSlave(slave);
141
142      DT.Slave slaveLoaded = dao.GetSlave(slave.Id);
143      Assert.AreEqual(slave.FreeMemory, slaveLoaded.FreeMemory);
144
145      dao.DeleteSlave(slave.Id);
146      dao.DeleteSlaveGroup(slaveGroup.Id);
147    }
148
149    [TestMethod]
150    public void TestPluginDao() {
151      IHiveDao dao = ServiceLocator.Instance.HiveDao;
152
153      DT.Plugin plugin1 = new DT.Plugin();
154      plugin1.DateCreated = DateTime.Now;
155      plugin1.IsLocal = false;
156      plugin1.Name = "Tests.MyPlugin";
157      plugin1.Version = new Version("1.0.0.0");
158      plugin1.UserId = Guid.Empty;
159
160      plugin1.Id = dao.AddPlugin(plugin1);
161
162      DT.Plugin plugin1loaded = dao.GetPlugin(plugin1.Id);
163      Assert.AreEqual(plugin1.Id, plugin1loaded.Id);
164      Assert.AreEqual(plugin1.Name, plugin1loaded.Name);
165      Assert.AreEqual(plugin1.Version, plugin1loaded.Version);
166      Assert.AreEqual(plugin1.UserId, plugin1loaded.UserId);
167      Assert.AreEqual(plugin1.DateCreated.ToString(), plugin1loaded.DateCreated.ToString());
168      Assert.AreEqual(plugin1.IsLocal, plugin1loaded.IsLocal);
169
170      DT.PluginData pluginData1 = new DT.PluginData();
171      pluginData1.PluginId = plugin1.Id;
172      pluginData1.FileName = "Tests.MyPlugin-1.0.dll";
173      pluginData1.Data = new byte[] { 0, 1, 2, 3, 4, 5 };
174
175      pluginData1.Id = dao.AddPluginData(pluginData1);
176
177      DT.PluginData pluginData1loaded = dao.GetPluginData(pluginData1.Id);
178      Assert.AreEqual(pluginData1.Id, pluginData1loaded.Id);
179
180      Assert.AreEqual(pluginData1.PluginId, pluginData1loaded.PluginId);
181      Assert.AreEqual(pluginData1.FileName, pluginData1loaded.FileName);
182      Assert.IsTrue(pluginData1.Data.SequenceEqual(pluginData1loaded.Data));
183
184      dao.DeletePluginData(pluginData1.Id);
185      dao.DeletePlugin(plugin1.Id);
186
187      Assert.AreEqual(null, dao.GetPlugin(plugin1.Id));
188      Assert.AreEqual(null, dao.GetPluginData(pluginData1.Id));
189    }
190
191    [TestMethod]
192    public void TestHiveExperimentDao() {
193      IHiveDao dao = ServiceLocator.Instance.HiveDao;
194
195      DT.HiveExperiment he = new DT.HiveExperiment();
196      he.DateCreated = DateTime.Now;
197      he.Name = "TestExperiment";
198      he.OwnerUserId = Guid.NewGuid();
199      he.ResourceNames = "HEAL";
200      he.Id = dao.AddHiveExperiment(he);
201
202      DT.Job job = new DT.Job();
203      job.State = JobState.Offline;
204      job.StateLog.Add(new DT.StateLog { State = JobState.Offline, DateTime = DateTime.Now });
205      job.HiveExperimentId = he.Id;
206      job.Id = dao.AddJob(job);
207
208      DT.HiveExperimentPermission perm = new DT.HiveExperimentPermission();
209      perm.HiveExperimentId = he.Id;
210      perm.GrantedByUserId = he.OwnerUserId;
211      perm.GrantedUserId = Guid.NewGuid();
212      perm.Permission = Permission.Write;
213      dao.AddHiveExperimentPermission(perm);
214
215      DT.HiveExperiment heLoaded = dao.GetHiveExperiment(he.Id);
216      Assert.AreEqual(he.Id, heLoaded.Id);
217      Assert.AreEqual(he.Name, heLoaded.Name);
218      Assert.AreEqual(he.ResourceNames, heLoaded.ResourceNames);
219      //Assert.AreEqual(he.LastAccessed, heLoaded.LastAccessed);
220      //Assert.AreEqual(he.DateCreated, heLoaded.DateCreated);
221
222      DT.Job jobLoaded = dao.GetJob(job.Id);
223      Assert.AreEqual(job.Id, jobLoaded.Id);
224      Assert.AreEqual(job.State, jobLoaded.State);
225      Assert.AreEqual(job.HiveExperimentId, jobLoaded.HiveExperimentId);
226
227      DT.HiveExperimentPermission permLoaded = dao.GetHiveExperimentPermission(he.Id, perm.GrantedUserId);
228      Assert.AreEqual(perm.HiveExperimentId, permLoaded.HiveExperimentId);
229      Assert.AreEqual(perm.GrantedUserId, permLoaded.GrantedUserId);
230      Assert.AreEqual(perm.GrantedByUserId, permLoaded.GrantedByUserId);
231      Assert.AreEqual(perm.Permission, permLoaded.Permission);
232
233      dao.DeleteHiveExperiment(he.Id);
234      Assert.AreEqual(null, dao.GetHiveExperiment(he.Id));
235      Assert.AreEqual(null, dao.GetJob(job.Id));
236      Assert.AreEqual(null, dao.GetHiveExperimentPermission(perm.HiveExperimentId, perm.GrantedUserId));
237
238    }
239
240
241    [TestMethod]
242    public void TestStatistics() {
243      IHiveDao dao = ServiceLocator.Instance.HiveDao;
244
245      var stats = new DT.Statistics();
246      stats.TimeStamp = DateTime.Now;
247      var slaveStats = new List<DT.SlaveStatistics>();
248      slaveStats.Add(new DT.SlaveStatistics() { Cores = 8, FreeCores = 3, Memory = 8000, FreeMemory = 3000, CpuUtilization = 0.8, SlaveId = Guid.NewGuid() });
249      slaveStats.Add(new DT.SlaveStatistics() { Cores = 2, FreeCores = 0, Memory = 1024, FreeMemory = 100, CpuUtilization = 0.99, SlaveId = Guid.NewGuid() });
250      slaveStats.Add(new DT.SlaveStatistics() { Cores = 4, FreeCores = 4, Memory = 3024, FreeMemory = 2300, CpuUtilization = 0.01, SlaveId = Guid.NewGuid() });
251      stats.SlaveStatistics = slaveStats;
252
253      var userStats = new List<DT.UserStatistics>();
254      userStats.Add(new DT.UserStatistics() { UsedCores = 15, ExecutionTime = TimeSpan.FromHours(123), UserId = Guid.NewGuid() });
255      userStats.Add(new DT.UserStatistics() { UsedCores = 42, ExecutionTime = TimeSpan.FromHours(945), UserId = Guid.NewGuid() });
256      stats.UserStatistics = userStats;
257
258      stats.Id = dao.AddStatistics(stats);
259
260      var statsLoaded = dao.GetStatistic(stats.Id);
261      Assert.AreEqual(stats.Id, statsLoaded.Id);
262      Assert.IsTrue(Math.Abs((stats.TimeStamp - statsLoaded.TimeStamp).TotalSeconds) < 1);
263
264      for (int i = 0; i  < slaveStats.Count; i ++) {
265        Assert.AreEqual(slaveStats[i].Cores, statsLoaded.SlaveStatistics.Single(x => x.SlaveId == slaveStats[i].SlaveId).Cores);
266        Assert.AreEqual(slaveStats[i].FreeCores, statsLoaded.SlaveStatistics.Single(x => x.SlaveId == slaveStats[i].SlaveId).FreeCores);
267        Assert.AreEqual(slaveStats[i].Memory, statsLoaded.SlaveStatistics.Single(x => x.SlaveId == slaveStats[i].SlaveId).Memory);
268        Assert.AreEqual(slaveStats[i].FreeMemory, statsLoaded.SlaveStatistics.Single(x => x.SlaveId == slaveStats[i].SlaveId).FreeMemory);
269        Assert.AreEqual(slaveStats[i].CpuUtilization, statsLoaded.SlaveStatistics.Single(x => x.SlaveId == slaveStats[i].SlaveId).CpuUtilization);
270      }
271
272      for (int i = 0; i < userStats.Count; i++) {
273        Assert.AreEqual(userStats[i].ExecutionTime, statsLoaded.UserStatistics.Single(x => x.UserId == userStats[i].UserId).ExecutionTime);
274        Assert.AreEqual(userStats[i].UsedCores, statsLoaded.UserStatistics.Single(x => x.UserId == userStats[i].UserId).UsedCores);       
275      }
276
277      dao.DeleteStatistics(stats.Id);
278      Assert.AreEqual(null, dao.GetStatistic(stats.Id));
279    }
280  }
281}
Note: See TracBrowser for help on using the repository browser.