public WwwCallFactory(AbstractLogger logger, ICoroutineManager coroutineManager, IStopwatchFactory stopwatchFactory, IWwwFactory wwwFactory) { this.logger = logger; this.coroutineManager = coroutineManager; this.stopwatchFactory = stopwatchFactory; this.wwwFactory = wwwFactory; }
/// <summary> /// Creates a memoization cache factory for weak memoization caches that use an eviction strategy based on a function to rank cache entries based on metrics. /// </summary> /// <param name="ranker">The ranker function used to obtain the metric for each entry upon evicting entries from the cache.</param> /// <param name="maxCapacity">The maximum capacity of memoization caches returned by the factory.</param> /// <param name="ageThreshold">The threshold used to decide whether an entry has aged sufficiently in order to be considered for eviction. E.g. a value of 0.9 means that the youngest 10% of entries cannot get evicted.</param> /// <param name="descending">Indicates whether the ranker should evict entries with the highest or lowest score.</param> /// <param name="stopwatchFactory">The stopwatch factory used to create stopwatches that measure access times and function invocation times. If omitted, the default stopwatch is used.</param> /// <returns>Memoization cache factory for memoization caches that use a ranking-based cache eviction strategy.</returns> public EvictImpl(Func <IMemoizationCacheEntryMetrics, TMetric> ranker, int maxCapacity, double ageThreshold, IStopwatchFactory stopwatchFactory, bool descending) { _ranker = ranker; _maxCapacity = maxCapacity; _descending = descending; _ageThreshold = ageThreshold; _stopwatchFactory = stopwatchFactory ?? StopwatchFactory.Diagnostics; }
/// <summary> /// Creates a new stopwatch that gets started immediately before being returned to the called. /// </summary> /// <param name="factory">The factory used to create the stopwatch.</param> /// <returns>New stopwach instance.</returns> public static IStopwatch StartNew(this IStopwatchFactory factory) { if (factory == null) { throw new ArgumentNullException(nameof(factory)); } var sw = factory.Create(); sw.Start(); return(sw); }
/// <summary> /// Initializes a new instance of the <see cref="PumpProcessor" /> class. /// </summary> /// <param name="loggerFactory">The logger factory.</param> /// <param name="buffer">The buffer.</param> /// <param name="stopwatchFactory">The stopwatch factory.</param> /// <param name="delayCalculator">The delay calculator.</param> /// <param name="scheduleSettings">The schedule settings.</param> public PumpProcessor( [NotNull] ILoggerFactory loggerFactory, [NotNull] IBuffer buffer, [NotNull] IStopwatchFactory stopwatchFactory, [NotNull] IDelayCalculator delayCalculator, [NotNull] ScheduleSettings scheduleSettings) { this.buffer = buffer; this.stopwatchFactory = stopwatchFactory; this.delayCalculator = delayCalculator; this.scheduleSettings = scheduleSettings; this.logger = loggerFactory.CreateLogger <PumpProcessor>(); }
public FitnessChart(IStopwatchFactory stopwatchFactory) { this.stopwatchFactory = stopwatchFactory; this.PlotModel = new PlotModel(); this.categoryAxis = new CategoryAxis { TickStyle = TickStyle.None, Title = Wpf.Resources.FitnessChart_XAxisTitle }; this.PlotModel.Axes.Add(this.categoryAxis); this.PlotModel.Axes.Add(new LinearAxis() { Title = Wpf.Resources.FitnessChart_YAxisTitle }); this.columnSeries = new ColumnSeries { TrackerFormatString = "{" + nameof(GeneticEntity.Representation) + "} : {2}" }; this.PlotModel.Series.Add(this.columnSeries); }
public Cache(Func <T, R> function, Func <IMemoizationCacheEntryMetrics, TMetric> ranker, int maxCapacity, bool descending, double ageThreshold, bool cacheError, IStopwatchFactory stopwatchFactory) { _function = args => { var weakArgs = WeakReferenceExtensions.Create(args); var invokeDuration = default(TimeSpan); #if DEBUG Interlocked.Increment(ref _invocationCount); #endif Trim(); var res = default(IMetricsCacheEntry <WeakReference <T>, R>); try { var swInvokeStart = _stopwatch.ElapsedTicks; var value = function(args); invokeDuration = new TimeSpan(_stopwatch.ElapsedTicks - swInvokeStart); res = new MetricsValueCacheEntry <WeakReference <T>, R>(weakArgs, value); } catch (Exception ex) when(_cacheError) { res = new MetricsErrorCacheEntry <WeakReference <T>, R>(weakArgs, ex); } res.CreationTime = new TimeSpan(_stopwatch.ElapsedTicks); res.InvokeDuration = invokeDuration; _lock.EnterWriteLock(); try { _entries.Add(res); } finally { _lock.ExitWriteLock(); } return(res); }; _cache = new WeakCacheDictionary <T, IMetricsCacheEntry <WeakReference <T>, R> >(); _lock = new ReaderWriterLockSlim(LockRecursionPolicy.NoRecursion); _entries = new HashSet <IMetricsCacheEntry <WeakReference <T>, R> >(); _stopwatch = stopwatchFactory.StartNew(); // // Exclude newest items which have statically irrelevant data, so they get a chance to become relevant. // var candidates = _entries.OrderBy(e => _stopwatch.ElapsedTicks - e.CreationTime.Ticks).Take(Math.Max(1, (int)(maxCapacity * ageThreshold))); _ranker = descending ? candidates.OrderByDescending(e => ranker(e)) : candidates.OrderBy(e => ranker(e)); _maxCapacity = maxCapacity; _cacheError = cacheError; }
/// <summary> /// Creates a memoization cache factory for memoization caches that use an eviction strategy based on a function to rank cache entries based on metrics. /// Entries that meet the age threshold and have the highest score as computed by the ranker get evicted. /// </summary> /// <typeparam name="TMetric">Type of the metric to rank cache entries by.</typeparam> /// <param name="ranker">The ranker function used to obtain the metric for each entry upon evicting entries from the cache.</param> /// <param name="maxCapacity">The maximum capacity of memoization caches returned by the factory.</param> /// <param name="ageThreshold">The threshold used to decide whether an entry has aged sufficiently in order to be considered for eviction. E.g. a value of 0.9 means that the youngest 10% of entries cannot get evicted.</param> /// <param name="stopwatchFactory">The stopwatch factory used to create stopwatches that measure access times and function invocation times. If omitted, the default stopwatch is used.</param> /// <returns>Memoization cache factory for memoization caches that use a ranking-based cache eviction strategy.</returns> public static IMemoizationCacheFactory CreateEvictedByHighest <TMetric>(Func <IMemoizationCacheEntryMetrics, TMetric> ranker, int maxCapacity, double ageThreshold = 0.9, IStopwatchFactory stopwatchFactory = null) { if (ranker == null) { throw new ArgumentNullException(nameof(ranker)); } if (maxCapacity < 1) { throw new ArgumentOutOfRangeException(nameof(maxCapacity), "A cache should have at a capacity of at least one."); } if (ageThreshold is <= 0 or > 1) { throw new ArgumentOutOfRangeException(nameof(ageThreshold), "The age threshold should be a number between 0 (inclusive) and 1 (exclusive)."); } return(new EvictImpl <TMetric>(ranker, maxCapacity, ageThreshold, stopwatchFactory, descending: true)); }
public AccessTokenFactory(IStopwatchFactory stopwatchFactory) => stopwatch = stopwatchFactory.Start();
public CreateAccessToken() { stopwatchFactory = Substitute.For <IStopwatchFactory>(); stopwatch = Substitute.For <IStopwatch>(); stopwatchFactory.Start().Returns(stopwatch); }
/// <summary> /// Creates a memoization cache factory for memoization caches that use an eviction strategy based on a function to rank cache entries based on metrics. /// Entries that meet the age threshold and have the highest score as computed by the ranker get evicted. /// </summary> /// <typeparam name="TMetric">Type of the metric to rank cache entries by.</typeparam> /// <param name="ranker">The ranker function used to obtain the metric for each entry upon evicting entries from the cache.</param> /// <param name="maxCapacity">The maximum capacity of memoization caches returned by the factory.</param> /// <param name="ageThreshold">The threshold used to decide whether an entry has aged sufficiently in order to be considered for eviction. E.g. a value of 0.9 means that the youngest 10% of entries cannot get evicted.</param> /// <param name="stopwatchFactory">The stopwatch factory used to create stopwatches that measure access times and function invocation times. If omitted, the default stopwatch is used.</param> /// <returns>Memoization cache factory for memoization caches that use a ranking-based cache eviction strategy.</returns> public static IMemoizationCacheFactory CreateEvictedByHighest <TMetric>(Func <IMemoizationCacheEntryMetrics, TMetric> ranker, int maxCapacity, double ageThreshold = 0.9, IStopwatchFactory stopwatchFactory = null) { if (ranker == null) { throw new ArgumentNullException(nameof(ranker)); } if (maxCapacity < 1) { throw new ArgumentOutOfRangeException(nameof(maxCapacity), "A cache should have at a capacity of at least one."); } if (ageThreshold is <= 0 or > 1) { throw new ArgumentOutOfRangeException(nameof(ageThreshold), "The age threshold should be a number between 0 (inclusive) and 1 (exclusive)."); } // // NB: For now, we use the Synchronized sledgehammer to achieve the concurrency safety. There may be // cheaper ways by creating a custom implementation but we'll postpone this until later. // return(MemoizationCacheFactory.CreateEvictedByHighest(ranker, maxCapacity, ageThreshold, stopwatchFactory).Synchronized()); }
public Cache(Func <T, R> function, Func <IMemoizationCacheEntryMetrics, TMetric> ranker, int maxCapacity, bool descending, double ageThreshold, IEqualityComparer <T> comparer, bool cacheError, IStopwatchFactory stopwatchFactory) { _function = args => { var invokeDuration = default(TimeSpan); #if DEBUG _invocationCount++; #endif Trim(); var res = default(IMetricsCacheEntry <T, R>); try { var swInvokeStart = _stopwatch.ElapsedTicks; var value = function(args); invokeDuration = new TimeSpan(_stopwatch.ElapsedTicks - swInvokeStart); res = new MetricsValueCacheEntry <T, R>(args, value); } catch (Exception ex) when(_cacheError) { res = new MetricsErrorCacheEntry <T, R>(args, ex); } res.CreationTime = new TimeSpan(_stopwatch.ElapsedTicks); res.InvokeDuration = invokeDuration; return(res); }; _cache = new CacheDictionary <T, IMetricsCacheEntry <T, R> >(comparer); _stopwatch = stopwatchFactory.StartNew(); // // Exclude newest items which have statically irrelevant data, so they get a chance to become relevant. // var candidates = _cache.Values.OrderBy(e => _stopwatch.ElapsedTicks - e.CreationTime.Ticks).Take(Math.Max(1, (int)(maxCapacity * ageThreshold))); _ranker = descending ? candidates.OrderByDescending(e => ranker(e)) : candidates.OrderBy(e => ranker(e)); _maxCapacity = maxCapacity; _cacheError = cacheError; }