Free cookie consent management tool by TermsFeed Policy Generator

Changeset 6948


Ignore:
Timestamp:
11/04/11 13:13:17 (13 years ago)
Author:
spimming
Message:

#1659:

  • restructuring of the benchmarking algorithms
  • common interface for benchmarks
  • 'benchmark' class discovers benchmarking algorithms
  • added 'TimeLimit' and 'ChunkSize' parameters to benchmark
Location:
branches/Benchmarking/sources
Files:
4 added
3 deleted
3 edited

Legend:

Unmodified
Added
Removed
  • branches/Benchmarking/sources/HeuristicLab.Algorithms.Benchmarks.Views/3.3/BenchmarkView.cs

    r6934 r6948  
    2626namespace HeuristicLab.Algorithms.Benchmarks.Views {
    2727  [View("Benchmark View")]
    28   [Content(typeof(Whetstone), true)]
    29   [Content(typeof(Dhrystone), true)]
    30   [Content(typeof(Linpack), true)]
     28  [Content(typeof(Benchmark), true)]
    3129  public partial class BenchmarkView : AlgorithmView {
    3230    public BenchmarkView() {
    3331      InitializeComponent();
    3432      tabControl.TabPages.Remove(this.problemTabPage);
    35       tabControl.TabPages.Remove(this.parametersTabPage);
    3633    }
    3734  }
  • branches/Benchmarking/sources/HeuristicLab.Algorithms.Benchmarks/3.3/Benchmark.cs

    r6934 r6948  
    3131using HeuristicLab.Data;
    3232using HeuristicLab.Optimization;
     33using HeuristicLab.Parameters;
    3334using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     35using HeuristicLab.PluginInfrastructure;
    3436
    3537namespace HeuristicLab.Algorithms.Benchmarks {
     
    3739  /// A base class for benchmarks.
    3840  /// </summary>
    39   [Item("Benchmark", "A base class for benchmarks.")]
     41  [Item("Benchmark", "A wrapper for benchmark algorithms.")]
     42  [Creatable("Benchmarks")]
    4043  [StorableClass]
    41   public abstract class Benchmark : IAlgorithm {
    42 
     44  public class Benchmark : IAlgorithm {
     45    private Random random = new Random();
     46
     47    [Storable]
    4348    private DateTime lastUpdateTime;
     49
     50    [Storable]
     51    private IBenchmark benchmarkAlgorithm;
     52    public IBenchmark BenchmarkAlgorithm {
     53      get { return benchmarkAlgorithm; }
     54      set {
     55        if (value == null) throw new ArgumentNullException();
     56        benchmarkAlgorithm = value;
     57      }
     58    }
     59
     60    private CancellationTokenSource cancellationTokenSource;
    4461
    4562    [Storable]
     
    202219    }
    203220
     221    #region Parameter Properties
     222
     223    public ConstrainedValueParameter<IBenchmark> BenchmarkAlgorithmParameter {
     224      get { return (ConstrainedValueParameter<IBenchmark>)Parameters["BenchmarkAlgorithm"]; }
     225    }
     226
     227    private ValueParameter<IntValue> ChunkSizeParameter {
     228      get { return (ValueParameter<IntValue>)Parameters["ChunkSize"]; }
     229    }
     230
     231    private ValueParameter<DoubleValue> TimeLimitParameter {
     232      get { return (ValueParameter<DoubleValue>)Parameters["TimeLimit"]; }
     233    }
     234
     235    #endregion
     236
    204237    #region Constructors
    205238
    206     protected Benchmark() {
     239    public Benchmark() {
    207240      name = ItemName;
    208241      description = ItemDescription;
     
    215248      Runs = new RunCollection();
    216249      results = new ResultCollection();
     250      CreateParameters();
     251      DiscoverBenchmarks();
    217252      Prepare();
    218253    }
    219254
    220     protected Benchmark(Benchmark original, Cloner cloner) {
     255    public Benchmark(Benchmark original, Cloner cloner) {
    221256      cloner.RegisterClonedObject(original, this);
    222257      name = original.name;
     
    232267      Initialize();
    233268
    234       results = new ResultCollection();
     269      results = cloner.Clone(original.results);
     270      DiscoverBenchmarks();
    235271      Prepare();
    236272    }
    237273
    238274    #endregion
     275
     276    private void CreateParameters() {
     277      Parameters.Add(new ValueParameter<IntValue>("ChunkSize", "The size (MB) of the chunk array that gets generated", new IntValue(0)));
     278      Parameters.Add(new ValueParameter<DoubleValue>("TimeLimit", "The time limit (in minutes) for a benchmark run", new DoubleValue(0)));
     279    }
     280
     281    private void DiscoverBenchmarks() {
     282      var benchmarks = from t in ApplicationManager.Manager.GetTypes(typeof(IBenchmark))
     283                       select t;
     284      ItemSet<IBenchmark> values = new ItemSet<IBenchmark>();
     285      foreach (var benchmark in benchmarks) {
     286        IBenchmark b = (IBenchmark)Activator.CreateInstance(benchmark);
     287        values.Add(b);
     288      }
     289      string paramName = "BenchmarkAlgorithm";
     290      if (!Parameters.ContainsKey(paramName)) {
     291        if (values.Count > 0) {
     292          Parameters.Add(new ConstrainedValueParameter<IBenchmark>(paramName, values, values.First(a => a is IBenchmark)));
     293        } else {
     294          // throw exception?
     295          Parameters.Add(new ConstrainedValueParameter<IBenchmark>(paramName, values));
     296        }
     297      }
     298    }
    239299
    240300    private void Initialize() {
     
    264324      if ((ExecutionState != ExecutionState.Started) && (ExecutionState != ExecutionState.Paused))
    265325        throw new InvalidOperationException(string.Format("Stop not allowed in execution state \"{0}\".", ExecutionState));
     326      cancellationTokenSource.Cancel();
    266327    }
    267328
    268329    public virtual void Start() {
    269       var cancellationTokenSource = new CancellationTokenSource();
     330      cancellationTokenSource = new CancellationTokenSource();
    270331      OnStarted();
    271332      Task task = Task.Factory.StartNew(Run, cancellationTokenSource.Token, cancellationTokenSource.Token);
     
    283344          }
    284345        }
     346
    285347        cancellationTokenSource.Dispose();
    286348        cancellationTokenSource = null;
     
    297359      timer.Start();
    298360      try {
    299         RunBenchmark();
     361        BenchmarkAlgorithm = (IBenchmark)BenchmarkAlgorithmParameter.ActualValue;
     362        int chunkSize = ((IntValue)ChunkSizeParameter.ActualValue).Value;
     363        if (chunkSize > 0) {
     364          BenchmarkAlgorithm.ChunkData = CreateDataChuck(chunkSize);
     365        }
     366        BenchmarkAlgorithm.TimeLimit = TimeSpan.FromMinutes(((DoubleValue)TimeLimitParameter.ActualValue).Value);
     367        BenchmarkAlgorithm.Run(cancellationToken, results);
     368      }
     369      catch (OperationCanceledException) {
     370        //Console.WriteLine(ex.ToString());
    300371      }
    301372      finally {
     
    304375        ExecutionTime += DateTime.Now - lastUpdateTime;
    305376      }
    306 
    307       cancellationToken.ThrowIfCancellationRequested();
    308     }
    309 
    310     protected abstract void RunBenchmark();
     377    }
    311378
    312379    private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) {
     
    348415    }
    349416
     417    private byte[][] CreateDataChuck(int megaBytes) {
     418      if (megaBytes <= 0) {
     419        throw new ArgumentException("MegaBytes must be greater than zero", "megaBytes");
     420      }
     421      byte[][] chunk = new byte[megaBytes][];
     422      for (int i = 0; i < chunk.Length; i++) {
     423        chunk[i] = new byte[1024 * 1024];
     424        random.NextBytes(chunk[i]);
     425      }
     426      return chunk;
     427    }
     428    /*
     429    private int[] CreateDataChuck(int megaBytes) {
     430      if (megaBytes <= 0) {
     431        throw new ArgumentException("MegaBytes must be greater than zero", "megaBytes");
     432      }
     433      int size = (megaBytes * 1020 * 1024) / sizeof(int);
     434      int[] data = new int[size];
     435      for (int i = 0; i < data.Length; i++) {
     436        data[i] = random.Next();
     437      }
     438      return data;
     439    }
     440    */
     441
    350442    #region Events
    351443
     
    464556    #endregion
    465557
    466     #region IDeepCloneable Members
    467 
    468     public abstract IDeepCloneable Clone(Cloner cloner);
    469 
    470     #endregion
    471 
    472     #region ICloneable Members
     558    #region Clone
     559
     560    public IDeepCloneable Clone(Cloner cloner) {
     561      return new Benchmark(this, cloner);
     562    }
    473563
    474564    public object Clone() {
  • branches/Benchmarking/sources/HeuristicLab.Algorithms.Benchmarks/3.3/HeuristicLab.Algorithms.Benchmarks-3.3.csproj

    r6934 r6948  
    5151      <SubType>Code</SubType>
    5252    </Compile>
    53     <Compile Include="Whetstone.cs" />
    54     <Compile Include="Dhrystone.cs" />
    55     <Compile Include="Linpack.cs" />
     53    <Compile Include="DhrystoneBenchmark.cs" />
     54    <Compile Include="IBenchmark.cs" />
     55    <Compile Include="LinpackBenchmark.cs" />
    5656    <Compile Include="Plugin.cs" />
    5757    <Compile Include="Properties\AssemblyInfo.cs" />
     58    <Compile Include="WhetstoneAlgorithm.cs" />
    5859  </ItemGroup>
    5960  <ItemGroup>
Note: See TracChangeset for help on using the changeset viewer.