Ejemplo n.º 1
0
        public void Add_WhenItemsHaveUnorderedTimeStamps_ThrowsException()
        {
            // Arrange
            var window    = new SlidingWindow <Trade>(TimeSpan.FromMilliseconds(100), 1000, Trade.CreateCalculation);
            var olderItem = CreateTradeItem(DateTime.Now.AddMilliseconds(-1000));
            var newerItem = CreateTradeItem(DateTime.Now);

            // Act
            window.Add(newerItem);

            // Assert
            Assert.Throws <ArgumentException>(() => window.Add(olderItem));
        }
Ejemplo n.º 2
0
        public void Add_WhenItemIsNotInsideOfWindowRangeButItsBucketIs_ShouldIncludeTheBucketInWindow()
        {
            // Arrange
            var items  = CreateTradeItemsList(new[] { 0, 990 }, new[] { 10, 20 });
            var window = new SlidingWindow <Trade>(TimeSpan.FromMilliseconds(999), 100, Trade.CreateCalculation, testDateTimeProvider);

            // Act
            testDateTimeProvider.Now = items[0].TimeStamp;
            window.Add(items[0]);
            testDateTimeProvider.Now = items[1].TimeStamp;
            window.Add(items[1]);
            var calculation = window.GetCalculationFor(items[0].TimeStamp.AddMilliseconds(900)) as Calculation;

            // Assert
            Assert.Equal((15, 2), (calculation.Volume, calculation.CurrentCount));
        }
Ejemplo n.º 3
0
        public void Add_WhenFirstBucketShiftedOutsideOfTheWindow_ShouldRemoveTheBucketFromWindow()
        {
            // Arrange
            var items  = CreateTradeItemsList(new[] { -90, 0 }, new[] { 10, 20 });
            var window = new SlidingWindow <Trade>(TimeSpan.FromMilliseconds(100), 10, Trade.CreateCalculation, testDateTimeProvider);

            // Act
            testDateTimeProvider.Now = items[0].TimeStamp;
            window.Add(items[0]);
            testDateTimeProvider.Now = items[1].TimeStamp;
            window.Add(items[1]);
            testDateTimeProvider.Now = testDateTimeProvider.Now.AddMilliseconds(20);

            var calculationAfterSecondItem = window.GetCurrentWindowCalculation() as Calculation;

            // Assert
            Assert.Equal((items[1].Volume, 1), (calculationAfterSecondItem.Volume, calculationAfterSecondItem.CurrentCount));
        }
Ejemplo n.º 4
0
        public void Add_IfTimestampsDifferMoreThanBucketInterval_ShouldCreateNewBucket(int bucketInterval, int shift)
        {
            // Arrange
            var window    = new SlidingWindow <Trade>(TimeSpan.FromMinutes(1), bucketInterval, Trade.CreateCalculation);
            var olderItem = CreateTradeItem(DateTime.Now.AddMilliseconds(shift));
            var newerItem = CreateTradeItem(DateTime.Now);

            // Act
            window.Add(olderItem);
            window.Add(newerItem);

            // Assert
            var buckets      = window.GetBuckets().Item2;
            var firstBucket  = buckets[window.GetTimeStampKey(olderItem.TimeStamp)];
            var secondBucket = buckets[window.GetTimeStampKey(newerItem.TimeStamp)];

            Assert.NotEqual(firstBucket, secondBucket);
        }
Ejemplo n.º 5
0
        public void Add_NewItemAdded_ShouldCleanUpOldBuckets()
        {
            // Arrange
            var window    = new SlidingWindow <Trade>(TimeSpan.FromMilliseconds(100), 1000, Trade.CreateCalculation, testDateTimeProvider);
            var olderItem = CreateTradeItem(DateTime.Now.AddMilliseconds(-1000));
            var newerItem = CreateTradeItem(DateTime.Now);

            // Act
            testDateTimeProvider.Now = olderItem.TimeStamp;
            window.Add(olderItem);
            testDateTimeProvider.Now = newerItem.TimeStamp;
            window.Add(newerItem);

            // Assert
            var bucketsList = window.GetBuckets().Item1;
            var buckets     = window.GetBuckets().Item2;

            Assert.Single(buckets);
            Assert.Single(bucketsList);
            Assert.False(buckets.ContainsKey(window.GetTimeStampKey(olderItem.TimeStamp)));
            Assert.Equal(window.GetTimeStampKey(newerItem.TimeStamp), bucketsList.First.Value.TimeStampKey);
        }
Ejemplo n.º 6
0
        public void GetCurrentCalculation_WhenMultipleItemsAdded_ShouldUpdateWindowCorrectly(
            Trade[] items, int windowLengthInMs, decimal expectedVolume, decimal expectedCount)
        {
            // Arrange
            var window = new SlidingWindow <Trade>(TimeSpan.FromMilliseconds(windowLengthInMs), 10, Trade.CreateCalculation, testDateTimeProvider);

            // Act
            foreach (var item in items)
            {
                testDateTimeProvider.Now = item.TimeStamp;
                window.Add(item);
            }

            testDateTimeProvider.Now = items[^ 1].TimeStamp;
Ejemplo n.º 7
0
        public void Main(string argument, UpdateType updateSource)
        {
            Log.ClearScreen();
            Cycles++;
            Log.Info($"Cycles: {Cycles}");
            Log.Info($"State: {RunState}");
            Log.Info();

            switch (updateSource)
            {
            case UpdateType.Terminal:
                argument = argument.ToUpper();
                if (Commands.ContainsKey(argument))
                {
                    Commands[argument](argument, updateSource);
                }
                else
                {
                    // TODO: Unhandled argument. Panic?
                }
                break;

            case UpdateType.Update1:
                if (Updater != null)
                {
                    Updater.Update();
                }
                if (Ticks.ContainsKey(RunState))
                {
                    Ticks[RunState]?.Invoke(argument, updateSource);
                }
                else
                {
                    throw new Exception($"Unhandled state during dispatch: {RunState}");
                }
                break;

            default:
                throw new Exception($"Unhandled update source: {updateSource}");
            }

            Log.Info();
            Log.Info($"Last run time: {Runtime.LastRunTimeMs:N4}ms");
            Average.Add((int)(Runtime.LastRunTimeMs * 10000));
            Log.Info($"Min: {Average.Min / 10000f:N4}, Avg: {Average.Average / 10000f:N4}, Max: {Average.Max / 10000f:N4}");
            Log.Info($"Instructions: {Runtime.CurrentInstructionCount}/{Runtime.MaxInstructionCount}");
        }
Ejemplo n.º 8
0
        public void SlidingWindow_WhenWindowLengthIsNotAFactorOfBucketInterval_AdjustsCorrectly(
            Trade[] items, int windowLengthInMs, int bucketInterval, decimal expectedVolume, decimal expectedCount)
        {
            // Arrange
            var window = new SlidingWindow <Trade>(TimeSpan.FromMilliseconds(windowLengthInMs), bucketInterval, Trade.CreateCalculation, testDateTimeProvider);

            // Act
            foreach (var item in items)
            {
                testDateTimeProvider.Now = item.TimeStamp;
                window.Add(item);
            }

            var calculation = window.GetCurrentWindowCalculation() as Calculation;

            // Assert
            Assert.Equal((expectedVolume, expectedCount), (calculation.Volume, calculation.CurrentCount));
        }
Ejemplo n.º 9
0
        public void GetCurrentCalculation_WhenNoItemInWindow_ShouldUpdateWindowAndReturnEmpty(
            Trade[] items, int windowLengthInMs, decimal expectedVolume, decimal expectedCount)
        {
            // Arrange
            var window = new SlidingWindow <Trade>(TimeSpan.FromMilliseconds(windowLengthInMs), 10, Trade.CreateCalculation, testDateTimeProvider);

            // Act
            foreach (var item in items)
            {
                testDateTimeProvider.Now = item.TimeStamp;
                window.Add(item);
            }

            testDateTimeProvider.Now = DateTime.Now;
            var calculation = window.GetCurrentWindowCalculation() as Calculation;

            // Assert
            Assert.Equal((expectedVolume, expectedCount), (calculation.Volume, calculation.CurrentCount));
        }
Ejemplo n.º 10
0
        public void Add_WhenBucketsAreOlderThanInterval_ShouldRemoveOlderBuckets()
        {
            // Arrange
            var windowInterval = TimeSpan.FromMilliseconds(500);
            var items          = CreateTradeItemsList(new[] { -2000, -1000, 0 }, new[] { 200, 100, 10 });
            var window         = new SlidingWindow <Trade>(windowInterval, 500, Trade.CreateCalculation, testDateTimeProvider);

            // Act
            foreach (var item in items)
            {
                testDateTimeProvider.Now = item.TimeStamp;
                window.Add(item);
            }

            var buckets = window.GetBuckets().Item1;
            var expectedOldestTimeStampKey = window.GetTimeStampKey(testDateTimeProvider.Now.Add(-windowInterval * 2));

            // Assert
            Assert.True(buckets.All(b => b.TimeStampKey >= expectedOldestTimeStampKey));
        }
Ejemplo n.º 11
0
        public void SlidingWindow_IfShiftToRight_AdjustsCorrectly(int shift, int expectedVolume, int expectedCount)
        {
            // Arrange
            var items = CreateTradeItemsList(new[] { -1000, -900, -800, -700, -600, -500, -400, -300, -200, -100, 0 },
                                             new[] { 1000, 900, 800, 700, 600, 500, 400, 300, 200, 100, 0 });

            var window = new SlidingWindow <Trade>(TimeSpan.FromMilliseconds(500), 10, Trade.CreateCalculation, testDateTimeProvider);

            // Act
            foreach (var item in items)
            {
                testDateTimeProvider.Now = item.TimeStamp;
                window.Add(item);
            }

            var currentCalculation = window.GetCurrentWindowCalculation() as Calculation;
            var shiftRight         = window.GetCalculationFor(testDateTimeProvider.Now.AddMilliseconds(shift)) as Calculation;

            // Assert
            Assert.Equal((1500m / 6, 6), (currentCalculation.Volume, currentCalculation.CurrentCount));
            Assert.Equal((expectedVolume, expectedCount), (shiftRight.Volume, shiftRight.CurrentCount));
        }
Ejemplo n.º 12
0
        public void SlidingWindow_IfShiftToLeft_AdjustsCorrectly()
        {
            // Arrange
            var trades = CreateTradeItemsList(new[] { -1000, -900, -800, -700, -600, -500, -400, -300, -200, -100, 0 },
                                              new[] { 1000, 900, 800, 700, 600, 500, 400, 300, 200, 100, 0 });

            var window = new SlidingWindow <Trade>(TimeSpan.FromMilliseconds(500), 10, Trade.CreateCalculation, testDateTimeProvider);

            // Act
            foreach (var trade in trades)
            {
                testDateTimeProvider.Now = trade.TimeStamp;
                window.Add(trade);
            }

            var currentCalculation = window.GetCurrentWindowCalculation() as Calculation;
            var shiftLeft          = window.GetCalculationFor(testDateTimeProvider.Now.AddMilliseconds(-250)) as Calculation;

            // Assert
            Assert.Equal((250, 6), (currentCalculation.Volume, currentCalculation.CurrentCount));
            Assert.Equal((2500m / 5, 5), (shiftLeft.Volume, shiftLeft.CurrentCount));
        }
Ejemplo n.º 13
0
        // Starts scanning at character pos of string text for occurrence of any word
        // in stemmed_terms. Returns a list of (words)*[(matched word)(words)*]+
        private SnippetLine MarkTerms(ArrayList stemmed_terms, string text, ref int pos)
        {
            SnippetLine snippet_line       = null;
            int         prev_match_end_pos = pos;     // misnomer; means 1 + end_pos of previous word

            // 1. get next word
            // 2. if no next word, return arraylist
            // 3. if word is not a match, following_words ++
            // 4. else {
            // 4a. add list to the arraylist
            // 4b. add word to the arraylist
            // 4c. clear list
            // 4d. following_words=0
            // }
            // 5. if (following_words >= max_following_words) {
            // 5a. add list to the arraylist
            // 5b. clear list
            // 5c. return list
            // }

            while (pos < text.Length)
            {
                // Find the beginning of the next token
                if (IsTokenSeparator(text [pos]))
                {
                    ++pos;
                    continue;
                }

                // Find the end of the next token
                int end_pos = pos + 1;
                while (end_pos < text.Length && !IsTokenSeparator(text [end_pos]))
                {
                    ++end_pos;
                }

                string token         = text.Substring(pos, end_pos - pos);
                string stemmed_token = null;
                bool   found_match   = false;

                // Iterate through the stemmed terms and match the token
                for (int i = 0; i < stemmed_terms.Count; i++)
                {
                    // If this term is longer than the token in question, give up.
                    if (end_pos - pos < ((string)stemmed_terms [i]).Length)
                    {
                        continue;
                    }

                    // We cache the token, so as to avoid stemming it more than once
                    // when considering multiple terms.
                    if (stemmed_token == null)
                    {
                        stemmed_token = LuceneCommon.Stem(token.ToLower());
                    }

                    if (String.Compare((string)stemmed_terms [i], stemmed_token, true) != 0)
                    {
                        continue;
                    }

                    // We have a match!
                    found_match = true;
                    //Console.WriteLine ("Found match");

                    if (snippet_line == null)
                    {
                        snippet_line = new SnippetLine();
                    }

                    // Find the fragment before the match
                    int start_pos = sliding_window.StartValue;
                    if (start_pos == -1)                     // If no non-match words seen after last match
                    {
                        start_pos = prev_match_end_pos;      // Use wherever previous word ended
                    }
                    sliding_window.Reset();

                    string before_match = text.Substring(start_pos, pos - start_pos);
                    snippet_line.AddNonMatchFragment(before_match);
                    //Console.WriteLine ("Adding [{0}, {1}]:[{2}]", start_pos, pos - 1, before_match);

                    snippet_line.AddMatchFragment(i, token);
                    //Console.WriteLine ("Adding word [{0}, {1}]:[{2}]", pos, end_pos - 1, token);
                    prev_match_end_pos = end_pos;

                    break;
                }

                if (!found_match)
                {
                    // Add the start pos of the token to the window
                    sliding_window.Add(pos);
                    // If we found a match previously and saw enough following words, stop
                    if (snippet_line != null && snippet_line.Count > 0 && sliding_window.Count == context_length)
                    {
                        sliding_window.Reset();
                        string after_match = text.Substring(prev_match_end_pos, end_pos - prev_match_end_pos);
                        snippet_line.AddNonMatchFragment(after_match);
                        //Console.WriteLine ("Adding [{0}, {1}]:[{2}]", prev_match_end_pos, end_pos - 1, after_match);
                        return(snippet_line);
                    }
                }

                pos = end_pos;
            }

            // If less than 6 words came after the last match, add the rest here
            if (snippet_line != null && snippet_line.Count > 0)
            {
                sliding_window.Reset();
                string after_match = text.Substring(prev_match_end_pos, pos - prev_match_end_pos);
                snippet_line.AddNonMatchFragment(after_match);
                //Console.WriteLine ("Adding [{0}, {1}]:[{2}]", prev_match_end_pos, pos - 1, after_match);

                //Console.WriteLine ("Sending snippet: {0}", snippet_line.ToString ());
                return(snippet_line);
            }

            sliding_window.Reset();
            return(null);
        }