public override MetricCollector Create(RunMode runMode, WarmupData warmup, IBenchmarkSetting setting)
        {
            var timingSetting = setting as TimingBenchmarkSetting;

            Contract.Assert(timingSetting != null);

            return(new TimingCollector(timingSetting.TimingMetricName));
        }
Exemple #2
0
        public override IEnumerable <MetricCollector> Create(RunMode runMode, WarmupData warmup, IBenchmarkSetting setting)
        {
            Contract.Assert(setting != null);
            Contract.Assert(setting is CreateCounterBenchmarkSetting);
            var createCounter = setting as CreateCounterBenchmarkSetting;

            // ReSharper disable once PossibleNullReferenceException
            // resolved with Code Contracts
            return(new[] { new CounterMetricCollector(createCounter.BenchmarkSetting.CounterName, createCounter.Counter) });
        }
        public override MetricCollector Create(RunMode runMode, WarmupData warmup,
                                               IBenchmarkSetting setting)
        {
            Contract.Assert(setting != null);
            Contract.Assert(setting is GcBenchmarkSetting);
            var gcSetting = setting as GcBenchmarkSetting;

            // ReSharper disable once PossibleNullReferenceException
            // covered by Code Contracts
            if (gcSetting.Generation == GcGeneration.AllGc)
            {
                throw new InvalidOperationException($"{gcSetting.Generation} is not supported by this collector");
            }

            return(CreateInstanceInternal((int)gcSetting.Generation));
        }
        public override IEnumerable <MetricCollector> Create(RunMode runMode, WarmupData warmup,
                                                             IBenchmarkSetting setting)
        {
            Contract.Assert(setting != null);
            Contract.Assert(setting is GcBenchmarkSetting);
            var gcSetting = setting as GcBenchmarkSetting;

            // ReSharper disable once PossibleNullReferenceException
            // covered by Code Contracts
            if (gcSetting.Generation == GcGeneration.AllGc)
            {
                var collectors = new List <MetricCollector>(SystemInfo.MaxGcGeneration + 1);
                for (var i = 0; i <= SystemInfo.MaxGcGeneration; i++)
                {
                    collectors.Add(CreateInstanceInternal(i));
                }
                return(collectors);
            }

            return(new[] { CreateInstanceInternal((int)gcSetting.Generation) });
        }
 /// <summary>
 ///     Creates an instance for all applicable <see cref="MetricCollector" />s for this metric type.
 /// </summary>
 /// <param name="runMode">
 ///     The <see cref="RunMode" /> for this benchmark. Influences the type of
 ///     <see cref="MetricCollector" /> used in some instances.
 /// </param>
 /// <param name="warmup">Warmup data. Influences the type of <see cref="MetricCollector" /> used in some instances.</param>
 /// <param name="setting">An implementation-specific <see cref="IBenchmarkSetting" /></param>
 /// <returns>A new <see cref="MetricCollector" /> instance.</returns>
 public abstract MetricCollector Create(RunMode runMode, WarmupData warmup,
                                        IBenchmarkSetting setting);
        public override MetricCollector Create(RunMode runMode, WarmupData warmup, IBenchmarkSetting setting)
        {
            Contract.Assert(setting != null);
            Contract.Assert(setting is PerformanceCounterBenchmarkSetting);
            var counterBenchmarkSetting = setting as PerformanceCounterBenchmarkSetting;
            var name = counterBenchmarkSetting.PerformanceCounterMetric;

            // re-use the PerformanceCounter objects in our pool if possible
            if (_cache.Exists(name))
            {
                return(new PerformanceCounterValueCollector(name, name.UnitName ?? MetricNames.DefaultUnitName, _cache.Get(name), true));
            }

            // otherwise, warm up new ones
            var maxRetries     = 3;
            var currentRetries = 0;

            if (!PerformanceCounterCategory.CounterExists(name.CounterName, name.CategoryName))
            {
                throw new NBenchException($"Performance counter {name.ToHumanFriendlyString()} is not registered on this machine. Please create it first.");
            }

            // check to see that the instance we're interested in is registered
            if (!string.IsNullOrEmpty(name.InstanceName))
            {
                var categories = PerformanceCounterCategory.GetCategories().Where(x => x.CategoryType == PerformanceCounterCategoryType.MultiInstance).ToList();
#if DEBUG
                Console.WriteLine("---- DEBUG -----");
                Console.WriteLine("{0} multi-instance categories detected", categories.Count);
#endif
                var category  = categories.Single(x => x.CategoryName == name.CategoryName);
                var instances = category.GetInstanceNames();

                if (!instances.Contains(name.InstanceName))
                {
#if DEBUG
                    Console.WriteLine("---- DEBUG -----");
                    Console.WriteLine("Multi-instance? {0}", category.CategoryType);
                    foreach (var instance in instances)
                    {
                        Console.WriteLine(instance);
                    }
#endif
                    throw new NBenchException($"Performance counter {name.CategoryName}:{name.CounterName} exists, but we could not find an instance {name.InstanceName}.");
                }
            }

            var proxy = new PerformanceCounterProxy(() =>
            {
                var counter = new PerformanceCounter(name.CategoryName, name.CounterName,
                                                     name.InstanceName ?? string.Empty, true);

                return(counter);
            });
            while (!CanFindPerformanceCounter(name) && currentRetries <= maxRetries)
            {
                Thread.Sleep(TimeSpan.FromMilliseconds(1000 + 100 * (currentRetries ^ 2))); // little bit of exponential backoff
                if (proxy.CanWarmup)
                {
                    break;
                }
                currentRetries++;
            }

            if (!proxy.CanWarmup)
            {
                throw new NBenchException($"Performance counter {name.ToHumanFriendlyString()} is not registered on this machine. Please create it first.");
            }

            // cache this performance counter and pool it for re-use
            _cache.Put(name, proxy);
            return(new PerformanceCounterValueCollector(name, name.UnitName ?? MetricNames.DefaultUnitName, _cache.Get(name), true));
        }
Exemple #7
0
 public override IEnumerable <MetricCollector> Create(RunMode runMode, WarmupData warmup, IBenchmarkSetting setting)
 {
     //if (warmup.ElapsedTicks <= BenchmarkConstants.SamplingPrecisionTicks)
     return(new[] { new GcTotalMemoryCollector(MemoryMetricName) });
     //return new[] {new PerformanceCounterTotalMemoryCollector(MemoryMetricName)};
 }
 /// <summary>
 ///     Creates an instance for all applicable <see cref="MetricCollector" />s for this metric type.
 /// </summary>
 /// <param name="runMode">
 ///     The <see cref="RunMode" /> for this benchmark. Influences the type of
 ///     <see cref="MetricCollector" /> used in some instances.
 /// </param>
 /// <param name="warmup">Warmup data. Influences the type of <see cref="MetricCollector" /> used in some instances.</param>
 /// <param name="setting">An implementation-specific <see cref="IBenchmarkSetting" /></param>
 /// <returns>At least 1 new <see cref="MetricCollector"/> instance. Each instance will be uniquely named.</returns>
 public abstract IEnumerable <MetricCollector> Create(RunMode runMode, WarmupData warmup,
                                                      IBenchmarkSetting setting);