Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 2909 was 2904, checked in by kgrading, 14 years ago

added functionality (#830)

File size: 10.9 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 =
[2904]301        session.GetDataAdapter<HeuristicLab.Hive.Contracts.BusinessObjects.Job, 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
351    private void TestJobResultDeserialization() {
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);
[2122]361    }
362
[2904]363    private void TestLINQImplementation() {     
364      ClientDao clientDao = new ClientDao();     
365      ClientInfo info = new ClientInfo();
366      info.Id = Guid.NewGuid();
367      info.FreeMemory = 1000;
368      info.Login = DateTime.Now;
369      info.Memory = 1000;
370      info.Name = "jackie";
371      info.NrOfCores = 3;
372      info.NrOfFreeCores = 2;
373      info.CpuSpeedPerCore = 2500;
374      info.State = State.idle;
375      info = clientDao.Insert(info);
376
377    }
378
[1515]379    public override void Run() {
[2117]380      //TestClientGroupAdapter();
[1656]381      //InsertTestClientGroups();
[2122]382      //TestJobStreaming();
383      //TestJobResultStreaming();
[2904]384      //TestJobResultDeserialization();
385
386      TestLINQImplementation();
387
[2591]388    }
[2904]389
390
[826]391  }
392}
Note: See TracBrowser for help on using the repository browser.