Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.3-HiveMigration/sources/HeuristicLab.Hive/HeuristicLab.Hive.Server.Core/3.3/DbTestApp.cs @ 4116

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

worked on HiveExperiment (#1115)

File size: 10.8 KB
Line 
1#region License Information
2
3/* HeuristicLab
4 * Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
5 *
6 * This file is part of HeuristicLab.
7 *
8 * HeuristicLab is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * HeuristicLab is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#endregion
23
24using System;
25using System.Collections.Generic;
26using System.IO;
27using System.Threading;
28using System.Transactions;
29using HeuristicLab.Hive.Contracts;
30using HeuristicLab.Hive.Contracts.BusinessObjects;
31using HeuristicLab.Hive.Server.Core;
32using HeuristicLab.Hive.Server.LINQDataAccess;
33using HeuristicLab.PluginInfrastructure;
34using HeuristicLab.Tracing;
35using HeuristicLab.Hive.Server.DataAccess;
36
37namespace HeuristicLab.Hive.Server {
38  [Application("Hive DB Test App", "Test Application for the Hive DataAccess Layer")]
39  internal class HiveDbTestApplication : ApplicationBase {
40    private IContextFactory contextFactory = ServiceLocator.GetContextFactory();
41
42    /*private void TestJobStreaming() {
43      ISessionFactory factory =
44         ServiceLocator.GetSessionFactory();
45
46      ISession session =
47           factory.GetSessionForCurrentThread();
48
49      IJobAdapter jobAdapter =
50        session.GetDataAdapter<HeuristicLab.Hive.Contracts.BusinessObjects.JobDto, IJobAdapter>();
51
52      Stream s = jobAdapter.GetSerializedJobStream(
53        new Guid("1b35f32b-d880-4c76-86af-4b4e283b30e6"), true);
54
55      int length = 0;
56
57      FileStream fs =
58        new FileStream(@"W:\\result.gz", FileMode.Create);
59
60      byte[] buffer = new byte[1024];
61      while ((length = s.Read(buffer, 0, buffer.Length)) > 0) {
62        fs.Write(buffer, 0, length);
63      }
64
65      fs.Close();
66      s.Close();
67
68      session.EndSession();
69    }
70
71    private void TestJobResultStreaming() {
72      ISessionFactory factory =
73         ServiceLocator.GetSessionFactory();
74
75      ISession session =
76           factory.GetSessionForCurrentThread();
77
78      IJobResultsAdapter jobResultsAdapter =
79        session.GetDataAdapter<JobResult, IJobResultsAdapter>();
80
81      Stream s = jobResultsAdapter.GetSerializedJobResultStream(
82        new Guid("c20b11a9-cde1-4d7f-8499-23dedb5a65ed"), true);
83
84      int length = 0;
85
86      FileStream fs =
87        new FileStream(@"W:\\result.gz", FileMode.Create);
88
89      byte[] buffer = new byte[1024];
90      while ((length = s.Read(buffer, 0, buffer.Length)) > 0) {
91        fs.Write(buffer, 0, length);
92      }
93
94      fs.Close();
95      s.Close();
96
97      session.EndSession();
98    }
99
100    /*private void TestJobResultDeserialization() {
101      ExecutionEngineFacade executionEngineFacade =
102        new ExecutionEngineFacade();
103
104      ResponseObject<SerializedJobResult> response =
105        executionEngineFacade.GetLastSerializedResult(
106        new Guid("56ce20bc-067b-424d-a7df-67aaace7c850"), false);
107
108      IStorable restoredJob =
109        PersistenceManager.RestoreFromGZip(response.Obj.SerializedJobResultData);
110    } */
111
112    private ClientDao clientDao = new ClientDao();
113    private ClientGroupDao cgd = new ClientGroupDao();
114
115    private void TestLINQImplementation() {
116      ClientDto c1 = new ClientDto();
117      c1.Id = Guid.NewGuid();
118      c1.FreeMemory = 1000;
119      c1.Login = DateTime.Now;
120      c1.Memory = 1000;
121      c1.Name = "jackie";
122      c1.NrOfCores = 3;
123      c1.NrOfFreeCores = 2;
124      c1.CpuSpeedPerCore = 2500;
125      c1.State = State.Idle;
126      c1 = clientDao.Insert(c1);
127
128      clientDao.Update(c1);
129
130      ClientDto c2 = new ClientDto();
131      c2.Id = Guid.NewGuid();
132      c2.FreeMemory = 600;
133      c2.Login = DateTime.Now;
134      c2.Memory = 2048;
135      c2.Name = "HPCs";
136      c2.NrOfCores = 4;
137      c2.NrOfFreeCores = 1;
138      c2.CpuSpeedPerCore = 4000;
139      c2.State = State.Idle;
140      c2 = clientDao.Insert(c2);
141
142      ClientGroupDto tg = new ClientGroupDto();
143      tg.Name = "TopGroup";
144      tg = cgd.Insert(tg);
145
146      ClientGroupDto sg = new ClientGroupDto();
147      sg.Name = "Subgroup";
148      sg = cgd.Insert(sg);
149
150      cgd.AddRessourceToClientGroup(sg.Id, tg.Id);
151      cgd.AddRessourceToClientGroup(c1.Id, tg.Id);
152      cgd.AddRessourceToClientGroup(c2.Id, tg.Id);
153
154      JobDto job = new JobDto {
155        Client = c1,
156        CoresNeeded = 2,
157        DateCreated = DateTime.Now,
158        MemoryNeeded = 500,
159        Percentage = 0,
160        Priority = 1,
161        State = State.Offline
162      };
163
164      job = DaoLocator.JobDao.Insert(job);
165
166
167      DaoLocator.JobDao.AssignClientToJob(c1.Id, job.Id);
168
169      List<ClientGroupDto> list = new List<ClientGroupDto>(cgd.FindAllWithSubGroupsAndClients());
170
171      cgd.RemoveRessourceFromClientGroup(sg.Id, tg.Id);
172
173      cgd.Delete(sg);
174      cgd.Delete(tg);
175      clientDao.Delete(c1);
176      clientDao.Delete(c2);
177    }
178
179    private void StressTest() {
180      //Speed Test
181      Random r = new Random();
182
183
184      for (int i = 0; i < 200; i++) {
185        ClientGroupDto mg = new ClientGroupDto();
186        mg.Name = "MainGroup" + i;
187        mg = cgd.Insert(mg);
188
189        populateMainGroup(mg, 3);
190      }
191    }
192
193    private void populateMainGroup(ClientGroupDto mg, int p) {
194      Random r = new Random();
195
196      for (int j = 0; j < r.Next(15); j++) {
197        ClientDto client = new ClientDto();
198        client.Id = Guid.NewGuid();
199        client.FreeMemory = r.Next(1000);
200        client.Login = DateTime.Now;
201        client.Memory = r.Next(500);
202        client.Name = "client" + mg.Name + "_" + j;
203        client.NrOfCores = 3;
204        client.NrOfFreeCores = 2;
205        client.CpuSpeedPerCore = 2500;
206        client.State = State.Idle;
207        client = clientDao.Insert(client);
208        cgd.AddRessourceToClientGroup(client.Id, mg.Id);
209      }
210      for (int i = 0; i < r.Next(p); i++) {
211        ClientGroupDto sg = new ClientGroupDto();
212        sg.Name = "SubGroup " + mg.Name + " - " + p;
213        sg = cgd.Insert(sg);
214        cgd.AddRessourceToClientGroup(sg.Id, mg.Id);
215        populateMainGroup(sg, p - 1);
216      }
217    }
218
219   
220    public override void Run() {
221      //TestClientGroupAdapter();
222      //InsertTestClientGroups();
223      //TestJobStreaming();
224      //TestJobResultStreaming();
225      //TestJobResultDeserialization();
226      using (contextFactory.GetContext(false)) {
227        if (contextFactory.CurrentContext.DatabaseExists())
228          contextFactory.CurrentContext.DeleteDatabase();
229        contextFactory.CurrentContext.CreateDatabase();
230      }
231
232      using (contextFactory.GetContext()) {
233        //TestLINQImplementation();
234        TestLinqFileHandling();
235
236        //StressTest();
237
238        //SpeedTest();
239        //TestJobBytearrFetching();
240        //TestJobStreamFetching();
241      }
242
243    }
244
245    private void WriteToJobWithByte(object jobid) {
246      using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required,
247                                                           new TransactionOptions { IsolationLevel = ApplicationConstants.ISOLATION_LEVEL_SCOPE })) {
248        Logger.Info("starting bytestuff for job " + jobid);
249        DaoLocator.JobDao.SetBinaryJobFile((Guid)jobid, jobmap[(Guid)jobid]);
250        scope.Complete();
251        Logger.Info("ended bytestuff for job " + jobid);
252      }
253    }
254
255    private void UpdateJobStatus(object jobid) {
256      using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required,
257                                                           new TransactionOptions { IsolationLevel = ApplicationConstants.ISOLATION_LEVEL_SCOPE })) {
258        Thread.Sleep(1500);
259        Logger.Info("starting now");
260        JobDto job = DaoLocator.JobDao.FindById((Guid)jobid);
261        job.Percentage = new Random().NextDouble();
262        DaoLocator.JobDao.Update(job);
263        scope.Complete();
264        Logger.Info("ended");
265      }
266    }
267
268    Dictionary<Guid, byte[]> jobmap = new Dictionary<Guid, byte[]>();
269
270
271    private void TestLinqFileHandling() {
272      List<Thread> jobupdateThreads = new List<Thread>();
273      List<Thread> jobProgressThreads = new List<Thread>();
274
275      Random r = new Random();
276      Stack<JobDto> jobs = new Stack<JobDto>(DaoLocator.JobDao.FindAll());
277      Logger.Info("Fetched jobs");
278      for (int x = 0; x < 10 && jobs.Count > 0; x++) {
279        Logger.Info("Creating data for Job");
280        JobDto job = jobs.Pop();
281        byte[] jobarr = new byte[50 * 1024 * 1024];
282        for (int i = 0; i < jobarr.Length; i++) {
283          jobarr[i] = (byte)r.Next(255);
284        }
285        jobmap.Add(job.Id, jobarr);
286      }
287      Logger.Info("filled");
288      foreach (KeyValuePair<Guid, byte[]> kvp in jobmap) {
289        Thread tjob = new Thread(new ParameterizedThreadStart(WriteToJobWithByte));
290        Thread tupdate = new Thread(new ParameterizedThreadStart(UpdateJobStatus));
291        jobupdateThreads.Add(tupdate);
292        jobProgressThreads.Add(tjob);
293        tupdate.Start(kvp.Key);
294        tjob.Start(kvp.Key);
295      }
296      foreach (Thread t in jobupdateThreads) {
297        t.Join();
298      }
299      foreach (Thread t in jobProgressThreads) {
300        t.Join();
301      }
302    }
303
304
305    private void TestJobStreamFetching() {
306      //using (TransactionScope scope = new TransactionScope()) {
307
308      HiveDataContext context = contextFactory.CurrentContext as HiveDataContext;
309
310      context.Connection.Open();
311      context.Transaction = context.Connection.BeginTransaction();
312
313      ClientFacade facade = new ClientFacade();
314      Stream stream = facade.SendStreamedJob(new Guid("F5CFB334-66A0-417C-A585-71711BA21D3F"));
315
316      byte[] buffer = new byte[3024];
317      int read = 0;
318      while ((read = stream.Read(buffer, 0, buffer.Length)) > 0) { }
319      stream.Close();
320
321      //Stream stream = DaoLocator.JobDao.GetSerializedJobStream(new Guid("bbb51f87-4e2f-4499-a9b6-884e589c78b6"));
322      //}
323    }
324
325    private void TestJobBytearrFetching() {
326      byte[] arr = DaoLocator.JobDao.GetBinaryJobFile(new Guid("A3386907-2B3C-4976-BE07-04D660D40A5B"));
327      Console.WriteLine(arr);
328    }
329
330    private void SpeedTest() {
331      DateTime start = new DateTime();
332
333      List<ClientGroupDto> list = new List<ClientGroupDto>(cgd.FindAllWithSubGroupsAndClients());
334
335      DateTime end = new DateTime();
336
337      TimeSpan used = end - start;
338      Console.WriteLine(used.TotalMilliseconds);
339    }
340  }
341}
Note: See TracBrowser for help on using the repository browser.