Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3155 was 3011, checked in by kgrading, 15 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
RevLine 
[826]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;
[1377]29using HeuristicLab.Hive.Server.DataAccess;
[826]30using HeuristicLab.Hive.Contracts.BusinessObjects;
31using System.Diagnostics;
[1377]32using HeuristicLab.DataAccess.Interfaces;
[2117]33using System.IO;
[2122]34using HeuristicLab.Hive.Server.Core;
35using HeuristicLab.Core;
[2904]36using HeuristicLab.Hive.Server.LINQDataAccess;
[826]37
38namespace HeuristicLab.Hive.Server {
[2591]39  [Application("Hive DB Test App", "Test Application for the Hive DataAccess Layer")]
[826]40  class HiveDbTestApplication : ApplicationBase {
[2117]41    /*  private void TestClientAdapter() {
42        IClientAdapter clientAdapter =
43          ServiceLocator.GetClientAdapter();
[826]44
[2117]45        ClientInfo client = new ClientInfo();
46        client.Login = DateTime.Now;
47        clientAdapter.Update(client);
[826]48
[2117]49        ClientInfo clientRead =
50          clientAdapter.GetById(client.Id);
51        Debug.Assert(
52          clientRead != null &&
53          client.Id == clientRead.Id);
[826]54
[2117]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);
[826]63
[2117]64        ICollection<ClientInfo> clients =
65          clientAdapter.GetAll();
66        int count = clients.Count;
[826]67
[2117]68        clientAdapter.Delete(client);
[826]69
[2117]70        clients = clientAdapter.GetAll();
71        Debug.Assert(count - 1 == clients.Count);
72      }
[905]73
[2117]74      private void TestClientGroupAdapter() {
75        ISessionFactory factory =
76          ServiceLocator.GetSessionFactory();
[965]77
[2117]78        ISession session =
79          factory.GetSessionForCurrentThread();
[965]80
[2117]81        ITransaction trans = null;
[965]82
[2117]83        try {
84          IClientGroupAdapter clientGroupAdapter =
85          session.GetDataAdapter<ClientGroup, IClientGroupAdapter>();
[965]86
[2117]87          trans =
88            session.BeginTransaction();
[965]89
[2117]90          ClientInfo client =
91            new ClientInfo();
92          client.Name = "Stefan";
93          client.Login = DateTime.Now;
[965]94
[2117]95          ClientInfo client2 =
96            new ClientInfo();
97          client2.Name = "Martin";
98          client2.Login = DateTime.Now;
[965]99
[2117]100          ClientInfo client3 =
101            new ClientInfo();
102          client3.Name = "Heinz";
103          client3.Login = DateTime.Now;
[965]104
[2117]105          ClientGroup group =
106            new ClientGroup();
[965]107
[2117]108          ClientGroup subGroup =
109            new ClientGroup();
110          subGroup.Resources.Add(client);
[965]111
[2117]112          group.Resources.Add(client3);
113          group.Resources.Add(client2);
114          group.Resources.Add(subGroup);
[965]115
[2117]116          clientGroupAdapter.Update(group);
[965]117
[2117]118          ClientGroup read =
119            clientGroupAdapter.GetById(group.Id);
[965]120
[2117]121          ICollection<ClientGroup> clientGroups =
122            clientGroupAdapter.GetAll();
[965]123
[2117]124          IClientAdapter clientAdapter =
125            session.GetDataAdapter<ClientInfo, IClientAdapter>();
[965]126
[2117]127          clientAdapter.Delete(client3);
[965]128
[2117]129          read =
130             clientGroupAdapter.GetById(group.Id);
[965]131
[2117]132          clientGroupAdapter.Delete(subGroup);
[965]133
[2117]134          read =
135             clientGroupAdapter.GetById(group.Id);
[1515]136
[2117]137          clientGroups =
138            clientGroupAdapter.GetAll();
[1515]139
[2117]140          clientGroupAdapter.Delete(group);
[1515]141
[2117]142          clientGroups =
143            clientGroupAdapter.GetAll();
[1515]144
[2117]145          clientAdapter.Delete(client);
146          clientAdapter.Delete(client2);
147        }
148        finally {
149          if (trans != null)
150            trans.Rollback();
[1515]151
[2117]152          session.EndSession();
153        }
[1515]154      }
[965]155
[2117]156      private void InsertTestClientGroups() {
157        ISessionFactory factory =
158          ServiceLocator.GetSessionFactory();
[1656]159
[2117]160        ISession session =
161          factory.GetSessionForCurrentThread();
[1656]162
[2117]163        ITransaction trans = null;
[1656]164
[2117]165        try {
166          IClientGroupAdapter clientGroupAdapter =
167          session.GetDataAdapter<ClientGroup, IClientGroupAdapter>();
[1656]168
[2117]169          trans =
170            session.BeginTransaction();
[1656]171
[2117]172          ClientInfo client =
173            new ClientInfo();
174          client.Name = "Stefan";
175          client.Login = DateTime.Now;
[1656]176
[2117]177          ClientInfo client2 =
178            new ClientInfo();
179          client2.Name = "Martin";
180          client2.Login = DateTime.Now;
[1656]181
[2117]182          ClientGroup group =
183            new ClientGroup();
184          group.Name = "Gruppe1";
[1656]185
[2117]186          ClientGroup subGroup =
187            new ClientGroup();
188          subGroup.Name = "Untergruppe1";
189          subGroup.Resources.Add(client);
[1656]190
[2117]191          group.Resources.Add(client2);
192          group.Resources.Add(subGroup);
[1656]193
[2117]194          clientGroupAdapter.Update(group);
[1656]195
[2117]196          trans.Commit();
197        }
198        finally {
199          session.EndSession();
200        }
[1656]201      }
[2117]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();
[1656]223      }
224
[2117]225      private void TestJobResultsAdapter() {
226        Job job = new Job();
[971]227
[2117]228        ClientInfo client = new ClientInfo();
229        client.Login = DateTime.Now;
[971]230
[2117]231        job.Client = client;
[971]232
[2117]233        IJobResultsAdapter resultsAdapter =
234          ServiceLocator.GetJobResultsAdapter();
[971]235
[2117]236        byte[] resultByte = {0x0f, 0x1f, 0x2f, 0x3f, 0x4f};
[971]237
[2117]238        JobResult result = new JobResult();
239        result.Client = client;
240        result.Job = job;
241        result.Result = resultByte;
[971]242
[2117]243        resultsAdapter.Update(result);
[971]244
[2117]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);
[1005]252
[2117]253        int count =
254          resultsAdapter.GetAll().Count;
[1005]255
[2117]256        resultsAdapter.Delete(result);
[1005]257
[2117]258        ICollection<JobResult> allResults =
259          resultsAdapter.GetAll();
[1005]260
[2117]261        Debug.Assert(allResults.Count == count - 1);
[1005]262
[2117]263        IJobAdapter jboAdapter =
264          ServiceLocator.GetJobAdapter();
265        jboAdapter.Delete(job);
266        IClientAdapter clientAdapter =
267          ServiceLocator.GetClientAdapter();
268        clientAdapter.Delete(client);
269      }     
[1005]270
[2117]271      private void TestTransaction() {
272        ISessionFactory factory =
273          ServiceLocator.GetSessionFactory();
[1005]274
[2117]275        ISession session =
276          factory.GetSessionForCurrentThread();
[1005]277
[2117]278        IClientAdapter clientAdapter =
279          session.GetDataAdapter<ClientInfo, IClientAdapter>();
[1005]280
[2117]281        ITransaction trans =
282          session.BeginTransaction();
[1005]283
[2117]284        ClientInfo client = new ClientInfo();
285        client.Login = DateTime.Now;
286        clientAdapter.Update(client);
[1005]287
[2117]288        trans.Rollback();
[1025]289
[2117]290        session.EndSession();
291      }  */
[1005]292
[2117]293    private void TestJobStreaming() {
[1468]294      ISessionFactory factory =
[2117]295         ServiceLocator.GetSessionFactory();
[995]296
[1515]297      ISession session =
[2117]298           factory.GetSessionForCurrentThread();
[995]299
[2117]300      IJobAdapter jobAdapter =
[3011]301        session.GetDataAdapter<HeuristicLab.Hive.Contracts.BusinessObjects.JobDto, IJobAdapter>();
[1005]302
[2117]303      Stream s = jobAdapter.GetSerializedJobStream(
[2122]304        new Guid("1b35f32b-d880-4c76-86af-4b4e283b30e6"), true);
[1468]305
[2122]306      int length = 0;
307
308      FileStream fs =
309        new FileStream(@"W:\\result.gz", FileMode.Create);
310
[2117]311      byte[] buffer = new byte[1024];
[2591]312      while ((length = s.Read(buffer, 0, buffer.Length)) > 0) {
[2122]313        fs.Write(buffer, 0, length);
[2117]314      }
[1468]315
[2122]316      fs.Close();
[2117]317      s.Close();
[1468]318
319      session.EndSession();
[1515]320    }
321
[2122]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
[3011]351    /*private void TestJobResultDeserialization() {
[2122]352      ExecutionEngineFacade executionEngineFacade =
353        new ExecutionEngineFacade();
354
[2591]355      ResponseObject<SerializedJobResult> response =
356        executionEngineFacade.GetLastSerializedResult(
357        new Guid("56ce20bc-067b-424d-a7df-67aaace7c850"), false);
[2122]358
[2591]359      IStorable restoredJob =
360        PersistenceManager.RestoreFromGZip(response.Obj.SerializedJobResultData);
[3011]361    } */
[2122]362
[3011]363    ClientDao clientDao = new ClientDao();
364    ClientGroupDao cgd = new ClientGroupDao();
365
[2904]366    private void TestLINQImplementation() {     
[3011]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);
[2904]380
[3011]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
[2904]434    }
435
[3011]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
[1515]480    public override void Run() {
[2117]481      //TestClientGroupAdapter();
[1656]482      //InsertTestClientGroups();
[2122]483      //TestJobStreaming();
484      //TestJobResultStreaming();
[2904]485      //TestJobResultDeserialization();
486
[3011]487      //TestLINQImplementation();
488      //StressTest();
[2904]489
[3011]490      //SpeedTest();
491      TestJobBytearrFetching();
492
[2591]493    }
[2904]494
[3011]495    private void TestJobBytearrFetching() {
496      byte[] arr = DaoLocator.JobDao.GetBinaryJobFile(new Guid("A3386907-2B3C-4976-BE07-04D660D40A5B"));
497      Console.WriteLine(arr);
498    }
[2904]499
[3011]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
[826]509  }
510}
Note: See TracBrowser for help on using the repository browser.