public RuntimeStats Run(int threadCount, int programCount, int timeout) { _stats = new RuntimeStats(programCount); using (CancellationTokenSource source = new CancellationTokenSource()) { CancellationToken token = source.Token; Task[] runners = new Task[threadCount + 1]; runners[0] = Task.Delay(timeout); for (int i = 1; i <= threadCount; ++i) { runners[i] = Task.Factory.StartNew(() => _run(token), token); } while (runners.Skip(1).Any(r => !r.IsCompleted)) { var id = Task.WaitAny(runners); if (id == 0 || runners[id].IsFaulted) { try { source.Cancel(); } catch { } Console.WriteLine(runners[id].Exception); Console.WriteLine($"Completed: {_stats.Percent}"); break; } } } return(_stats); }
private static RuntimeStats MakeStats(int threads, int count, int timeout, long seed, ProgramOptions options, bool silent, bool evalTest, Uri runnerUri) { if (!silent) { Console.WriteLine($"Collecting statistics from {count} random program executions (seed = {seed})"); } double step = 0.05; double next = step; var runner = new ParallelRunner(seed, options, evalTest, runnerUri); if (!silent) { runner.Progress += (s, e) => { if (runner.Percent > next) { Console.Write($"{runner.Percent:P0}, "); next += step; } } } ; var sw = Stopwatch.StartNew(); RuntimeStats stats = null; try { stats = runner.Run(threads, count, timeout); } catch (Exception e) { if (!silent) { Console.WriteLine(e); } } sw.Stop(); if (!silent) { Console.WriteLine(); Console.WriteLine($"Completed in {sw.Elapsed.TotalSeconds} seconds"); } var fail = stats?.FirstOrDefault(ri => !ri.Success); if (fail != null) { if (!silent) { Console.WriteLine($"Error seed: {fail.Seed}"); Console.WriteLine(fail.Output); } stats = null; } else if (stats != null && stats.IsComplete) { stats.Calculate(); } return(stats); }
public RuntimeStats Run(int threadCount, int programCount, ProgramOptions options = null) { _stats = new RuntimeStats(programCount); _options = options ?? ProgramOptions.FromXml(); Task[] runners = new Task[threadCount]; for (int i = 0; i < threadCount; ++i) { runners[i] = Task.Factory.StartNew(() => _run()); } Task.WaitAll(runners); return(_stats); }