/// <summary> /// Enqueues the event. /// </summary> /// <param name="mdEvent">The md event.</param> protected void EnqueueEvent(object sender, MarketData mdEvent) { var nsTop = PerformanceObserver.NanoTime; _executor.Execute( delegate { if (mdEvent != null) { var ns = PerformanceObserver.NanoTime; _cepProvider.SendMarketDataEvent(mdEvent); var nsDone = PerformanceObserver.NanoTime; var msDelta = (nsDone - mdEvent.Time) / 1000000; var nsDelta1 = nsDone - ns; var nsDelta2 = nsDone - nsTop; //Console.WriteLine("D1:{0,10},\tD2:{1,15},\tMS:{2,6},\tQD:{3}", nsDelta1, nsDelta2, msDelta, e.QueueDepth); StatsHolder.Engine.Update(nsDelta1); StatsHolder.Server.Update(nsDelta2); StatsHolder.EndToEnd.Update(msDelta); } }); //stats _countForStatSec++; if (Environment.TickCount - _lastThroughputTick > _statSec * 1E3) { _countForStatSecLast = _countForStatSec; _countForStatSec = 0; _lastThroughputTick = Environment.TickCount; } }
public void Run() { Console.WriteLine("Event per s = {0}", _simulationRate); var eventPer10Millis = (_simulationRate / 100); Console.WriteLine("Event per 10ms = " + Math.Max(eventPer10Millis, 1)); var market = new MarketData[Symbols.SYMBOLS.Length]; for (var i = 0; i < market.Length; i++) { market[i] = new MarketData(Symbols.SYMBOLS[i], Symbols.NextPrice(10), Symbols.NextVolume(10)); } try { var tickerIndex = 0; while (--_iterationsLeft != 0) { var ms = _highResolutionTimeProvider.CurrentTime; for (var i = 0; i < eventPer10Millis; i++) { tickerIndex = tickerIndex % Symbols.SYMBOLS.Length; var eventObj = market[tickerIndex++]; //note the cloning here, although we don't change volume or price var simulatedEvent = (MarketData)eventObj.Clone(); _executor.Execute( delegate { var ns = _highResolutionTimeProvider.CurrentTime; _cepProvider.SendMarketDataEvent(simulatedEvent); var nsDone = _highResolutionTimeProvider.CurrentTime; var statsInstance = StatsHolder.All; statsInstance.Engine.Update(nsDone - ns); statsInstance.Server.Update(nsDone - simulatedEvent.InTime); statsInstance.EndToEnd.Update((nsDone - simulatedEvent.Time) / 1000000); }); //stats _countLast10S++; } var currentTime = _highResolutionTimeProvider.CurrentTime; if (currentTime - _lastThroughputTick > _statSec * 1E9) { //System.out.Println("Avg["+myID+"] " + countLast10s/10 + " active " + executor.GetPoolSize() + " pending " + executor.GetQueue().Count); _countLast10SLast = _countLast10S; _countLast10S = 0; _lastThroughputTick = currentTime; } // going to fast compared to target rate var deltaTime = currentTime - ms; if (deltaTime < 10000) { var slowDown = (int)Math.Max(1, 10000 - deltaTime); Thread.Sleep(slowDown); } } } catch (Exception e) { Console.WriteLine("Error receiving data from market. Did market disconnect?"); Console.WriteLine("Error message: {0}", e.Message); Console.WriteLine(e.StackTrace); } finally { lock (CLIENT_CONNECTIONS_LOCK) { CLIENT_CONNECTIONS.Remove(_myId); } } }