Free cookie consent management tool by TermsFeed Policy Generator

Changeset 6495 for trunk/sources


Ignore:
Timestamp:
06/28/11 23:29:26 (13 years ago)
Author:
cneumuel
Message:

#1569 checked in test-case

Location:
trunk/sources/HeuristicLab/3.3/Tests
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab/3.3/Tests/AlgorithmExtensions.cs

    r6205 r6495  
    2222using System;
    2323using System.Threading;
     24using HeuristicLab.Common;
    2425using HeuristicLab.Core;
    25 using HeuristicLab.Optimization;
    2626
    2727namespace HeuristicLab_33.Tests {
    2828  public static class AlgorithmExtensions {
    29     public static void StartSync(this IAlgorithm algorithm, CancellationToken cancellationToken) {
    30       var executor = new AlgorithmExecutor(algorithm, cancellationToken);
     29    public static void StartSync(this IExecutable executable, CancellationToken cancellationToken) {
     30      var executor = new AlgorithmExecutor(executable, cancellationToken);
    3131      executor.StartSync();
    3232    }
     
    3737  /// </summary>
    3838  internal class AlgorithmExecutor {
    39     private IAlgorithm algorithm;
     39    private IExecutable executable;
    4040    private AutoResetEvent waitHandle = new AutoResetEvent(false);
    4141    private CancellationToken cancellationToken;
     42    private Exception occuredException;
    4243
    43     public AlgorithmExecutor(IAlgorithm algorithm, CancellationToken cancellationToken) {
    44       this.algorithm = algorithm;
     44    public AlgorithmExecutor(IExecutable executable, CancellationToken cancellationToken) {
     45      this.executable = executable;
    4546      this.cancellationToken = cancellationToken;
     47      this.occuredException = null;
    4648    }
    4749
    4850    public void StartSync() {
    49       algorithm.Stopped += new EventHandler(algorithm_Stopped);
    50       algorithm.Paused += new EventHandler(algorithm_Paused);
     51      executable.Stopped += new EventHandler(executable_Stopped);
     52      executable.Paused += new EventHandler(executable_Paused);
     53      executable.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(executable_ExceptionOccurred);
    5154
    5255      using (CancellationTokenRegistration registration = cancellationToken.Register(new Action(cancellationToken_Canceled))) {
    53         algorithm.Start();
    54         waitHandle.WaitOne();
     56        executable.Start();
     57        waitHandle.WaitOne(-1, false);
    5558        waitHandle.Dispose();
    5659      }
    5760
    58       algorithm.Stopped -= new EventHandler(algorithm_Stopped);
    59       algorithm.Paused -= new EventHandler(algorithm_Paused);
    60       if (algorithm.ExecutionState == ExecutionState.Started) {
    61         algorithm.Pause();
     61      executable.Stopped -= new EventHandler(executable_Stopped);
     62      executable.Paused -= new EventHandler(executable_Paused);
     63      if (executable.ExecutionState == ExecutionState.Started) {
     64        executable.Pause();
    6265      }
    6366      cancellationToken.ThrowIfCancellationRequested();
     67      if (occuredException != null) throw occuredException;
    6468    }
    6569
    66     private void algorithm_Paused(object sender, EventArgs e) {
     70    private void executable_Paused(object sender, EventArgs e) {
    6771      waitHandle.Set();
    6872    }
    6973
    70     private void algorithm_Stopped(object sender, EventArgs e) {
     74    private void executable_Stopped(object sender, EventArgs e) {
    7175      waitHandle.Set();
     76    }
     77
     78    private void executable_ExceptionOccurred(object sender, EventArgs<Exception> e) {
     79      occuredException = e.Value; // after an exception occured the executable pauses
    7280    }
    7381
  • trunk/sources/HeuristicLab/3.3/Tests/CollectObjectGraphTest.cs

    r6209 r6495  
    2525using System.Linq;
    2626using System.Threading;
     27using System.Threading.Tasks;
    2728using HeuristicLab.Algorithms.GeneticAlgorithm;
    2829using HeuristicLab.Common;
     
    3031using HeuristicLab.Persistence.Default.Xml;
    3132using HeuristicLab.Problems.TestFunctions;
    32 using HeuristicLab.Random;
    3333using HeuristicLab.SequentialEngine;
    3434using Microsoft.VisualStudio.TestTools.UnitTesting;
     
    9696    [TestMethod]
    9797    public void AlgorithmExecutions() {
    98       var random = new MersenneTwister(0);
    9998      var algs = new List<IAlgorithm>();
    10099
     
    117116      }
    118117    }
     118
     119    /// <summary>
     120    /// Test the execution of many algorithms in parallel
     121    /// </summary>
     122    [TestMethod]
     123    public void ParallelAlgorithmExecutions() {
     124      int n = 60;
     125      var tasks = new Task[n];
     126
     127      TestContext.WriteLine("creating tasks...");
     128      for (int i = 0; i < n; i++) {
     129        tasks[i] = new Task((iobj) => {
     130          int locali = (int)iobj;
     131          GeneticAlgorithm ga = new GeneticAlgorithm();
     132          ga.Name = "Alg " + locali;
     133          ga.PopulationSize.Value = 5;
     134          ga.MaximumGenerations.Value = 5;
     135          ga.Engine = new SequentialEngine();
     136          ga.Problem = new SingleObjectiveTestFunctionProblem();
     137          ga.Prepare(true);
     138          Console.WriteLine("{0}; Objects before execution: {1}", ga.Name, ga.GetObjectGraphObjects().Count());
     139          var sw = new Stopwatch();
     140          sw.Start();
     141          ga.StartSync(new CancellationToken());
     142          sw.Stop();
     143          Console.WriteLine("{0}; Objects after execution: {1}", ga.Name, ga.GetObjectGraphObjects().Count());
     144          Console.WriteLine("{0}; ExecutionTime: {1} ", ga.Name, sw.Elapsed);
     145        }, i);
     146      }
     147      TestContext.WriteLine("starting tasks...");
     148      for (int i = 0; i < n; i++) {
     149        tasks[i].Start();
     150      }
     151      TestContext.WriteLine("waiting for tasks to finish...");
     152      Task.WaitAll(tasks);
     153    }
    119154  }
    120155}
Note: See TracChangeset for help on using the changeset viewer.