Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.2/sources/HeuristicLab.Hive.Server.Core/3.2/DbTestApp.cs @ 3931

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

added minor speedups and better transaction handling to the server (#828)

File size: 11.4 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.Data.SqlClient;
27using System.Text;
28using HeuristicLab.PluginInfrastructure;
29using System.Net;
30using HeuristicLab.Hive.Contracts;
31using HeuristicLab.Hive.Contracts.Interfaces;
32using HeuristicLab.Hive.Server.DataAccess;
33using HeuristicLab.Hive.Contracts.BusinessObjects;
34using System.Diagnostics;
35using HeuristicLab.DataAccess.Interfaces;
36using System.IO;
37using HeuristicLab.Hive.Server.Core;
38using HeuristicLab.Core;
39using HeuristicLab.Hive.Server.LINQDataAccess;
40using System.Transactions;
41using System.Threading;
42using HeuristicLab.Tracing;
43
44namespace HeuristicLab.Hive.Server {
45  [Application("Hive DB Test App", "Test Application for the Hive DataAccess Layer")]
46  internal class HiveDbTestApplication : ApplicationBase {
47    /*private void TestJobStreaming() {
48      ISessionFactory factory =
49         ServiceLocator.GetSessionFactory();
50
51      ISession session =
52           factory.GetSessionForCurrentThread();
53
54      IJobAdapter jobAdapter =
55        session.GetDataAdapter<HeuristicLab.Hive.Contracts.BusinessObjects.JobDto, IJobAdapter>();
56
57      Stream s = jobAdapter.GetSerializedJobStream(
58        new Guid("1b35f32b-d880-4c76-86af-4b4e283b30e6"), true);
59
60      int length = 0;
61
62      FileStream fs =
63        new FileStream(@"W:\\result.gz", FileMode.Create);
64
65      byte[] buffer = new byte[1024];
66      while ((length = s.Read(buffer, 0, buffer.Length)) > 0) {
67        fs.Write(buffer, 0, length);
68      }
69
70      fs.Close();
71      s.Close();
72
73      session.EndSession();
74    }
75
76    private void TestJobResultStreaming() {
77      ISessionFactory factory =
78         ServiceLocator.GetSessionFactory();
79
80      ISession session =
81           factory.GetSessionForCurrentThread();
82
83      IJobResultsAdapter jobResultsAdapter =
84        session.GetDataAdapter<JobResult, IJobResultsAdapter>();
85
86      Stream s = jobResultsAdapter.GetSerializedJobResultStream(
87        new Guid("c20b11a9-cde1-4d7f-8499-23dedb5a65ed"), true);
88
89      int length = 0;
90
91      FileStream fs =
92        new FileStream(@"W:\\result.gz", FileMode.Create);
93
94      byte[] buffer = new byte[1024];
95      while ((length = s.Read(buffer, 0, buffer.Length)) > 0) {
96        fs.Write(buffer, 0, length);
97      }
98
99      fs.Close();
100      s.Close();
101
102      session.EndSession();
103    }
104
105    /*private void TestJobResultDeserialization() {
106      ExecutionEngineFacade executionEngineFacade =
107        new ExecutionEngineFacade();
108
109      ResponseObject<SerializedJobResult> response =
110        executionEngineFacade.GetLastSerializedResult(
111        new Guid("56ce20bc-067b-424d-a7df-67aaace7c850"), false);
112
113      IStorable restoredJob =
114        PersistenceManager.RestoreFromGZip(response.Obj.SerializedJobResultData);
115    } */
116
117    private ClientDao clientDao = new ClientDao();
118    private ClientGroupDao cgd = new ClientGroupDao();
119
120    private void TestLINQImplementation() {
121      ClientDto c1 = new ClientDto();
122      c1.Id = Guid.NewGuid();
123      c1.FreeMemory = 1000;
124      c1.Login = DateTime.Now;
125      c1.Memory = 1000;
126      c1.Name = "jackie";
127      c1.NrOfCores = 3;
128      c1.NrOfFreeCores = 2;
129      c1.CpuSpeedPerCore = 2500;
130      c1.State = State.idle;
131      c1 = clientDao.Insert(c1);
132
133      clientDao.Update(c1);
134
135      ClientDto c2 = new ClientDto();
136      c2.Id = Guid.NewGuid();
137      c2.FreeMemory = 600;
138      c2.Login = DateTime.Now;
139      c2.Memory = 2048;
140      c2.Name = "HPCs";
141      c2.NrOfCores = 4;
142      c2.NrOfFreeCores = 1;
143      c2.CpuSpeedPerCore = 4000;
144      c2.State = State.idle;
145      c2 = clientDao.Insert(c2);
146
147      ClientGroupDto tg = new ClientGroupDto();
148      tg.Name = "TopGroup";
149      tg = cgd.Insert(tg);
150
151      ClientGroupDto sg = new ClientGroupDto();
152      sg.Name = "Subgroup";
153      sg = cgd.Insert(sg);
154
155      cgd.AddRessourceToClientGroup(sg.Id, tg.Id);
156      cgd.AddRessourceToClientGroup(c1.Id, tg.Id);
157      cgd.AddRessourceToClientGroup(c2.Id, tg.Id);
158
159      JobDto job = new JobDto {
160                                Client = c1,
161                                CoresNeeded = 2,
162                                DateCreated = DateTime.Now,
163                                MemoryNeeded = 500,
164                                Percentage = 0,
165                                Priority = 1,
166                                State = State.offline
167                              };
168
169      job = DaoLocator.JobDao.Insert(job);
170
171
172      DaoLocator.JobDao.AssignClientToJob(c1.Id, job.Id);
173
174      List<ClientGroupDto> list = new List<ClientGroupDto>(cgd.FindAllWithSubGroupsAndClients());
175
176      cgd.RemoveRessourceFromClientGroup(sg.Id, tg.Id);
177
178      cgd.Delete(sg);
179      cgd.Delete(tg);
180      clientDao.Delete(c1);
181      clientDao.Delete(c2);
182    }
183
184    private void StressTest() {
185      //Speed Test
186      Random r = new Random();
187
188
189      for (int i = 0; i < 200; i++) {
190        ClientGroupDto mg = new ClientGroupDto();
191        mg.Name = "MainGroup" + i;
192        mg = cgd.Insert(mg);
193
194        populateMainGroup(mg, 3);
195      }
196    }
197
198    private void populateMainGroup(ClientGroupDto mg, int p) {
199      Random r = new Random();
200
201      for (int j = 0; j < r.Next(15); j++) {
202        ClientDto client = new ClientDto();
203        client.Id = Guid.NewGuid();
204        client.FreeMemory = r.Next(1000);
205        client.Login = DateTime.Now;
206        client.Memory = r.Next(500);
207        client.Name = "client" + mg.Name + "_" + j;
208        client.NrOfCores = 3;
209        client.NrOfFreeCores = 2;
210        client.CpuSpeedPerCore = 2500;
211        client.State = State.idle;
212        client = clientDao.Insert(client);
213        cgd.AddRessourceToClientGroup(client.Id, mg.Id);
214      }
215      for (int i = 0; i < r.Next(p); i++) {
216        ClientGroupDto sg = new ClientGroupDto();
217        sg.Name = "SubGroup " + mg.Name + " - " + p;
218        sg = cgd.Insert(sg);
219        cgd.AddRessourceToClientGroup(sg.Id, mg.Id);
220        populateMainGroup(sg, p - 1);
221      }
222    }
223
224
225    public override void Run() {
226      //TestClientGroupAdapter();
227      //InsertTestClientGroups();
228      //TestJobStreaming();
229      //TestJobResultStreaming();
230      //TestJobResultDeserialization();
231
232      //if (ContextFactory.Context.DatabaseExists())
233      //  ContextFactory.Context.DeleteDatabase();
234      //ContextFactory.Context.CreateDatabase();
235      //TestLINQImplementation();
236      TestLinqFileHandling();
237      //StressTest();
238
239      //SpeedTest();
240      //TestJobBytearrFetching();
241      //TestJobStreamFetching();
242    }
243
244    private void WriteToJobWithByte(object jobid) {
245      using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required,
246                                                           new TransactionOptions
247                                                           {IsolationLevel = ApplicationConstants.ISOLATION_LEVEL_SCOPE})
248        ) {
249        Logger.Info("starting bytestuff for job " + jobid);
250        DaoLocator.JobDao.SetBinaryJobFile((Guid)jobid, jobmap[(Guid)jobid]);
251        scope.Complete();
252        Logger.Info("ended bytestuff for job " + jobid);
253      }
254    }
255
256    private void UpdateJobStatus(object jobid) {
257      using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required,
258                                                           new TransactionOptions
259                                                           {IsolationLevel = ApplicationConstants.ISOLATION_LEVEL_SCOPE})
260        ) {
261        Thread.Sleep(1500);
262        Logger.Info("starting now");
263        JobDto job = DaoLocator.JobDao.FindById((Guid)jobid);
264        job.Percentage = new Random().NextDouble();
265        DaoLocator.JobDao.Update(job);
266        scope.Complete();
267        Logger.Info("ended");
268      }
269    }
270
271    Dictionary<Guid, byte[]> jobmap = new Dictionary<Guid, byte[]>();
272
273
274    private void TestLinqFileHandling() {
275      List<Thread> jobupdateThreads = new List<Thread>();
276      List<Thread> jobProgressThreads = new List<Thread>();
277     
278      Random r = new Random();
279      Stack<JobDto> jobs = new Stack<JobDto>(DaoLocator.JobDao.FindAll());
280      Logger.Info("Fetched jobs");
281      for (int x = 0; x < 10 && jobs.Count > 0; x++ ) {
282        Logger.Info("Creating data for Job");
283        JobDto job = jobs.Pop();
284        byte[] jobarr = new byte[50*1024*1024];
285        for (int i = 0; i < jobarr.Length; i++) {
286          jobarr[i] = (byte) r.Next(255);
287        }
288        jobmap.Add(job.Id, jobarr);
289      }
290      Logger.Info("filled");
291      foreach(KeyValuePair<Guid, byte[]> kvp in jobmap) {
292        Thread tjob = new Thread(new ParameterizedThreadStart(WriteToJobWithByte));     
293        Thread tupdate = new Thread(new ParameterizedThreadStart(UpdateJobStatus));
294        jobupdateThreads.Add(tupdate);
295        jobProgressThreads.Add(tjob);
296        tupdate.Start(kvp.Key);
297        tjob.Start(kvp.Key);
298      }
299      foreach (Thread t in jobupdateThreads) {
300        t.Join();
301      }
302      foreach (Thread t in jobProgressThreads) {
303        t.Join();
304      }
305    }
306
307
308    private
309      void TestJobStreamFetching() {
310      //using (TransactionScope scope = new TransactionScope()) {
311
312      HiveDataContext context = ContextFactory.Context;
313
314      ContextFactory.Context.Connection.Open
315        ();
316      ContextFactory.Context.Transaction
317        =
318        ContextFactory.Context.Connection.BeginTransaction
319          ();
320
321
322      ClientFacade facade = new ClientFacade();
323
324      Stream stream = facade.SendStreamedJob(new Guid("F5CFB334-66A0-417C-A585-71711BA21D3F"));
325
326
327      byte[] buffer = new byte[3024];
328
329      int read = 0;
330
331      while ((
332               read
333               =
334               stream.Read
335                 (
336                 buffer
337                 , 0,
338                 buffer.Length
339                 )) > 0) {}
340
341      stream.Close
342        ();
343
344      //Stream stream = DaoLocator.JobDao.GetSerializedJobStream(new Guid("bbb51f87-4e2f-4499-a9b6-884e589c78b6"));
345      //}
346    }
347
348    private
349      void TestJobBytearrFetching() {
350      byte[] arr = DaoLocator.JobDao.GetBinaryJobFile(new Guid("A3386907-2B3C-4976-BE07-04D660D40A5B"));
351      Console.WriteLine
352        (
353        arr
354        );
355    }
356
357    private
358      void SpeedTest() {
359      DateTime start = new DateTime();
360
361      List<ClientGroupDto> list = new List<ClientGroupDto>(cgd.FindAllWithSubGroupsAndClients());
362
363      DateTime end = new DateTime();
364
365      TimeSpan used = end - start;
366      Console.WriteLine
367        (
368        used.TotalMilliseconds
369        );
370    }
371  }
372}
Note: See TracBrowser for help on using the repository browser.