public void Stop()
    {
        base.Stop();

        TotalUsers = UserSessions.Count;
        double totalTime = 0.0;

        foreach (UserSession u in UserSessions)
        {
            totalTime += u.SessionLength;
        }

        AverageTimePerUser = totalTime / TotalUsers;

        TelemetrySerializer.SerializeRoomSession(this);
    }
Exemple #2
0
        internal ServerTelemetryChannel(INetwork network, IApplicationLifecycle applicationLifecycle)
        {
            var policies = new TransmissionPolicy[]
            {
                new ApplicationLifecycleTransmissionPolicy(applicationLifecycle),
                new ErrorHandlingTransmissionPolicy(),
                new NetworkAvailabilityTransmissionPolicy(network),
                new ThrottlingTransmissionPolicy()
            };

            this.Transmitter = new Transmitter(policies: policies);

            this.TelemetrySerializer     = new TelemetrySerializer(this.Transmitter);
            this.TelemetryBuffer         = new Implementation.TelemetryBuffer(this.TelemetrySerializer, applicationLifecycle);
            this.telemetryBufferCapacity = this.TelemetryBuffer.Capacity;

            this.TelemetryProcessor = this.TelemetryBuffer;
        }
Exemple #3
0
        internal ServerTelemetryChannel(INetwork network, IApplicationLifecycle applicationLifecycle)
        {
            var policies = new TransmissionPolicy[]
            {
#if !NETSTANDARD
                // We don't have implementation for IApplicationLifecycle for .NET Core
                new ApplicationLifecycleTransmissionPolicy(applicationLifecycle),
#endif
                new ThrottlingTransmissionPolicy(),
                new ErrorHandlingTransmissionPolicy(),
                new PartialSuccessTransmissionPolicy(),
                new NetworkAvailabilityTransmissionPolicy(network),
            };

            this.Transmitter = new Transmitter(policies: policies);

            this.TelemetrySerializer     = new TelemetrySerializer(this.Transmitter);
            this.TelemetryBuffer         = new TelemetryBuffer(this.TelemetrySerializer, applicationLifecycle);
            this.telemetryBufferCapacity = this.TelemetryBuffer.Capacity;

            this.TelemetryProcessor = this.TelemetryBuffer;
            this.isInitialized      = false;
        }
Exemple #4
0
        private static async Task GenerateTelemetryAsync <T>(Func <T, string, bool, T> factory,
                                                             ObjectPool <EventHubClient> pool, TelemetrySerializer <T> serializer, int randomSeed, AsyncConsole console, int generateKeyframeGap, int simulatedDelayInMs, int waittime, string deviceId) where T : class
        {
            if (factory == null)
            {
                throw new ArgumentNullException(nameof(factory));
            }

            if (pool == null)
            {
                throw new ArgumentNullException(nameof(pool));
            }

            if (console == null)
            {
                throw new ArgumentNullException(nameof(console));
            }

            if (waittime > 0)
            {
                TimeSpan span = TimeSpan.FromMilliseconds(waittime);
                await Task.Delay(span);
            }

            if (deviceId == null)
            {
                throw new ArgumentNullException(nameof(deviceId));
            }

            Random random = new Random(randomSeed);

            // buffer block that holds the messages . consumer will fetch records from this block asynchronously.
            BufferBlock <T> buffer = new BufferBlock <T>(new DataflowBlockOptions()
            {
                BoundedCapacity = 100000
            });

            // consumer that sends the data to event hub asynchronoulsy.
            var consumer = new ActionBlock <T>(
                action: (t) =>
            {
                using (var client = pool.GetObject())
                {
                    return(client.Value.SendAsync(new EventData(serializer.Serialize(t))).ContinueWith(
                               async task =>
                    {
                        cts.Cancel();
                        await console.WriteLine(task.Exception.InnerException.Message);
                        await console.WriteLine($"event hub client failed for {deviceId}");
                    }, TaskContinuationOptions.OnlyOnFaulted
                               ));
                }
            },
                new ExecutionDataflowBlockOptions
            {
                BoundedCapacity        = 100000,
                CancellationToken      = cts.Token,
                MaxDegreeOfParallelism = 100,
            }
                );

            // link the buffer to consumer .
            buffer.LinkTo(consumer, new DataflowLinkOptions()
            {
                PropagateCompletion = true
            });

            long messages = 0;

            List <Task> taskList                    = new List <Task>();
            T           telemetryObject             = null;
            T           lastKeyFrameTelemetryObject = null;
            bool        keyFrame                    = true;
            var         generateTask                = Task.Factory.StartNew(
                async() =>
            {
                // generate telemetry records and send them to buffer block
                for (; ;)
                {
                    telemetryObject = factory(lastKeyFrameTelemetryObject, deviceId, keyFrame);
                    await buffer.SendAsync(telemetryObject).ConfigureAwait(false);

                    // Save the last key frame for stateful generation
                    if (keyFrame)
                    {
                        lastKeyFrameTelemetryObject = telemetryObject;

                        // Turn key frame off after sending a key frame
                        keyFrame = false;
                    }

                    if (++messages % generateKeyframeGap == 0)
                    {
                        await console.WriteLine($"Created records for {deviceId} - generating key frame").ConfigureAwait(false);

                        // since rec is changing, makes sense to generate a key frame
                        keyFrame = true;
                    }
                    else
                    {
                        await console.WriteLine($"Created {messages} records for {deviceId}").ConfigureAwait(false);

                        // Wait for given number of milliseconds after each messaage
                        await Task.Delay(simulatedDelayInMs).ConfigureAwait(false);
                    }

                    // Every few messages, send a key frame randomly
                    if (messages % random.Next(10, 100) == 0)
                    {
                        keyFrame = true;
                    }


                    if (cts.IsCancellationRequested)
                    {
                        break;
                    }
                }

                buffer.Complete();
                await Task.WhenAll(buffer.Completion, consumer.Completion);
                await console.WriteLine($"Created total {messages} records for {deviceId}").ConfigureAwait(false);
            }
                ).Unwrap().ContinueWith(
                async task =>
            {
                cts.Cancel();
                await console.WriteLine($"failed to generate telemetry for {deviceId}").ConfigureAwait(false);
                await console.WriteLine(task.Exception.InnerException.Message).ConfigureAwait(false);
            }, TaskContinuationOptions.OnlyOnFaulted
                );

            // await on consumer completion. Incase if sending is failed at any moment ,
            // exception is thrown and caught . This is used to signal the cancel the reading operation and abort all activity further

            try
            {
                await Task.WhenAll(consumer.Completion, generateTask);
            }
            catch (Exception ex)
            {
                cts.Cancel();
                await console.WriteLine(ex.Message).ConfigureAwait(false);

                await console.WriteLine($"failed to generate telemetry").ConfigureAwait(false);

                throw;
            }
        }
 public TelemetryBufferWhichDoesNothingOnFlush(TelemetrySerializer serializer, IApplicationLifecycle applicationLifecycle) : base(serializer, applicationLifecycle)
 {
 }