Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Hive.Server.Core/3.2/DbTestApp.cs @ 3031

Last change on this file since 3031 was 3011, checked in by kgrading, 14 years ago

changed the complete DAL to LINQ 2 SQL (with the exception of the job streaming), did a lot of refactoring, Introduced DTOs (that are named DTOs for better understanding), added the spring.NET Interceptor, reintroduced transactions and cleaned up the whole JobResult thing and updated a part of the config merger (#830)

File size: 14.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Text;
25using HeuristicLab.PluginInfrastructure;
26using System.Net;
27using HeuristicLab.Hive.Contracts;
28using HeuristicLab.Hive.Contracts.Interfaces;
29using HeuristicLab.Hive.Server.DataAccess;
30using HeuristicLab.Hive.Contracts.BusinessObjects;
31using System.Diagnostics;
32using HeuristicLab.DataAccess.Interfaces;
33using System.IO;
34using HeuristicLab.Hive.Server.Core;
35using HeuristicLab.Core;
36using HeuristicLab.Hive.Server.LINQDataAccess;
37
38namespace HeuristicLab.Hive.Server {
39  [Application("Hive DB Test App", "Test Application for the Hive DataAccess Layer")]
40  class HiveDbTestApplication : ApplicationBase {
41    /*  private void TestClientAdapter() {
42        IClientAdapter clientAdapter =
43          ServiceLocator.GetClientAdapter();
44
45        ClientInfo client = new ClientInfo();
46        client.Login = DateTime.Now;
47        clientAdapter.Update(client);
48
49        ClientInfo clientRead =
50          clientAdapter.GetById(client.Id);
51        Debug.Assert(
52          clientRead != null &&
53          client.Id == clientRead.Id);
54
55        client.CpuSpeedPerCore = 2000;
56        clientAdapter.Update(client);
57        clientRead =
58          clientAdapter.GetById(client.Id);
59        Debug.Assert(
60         clientRead != null &&
61         client.Id == clientRead.Id &&
62         clientRead.CpuSpeedPerCore == 2000);
63
64        ICollection<ClientInfo> clients =
65          clientAdapter.GetAll();
66        int count = clients.Count;
67
68        clientAdapter.Delete(client);
69
70        clients = clientAdapter.GetAll();
71        Debug.Assert(count - 1 == clients.Count);
72      }
73
74      private void TestClientGroupAdapter() {
75        ISessionFactory factory =
76          ServiceLocator.GetSessionFactory();
77
78        ISession session =
79          factory.GetSessionForCurrentThread();
80
81        ITransaction trans = null;
82
83        try {
84          IClientGroupAdapter clientGroupAdapter =
85          session.GetDataAdapter<ClientGroup, IClientGroupAdapter>();
86
87          trans =
88            session.BeginTransaction();
89
90          ClientInfo client =
91            new ClientInfo();
92          client.Name = "Stefan";
93          client.Login = DateTime.Now;
94
95          ClientInfo client2 =
96            new ClientInfo();
97          client2.Name = "Martin";
98          client2.Login = DateTime.Now;
99
100          ClientInfo client3 =
101            new ClientInfo();
102          client3.Name = "Heinz";
103          client3.Login = DateTime.Now;
104
105          ClientGroup group =
106            new ClientGroup();
107
108          ClientGroup subGroup =
109            new ClientGroup();
110          subGroup.Resources.Add(client);
111
112          group.Resources.Add(client3);
113          group.Resources.Add(client2);
114          group.Resources.Add(subGroup);
115
116          clientGroupAdapter.Update(group);
117
118          ClientGroup read =
119            clientGroupAdapter.GetById(group.Id);
120
121          ICollection<ClientGroup> clientGroups =
122            clientGroupAdapter.GetAll();
123
124          IClientAdapter clientAdapter =
125            session.GetDataAdapter<ClientInfo, IClientAdapter>();
126
127          clientAdapter.Delete(client3);
128
129          read =
130             clientGroupAdapter.GetById(group.Id);
131
132          clientGroupAdapter.Delete(subGroup);
133
134          read =
135             clientGroupAdapter.GetById(group.Id);
136
137          clientGroups =
138            clientGroupAdapter.GetAll();
139
140          clientGroupAdapter.Delete(group);
141
142          clientGroups =
143            clientGroupAdapter.GetAll();
144
145          clientAdapter.Delete(client);
146          clientAdapter.Delete(client2);
147        }
148        finally {
149          if (trans != null)
150            trans.Rollback();
151
152          session.EndSession();
153        }
154      }
155
156      private void InsertTestClientGroups() {
157        ISessionFactory factory =
158          ServiceLocator.GetSessionFactory();
159
160        ISession session =
161          factory.GetSessionForCurrentThread();
162
163        ITransaction trans = null;
164
165        try {
166          IClientGroupAdapter clientGroupAdapter =
167          session.GetDataAdapter<ClientGroup, IClientGroupAdapter>();
168
169          trans =
170            session.BeginTransaction();
171
172          ClientInfo client =
173            new ClientInfo();
174          client.Name = "Stefan";
175          client.Login = DateTime.Now;
176
177          ClientInfo client2 =
178            new ClientInfo();
179          client2.Name = "Martin";
180          client2.Login = DateTime.Now;
181
182          ClientGroup group =
183            new ClientGroup();
184          group.Name = "Gruppe1";
185
186          ClientGroup subGroup =
187            new ClientGroup();
188          subGroup.Name = "Untergruppe1";
189          subGroup.Resources.Add(client);
190
191          group.Resources.Add(client2);
192          group.Resources.Add(subGroup);
193
194          clientGroupAdapter.Update(group);
195
196          trans.Commit();
197        }
198        finally {
199          session.EndSession();
200        }
201      }
202
203      private void TestJobAdapter() {
204        IJobAdapter jobAdapter =
205          ServiceLocator.GetJobAdapter();
206        IClientAdapter clientAdapter =
207          ServiceLocator.GetClientAdapter();
208
209        Job job = new Job();
210
211        ClientInfo client = new ClientInfo();
212        client.Login = DateTime.Now;
213
214        job.Client = client;
215        jobAdapter.Update(job);
216
217        ICollection<Job> jobs = jobAdapter.GetAll();
218
219        jobAdapter.Delete(job);
220        clientAdapter.Delete(client);
221
222        jobs = jobAdapter.GetAll();
223      }
224
225      private void TestJobResultsAdapter() {
226        Job job = new Job();
227
228        ClientInfo client = new ClientInfo();
229        client.Login = DateTime.Now;
230
231        job.Client = client;
232
233        IJobResultsAdapter resultsAdapter =
234          ServiceLocator.GetJobResultsAdapter();
235
236        byte[] resultByte = {0x0f, 0x1f, 0x2f, 0x3f, 0x4f};
237
238        JobResult result = new JobResult();
239        result.Client = client;
240        result.Job = job;
241        result.Result = resultByte;
242
243        resultsAdapter.Update(result);
244
245        JobResult read =
246          resultsAdapter.GetById(result.Id);
247        Debug.Assert(
248          read.Id == result.Id &&
249          result.Client.Id == read.Client.Id &&
250          result.Job.Id == read.Job.Id &&
251          result.Result == result.Result);
252
253        int count =
254          resultsAdapter.GetAll().Count;
255
256        resultsAdapter.Delete(result);
257
258        ICollection<JobResult> allResults =
259          resultsAdapter.GetAll();
260
261        Debug.Assert(allResults.Count == count - 1);
262
263        IJobAdapter jboAdapter =
264          ServiceLocator.GetJobAdapter();
265        jboAdapter.Delete(job);
266        IClientAdapter clientAdapter =
267          ServiceLocator.GetClientAdapter();
268        clientAdapter.Delete(client);
269      }     
270
271      private void TestTransaction() {
272        ISessionFactory factory =
273          ServiceLocator.GetSessionFactory();
274
275        ISession session =
276          factory.GetSessionForCurrentThread();
277
278        IClientAdapter clientAdapter =
279          session.GetDataAdapter<ClientInfo, IClientAdapter>();
280
281        ITransaction trans =
282          session.BeginTransaction();
283
284        ClientInfo client = new ClientInfo();
285        client.Login = DateTime.Now;
286        clientAdapter.Update(client);
287
288        trans.Rollback();
289
290        session.EndSession();
291      }  */
292
293    private void TestJobStreaming() {
294      ISessionFactory factory =
295         ServiceLocator.GetSessionFactory();
296
297      ISession session =
298           factory.GetSessionForCurrentThread();
299
300      IJobAdapter jobAdapter =
301        session.GetDataAdapter<HeuristicLab.Hive.Contracts.BusinessObjects.JobDto, IJobAdapter>();
302
303      Stream s = jobAdapter.GetSerializedJobStream(
304        new Guid("1b35f32b-d880-4c76-86af-4b4e283b30e6"), true);
305
306      int length = 0;
307
308      FileStream fs =
309        new FileStream(@"W:\\result.gz", FileMode.Create);
310
311      byte[] buffer = new byte[1024];
312      while ((length = s.Read(buffer, 0, buffer.Length)) > 0) {
313        fs.Write(buffer, 0, length);
314      }
315
316      fs.Close();
317      s.Close();
318
319      session.EndSession();
320    }
321
322    private void TestJobResultStreaming() {
323      ISessionFactory factory =
324         ServiceLocator.GetSessionFactory();
325
326      ISession session =
327           factory.GetSessionForCurrentThread();
328
329      IJobResultsAdapter jobResultsAdapter =
330        session.GetDataAdapter<JobResult, IJobResultsAdapter>();
331
332      Stream s = jobResultsAdapter.GetSerializedJobResultStream(
333        new Guid("c20b11a9-cde1-4d7f-8499-23dedb5a65ed"), true);
334
335      int length = 0;
336
337      FileStream fs =
338        new FileStream(@"W:\\result.gz", FileMode.Create);
339
340      byte[] buffer = new byte[1024];
341      while ((length = s.Read(buffer, 0, buffer.Length)) > 0) {
342        fs.Write(buffer, 0, length);
343      }
344
345      fs.Close();
346      s.Close();
347
348      session.EndSession();
349    }
350
351    /*private void TestJobResultDeserialization() {
352      ExecutionEngineFacade executionEngineFacade =
353        new ExecutionEngineFacade();
354
355      ResponseObject<SerializedJobResult> response =
356        executionEngineFacade.GetLastSerializedResult(
357        new Guid("56ce20bc-067b-424d-a7df-67aaace7c850"), false);
358
359      IStorable restoredJob =
360        PersistenceManager.RestoreFromGZip(response.Obj.SerializedJobResultData);
361    } */
362
363    ClientDao clientDao = new ClientDao();
364    ClientGroupDao cgd = new ClientGroupDao();
365
366    private void TestLINQImplementation() {     
367     
368     
369      ClientDto c1 = new ClientDto();
370      c1.Id = Guid.NewGuid();
371      c1.FreeMemory = 1000;
372      c1.Login = DateTime.Now;
373      c1.Memory = 1000;
374      c1.Name = "jackie";
375      c1.NrOfCores = 3;
376      c1.NrOfFreeCores = 2;
377      c1.CpuSpeedPerCore = 2500;
378      c1.State = State.idle;
379      c1 = clientDao.Insert(c1);
380
381      clientDao.Update(c1);
382
383      ClientDto c2 = new ClientDto();
384      c2.Id = Guid.NewGuid();
385      c2.FreeMemory = 600;
386      c2.Login = DateTime.Now;
387      c2.Memory = 2048;
388      c2.Name = "HPCs";
389      c2.NrOfCores = 4;
390      c2.NrOfFreeCores = 1;
391      c2.CpuSpeedPerCore = 4000;
392      c2.State = State.idle;
393      c2 = clientDao.Insert(c2);
394
395      //ClientDto info2 = clientDao.FindById(info.Id);
396      //Console.WriteLine(info2);
397     
398      ClientGroupDto tg = new ClientGroupDto();
399      tg.Name = "TopGroup";
400      tg = cgd.Insert(tg);
401
402      ClientGroupDto sg = new ClientGroupDto();
403      sg.Name = "Subgroup";
404      sg = cgd.Insert(sg);
405
406      cgd.AddRessourceToClientGroup(sg.Id, tg.Id);
407      cgd.AddRessourceToClientGroup(c1.Id, tg.Id);
408      cgd.AddRessourceToClientGroup(c2.Id, tg.Id);
409
410      JobDto job = new JobDto {
411                                Client = c1,
412                                CoresNeeded = 2,
413                                DateCreated = DateTime.Now,
414                                MemoryNeeded = 500,
415                                Percentage = 0,
416                                Priority = 1,
417                                State = State.offline
418                              };
419
420      job = DaoLocator.JobDao.Insert(job);
421
422
423      DaoLocator.JobDao.AssignClientToJob(c1.Id, job.Id);
424
425      List<ClientGroupDto> list = new List<ClientGroupDto>(cgd.FindAllWithSubGroupsAndClients());
426     
427      cgd.RemoveRessourceFromClientGroup(sg.Id, tg.Id);
428
429      cgd.Delete(sg);
430      cgd.Delete(tg);
431      clientDao.Delete(c1);
432      clientDao.Delete(c2);
433
434    }
435
436    private void StressTest() {
437      //Speed Test
438      Random r = new Random();
439
440
441      for (int i = 0; i < 200; i++) {
442        ClientGroupDto mg = new ClientGroupDto();
443        mg.Name = "MainGroup" + i;
444        mg = cgd.Insert(mg);
445
446        populateMainGroup(mg, 3);
447      }
448    }
449
450    private void populateMainGroup(ClientGroupDto mg, int p) {
451      Random r = new Random();
452
453      for (int j = 0; j < r.Next(15); j++) {
454        ClientDto client = new ClientDto();
455        client.Id = Guid.NewGuid();
456        client.FreeMemory = r.Next(1000);
457        client.Login = DateTime.Now;
458        client.Memory = r.Next(500);
459        client.Name = "client" + mg.Name + "_" + j;
460        client.NrOfCores = 3;
461        client.NrOfFreeCores = 2;
462        client.CpuSpeedPerCore = 2500;
463        client.State = State.idle;
464        client = clientDao.Insert(client);
465        cgd.AddRessourceToClientGroup(client.Id, mg.Id);
466      }
467      for (int i = 0; i < r.Next(p); i++) {
468        ClientGroupDto sg = new ClientGroupDto();
469        sg.Name = "SubGroup " + mg.Name + " - " + p;
470        sg = cgd.Insert(sg);
471        cgd.AddRessourceToClientGroup(sg.Id, mg.Id);
472        populateMainGroup(sg, p-1);
473      }
474
475     
476    }
477
478
479
480    public override void Run() {
481      //TestClientGroupAdapter();
482      //InsertTestClientGroups();
483      //TestJobStreaming();
484      //TestJobResultStreaming();
485      //TestJobResultDeserialization();
486
487      //TestLINQImplementation();
488      //StressTest();
489
490      //SpeedTest();
491      TestJobBytearrFetching();
492
493    }
494
495    private void TestJobBytearrFetching() {
496      byte[] arr = DaoLocator.JobDao.GetBinaryJobFile(new Guid("A3386907-2B3C-4976-BE07-04D660D40A5B"));
497      Console.WriteLine(arr);
498    }
499
500    private void SpeedTest() {
501      DateTime start = new DateTime();
502      List<ClientGroupDto> list = new List<ClientGroupDto>(cgd.FindAllWithSubGroupsAndClients());
503      DateTime end = new DateTime();
504      TimeSpan used = end - start;
505      Console.WriteLine(used.TotalMilliseconds);
506    }
507
508
509  }
510}
Note: See TracBrowser for help on using the repository browser.