Example #1
0
        public void CacheHappyPerformance()
        {
            long counter = 0;
            var  datas   = new List <IEnumerable <BaseData> >();

            var factory       = new TradeBar();
            var cacheProvider = new CustomEphemeralDataCacheProvider();

            // we load SPY hour zip into memory and use it as the source of different fake tickers
            var config = new SubscriptionDataConfig(typeof(TradeBar),
                                                    Symbols.SPY,
                                                    Resolution.Hour,
                                                    TimeZones.NewYork,
                                                    TimeZones.NewYork,
                                                    true,
                                                    true,
                                                    false);
            var fakeSource = factory.GetSource(config, new DateTime(2013, 10, 07), false);

            cacheProvider.Data = string.Join(Environment.NewLine, QuantConnect.Compression.ReadLines(fakeSource.Source));

            for (var i = 0; i < 500; i++)
            {
                var ticker     = $"{i}";
                var fakeConfig = new SubscriptionDataConfig(
                    typeof(TradeBar),
                    Symbol.Create(ticker, SecurityType.Equity, Market.USA),
                    Resolution.Hour,
                    TimeZones.NewYork,
                    TimeZones.NewYork,
                    true,
                    true,
                    false);
                var reader = new TextSubscriptionDataSourceReader(cacheProvider, fakeConfig, Time.EndOfTime, false);

                var source = factory.GetSource(fakeConfig, Time.BeginningOfTime, false);
                datas.Add(reader.Read(source));
            }

            var timer = new Stopwatch();

            timer.Start();
            Parallel.ForEach(datas, enumerable =>
            {
                // after the first call should use the cache
                foreach (var data in enumerable)
                {
                    Interlocked.Increment(ref counter);
                }
                foreach (var data in enumerable)
                {
                    Interlocked.Increment(ref counter);
                }
                foreach (var data in enumerable)
                {
                    Interlocked.Increment(ref counter);
                }
            });
            timer.Stop();
            Log.Trace($"Took {timer.ElapsedMilliseconds}ms. Data count {counter}");
        }
 public override SubscriptionDataSource GetSource(SubscriptionDataConfig config, DateTime date, bool isLiveMode)
 {
     return(_factory.GetSource(config, date, isLiveMode));
 }