Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3362 was 3220, checked in by kgrading, 15 years ago

improved the DAL further, changed minor details for the presentation (#830)

File size: 8.5 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.Data.SqlClient;
25using System.Text;
26using HeuristicLab.PluginInfrastructure;
27using System.Net;
28using HeuristicLab.Hive.Contracts;
29using HeuristicLab.Hive.Contracts.Interfaces;
30using HeuristicLab.Hive.Server.DataAccess;
31using HeuristicLab.Hive.Contracts.BusinessObjects;
32using System.Diagnostics;
33using HeuristicLab.DataAccess.Interfaces;
34using System.IO;
35using HeuristicLab.Hive.Server.Core;
36using HeuristicLab.Core;
37using HeuristicLab.Hive.Server.LINQDataAccess;
38using System.Transactions;
39
40namespace HeuristicLab.Hive.Server {
41  [Application("Hive DB Test App", "Test Application for the Hive DataAccess Layer")]
42  class HiveDbTestApplication : ApplicationBase {
43    /*private void TestJobStreaming() {
44      ISessionFactory factory =
45         ServiceLocator.GetSessionFactory();
46
47      ISession session =
48           factory.GetSessionForCurrentThread();
49
50      IJobAdapter jobAdapter =
51        session.GetDataAdapter<HeuristicLab.Hive.Contracts.BusinessObjects.JobDto, IJobAdapter>();
52
53      Stream s = jobAdapter.GetSerializedJobStream(
54        new Guid("1b35f32b-d880-4c76-86af-4b4e283b30e6"), true);
55
56      int length = 0;
57
58      FileStream fs =
59        new FileStream(@"W:\\result.gz", FileMode.Create);
60
61      byte[] buffer = new byte[1024];
62      while ((length = s.Read(buffer, 0, buffer.Length)) > 0) {
63        fs.Write(buffer, 0, length);
64      }
65
66      fs.Close();
67      s.Close();
68
69      session.EndSession();
70    }
71
72    private void TestJobResultStreaming() {
73      ISessionFactory factory =
74         ServiceLocator.GetSessionFactory();
75
76      ISession session =
77           factory.GetSessionForCurrentThread();
78
79      IJobResultsAdapter jobResultsAdapter =
80        session.GetDataAdapter<JobResult, IJobResultsAdapter>();
81
82      Stream s = jobResultsAdapter.GetSerializedJobResultStream(
83        new Guid("c20b11a9-cde1-4d7f-8499-23dedb5a65ed"), true);
84
85      int length = 0;
86
87      FileStream fs =
88        new FileStream(@"W:\\result.gz", FileMode.Create);
89
90      byte[] buffer = new byte[1024];
91      while ((length = s.Read(buffer, 0, buffer.Length)) > 0) {
92        fs.Write(buffer, 0, length);
93      }
94
95      fs.Close();
96      s.Close();
97
98      session.EndSession();
99    }
100
101    /*private void TestJobResultDeserialization() {
102      ExecutionEngineFacade executionEngineFacade =
103        new ExecutionEngineFacade();
104
105      ResponseObject<SerializedJobResult> response =
106        executionEngineFacade.GetLastSerializedResult(
107        new Guid("56ce20bc-067b-424d-a7df-67aaace7c850"), false);
108
109      IStorable restoredJob =
110        PersistenceManager.RestoreFromGZip(response.Obj.SerializedJobResultData);
111    } */
112
113    ClientDao clientDao = new ClientDao();
114    ClientGroupDao cgd = new ClientGroupDao();
115
116    private void TestLINQImplementation() {     
117     
118     
119      ClientDto c1 = new ClientDto();
120      c1.Id = Guid.NewGuid();
121      c1.FreeMemory = 1000;
122      c1.Login = DateTime.Now;
123      c1.Memory = 1000;
124      c1.Name = "jackie";
125      c1.NrOfCores = 3;
126      c1.NrOfFreeCores = 2;
127      c1.CpuSpeedPerCore = 2500;
128      c1.State = State.idle;
129      c1 = clientDao.Insert(c1);
130
131      clientDao.Update(c1);
132
133      ClientDto c2 = new ClientDto();
134      c2.Id = Guid.NewGuid();
135      c2.FreeMemory = 600;
136      c2.Login = DateTime.Now;
137      c2.Memory = 2048;
138      c2.Name = "HPCs";
139      c2.NrOfCores = 4;
140      c2.NrOfFreeCores = 1;
141      c2.CpuSpeedPerCore = 4000;
142      c2.State = State.idle;
143      c2 = clientDao.Insert(c2);
144
145      //ClientDto info2 = clientDao.FindById(info.Id);
146      //Console.WriteLine(info2);
147     
148      ClientGroupDto tg = new ClientGroupDto();
149      tg.Name = "TopGroup";
150      tg = cgd.Insert(tg);
151
152      ClientGroupDto sg = new ClientGroupDto();
153      sg.Name = "Subgroup";
154      sg = cgd.Insert(sg);
155
156      cgd.AddRessourceToClientGroup(sg.Id, tg.Id);
157      cgd.AddRessourceToClientGroup(c1.Id, tg.Id);
158      cgd.AddRessourceToClientGroup(c2.Id, tg.Id);
159
160      JobDto job = new JobDto {
161                                Client = c1,
162                                CoresNeeded = 2,
163                                DateCreated = DateTime.Now,
164                                MemoryNeeded = 500,
165                                Percentage = 0,
166                                Priority = 1,
167                                State = State.offline
168                              };
169
170      job = DaoLocator.JobDao.Insert(job);
171
172
173      DaoLocator.JobDao.AssignClientToJob(c1.Id, job.Id);
174
175      List<ClientGroupDto> list = new List<ClientGroupDto>(cgd.FindAllWithSubGroupsAndClients());
176     
177      cgd.RemoveRessourceFromClientGroup(sg.Id, tg.Id);
178
179      cgd.Delete(sg);
180      cgd.Delete(tg);
181      clientDao.Delete(c1);
182      clientDao.Delete(c2);
183
184    }
185
186    private void StressTest() {
187      //Speed Test
188      Random r = new Random();
189
190
191      for (int i = 0; i < 200; i++) {
192        ClientGroupDto mg = new ClientGroupDto();
193        mg.Name = "MainGroup" + i;
194        mg = cgd.Insert(mg);
195
196        populateMainGroup(mg, 3);
197      }
198    }
199
200    private void populateMainGroup(ClientGroupDto mg, int p) {
201      Random r = new Random();
202
203      for (int j = 0; j < r.Next(15); j++) {
204        ClientDto client = new ClientDto();
205        client.Id = Guid.NewGuid();
206        client.FreeMemory = r.Next(1000);
207        client.Login = DateTime.Now;
208        client.Memory = r.Next(500);
209        client.Name = "client" + mg.Name + "_" + j;
210        client.NrOfCores = 3;
211        client.NrOfFreeCores = 2;
212        client.CpuSpeedPerCore = 2500;
213        client.State = State.idle;
214        client = clientDao.Insert(client);
215        cgd.AddRessourceToClientGroup(client.Id, mg.Id);
216      }
217      for (int i = 0; i < r.Next(p); i++) {
218        ClientGroupDto sg = new ClientGroupDto();
219        sg.Name = "SubGroup " + mg.Name + " - " + p;
220        sg = cgd.Insert(sg);
221        cgd.AddRessourceToClientGroup(sg.Id, mg.Id);
222        populateMainGroup(sg, p-1);
223      }
224
225     
226    }
227
228
229
230    public override void Run() {
231      //TestClientGroupAdapter();
232      //InsertTestClientGroups();
233      //TestJobStreaming();
234      //TestJobResultStreaming();
235      //TestJobResultDeserialization();
236
237      //TestLINQImplementation();
238      //StressTest();
239
240      //SpeedTest();
241      //TestJobBytearrFetching();
242      TestJobStreamFetching();
243
244    }
245
246    private void TestJobStreamFetching() {
247      //using (TransactionScope scope = new TransactionScope()) {
248      HiveDataContext context = ContextFactory.Context;
249
250      ContextFactory.Context.Connection.Open();
251      ContextFactory.Context.Transaction = ContextFactory.Context.Connection.BeginTransaction();
252     
253      ClientFacade facade = new ClientFacade();
254      Stream stream = facade.SendStreamedJob(new Guid("F5CFB334-66A0-417C-A585-71711BA21D3F"));
255     
256      byte[] buffer = new byte[3024];
257      int read = 0;
258     
259      while ((read = stream.Read(buffer, 0, buffer.Length)) > 0) {}
260     
261      stream.Close();
262
263      //Stream stream = DaoLocator.JobDao.GetSerializedJobStream(new Guid("bbb51f87-4e2f-4499-a9b6-884e589c78b6"));
264      //}
265    }
266
267    private void TestJobBytearrFetching() {
268      byte[] arr = DaoLocator.JobDao.GetBinaryJobFile(new Guid("A3386907-2B3C-4976-BE07-04D660D40A5B"));
269      Console.WriteLine(arr);
270    }
271
272    private void SpeedTest() {
273      DateTime start = new DateTime();
274      List<ClientGroupDto> list = new List<ClientGroupDto>(cgd.FindAllWithSubGroupsAndClients());
275      DateTime end = new DateTime();
276      TimeSpan used = end - start;
277      Console.WriteLine(used.TotalMilliseconds);
278    }
279
280
281  }
282}
Note: See TracBrowser for help on using the repository browser.