Example #1
0
        /// <summary>
        /// Decorates the remapping function to record statistics if enabled
        /// </summary>
        /// <typeparam name="T1"></typeparam>
        /// <typeparam name="T2"></typeparam>
        /// <typeparam name="TR"></typeparam>
        /// <param name="remappingFunction"></param>
        /// <param name="shouldRecordMiss"></param>
        /// <param name="shouldRecordLoad"></param>
        /// <returns></returns>
        protected Func <T1, T2, TR> StatsAware <T1, T2, TR>(Func <T1, T2, TR> remappingFunction, bool shouldRecordMiss, bool shouldRecordLoad)
        {
            if (!IsRecordingStats)
            {
                return(remappingFunction);
            }

            return((t, u) =>
            {
                TR result;

                if ((u == null) && shouldRecordMiss)
                {
                    StatsCounter.RecordMisses(1);
                }

                long startTime = StatsTicker.Ticks();

                try
                {
                    result = remappingFunction(t, u);
                }
                catch (Exception e)
                {
                    StatsCounter.RecordLoadFailure(StatsTicker.Ticks() - startTime);
                    throw;
                }

                long loadTime = StatsTicker.Ticks() - startTime;

                if (shouldRecordLoad)
                {
                    if (result == null)
                    {
                        StatsCounter.RecordLoadFailure(loadTime);
                    }
                    else
                    {
                        StatsCounter.RecordLoadSuccess(loadTime);
                    }
                }

                return result;
            });
        }
Example #2
0
        /// <summary>
        /// Decorates the remapping function to record statistics if enabled.
        /// </summary>
        /// <param name="mappingFunction"></param>
        /// <param name="shouldRecordLoad"></param>
        /// <returns></returns>
        protected virtual Func <K, V> StatsAware(Func <K, V> mappingFunction, bool shouldRecordLoad)
        {
            if (!IsRecordingStats)
            {
                return(mappingFunction);
            }

            return((k) =>
            {
                V value;

                StatsCounter.RecordMisses(1);
                long startTime = StatsTicker.Ticks();
                try
                {
                    value = mappingFunction(k);
                }
                catch (Exception e)
                {
                    StatsCounter.RecordLoadFailure(StatsTicker.Ticks() - startTime);
                    throw;
                }

                long loadTime = StatsTicker.Ticks() - startTime;

                if (shouldRecordLoad)
                {
                    if (value == null)
                    {
                        StatsCounter.RecordLoadFailure(loadTime);
                    }
                    else
                    {
                        StatsCounter.RecordLoadSuccess(loadTime);
                    }
                }

                return value;
            });
        }