Ejemplo n.º 1
0
        [Ignore] // this is an integration test
        public void StressUttServerLoadAndSubscribe()
        {
            // return an increasing number of trades until failure
            // on a 4GB workstation, the limit is about 13,000 trades.
            Trade trade = XmlSerializerHelper.DeserializeFromString <Trade>(
                ResourceHelper.GetResourceWithPartialName(Assembly.GetExecutingAssembly(), "SampleSwap.xml"));
            NamedValueSet tradeProps = new NamedValueSet(
                ResourceHelper.GetResourceWithPartialName(Assembly.GetExecutingAssembly(), "SampleSwap.nvs"));

            using (Reference <ILogger> loggerRef = Reference <ILogger> .Create(new TraceLogger(true)))
            {
                //NamedValueSet serverSettings = new NamedValueSet();
                //serverSettings.Set(CfgPropName.NodeType, (int)NodeType.Router);
                //serverSettings.Set(CfgPropName.EnvName, "UTT");
                //using (CoreServer server = new CoreServer(logger, serverSettings))
                {
                    //server.Start();
                    //using (ICoreClient client = new CoreClientFactory(loggerRef).SetEnv("UTT").SetHosts("localhost:8113").Create())
                    using (ICoreClient client = new CoreClientFactory(loggerRef).SetEnv("UTT").Create())
                    {
                        // delete the test trades
                        IExpression deleteExpr = Expr.ALL;
                        client.DeleteObjects <Trade>(deleteExpr);
                        const int maxLoops         = 3;
                        const int loopRepeat       = 3;
                        const int incrementPerLoop = 1024;
                        int       itemsPerLoop     = 1024;
                        int       itemsSaved       = 0;
                        for (int loop = 0; loop < maxLoops; loop++)
                        {
                            string loopName = $"Test.{loop}.";
                            for (int repeat = 0; repeat < loopRepeat; repeat++)
                            {
                                // create trades
                                loggerRef.Target.LogDebug("[{0}-{1}] Creating {2} trades...", loop, repeat, itemsPerLoop);
                                ICoreItem[] items = new ICoreItem[itemsPerLoop];
                                for (int i = 0; i < itemsPerLoop; i++)
                                {
                                    string tradeName = $"Test.{loop}.{i}";
                                    items[i] = client.MakeObject <Trade>(trade, tradeName, tradeProps);
                                    itemsSaved++;
                                }
                                client.SaveItems(items);
                                // start subscription
                                loggerRef.Target.LogDebug("[{0}-{1}] Receiving {2} trades...", loop, repeat, itemsPerLoop);
                                long          itemsReceived   = 0;
                                DateTime      testStartedTime = DateTime.Now;
                                DateTime      lastReceiveTime = testStartedTime;
                                ISubscription subscription    = client.CreateSubscription <Trade>(Expr.StartsWith(Expr.SysPropItemName, loopName));
                                subscription.UserCallback = delegate
                                {
                                    Interlocked.Increment(ref itemsReceived);
                                    lastReceiveTime = DateTime.Now;
                                };
                                //subscription.ExcludeDataBody = true;
                                subscription.Start();
                                // wait a bit to ensure subscription is up to date
                                while ((Interlocked.Add(ref itemsReceived, 0) < itemsPerLoop) && ((DateTime.Now - testStartedTime) < TimeSpan.FromSeconds(10)))
                                {
                                    Thread.Sleep(100);
                                }
                                Thread.Sleep(100);
                                subscription.Cancel();
                                // load all that have been saved
                                loggerRef.Target.LogDebug("[{0}-{1}] Received {2} trades in {3} seconds.", loop, repeat, itemsReceived, (lastReceiveTime - testStartedTime).TotalSeconds);
                                Assert.AreEqual(itemsPerLoop, Interlocked.Add(ref itemsReceived, 0));
                                // delete the test trades
                                //client.DeleteObjects<Trade>(deleteExpr);
                                //GC.Collect();
                            }

                            // next loop
                            itemsPerLoop += incrementPerLoop;
                        } // for loop
                    }
                }
            }
        }