/// <summary> /// Monitor average response time using a counter having the specified instance name /// </summary> /// <param name="counterInstanceName">Performance counter instance name</param> /// <param name="starttime">the start of the interval for a single sample or response time being averaged</param> /// <param name="lockType">some type used internally for locking</param> /// <param name="isMonitoringActive">caller can forward external monitoring control</param> /// <returns>Current time used as end time for previous sample. Can be used as starttime for next sample</returns> public static DateTime MonitorAverageResponse(string counterInstanceName, DateTime starttime, System.Type lockType, bool isMonitoringActive) { try { // access counter in hashlist if it is there, else null CounterBase counter = (CounterBase)PerformanceCounterFacade.Counters[counterInstanceName]; // allocate counter if needed AverageCounter rtCounter = (AverageCounter)PerformanceCounterFacade.SetCounter(lockType, ref counter, "Average", counterInstanceName, isMonitoringActive); if (rtCounter != null) { // calculate milliseconds TimeSpan ts = DateTime.Now - starttime; long ms = (long)ts.TotalMilliseconds; // adjust counters rtCounter.AddSampleValue(ms); rtCounter.IncrementSampleCount();; } // persist static, threadsafe counter (null if perf monitoring off) for reuse by validator instances PerformanceCounterFacade.Counters[counterInstanceName] = rtCounter; } catch (InvalidOperationException ex) { } return(DateTime.Now); }
public CounterBase[] FindAll(GetAllCountersCriterion criterion) { IMongoCollection <CounterBase> counterCollection = Database.GetCollection <CounterBase>("Counter"); var filter = new BsonDocument(); if (counterCollection.Find <CounterBase>(filter).Count() == 0) { CounterBase[] counters = new CounterBase[] { new SingleCounter() { Id = 0, Name = "GDC Points", Value = 0, Type = 0 }, new GenericCounter() { Id = 1, Name = "Профессионализм", Type = 1, Value = 0, SubValue = 10, InflationIncreaseSteps = 1000 }, new GenericCounter() { Id = 2, Name = "Зарплата", Type = 1, Value = 0, UseInAutoSteps = true, InflationIncreaseSteps = 2000 }, new DelayedCounter() { Id = 3, Name = "Квартальный план", Type = 2, Value = 5000000, MiningTimeSeconds = 240 }, }; counterCollection.InsertMany(counters); } return(counterCollection.Find(filter).SortBy(_ => _.Id).ToList().ToArray()); }
/// <summary> /// Monitor an average value with a counter having the specified instance name /// </summary> /// <param name="counterInstanceName">Performance counter instance name</param> /// <param name="count">the value for a single sample being averaged</param> /// <param name="lockType">some type used internally for locking</param> /// <param name="isMonitoringActive">caller can forward external monitoring control</param> public static void MonitorAverageCount(string counterInstanceName, long count, System.Type lockType, bool isMonitoringActive) { try { // access counter in hashlist if it is there, else null CounterBase counter = (CounterBase)PerformanceCounterFacade.Counters[counterInstanceName]; // allocate counter if needed AverageCounter avgCounter = (AverageCounter)PerformanceCounterFacade.SetCounter(lockType, ref counter, "Average", counterInstanceName, isMonitoringActive); if (avgCounter != null) { // adjust counters avgCounter.AddSampleValue(count); avgCounter.IncrementSampleCount();; } // persist static, threadsafe counter (null if perf monitoring off) for reuse by validator instances PerformanceCounterFacade.Counters[counterInstanceName] = avgCounter; } catch (Exception ex) { NameValueCollection nvc = new NameValueCollection(2); nvc.Add("Method", "MonitorAverageCount"); nvc.Add("Probable cause", "PerfCounters not registered or wrong category in config file"); new LoggableException(ex, nvc); } }
static void TestCounter(CounterBase c) { for (int i = 0; i < 100000; i++) { c.Increment(); c.Decrement(); } }
private CounterBase[] PrepareCountersToPersist(IGame game) { var counters = new CounterBase[game.GameCash.Counters.Length]; foreach (var sourceCounter in game.GameCash.Counters) { CounterBase destinationCouner = null; var sourcenGenericCounter = sourceCounter as GenericCounter; if (sourcenGenericCounter != null) { var destinationGenericCounter = new Common.Contract.Counters.GenericCounter { BonusPercentage = sourcenGenericCounter.BonusPercentage, Bonus = sourcenGenericCounter.Bonus, Inflation = sourcenGenericCounter.Inflation, CurrentSteps = sourcenGenericCounter.CurrentSteps, SubValue = sourcenGenericCounter.SubValue }; destinationCouner = destinationGenericCounter; destinationCouner.Type = 1; } var sourceSingleCounter = sourceCounter as SingleCounter; if (sourceSingleCounter != null) { destinationCouner = new Common.Contract.Counters.SingleCounter(); destinationCouner.Type = 0; } var sourceDelayedCounter = sourceCounter as DelayedCounter; if (sourceDelayedCounter != null) { var destinationDelayedCounter = new Common.Contract.Counters.DelayedCounter { SecondsRemaining = sourceDelayedCounter.SecondsRemaining, MiningTimeSeconds = sourceDelayedCounter.SecondsToAchieve, UnlockValue = sourceDelayedCounter.UnlockValue }; destinationCouner = destinationDelayedCounter; destinationCouner.Type = 2; } if (destinationCouner != null) { destinationCouner.Name = sourceCounter.Name; destinationCouner.Id = sourceCounter.Id; destinationCouner.Value = sourceCounter.Value; } if (destinationCouner != null) { counters[destinationCouner.Id] = destinationCouner; } } return(counters); }
protected override GameCash GetFundsCounters() { var counters = new CounterBase[] { new SingleCounter { Id = (int)UssrCounterEnum.Communism, Name = "Communism" } , new GenericCounter { Id = (int)UssrCounterEnum.Production, Name = "Production", SubValue = 10, StepsToIncreaseInflation = 1000 } , new GenericCounter { Id = (int)UssrCounterEnum.Tax, Name = "Tax", IsUsedInAutoStep = true, StepsToIncreaseInflation = 2000 }, new DelayedCounter { Id = (int)UssrCounterEnum.FiveYearPlan, Name = "Five Year Plan", UnlockValue = 10000, SecondsToAchieve = 14400, SecondsRemaining = 0, IsMining = false, SubValue = 5000000 } }; return(new GameCash { Counters = counters, RootCounter = counters[(int)UssrCounterEnum.Communism] }); }
public int Work(IEnumerable <string> assemblyNames) { int maxResult = 0; int goodTypeCount = 0; int badTypeCount = 0; int goodMethodCount = 0; int badMethodCount = 0; DateTime startTime = DateTime.Now; visitor.Start(); foreach (string assemblyName in assemblyNames) { int thisResult = Work(assemblyName); if ((thisResult == 0) && visitor is CounterBase) { CounterBase counterVisitor = visitor as CounterBase; goodTypeCount += counterVisitor.TypeCount; goodMethodCount += counterVisitor.MethodCount; badTypeCount += counterVisitor.UninstantiableTypeCount; badMethodCount += counterVisitor.UninstantiableMethodCount; } maxResult = Math.Max(thisResult, maxResult); } visitor.Finish(); // Produce summary if visitor's final output is not sufficient. if ((badAssemblyCount > 0) || (nonAssemblyCount > 0) || (goodAssemblyCount > 1)) { DateTime stopTime = DateTime.Now; TimeSpan totalTime = stopTime - startTime; Console.WriteLine(); Console.WriteLine($"Overall: {goodAssemblyCount} assemblies {goodTypeCount} types {goodMethodCount} methods in {totalTime.TotalMilliseconds:F2}ms"); Console.WriteLine($" {nonAssemblyCount} non-assemblies {badAssemblyCount} skipped assemblies {badTypeCount} skipped types {badMethodCount} skipped methods"); } return(maxResult); }
/// <summary> /// Return a SimpleCounter for use by the caller /// </summary> /// <param name="counterInstanceName">Performance counter instance name</param> /// <param name="count">the value for a single sample being averaged</param> /// <param name="lockType">some type used internally for locking</param> /// <param name="isMonitoringActive">caller can forward external monitoring control</param> public static SimpleCounter GetSimpleCounter(string counterInstanceName, System.Type lockType, bool isMonitoringActive) { SimpleCounter simpleCounter = null; try { // access counter in hashlist if it is there, else null CounterBase counter = (CounterBase)PerformanceCounterFacade.Counters[counterInstanceName]; // allocate counter if needed simpleCounter = (SimpleCounter)PerformanceCounterFacade.SetCounter(lockType, ref counter, "SimpleCounter", counterInstanceName, isMonitoringActive); // persist static, threadsafe counter (null if perf monitoring off) for reuse by validator instances PerformanceCounterFacade.Counters[counterInstanceName] = simpleCounter; } catch (Exception ex) { NameValueCollection nvc = new NameValueCollection(2); nvc.Add("Method", "MonitorAverageCount"); nvc.Add("Probable cause", "PerfCounters not registered or wrong category in config file"); new LoggableException(ex, nvc); } return(simpleCounter); }
private bool IsFundsDriverAvailableForBuy(CounterBase rootCounter, Item item) { return(item.UnlockValue <= rootCounter.Value); }
/// <summary> /// Synchonize creation and destruction of performance counter references /// </summary> /// <param name="pc">reference to perfcounter variable from caller</param> /// <param name="counterType">underlying perf counter type for which new instance might be created</param> /// <param name="instancename">instance name to be assigned for new counter instance</param> /// <param name="si">sampling interval for new instance</param> /// <param name="estsamplesize">estimated initial sample size</param> /// <returns>true if perf counter ref is non null and monitoring should be done</returns> /// <remarks>This was done in order to have monitoring respond to config setting and dynamically allocate /// and deallocate performance counters according to the setting. It also keeps a lot of /// potentially messy code in one place</remarks> public static CounterBase SetCounter(Type senderType, ref CounterBase pc, string counterType, string instancename, bool isMonitoringOn) { // if they should continue not monitoring then exit if (!isMonitoringOn && pc == null) { return(null); } // if they should continue monitoring then exit if (isMonitoringOn && pc != null) { return(pc); } // either they are monitoring and should stop or they aren't and they should start CounterBase newpc = null; if (isMonitoringOn) { if (pc == null) { switch (counterType) { case "SimpleCounter": newpc = new SimpleCounter(instancename); break; case "Average": newpc = new AverageCounter(instancename); break; case "Percent": newpc = new PercentCounter(instancename); break; case "AverageResponse": newpc = new AverageResponseTimeCounter(instancename); break; case "Rate": newpc = new RateCounter(instancename); break; default: break; } } } // since perf counters are static shared objects we must lock lock (senderType) { // change their monitoring state from on to off or from off to on if (newpc == null) { pc.theCounter.RemoveInstance(); } pc = newpc; } return(newpc); }