1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.IO;
|
---|
4 | using System.Linq;
|
---|
5 | using System.Reflection;
|
---|
6 | using System.Threading;
|
---|
7 | using System.Threading.Tasks;
|
---|
8 | using HeuristicLab.Algorithms.GeneticAlgorithm;
|
---|
9 | using HeuristicLab.Clients.Hive.Jobs;
|
---|
10 | using HeuristicLab.Clients.Hive.SlaveCore;
|
---|
11 | using HeuristicLab.Common;
|
---|
12 | using HeuristicLab.Core;
|
---|
13 | using HeuristicLab.Hive;
|
---|
14 | using HeuristicLab.Persistence.Default.Xml;
|
---|
15 | using HeuristicLab.PluginInfrastructure;
|
---|
16 | using HeuristicLab.Problems.TestFunctions;
|
---|
17 |
|
---|
18 | namespace HeuristicLab.Clients.Hive.Slave.Tests {
|
---|
19 | #region Test Setup
|
---|
20 | public class Program {
|
---|
21 | static void Main(string[] args) {
|
---|
22 | //TestThreadSafeLog(); return;
|
---|
23 | //TestThreadSafePersistence(); return;
|
---|
24 | TestCappedLog(); return;
|
---|
25 | string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
|
---|
26 |
|
---|
27 | DeleteDirectory(Path.Combine(path, "PluginCache"));
|
---|
28 | DeleteDirectory(Path.Combine(path, "PluginTemp"));
|
---|
29 |
|
---|
30 | var pm = new HeuristicLab.PluginInfrastructure.Manager.PluginManager(path);
|
---|
31 | pm.DiscoverAndCheckPlugins();
|
---|
32 | var app = pm.Applications.Where(x => x.Name == "HeuristicLab.Clients.Hive.Slave.Tests").Single();
|
---|
33 | pm.Run(app);
|
---|
34 | }
|
---|
35 |
|
---|
36 | private static void TestCappedLog() {
|
---|
37 | ILog log = new Log(); // maxMessageCount = -1
|
---|
38 | Console.WriteLine("0=={0}", log.Messages.Count());
|
---|
39 | log.LogMessage("asdf");
|
---|
40 | Console.WriteLine("1=={0}", log.Messages.Count());
|
---|
41 | log.LogMessage("asdf");
|
---|
42 | log.LogMessage("asdf");
|
---|
43 | Console.WriteLine("3=={0}", log.Messages.Count());
|
---|
44 |
|
---|
45 | ILog log0 = new Log(0);
|
---|
46 | Console.WriteLine("0=={0}", log0.Messages.Count());
|
---|
47 | log0.LogMessage("asdf");
|
---|
48 | Console.WriteLine("0=={0}", log0.Messages.Count());
|
---|
49 | log0.LogMessage("asdf");
|
---|
50 | log0.LogMessage("asdf");
|
---|
51 | log0.LogMessage("asdf");
|
---|
52 | Console.WriteLine("0=={0}", log0.Messages.Count());
|
---|
53 |
|
---|
54 | ILog log1 = new Log(1);
|
---|
55 | log1.LogMessage("asdf 1");
|
---|
56 | Console.WriteLine("1=={0}", log1.Messages.Count());
|
---|
57 | log1.LogMessage("asdf 2");
|
---|
58 | log1.LogMessage("asdf 3");
|
---|
59 | Console.WriteLine("1=={0}", log1.Messages.Count());
|
---|
60 | Console.WriteLine("asdf 3=={0}", log1.Messages.First());
|
---|
61 | }
|
---|
62 |
|
---|
63 | private static void TestThreadSafeLog() {
|
---|
64 | ILog log = new ThreadSafeLog(new Log());
|
---|
65 | bool abortOutputThread = false;
|
---|
66 |
|
---|
67 | Task outputTask = Task.Factory.StartNew(() => {
|
---|
68 | while (!abortOutputThread) {
|
---|
69 | Console.Clear();
|
---|
70 | foreach (var m in log.Messages) {
|
---|
71 | Console.WriteLine(m);
|
---|
72 | }
|
---|
73 | }
|
---|
74 | });
|
---|
75 |
|
---|
76 | int n = 50;
|
---|
77 | Task[] tasks = new Task[n];
|
---|
78 | for (int i = 0; i < n; i++) {
|
---|
79 | tasks[i] = Task.Factory.StartNew((idx) => {
|
---|
80 | for (int j = 0; j < 5000; j++) {
|
---|
81 | log.LogMessage(string.Format("Message {0}, Task {1}", j, idx));
|
---|
82 | }
|
---|
83 | }, i);
|
---|
84 | }
|
---|
85 | Task.WaitAll(tasks);
|
---|
86 | abortOutputThread = true;
|
---|
87 | outputTask.Wait();
|
---|
88 | Console.WriteLine("Finished, {0} messages", log.Messages.Count());
|
---|
89 | Console.ReadLine();
|
---|
90 | }
|
---|
91 |
|
---|
92 | private static void TestThreadSafePersistence() {
|
---|
93 | int n = 100;
|
---|
94 | Task[] tasks = new Task[n];
|
---|
95 | for (int i = 0; i < n; i++) {
|
---|
96 | tasks[i] = Task.Factory.StartNew((idx) => {
|
---|
97 | var alg = new GeneticAlgorithm { Engine = new SequentialEngine.SequentialEngine(), Problem = new SingleObjectiveTestFunctionProblem() };
|
---|
98 | byte[] data = Serialize(alg);
|
---|
99 | Console.WriteLine("Object #{0} serialized", idx);
|
---|
100 | }, i);
|
---|
101 | }
|
---|
102 | Task.WaitAll(tasks);
|
---|
103 | }
|
---|
104 | public static byte[] Serialize(object obj) {
|
---|
105 | MemoryStream memStream = new MemoryStream();
|
---|
106 | XmlGenerator.Serialize(obj, memStream);
|
---|
107 | byte[] jobByteArray = memStream.ToArray();
|
---|
108 | memStream.Dispose();
|
---|
109 | return jobByteArray;
|
---|
110 | }
|
---|
111 |
|
---|
112 | public static bool DeleteDirectory(string target_dir) {
|
---|
113 | if (!Directory.Exists(target_dir))
|
---|
114 | return true;
|
---|
115 |
|
---|
116 | bool result = false;
|
---|
117 |
|
---|
118 | string[] files = Directory.GetFiles(target_dir);
|
---|
119 | string[] dirs = Directory.GetDirectories(target_dir);
|
---|
120 |
|
---|
121 | foreach (string file in files) {
|
---|
122 | File.SetAttributes(file, FileAttributes.Normal);
|
---|
123 | File.Delete(file);
|
---|
124 | }
|
---|
125 |
|
---|
126 | foreach (string dir in dirs) {
|
---|
127 | DeleteDirectory(dir);
|
---|
128 | }
|
---|
129 |
|
---|
130 | Directory.Delete(target_dir, false);
|
---|
131 |
|
---|
132 | return result;
|
---|
133 | }
|
---|
134 | }
|
---|
135 | #endregion
|
---|
136 |
|
---|
137 | [Plugin("HeuristicLab.Clients.Hive.Slave.Tests", "1.0.0.0")]
|
---|
138 | [PluginFile("HeuristicLab.Clients.Hive.Slave-3.4.Tests.exe", PluginFileType.Assembly)]
|
---|
139 | public class TestPlugin : PluginBase { }
|
---|
140 |
|
---|
141 | [Application("HeuristicLab.Clients.Hive.Slave.Tests")]
|
---|
142 | public class TestApp : ApplicationBase {
|
---|
143 |
|
---|
144 | public override void Run() {
|
---|
145 | var mockPluginService = new MockPluginService();
|
---|
146 | var pluginManager = new PluginManager(mockPluginService, new Log());
|
---|
147 | JobManager jobManager = new JobManager(pluginManager, new Log());
|
---|
148 |
|
---|
149 | jobManager.JobStarted += new EventHandler<EventArgs<SlaveJob>>(jobManager_JobStarted);
|
---|
150 | jobManager.JobPaused += new EventHandler<EventArgs<SlaveJob, JobData>>(jobManager_JobPaused);
|
---|
151 | jobManager.JobStopped += new EventHandler<EventArgs<SlaveJob, JobData>>(jobManager_JobStopped);
|
---|
152 | jobManager.JobFailed += new EventHandler<EventArgs<Tuple<SlaveJob, JobData, Exception>>>(jobManager_JobFailed);
|
---|
153 | jobManager.ExceptionOccured += new EventHandler<EventArgs<SlaveJob, Exception>>(jobManager_ExceptionOccured);
|
---|
154 |
|
---|
155 | ConfigManager.Instance = new ConfigManager(jobManager);
|
---|
156 |
|
---|
157 | //TestCalculateJobs(mockPluginService, jobManager);
|
---|
158 | //TestPauseJob(mockPluginService, jobManager);
|
---|
159 | //TestStopJob(mockPluginService, jobManager);
|
---|
160 | //TestFailJob(mockPluginService, jobManager);
|
---|
161 |
|
---|
162 | //TestAbortAll(mockPluginService, jobManager);
|
---|
163 | //TestStopAll(mockPluginService, jobManager);
|
---|
164 | //TestPauseAll(mockPluginService, jobManager);
|
---|
165 |
|
---|
166 | Console.WriteLine(jobManager.JobCount);
|
---|
167 | }
|
---|
168 |
|
---|
169 | // calculate many jobs and request execution times rapidly
|
---|
170 | private static void TestCalculateJobs(MockPluginService mockPluginService, JobManager jobManager) {
|
---|
171 | StartStatusObservationThread();
|
---|
172 |
|
---|
173 | Task[] tasks = CreateAndStartJobs(mockPluginService, jobManager, 10, 1000);
|
---|
174 | Task.WaitAll(tasks);
|
---|
175 |
|
---|
176 | Console.ReadLine();
|
---|
177 | }
|
---|
178 |
|
---|
179 | // calculate jobs and stop them
|
---|
180 | private void TestStopJob(MockPluginService mockPluginService, JobManager jobManager) {
|
---|
181 | for (int i = 0; i < 5; i++) {
|
---|
182 | var optimizerJob = new OptimizerJob(new GeneticAlgorithm { Engine = new SequentialEngine.SequentialEngine(), Problem = new SingleObjectiveTestFunctionProblem() });
|
---|
183 |
|
---|
184 | Job job;
|
---|
185 | JobData jobData;
|
---|
186 | CreateJob(mockPluginService, optimizerJob, out job, out jobData);
|
---|
187 |
|
---|
188 | jobManager.StartJobAsync(job, jobData);
|
---|
189 | Thread.Sleep(i * 1000);
|
---|
190 |
|
---|
191 | jobManager.StopJobAsync(job.Id);
|
---|
192 | }
|
---|
193 | }
|
---|
194 |
|
---|
195 | // calculate jobs and pause them
|
---|
196 | private void TestPauseJob(MockPluginService mockPluginService, JobManager jobManager) {
|
---|
197 | for (int i = 4; i < 5; i++) {
|
---|
198 | var optimizerJob = new OptimizerJob(new GeneticAlgorithm { Engine = new SequentialEngine.SequentialEngine(), Problem = new SingleObjectiveTestFunctionProblem() });
|
---|
199 |
|
---|
200 | Job job;
|
---|
201 | JobData jobData;
|
---|
202 | CreateJob(mockPluginService, optimizerJob, out job, out jobData);
|
---|
203 |
|
---|
204 | jobManager.StartJobAsync(job, jobData);
|
---|
205 | Thread.Sleep(i * 1000);
|
---|
206 |
|
---|
207 | jobManager.PauseJobAsync(job.Id);
|
---|
208 | }
|
---|
209 | }
|
---|
210 |
|
---|
211 | // start many jobs and then abort all
|
---|
212 | private void TestAbortAll(MockPluginService mockPluginService, JobManager jobManager) {
|
---|
213 | StartStatusObservationThread();
|
---|
214 |
|
---|
215 | Task[] tasks = CreateAndStartJobs(mockPluginService, jobManager, 4, 5000);
|
---|
216 | Task.WaitAll(tasks);
|
---|
217 |
|
---|
218 | Console.WriteLine("Aborting all jobs");
|
---|
219 | jobManager.AbortAllJobs();
|
---|
220 | Console.ReadLine();
|
---|
221 | }
|
---|
222 |
|
---|
223 | // start many jobs and then stop all and observe results from GA
|
---|
224 | private void TestStopAll(MockPluginService mockPluginService, JobManager jobManager) {
|
---|
225 | StartStatusObservationThread();
|
---|
226 |
|
---|
227 | Task[] tasks = CreateAndStartJobs(mockPluginService, jobManager, 8, 5000);
|
---|
228 | Task.WaitAll(tasks);
|
---|
229 |
|
---|
230 | Console.WriteLine("Stopping all jobs");
|
---|
231 | jobManager.StopAllJobsAsync();
|
---|
232 | Console.ReadLine();
|
---|
233 | }
|
---|
234 |
|
---|
235 | // start many jobs and then pause all and observe results from GA
|
---|
236 | private void TestPauseAll(MockPluginService mockPluginService, JobManager jobManager) {
|
---|
237 | StartStatusObservationThread();
|
---|
238 |
|
---|
239 | Task[] tasks = CreateAndStartJobs(mockPluginService, jobManager, 4, 1000);
|
---|
240 | Task.WaitAll(tasks);
|
---|
241 |
|
---|
242 | Console.WriteLine("Pausing all jobs");
|
---|
243 | jobManager.PauseAllJobsAsync();
|
---|
244 | Console.ReadLine();
|
---|
245 | }
|
---|
246 |
|
---|
247 | #region Helpers
|
---|
248 | private static Task[] CreateAndStartJobs(MockPluginService mockPluginService, JobManager jobManager, int n, int generations = 1000) {
|
---|
249 | Task[] tasks = new Task[n];
|
---|
250 | for (int i = 0; i < n; i++) {
|
---|
251 | tasks[i] = Task.Factory.StartNew((idx) => {
|
---|
252 | var ga = new GeneticAlgorithm { Engine = new SequentialEngine.SequentialEngine(), Problem = new SingleObjectiveTestFunctionProblem() };
|
---|
253 | ga.MaximumGenerations.Value = generations;
|
---|
254 | var mockJob = new OptimizerJob(ga);
|
---|
255 |
|
---|
256 | Job job;
|
---|
257 | JobData jobData;
|
---|
258 | CreateJob(mockPluginService, mockJob, out job, out jobData);
|
---|
259 |
|
---|
260 | jobManager.StartJobAsync(job, jobData);
|
---|
261 | }, i);
|
---|
262 | }
|
---|
263 | return tasks;
|
---|
264 | }
|
---|
265 |
|
---|
266 | private static void StartStatusObservationThread(int intervalMs = 5000) {
|
---|
267 | Task.Factory.StartNew(() => {
|
---|
268 | while (true) {
|
---|
269 | Thread.Sleep(intervalMs);
|
---|
270 | PrintInfo();
|
---|
271 | }
|
---|
272 | });
|
---|
273 | }
|
---|
274 |
|
---|
275 | private static void PrintInfo() {
|
---|
276 | var info = ConfigManager.Instance.GetClientInfo();
|
---|
277 | var heartBeatData = new Heartbeat {
|
---|
278 | SlaveId = info.Id,
|
---|
279 | FreeCores = info.Cores.HasValue ? info.Cores.Value - SlaveStatusInfo.UsedCores : 0,
|
---|
280 | FreeMemory = ConfigManager.GetFreeMemory(),
|
---|
281 | CpuUtilization = ConfigManager.Instance.GetCpuUtilization(),
|
---|
282 | JobProgress = ConfigManager.Instance.GetExecutionTimeOfAllJobs(),
|
---|
283 | AssignJob = !ConfigManager.Instance.Asleep
|
---|
284 | };
|
---|
285 | Console.WriteLine(heartBeatData);
|
---|
286 | Console.WriteLine(ConfigManager.Instance.GetStatusForClientConsole().ToString());
|
---|
287 | }
|
---|
288 |
|
---|
289 | private static object locker = new object();
|
---|
290 | private static void CreateJob(MockPluginService mockPluginService, IJob mockJob, out Job job, out JobData jobData) {
|
---|
291 | job = new Job { Id = Guid.NewGuid(), CoresNeeded = 1, MemoryNeeded = 0 };
|
---|
292 | job.PluginsNeededIds = new List<Guid>();
|
---|
293 | jobData = new JobData { JobId = job.Id };
|
---|
294 | IEnumerable<Type> types;
|
---|
295 | jobData.Data = PersistenceUtil.Serialize(mockJob, out types);
|
---|
296 |
|
---|
297 | // make plugins available through mockPluginService
|
---|
298 | var plugins = new List<IPluginDescription>();
|
---|
299 | PluginUtil.CollectDeclaringPlugins(plugins, types);
|
---|
300 | foreach (var plugin in plugins) {
|
---|
301 | var p = PluginUtil.CreatePlugin(plugin);
|
---|
302 | p.Id = Guid.NewGuid();
|
---|
303 | lock (locker) {
|
---|
304 | if (!mockPluginService.Plugins.ContainsKey(p.Id)) {
|
---|
305 | mockPluginService.Plugins[p.Id] = p;
|
---|
306 | mockPluginService.PluginDatas[p.Id] = PluginUtil.CreatePluginDatas(plugin);
|
---|
307 | }
|
---|
308 | }
|
---|
309 | foreach (var pd in mockPluginService.PluginDatas[p.Id]) {
|
---|
310 | pd.Id = Guid.NewGuid();
|
---|
311 | pd.PluginId = p.Id;
|
---|
312 | }
|
---|
313 | job.PluginsNeededIds.Add(p.Id);
|
---|
314 | }
|
---|
315 | }
|
---|
316 | #endregion
|
---|
317 |
|
---|
318 | #region Events
|
---|
319 | private void jobManager_JobStarted(object sender, EventArgs<SlaveJob> e) {
|
---|
320 | Console.WriteLine("# Job Started: {0}", e.Value.JobId);
|
---|
321 | }
|
---|
322 | private void jobManager_JobPaused(object sender, EventArgs<SlaveJob, JobData> e) {
|
---|
323 | Console.WriteLine("# Job Paused: {0}", e.Value.JobId);
|
---|
324 | PrintJobResultInfo(e.Value2);
|
---|
325 | }
|
---|
326 |
|
---|
327 | private void jobManager_JobStopped(object sender, EventArgs<SlaveJob, JobData> e) {
|
---|
328 | Console.WriteLine("# Job Stopped: {0}", e.Value.JobId);
|
---|
329 | PrintJobResultInfo(e.Value2);
|
---|
330 | }
|
---|
331 | private void jobManager_JobFailed(object sender, EventArgs<Tuple<SlaveJob, JobData, Exception>> e) {
|
---|
332 | Console.WriteLine("# Job Failed: {0}", e.Value.Item3.ToString());
|
---|
333 | if (e.Value.Item2 != null) {
|
---|
334 | PrintJobResultInfo(e.Value.Item2);
|
---|
335 | }
|
---|
336 | }
|
---|
337 | private void jobManager_ExceptionOccured(object sender, EventArgs<SlaveJob, Exception> e) {
|
---|
338 | Console.WriteLine("# Exception Occured: {0}", e.Value2);
|
---|
339 | }
|
---|
340 |
|
---|
341 | private static void PrintJobResultInfo(JobData jobData) {
|
---|
342 | var ga = (GeneticAlgorithm)PersistenceUtil.Deserialize<OptimizerJob>(jobData.Data).Item;
|
---|
343 | var results = ga.Results;
|
---|
344 | Console.WriteLine("# State: {0}, ExecTime: {1}, Generations: {2}", ga.ExecutionState, ga.ExecutionTime, results.ContainsKey("Generations") ? results["Generations"].Value.ToString() : "-");
|
---|
345 | }
|
---|
346 | #endregion
|
---|
347 | }
|
---|
348 | }
|
---|