/** <inheritDoc /> */ public override string ToString() { var sb = new StringBuilder(GetType().Name).Append('['); var first = true; var props = BenchmarkUtils.GetProperties(this); foreach (var prop in props) { if (first) { first = false; } else { sb.Append(", "); } sb.Append(prop.Name).Append('=').Append(prop.GetValue(this, null)); } sb.Append(']'); return(sb.ToString()); }
/// <summary> /// Consumes passed argument. /// </summary> /// <param name="name">Argument name.</param> /// <param name="val">Value.</param> /// <returns>True if argument was consumed.</returns> public void Configure(string name, string val) { var prop = BenchmarkUtils.GetProperty(this, name); if (prop != null) { BenchmarkUtils.SetProperty(this, prop, val); } }
/// <summary> /// Entry point. /// </summary> /// <param name="args">Arguments.</param> // ReSharper disable once RedundantAssignment public static void Main(string[] args) { args = new[] { //typeof(GetAllBenchmark).FullName, typeof(GetAllBinaryBenchmark).FullName, //typeof(ThinClientGetAllBenchmark).FullName, //typeof(ThinClientGetAllBinaryBenchmark).FullName, "-ConfigPath", Directory.GetCurrentDirectory() + @"\..\..\Config\benchmark.xml", "-Threads", "1", "-Warmup", "0", "-Duration", "60", "-BatchSize", "1" }; var gcSrv = System.Runtime.GCSettings.IsServerGC; Console.WriteLine("GC Server: " + gcSrv); if (!gcSrv) { Console.WriteLine("WARNING! GC server mode is disabled. This could yield in bad preformance."); } Console.WriteLine("DotNet benchmark process started: " + Process.GetCurrentProcess().Id); var argsStr = new StringBuilder(); foreach (var arg in args) { argsStr.Append(arg + " "); } if (args.Length < 1) { throw new Exception("Not enough arguments: " + argsStr); } Console.WriteLine("Arguments: " + argsStr); var benchmarkType = Type.GetType(args[0]); if (benchmarkType == null) { throw new InvalidOperationException("Could not find benchmark type: " + args[0]); } var benchmark = (BenchmarkBase)Activator.CreateInstance(benchmarkType); for (var i = 1; i < args.Length; i++) { var arg = args[i]; if (arg.StartsWith("-")) { arg = arg.Substring(1); } else { continue; } var prop = BenchmarkUtils.GetProperty(benchmark, arg); if (prop != null) { benchmark.Configure(prop.Name, prop.PropertyType == typeof(bool) ? bool.TrueString : args[++i]); } } benchmark.Run(); #if (DEBUG) Console.ReadLine(); #endif }
/// <summary> /// Task routine. /// </summary> public void Run() { try { _benchmark.OnThreadReady(); _benchmark.PrintDebug("Worker thread ready."); while (!_benchmark._start) { Thread.Sleep(10); } _benchmark.PrintDebug("Worker thread started benchmark execution."); var warmupIteration = true; long maxDur = 0; long maxOps = _benchmark.Operations; while (!_benchmark._stop) { if (warmupIteration && !_benchmark._warmup) { warmupIteration = false; _benchmark.OnWarmupFinished(); _state.StopWarmup(); _benchmark._barrier.SignalAndWait(); } if (!warmupIteration) { if (maxOps > 0 && Interlocked.Read(ref _benchmark._curOps) > maxOps) { break; } } var desc = _descs.Length == 1 ? _descs[0] : _descs[BenchmarkUtils.GetRandomInt(_descs.Length)]; var res = true; _benchmark.OnBatchStarted(_state); _watch.Start(); try { for (var i = 0; i < _benchmark.BatchSize; i++) { desc.Operation(_state); _state.IncrementCounter(); } if (!warmupIteration) { Interlocked.Add(ref _benchmark._curOps, _benchmark.BatchSize); } } catch (Exception e) { Console.WriteLine("Exception: " + e); res = false; if (_benchmark.MaxErrors > 0 && Interlocked.Increment(ref _benchmark._errorCount) > _benchmark.MaxErrors) { lock (_benchmark) { Console.WriteLine("Benchmark is stopped due to too much errors: " + _benchmark.MaxErrors); Environment.Exit(-1); } } } finally { _watch.Stop(); var curDur = _watch.ElapsedTicks; if (res) { res = _benchmark.OnBatchFinished(_state, curDur); } _state.Reset(); if (curDur > maxDur) { maxDur = curDur; _benchmark.PrintDebug("The longest execution [warmup=" + warmupIteration + ", dur(nsec)=" + maxDur * 1000000000 / Stopwatch.Frequency + ']'); } _watch.Reset(); if (!warmupIteration && res) { _results[desc.Name].Add(curDur); } } } } finally { _benchmark.PrintDebug("Worker thread stopped."); _benchmark.OnThreadFinished(); } }
/// <summary> /// Entry point. /// </summary> /// <param name="args">Arguments.</param> // ReSharper disable once RedundantAssignment public static void Main(string[] args) { #if (DEBUG) throw new Exception("Don't run benchmarks in Debug mode"); #endif #pragma warning disable 162 // ReSharper disable HeuristicUnreachableCode args = new[] { //typeof(GetAllBenchmark).FullName, typeof(PutWithPlatformCacheBenchmark).FullName, //typeof(ThinClientGetAllBenchmark).FullName, //typeof(ThinClientGetAllBinaryBenchmark).FullName, "-ConfigPath", GetConfigPath(), "-Threads", "1", "-Warmup", "5", "-Duration", "30", "-BatchSize", "1" }; var gcSrv = System.Runtime.GCSettings.IsServerGC; Console.WriteLine("GC Server: " + gcSrv); if (!gcSrv) { Console.WriteLine("WARNING! GC server mode is disabled. This could yield in bad performance."); } Console.WriteLine("DotNet benchmark process started: " + Process.GetCurrentProcess().Id); var argsStr = new StringBuilder(); foreach (var arg in args) { argsStr.Append(arg + " "); } if (args.Length < 1) { throw new Exception("Not enough arguments: " + argsStr); } Console.WriteLine("Arguments: " + argsStr); var benchmarkType = Type.GetType(args[0]); if (benchmarkType == null) { throw new InvalidOperationException("Could not find benchmark type: " + args[0]); } var benchmark = (BenchmarkBase)Activator.CreateInstance(benchmarkType); for (var i = 1; i < args.Length; i++) { var arg = args[i]; if (arg.StartsWith("-", StringComparison.Ordinal)) { arg = arg.Substring(1); } else { continue; } var prop = BenchmarkUtils.GetProperty(benchmark, arg); if (prop != null) { benchmark.Configure(prop.Name, prop.PropertyType == typeof(bool) ? bool.TrueString : args[++i]); } } benchmark.Run(); #pragma warning restore 162 }