SubmitSamples() public method

public SubmitSamples ( IEnumerable samples, string instrumentationKey ) : bool?
samples IEnumerable
instrumentationKey string
return bool?
        public void QuickPulseServiceClientDoesNotRetrySubmitSamples()
        {
            // ARRANGE
            var serviceClient = new QuickPulseServiceClient(
                this.serviceEndpoint,
                string.Empty,
                string.Empty,
                new Clock(),
                TimeSpan.FromMilliseconds(50));

            this.emulateTimeout = true;

            // ACT
            serviceClient.SubmitSamples(new QuickPulseDataSample[] { }, string.Empty);

            // ASSERT
            this.listener.Stop();

            Assert.AreEqual(1, this.submitCount);
        }
        public void QuickPulseServiceClientFillsInSampleWeightWhenSubmittingToService()
        {
            // ARRANGE
            var now = DateTimeOffset.UtcNow;
            var serviceClient = new QuickPulseServiceClient(this.serviceEndpoint, string.Empty, string.Empty, new Clock());
            var sample1 =
                new QuickPulseDataSample(
                    new QuickPulseDataAccumulator
                        {
                            AIRequestCountAndDurationInTicks = QuickPulseDataAccumulator.EncodeCountAndDuration(3, 10000),
                            StartTimestamp = now,
                            EndTimestamp = now.AddSeconds(1)
                        },
                    new Dictionary<string, Tuple<PerformanceCounterData, float>>());

            var sample2 =
                new QuickPulseDataSample(
                    new QuickPulseDataAccumulator
                        {
                            AIDependencyCallCountAndDurationInTicks =
                                QuickPulseDataAccumulator.EncodeCountAndDuration(4, 10000),
                            StartTimestamp = now,
                            EndTimestamp = now.AddSeconds(1)
                        },
                    new Dictionary<string, Tuple<PerformanceCounterData, float>>());

            // ACT
            serviceClient.SubmitSamples(new[] { sample1, sample2 }, string.Empty);

            // ASSERT
            this.listener.Stop();

            Assert.AreEqual(3, this.samples[0].Item2.Metrics.Single(m => m.Name == @"\ApplicationInsights\Request Duration").Weight);
            Assert.AreEqual(4, this.samples[1].Item2.Metrics.Single(m => m.Name == @"\ApplicationInsights\Dependency Call Duration").Weight);
        }
        public void QuickPulseServiceClientSubmitsSamplesToService()
        {
            // ARRANGE
            var now = DateTimeOffset.UtcNow;
            var serviceClient = new QuickPulseServiceClient(this.serviceEndpoint, string.Empty, string.Empty, new Clock());
            var sample1 =
                new QuickPulseDataSample(
                    new QuickPulseDataAccumulator { AIRequestSuccessCount = 5, StartTimestamp = now, EndTimestamp = now.AddSeconds(1) },
                    new Dictionary<string, Tuple<PerformanceCounterData, float>>());

            var sample2 =
                new QuickPulseDataSample(
                    new QuickPulseDataAccumulator { AIDependencyCallSuccessCount = 10, StartTimestamp = now, EndTimestamp = now.AddSeconds(1) },
                    new Dictionary<string, Tuple<PerformanceCounterData, float>>());

            var sample3 =
                new QuickPulseDataSample(
                    new QuickPulseDataAccumulator { AIExceptionCount = 15, StartTimestamp = now, EndTimestamp = now.AddSeconds(1) },
                    new Dictionary<string, Tuple<PerformanceCounterData, float>>());

            // ACT
            bool? sendMore = serviceClient.SubmitSamples(new[] { sample1, sample2, sample3 }, string.Empty);

            // ASSERT
            this.listener.Stop();

            Assert.AreEqual(true, sendMore);
            Assert.AreEqual(3, this.samples.Count);
            Assert.AreEqual(5, this.samples[0].Item2.Metrics.Single(m => m.Name == @"\ApplicationInsights\Requests Succeeded/Sec").Value);
            Assert.AreEqual(10, this.samples[1].Item2.Metrics.Single(m => m.Name == @"\ApplicationInsights\Dependency Calls Succeeded/Sec").Value);
            Assert.AreEqual(15, this.samples[2].Item2.Metrics.Single(m => m.Name == @"\ApplicationInsights\Exceptions/Sec").Value);
        }
        public void QuickPulseServiceClientSubmitsVersionToServiceWithSubmitSamples()
        {
            // ARRANGE
            var now = DateTimeOffset.UtcNow;
            var version = "this version";
            var serviceClient = new QuickPulseServiceClient(this.serviceEndpoint, string.Empty, version, new Clock());
            var sample = new QuickPulseDataSample(
                new QuickPulseDataAccumulator { StartTimestamp = now, EndTimestamp = now.AddSeconds(1) },
                new Dictionary<string, Tuple<PerformanceCounterData, float>>());

            // ACT
            serviceClient.SubmitSamples(new[] { sample }, string.Empty);

            // ASSERT
            this.listener.Stop();

            Assert.AreEqual(1, this.samples.Count);
            Assert.AreEqual(version, this.samples[0].Item2.Version);
        }
        public void QuickPulseServiceClientSetsTransmissionTimeCorrectly()
        {
            // ARRANGE
            var dummy = new Dictionary<string, Tuple<PerformanceCounterData, float>>();
            var timeProvider = new ClockMock();

            var serviceClient = new QuickPulseServiceClient(this.serviceEndpoint, string.Empty, string.Empty, timeProvider);
            var sample1 =
                new QuickPulseDataSample(
                    new QuickPulseDataAccumulator { StartTimestamp = timeProvider.UtcNow.AddSeconds(-1), EndTimestamp = timeProvider.UtcNow },
                    dummy);

            timeProvider.FastForward(TimeSpan.FromSeconds(1));
            var sample2 =
                new QuickPulseDataSample(
                    new QuickPulseDataAccumulator { StartTimestamp = timeProvider.UtcNow.AddSeconds(-1), EndTimestamp = timeProvider.UtcNow },
                    dummy);

            timeProvider.FastForward(TimeSpan.FromSeconds(1));
            var sample3 =
                new QuickPulseDataSample(
                    new QuickPulseDataAccumulator { StartTimestamp = timeProvider.UtcNow.AddSeconds(-1), EndTimestamp = timeProvider.UtcNow },
                    dummy);

            // ACT
            timeProvider.FastForward(TimeSpan.FromSeconds(5));
            serviceClient.SubmitSamples(new[] { sample1, sample2, sample3 }, string.Empty);

            // ASSERT
            this.listener.Stop();

            Assert.AreEqual(3, this.samples.Count);
            Assert.IsTrue((timeProvider.UtcNow - this.samples[0].Item1).Duration() < TimeSpan.FromMilliseconds(1));
            Assert.IsTrue((timeProvider.UtcNow - this.samples[1].Item1).Duration() < TimeSpan.FromMilliseconds(1));
            Assert.IsTrue((timeProvider.UtcNow - this.samples[2].Item1).Duration() < TimeSpan.FromMilliseconds(1));
            Assert.IsTrue(this.samples.All(s => (s.Item2.Timestamp - timeProvider.UtcNow).Duration() > TimeSpan.FromSeconds(1)));
        }
        public void QuickPulseServiceClientSubmitsInstrumentationKeyToService()
        {
            // ARRANGE
            var now = DateTimeOffset.UtcNow;
            var ikey = "some ikey";
            var serviceClient = new QuickPulseServiceClient(this.serviceEndpoint, string.Empty, string.Empty, new Clock());
            var sample = new QuickPulseDataSample(
                new QuickPulseDataAccumulator { StartTimestamp = now, EndTimestamp = now.AddSeconds(1) },
                new Dictionary<string, Tuple<PerformanceCounterData, float>>());

            // ACT
            serviceClient.SubmitSamples(new[] { sample }, ikey);

            // ASSERT
            this.listener.Stop();

            Assert.AreEqual(1, this.samples.Count);
            Assert.AreEqual(ikey, this.samples[0].Item2.InstrumentationKey);
        }
        public void QuickPulseServiceClientRoundsSampleValuesWhenSubmittingToService()
        {
            // ARRANGE
            var now = DateTimeOffset.UtcNow;
            var serviceClient = new QuickPulseServiceClient(this.serviceEndpoint, string.Empty, string.Empty, new Clock());
            var sample1 =
                new QuickPulseDataSample(
                    new QuickPulseDataAccumulator { AIRequestSuccessCount = 1, StartTimestamp = now, EndTimestamp = now.AddSeconds(3) },
                    new Dictionary<string, Tuple<PerformanceCounterData, float>>());

            // ACT
            serviceClient.SubmitSamples(new[] { sample1 }, string.Empty);

            // ASSERT
            this.listener.Stop();

            Assert.AreEqual(0.3333, this.samples[0].Item2.Metrics.Single(m => m.Name == @"\ApplicationInsights\Requests Succeeded/Sec").Value);
        }
        public void QuickPulseServiceClientInterpretsSubmitSamplesResponseCorrectlyWhenHeaderTrue()
        {
            // ARRANGE
            var serviceClient = new QuickPulseServiceClient(this.serviceEndpoint, string.Empty, string.Empty, new Clock());

            // ACT
            this.submitResponse = r => { r.AddHeader("x-ms-qps-subscribed", true.ToString()); };
            bool? response = serviceClient.SubmitSamples(new QuickPulseDataSample[] { }, string.Empty);

            // ASSERT
            this.listener.Stop();

            Assert.IsTrue(response.Value);
        }
        public void QuickPulseServiceClientInterpretsSubmitSamplesResponseCorrectlyWhenHeaderMissing()
        {
            // ARRANGE
            var serviceClient = new QuickPulseServiceClient(this.serviceEndpoint, string.Empty, string.Empty, new Clock());

            // ACT
            this.submitResponse = r => { };
            bool? response = serviceClient.SubmitSamples(new QuickPulseDataSample[] { }, string.Empty);

            // ASSERT
            this.listener.Stop();

            Assert.IsNull(response);
        }
        public void QuickPulseServiceClientInterpretsSubmitSamplesResponseCorrectlyWhenHeaderInvalid()
        {
            // ARRANGE
            var serviceClient = new QuickPulseServiceClient(this.serviceEndpoint, string.Empty, string.Empty, string.Empty, string.Empty, new Clock(), false);

            // ACT
            this.submitResponse = r => { r.AddHeader(QuickPulseConstants.XMsQpsSubscribedHeaderName, "bla"); };
            bool? response = serviceClient.SubmitSamples(new QuickPulseDataSample[] { }, string.Empty);

            // ASSERT
            this.listener.Stop();

            Assert.IsNull(response);
        }
        public void QuickPulseServiceClientFillsInTelemetryDocumentsWhenSubmittingToService()
        {
            // ARRANGE
            var now = DateTimeOffset.UtcNow;
            var serviceClient = new QuickPulseServiceClient(this.serviceEndpoint, string.Empty, string.Empty, string.Empty, string.Empty, new Clock(), false);
            var properties = new Dictionary<string, string>() { { "Prop1", "Val1" } };
            var sample =
                new QuickPulseDataSample(
                    new QuickPulseDataAccumulator
                        {
                            StartTimestamp = now,
                            EndTimestamp = now.AddSeconds(1),
                            TelemetryDocuments =
                                new ConcurrentStack<ITelemetryDocument>(
                                new ITelemetryDocument[]
                                    {
                                        new RequestTelemetryDocument() { Name = "Request1", Properties = properties.ToArray() },
                                        new DependencyTelemetryDocument() { Name = "Dependency1", Properties = properties.ToArray() },
                                        new ExceptionTelemetryDocument() { Exception = "Exception1", Properties = properties.ToArray() }
                                    })
                        },
                    new Dictionary<string, Tuple<PerformanceCounterData, double>>());

            // ACT
            serviceClient.SubmitSamples(new[] { sample }, string.Empty);

            // ASSERT
            this.listener.Stop();

            Assert.AreEqual("Request1", ((RequestTelemetryDocument)this.samples[0].Item2.Documents[0]).Name);
            Assert.AreEqual("Prop1", ((RequestTelemetryDocument)this.samples[0].Item2.Documents[0]).Properties.First().Key);
            Assert.AreEqual("Val1", ((RequestTelemetryDocument)this.samples[0].Item2.Documents[0]).Properties.First().Value);

            Assert.AreEqual("Dependency1", ((DependencyTelemetryDocument)this.samples[0].Item2.Documents[1]).Name);
            Assert.AreEqual("Prop1", ((DependencyTelemetryDocument)this.samples[0].Item2.Documents[1]).Properties.First().Key);
            Assert.AreEqual("Val1", ((DependencyTelemetryDocument)this.samples[0].Item2.Documents[1]).Properties.First().Value);

            Assert.AreEqual("Exception1", ((ExceptionTelemetryDocument)this.samples[0].Item2.Documents[2]).Exception);
            Assert.AreEqual("Prop1", ((ExceptionTelemetryDocument)this.samples[0].Item2.Documents[2]).Properties.First().Key);
            Assert.AreEqual("Val1", ((ExceptionTelemetryDocument)this.samples[0].Item2.Documents[2]).Properties.First().Value);
        }