示例#1
0
        protected static void Evict_Lowest_LastAccessTime(Func <Func <IMemoizationCacheEntryMetrics, TimeSpan>, int, double, IStopwatchFactory, IMemoizationCacheFactory> createEvictedByLowest)
        {
            var c = new VirtualTimeClock();

            var cache = createEvictedByLowest(m => m.LastAccessTime, 4, 1.0, StopwatchFactory.FromClock(c));

            var n = 0;
            var f = cache.Create <int, int>(x => { n++; c.Now += x * 1000; return(x + 1); }, MemoizationOptions.None, EqualityComparer <int> .Default);

            Assert.AreEqual(3, f.GetOrAdd(2)); // [2]
            Assert.AreEqual(2000, c.Now);

            Assert.AreEqual(2, f.GetOrAdd(1)); // [1, 2]
            Assert.AreEqual(3000, c.Now);

            Assert.AreEqual(4, f.GetOrAdd(3)); // [3, 1, 2]
            Assert.AreEqual(6000, c.Now);

            Assert.AreEqual(2, f.GetOrAdd(1)); // [1, 3, 2]
            Assert.AreEqual(6000, c.Now);

            Assert.AreEqual(5, f.GetOrAdd(4)); // [4, 1, 3, 2]
            Assert.AreEqual(10000, c.Now);

            Assert.AreEqual(6, f.GetOrAdd(5)); // [5, 4, 1, 3]
            Assert.AreEqual(15000, c.Now);

            Assert.AreEqual(2, f.GetOrAdd(1)); // [1, 5, 4, 3]
            Assert.AreEqual(15000, c.Now);

            Assert.AreEqual(3, f.GetOrAdd(2)); // [2, 1, 5, 4]
            Assert.AreEqual(17000, c.Now);
        }
 public SQLitePlatformWinRT(string directoryPath)
 {
     SQLiteApi         = new SQLiteApiWinRT(directoryPath);
     VolatileService   = new VolatileService();
     StopwatchFactory  = new StopwatchFactory();
     ReflectionService = new ReflectionService();
 }
示例#3
0
 public SQLitePlatformAndroid()
 {
     SQLiteApi         = new SQLiteApiAndroid();
     StopwatchFactory  = new StopwatchFactory();
     ReflectionService = new ReflectionService();
     VolatileService   = new VolatileService();
 }
 public SQLitePlatformGeneric()
 {
     SQLiteApi         = new SQLiteApiGeneric();
     StopwatchFactory  = new StopwatchFactory();
     ReflectionService = new ReflectionService();
     VolatileService   = new VolatileService();
 }
示例#5
0
 public SQLitePlatformOSX()
 {
     SQLiteApi         = new SQLiteApiOSX();
     StopwatchFactory  = new StopwatchFactory();
     ReflectionService = new ReflectionService();
     //TODO: The original VolatileService is slightly different on OSX. There's no indication as to whether this was done for compilation reasons, or OSX just behaves differently. So, in future we may need to dependency inject the VolatileService for OSX. If that happens, the VolatileService is available in the original SQLite PCL repo.
     VolatileService = new VolatileService();
 }
示例#6
0
        public void Stopwatch_Clock()
        {
            var c = new VirtualTimeClock();

            var sw = StopwatchFactory.FromClock(c).Create();

            Assert.IsFalse(sw.IsRunning);

            sw.Start();
            Assert.IsTrue(sw.IsRunning);
            Assert.AreEqual(0, sw.ElapsedTicks);

            c.Now += 1000;
            Assert.IsTrue(sw.IsRunning);
            Assert.AreEqual(1000, sw.ElapsedTicks);

            sw.Stop();

            c.Now += 2000;
            Assert.IsFalse(sw.IsRunning);
            Assert.AreEqual(1000, sw.ElapsedTicks);

            sw.Start();
            Assert.IsTrue(sw.IsRunning);
            Assert.AreEqual(1000, sw.ElapsedTicks);

            c.Now += 3000;
            Assert.IsTrue(sw.IsRunning);
            Assert.AreEqual(4000, sw.ElapsedTicks);

            sw.Reset();
            Assert.IsFalse(sw.IsRunning);
            Assert.AreEqual(0, sw.ElapsedTicks);

            sw.Start();
            Assert.IsTrue(sw.IsRunning);
            Assert.AreEqual(0, sw.ElapsedTicks);

            c.Now += 5000;
            Assert.IsTrue(sw.IsRunning);
            Assert.AreEqual(5000, sw.ElapsedTicks);

            sw.Restart();
            Assert.IsTrue(sw.IsRunning);
            Assert.AreEqual(0, sw.ElapsedTicks);

            c.Now += 1500;
            Assert.IsTrue(sw.IsRunning);
            Assert.AreEqual(1500, sw.ElapsedTicks);

            Assert.AreEqual(1500, sw.Elapsed.Ticks);
            Assert.AreEqual(0, sw.ElapsedMilliseconds); // not a double!

            c.Now += TimeSpan.FromMilliseconds(1).Ticks;
            Assert.AreEqual(1, sw.ElapsedMilliseconds); // not a double!
        }
示例#7
0
        public void StopwatchFactoryExtensions_StartNew()
        {
            var c = new VirtualTimeClock();

            var sw = StopwatchFactory.FromClock(c).StartNew();

            Assert.IsTrue(sw.IsRunning);
            Assert.AreEqual(0, sw.ElapsedTicks);

            c.Now += 1000;
            Assert.IsTrue(sw.IsRunning);
            Assert.AreEqual(1000, sw.ElapsedTicks);
        }
示例#8
0
        public void Send(Action actionToTime, string statName, double sampleRate = 1.0, string[] tags = null)
        {
            var stopwatch = StopwatchFactory.Get();

            try
            {
                stopwatch.Start();
                actionToTime();
            }
            finally
            {
                stopwatch.Stop();
                Send <Timing, int>(statName, stopwatch.ElapsedMilliseconds(), sampleRate, tags);
            }
        }
示例#9
0
        public async Task SendAsync(Action actionToTime, string statName, double sampleRate = 1.0, string[] tags = null)
        {
            var stopwatch = StopwatchFactory.Get();

            try
            {
                stopwatch.Start();
                actionToTime();
            }
            finally
            {
                stopwatch.Stop();
                await SendAsync <Timing, int>(statName, stopwatch.ElapsedMilliseconds(), sampleRate, tags).ConfigureAwait(false);
            }
        }
        public StopwatchMock(TimeInterval proactiveAuthTokenRefreshInterval)
        {
            this.proactiveAuthTokenRefreshInterval = proactiveAuthTokenRefreshInterval;
            stopwatches = new List <IStopwatch> {
                Substitute.For <IStopwatch>()
            };

            StopwatchFactory = Substitute.For <IStopwatchFactory>();
            StopwatchFactory.Start().Returns(_ =>
            {
                var lastStopwatch = stopwatches.Last();
                stopwatches.Add(Substitute.For <IStopwatch>());
                return(lastStopwatch);
            });
        }
示例#11
0
        public void Send(Action actionToTime, string statName)
        {
            var stopwatch = StopwatchFactory.Get();

            try
            {
                stopwatch.Start();
                actionToTime();
            }
            finally
            {
                stopwatch.Stop();
                Send <Timing>(statName, stopwatch.ElapsedMilliseconds());
            }
        }
示例#12
0
        /// <summary>
        /// Creates this instance.
        /// </summary>
        /// <param name="queueConfiguration">The queue configuration.</param>
        /// <param name="loggerFactory">The logger factory.</param>
        /// <returns>
        /// The <see cref="IQueue" />
        /// </returns>
        public static IQueue Create(
            QueueConfiguration queueConfiguration,
            ILoggerFactory loggerFactory = null)
        {
            var logger = loggerFactory ?? new LoggerFactory();

            var queueEndpoint = new QueueEndpoint
            {
                ConnectionString = queueConfiguration.QueueConnectionString,
                QueueName        = queueConfiguration.QueueName
            };

            var serializerSettings = new SerializerSettings
            {
                SerializerType = queueConfiguration.SerializerType
            };

            var batchSettings = new BatchSettings
            {
                MaxQueueMessagesPerSchedule = queueConfiguration.MaxQueueMessagesPerSchedule,
                MaxMessagesPerQueueMessage  = queueConfiguration.MaxMessagesPerQueueMessage
            };

            var scheduleSettings = new ScheduleSettings
            {
                ThrottleTime = queueConfiguration.ThrottleTime
            };

            var queueWrapper           = new QueueWrapper(queueEndpoint);
            var messageSerializer      = new MessageSerializer(serializerSettings);
            var queueMessageSerializer = new QueueMessageSerializer(batchSettings, messageSerializer);

            var buffer = new Buffer(logger, queueWrapper, queueMessageSerializer);

            var stopwatchFactory = new StopwatchFactory();
            var delayCalculator  = new DelayCalculator();
            var pumpProcessor    = new PumpProcessor(
                logger,
                buffer,
                stopwatchFactory,
                delayCalculator,
                scheduleSettings);

            var queuePump = new QueuePump(buffer, pumpProcessor);

            return(new Queue(queuePump));
        }
示例#13
0
        public void Send(Action actionToTime, string statName, double sampleRate = 1)
        {
            var stopwatch = StopwatchFactory.Get();

            try
            {
                stopwatch.Start();
                actionToTime();
            }
            finally
            {
                stopwatch.Stop();
                if (RandomGenerator.ShouldSend(sampleRate))
                {
                    Send <Timing>(statName, stopwatch.ElapsedMilliseconds());
                }
            }
        }
示例#14
0
        public void Add(Action actionToTime, string statName, double sampleRate = 1, IDictionary <String, String> dimensions = null)
        {
            var stopwatch = StopwatchFactory.Get();

            try
            {
                stopwatch.Start();
                actionToTime();
            }
            finally
            {
                stopwatch.Stop();
                if (RandomGenerator.ShouldSend(sampleRate))
                {
                    Add <Timing>(statName, stopwatch.ElapsedMilliseconds(), dimensions);
                }
            }
        }
示例#15
0
        private void HandleTiming(Action actionToTime, string statName, double sampleRate, Action <string, int> actionToStore)
        {
            var stopwatch = StopwatchFactory.Get();

            try
            {
                stopwatch.Start();
                actionToTime();
            }
            finally
            {
                stopwatch.Stop();
                if (RandomGenerator.ShouldSend(sampleRate))
                {
                    actionToStore(statName, stopwatch.ElapsedMilliseconds);
                }
            }
        }
示例#16
0
        public async Task SendAsync(Func <Task> actionToTime, ReadOnlyMemory <char> statName, double sampleRate = 1)
        {
            var stopwatch = StopwatchFactory.Get();

            try
            {
                stopwatch.Start();
                await actionToTime().ConfigureAwait(false);
            }
            finally
            {
                stopwatch.Stop();

                if (RandomGenerator.ShouldSend(sampleRate))
                {
                    await SendAsync <Timing>(statName.Span, stopwatch.ElapsedMilliseconds).ConfigureAwait(false);
                }
            }
        }
示例#17
0
 public void StopwatchFactory_ArgumentChecking()
 {
     Assert.ThrowsException <ArgumentNullException>(() => StopwatchFactory.FromClock(clock: null));
 }
        public void WeakMemoizationCacheFactory_Evict_Lowest_LastAccessTime()
        {
            var c = new VirtualTimeClock();

            var cache = WeakMemoizationCacheFactory.CreateEvictedByLowest(m => m.LastAccessTime, 4, 1.0, StopwatchFactory.FromClock(c));

            var n = 0;
            var f = cache.Create <string, string>(x => { n++; c.Now += int.Parse(x) * 1000; return(x + "!"); }, MemoizationOptions.None);

            Assert.AreEqual("2!", f.GetOrAdd("2")); // [2]
            Assert.AreEqual(2000, c.Now);

            Assert.AreEqual("1!", f.GetOrAdd("1")); // [1, 2]
            Assert.AreEqual(3000, c.Now);

            Assert.AreEqual("3!", f.GetOrAdd("3")); // [3, 1, 2]
            Assert.AreEqual(6000, c.Now);

            Assert.AreEqual("1!", f.GetOrAdd("1")); // [1, 3, 2]
            Assert.AreEqual(6000, c.Now);

            Assert.AreEqual("4!", f.GetOrAdd("4")); // [4, 1, 3, 2]
            Assert.AreEqual(10000, c.Now);

            Assert.AreEqual("5!", f.GetOrAdd("5")); // [5, 4, 1, 3]
            Assert.AreEqual(15000, c.Now);

            Assert.AreEqual("1!", f.GetOrAdd("1")); // [1, 5, 4, 3]
            Assert.AreEqual(15000, c.Now);

            Assert.AreEqual("2!", f.GetOrAdd("2")); // [2, 1, 5, 4]
            Assert.AreEqual(17000, c.Now);
        }