public void AmbientServiceProfilerCoordinatorSettingsNonCurrent()
        {
            string system1 = "DynamoDB/Table:My-table/Partition:342644/Result:Success";
            string system2 = "S3/Bucket:My-bucket/Prefix:abcdefg/Result:Retry";
            string system3 = "SQL/Database:My-database/Table:User/Result:Failed";
            BasicAmbientSettingsSet settingsSet = new BasicAmbientSettingsSet(nameof(AmbientServiceProfilerCoordinatorSettingsNonCurrent));

            settingsSet.ChangeSetting(nameof(AmbientServiceProfilerCoordinator) + "-DefaultSystemGroupTransform", "(?:([^:/]+)(?:(/Database:[^:/]*)|(/Bucket:[^:/]*)|(/Result:[^:/]*)|(?:/[^/]*))*)");
            using (ScopedLocalServiceOverride <IAmbientSettingsSet> o = new ScopedLocalServiceOverride <IAmbientSettingsSet>(settingsSet))
                using (ScopedLocalServiceOverride <IAmbientServiceProfiler> p = new ScopedLocalServiceOverride <IAmbientServiceProfiler>(new BasicAmbientServiceProfiler()))
                    using (AmbientClock.Pause())
                        using (AmbientServiceProfilerCoordinator coordinator = new AmbientServiceProfilerCoordinator())
                            using (IAmbientServiceProfile scopeProfile = coordinator.CreateCallContextProfiler(nameof(ServiceProfilerBasic)))
                            {
                                _ServiceProfiler.Local?.SwitchSystem(system1);
                                AmbientClock.SkipAhead(TimeSpan.FromMilliseconds(5));

                                _ServiceProfiler.Local?.SwitchSystem(system2);
                                AmbientClock.SkipAhead(TimeSpan.FromMilliseconds(200));

                                _ServiceProfiler.Local?.SwitchSystem(system3);
                                AmbientClock.SkipAhead(TimeSpan.FromMilliseconds(3000));

                                _ServiceProfiler.Local?.SwitchSystem(system1);
                                AmbientClock.SkipAhead(TimeSpan.FromMilliseconds(5));

                                _ServiceProfiler.Local?.SwitchSystem("noreport");

                                if (scopeProfile != null)
                                {
                                    foreach (AmbientServiceProfilerAccumulator stats in scopeProfile.ProfilerStatistics)
                                    {
                                        if (stats.Group.StartsWith("DynamoDB"))
                                        {
                                            Assert.AreEqual("DynamoDB/Result:Success", stats.Group);
                                            Assert.AreEqual(TimeSpan.FromMilliseconds(10), stats.TimeUsed);
                                            Assert.AreEqual(2, stats.ExecutionCount);
                                        }
                                        else if (stats.Group.StartsWith("S3"))
                                        {
                                            Assert.AreEqual("S3/Bucket:My-bucket/Result:Retry", stats.Group);
                                            Assert.AreEqual(TimeSpan.FromMilliseconds(200), stats.TimeUsed);
                                            Assert.AreEqual(1, stats.ExecutionCount);
                                        }
                                        else if (stats.Group.StartsWith("SQL"))
                                        {
                                            Assert.AreEqual("SQL/Database:My-database/Result:Failed", stats.Group);
                                            Assert.AreEqual(TimeSpan.FromMilliseconds(3000), stats.TimeUsed);
                                            Assert.AreEqual(1, stats.ExecutionCount);
                                        }
                                    }
                                }
                            }
        }
예제 #2
0
    private Task OnMostRecentWindowClosed(IAmbientServiceProfile profile)
    {
        Dictionary <string, long> serviceProfile = new Dictionary <string, long>();

        foreach (AmbientServiceProfilerAccumulator record in profile.ProfilerStatistics)
        {
            serviceProfile.Add(record.Group, record.TotalStopwatchTicksUsed);
        }
        System.Threading.Interlocked.Exchange(ref _mostRecentWindowServiceProfile, serviceProfile);
        return(Task.CompletedTask);
    }
 public void ServiceProfilerNull()
 {
     using (ScopedLocalServiceOverride <IAmbientServiceProfiler> o = new ScopedLocalServiceOverride <IAmbientServiceProfiler>(null))
         using (AmbientServiceProfilerCoordinator coordinator = new AmbientServiceProfilerCoordinator())
             using (IAmbientServiceProfile processProfile = coordinator.CreateProcessProfiler(nameof(ServiceProfilerNull)))
                 using (IDisposable timeWindowProfile = coordinator.CreateTimeWindowProfiler(nameof(ServiceProfilerNull), TimeSpan.FromMilliseconds(100), p => Task.CompletedTask))
                     using (IAmbientServiceProfile scopeProfile = coordinator.CreateCallContextProfiler(nameof(ServiceProfilerNull)))
                         using (AmbientClock.Pause())
                         {
                             _ServiceProfiler.Local?.SwitchSystem(nameof(ServiceProfilerNull));
                         }
 }
        public void AmbientServiceProfilerCoordinatorOverrideGroupTransform()
        {
            string system1Start   = "DynamoDB/Table:My-table/Partition:342644";
            string system1End     = "DynamoDB/Table:My-table/Partition:342644/Result:Success";
            string system2        = "S3/Bucket:My-bucket/Prefix:abcdefg/Result:Retry";
            string system3        = "SQL/Database:My-database/Table:User/Result:Failed";
            string groupTransform = "(?:([^:/]+)(?:(/Database:[^:/]*)|(/Bucket:[^:/]*)|(/Result:[^:/]*)|(?:/[^/]*))*)";
            IAmbientServiceProfile timeWindowProfile = null;

            using (AmbientClock.Pause())
                using (ScopedLocalServiceOverride <IAmbientServiceProfiler> o = new ScopedLocalServiceOverride <IAmbientServiceProfiler>(new BasicAmbientServiceProfiler()))
                    using (AmbientServiceProfilerCoordinator coordinator = new AmbientServiceProfilerCoordinator())
                        using (IAmbientServiceProfile processProfile = coordinator.CreateProcessProfiler(nameof(AmbientServiceProfilerCoordinatorOverrideGroupTransform), groupTransform))
                            using (IDisposable timeWindowProfiler = coordinator.CreateTimeWindowProfiler(nameof(AmbientServiceProfilerCoordinatorOverrideGroupTransform), TimeSpan.FromMilliseconds(10000), p => { timeWindowProfile = p; return(Task.CompletedTask); }, groupTransform))
                                using (IAmbientServiceProfile scopeProfile = coordinator.CreateCallContextProfiler(nameof(AmbientServiceProfilerCoordinatorOverrideGroupTransform), groupTransform))
                                {
                                    _ServiceProfiler.Local?.SwitchSystem(system1Start);
                                    AmbientClock.SkipAhead(TimeSpan.FromMilliseconds(5));

                                    _ServiceProfiler.Local?.SwitchSystem(system2, system1End);
                                    AmbientClock.SkipAhead(TimeSpan.FromMilliseconds(200));

                                    _ServiceProfiler.Local?.SwitchSystem(system3);
                                    AmbientClock.SkipAhead(TimeSpan.FromMilliseconds(3000));

                                    _ServiceProfiler.Local?.SwitchSystem(system1End);
                                    AmbientClock.SkipAhead(TimeSpan.FromMilliseconds(5));

                                    _ServiceProfiler.Local?.SwitchSystem("noreport");

                                    if (scopeProfile != null)
                                    {
                                        foreach (AmbientServiceProfilerAccumulator stats in scopeProfile.ProfilerStatistics)
                                        {
                                            if (stats.Group.StartsWith("DynamoDB"))
                                            {
                                                Assert.AreEqual("DynamoDB/Result:Success", stats.Group);
                                                Assert.AreEqual(TimeSpan.FromMilliseconds(10), stats.TimeUsed);
                                                Assert.AreEqual(2, stats.ExecutionCount);
                                            }
                                            else if (stats.Group.StartsWith("S3"))
                                            {
                                                Assert.AreEqual("S3/Bucket:My-bucket/Result:Retry", stats.Group);
                                                Assert.AreEqual(TimeSpan.FromMilliseconds(200), stats.TimeUsed);
                                                Assert.AreEqual(1, stats.ExecutionCount);
                                            }
                                            else if (stats.Group.StartsWith("SQL"))
                                            {
                                                Assert.AreEqual("SQL/Database:My-database/Result:Failed", stats.Group);
                                                Assert.AreEqual(TimeSpan.FromMilliseconds(3000), stats.TimeUsed);
                                                Assert.AreEqual(1, stats.ExecutionCount);
                                            }
                                        }
                                    }
                                    if (processProfile != null)
                                    {
                                        foreach (AmbientServiceProfilerAccumulator stats in processProfile.ProfilerStatistics)
                                        {
                                            if (stats.Group.StartsWith("DynamoDB"))
                                            {
                                                Assert.AreEqual("DynamoDB/Result:Success", stats.Group);
                                                Assert.AreEqual(TimeSpan.FromMilliseconds(10), stats.TimeUsed);
                                                Assert.AreEqual(2, stats.ExecutionCount);
                                            }
                                            else if (stats.Group.StartsWith("S3"))
                                            {
                                                Assert.AreEqual("S3/Bucket:My-bucket/Result:Retry", stats.Group);
                                                Assert.AreEqual(TimeSpan.FromMilliseconds(200), stats.TimeUsed);
                                                Assert.AreEqual(1, stats.ExecutionCount);
                                            }
                                            else if (stats.Group.StartsWith("SQL"))
                                            {
                                                Assert.AreEqual("SQL/Database:My-database/Result:Failed", stats.Group);
                                                Assert.AreEqual(TimeSpan.FromMilliseconds(3000), stats.TimeUsed);
                                                Assert.AreEqual(1, stats.ExecutionCount);
                                            }
                                        }
                                    }
                                    AmbientClock.SkipAhead(TimeSpan.FromMilliseconds(10000));
                                    if (timeWindowProfile != null)
                                    {
                                        foreach (AmbientServiceProfilerAccumulator stats in timeWindowProfile.ProfilerStatistics)
                                        {
                                            if (stats.Group.StartsWith("DynamoDB"))
                                            {
                                                Assert.AreEqual("DynamoDB/Result:Success", stats.Group);
                                                Assert.AreEqual(TimeSpan.FromMilliseconds(10), stats.TimeUsed);
                                                Assert.AreEqual(2, stats.ExecutionCount);
                                            }
                                            else if (stats.Group.StartsWith("S3"))
                                            {
                                                Assert.AreEqual("S3/Bucket:My-bucket/Result:Retry", stats.Group);
                                                Assert.AreEqual(TimeSpan.FromMilliseconds(200), stats.TimeUsed);
                                                Assert.AreEqual(1, stats.ExecutionCount);
                                            }
                                            else if (stats.Group.StartsWith("SQL"))
                                            {
                                                Assert.AreEqual("SQL/Database:My-database/Result:Failed", stats.Group);
                                                Assert.AreEqual(TimeSpan.FromMilliseconds(3000), stats.TimeUsed);
                                                Assert.AreEqual(1, stats.ExecutionCount);
                                            }
                                        }
                                    }
                                }
        }
        public void ServiceProfilerBasic()
        {
            using (ScopedLocalServiceOverride <IAmbientServiceProfiler> o = new ScopedLocalServiceOverride <IAmbientServiceProfiler>(new BasicAmbientServiceProfiler()))
                using (AmbientClock.Pause())
                    using (AmbientServiceProfilerCoordinator coordinator = new AmbientServiceProfilerCoordinator())
                        using (IAmbientServiceProfile processProfile = coordinator.CreateProcessProfiler(nameof(ServiceProfilerBasic)))
                            using (IDisposable timeWindowProfile = coordinator.CreateTimeWindowProfiler(nameof(ServiceProfilerBasic), TimeSpan.FromMilliseconds(100), p => Task.CompletedTask))
                                using (IAmbientServiceProfile scopeProfile = coordinator.CreateCallContextProfiler(nameof(ServiceProfilerBasic)))
                                {
                                    _ServiceProfiler.Local?.SwitchSystem("ServiceProfilerBasic1");
                                    Assert.AreEqual(nameof(ServiceProfilerBasic), processProfile?.ScopeName);
                                    if (processProfile != null)
                                    {
                                        foreach (AmbientServiceProfilerAccumulator stats in processProfile.ProfilerStatistics)
                                        {
                                            if (string.IsNullOrEmpty(stats.Group))
                                            {
                                                Assert.AreEqual("", stats.Group);
                                                Assert.AreEqual(1, stats.ExecutionCount);
                                                Assert.AreEqual(0, stats.TotalStopwatchTicksUsed);
                                            }
                                            else
                                            {
                                                Assert.AreEqual("ServiceProfilerBasic1", stats.Group);
                                                Assert.AreEqual(1, stats.ExecutionCount);
                                                Assert.AreEqual(0, stats.TotalStopwatchTicksUsed);
                                            }
                                        }
                                    }
                                    Assert.AreEqual(nameof(ServiceProfilerBasic), scopeProfile?.ScopeName);
                                    if (scopeProfile != null)
                                    {
                                        foreach (AmbientServiceProfilerAccumulator stats in scopeProfile.ProfilerStatistics)
                                        {
                                            if (string.IsNullOrEmpty(stats.Group))
                                            {
                                                Assert.AreEqual("", stats.Group);
                                                Assert.AreEqual(1, stats.ExecutionCount);
                                                Assert.AreEqual(0, stats.TotalStopwatchTicksUsed);
                                            }
                                            else
                                            {
                                                Assert.AreEqual("ServiceProfilerBasic1", stats.Group);
                                                Assert.AreEqual(1, stats.ExecutionCount);
                                                Assert.AreEqual(0, stats.TotalStopwatchTicksUsed);
                                            }
                                        }
                                    }

                                    AmbientClock.SkipAhead(TimeSpan.FromMilliseconds(100));

                                    _ServiceProfiler.Local?.SwitchSystem(null);

                                    AmbientClock.SkipAhead(TimeSpan.FromMilliseconds(113));

                                    _ServiceProfiler.Local?.SwitchSystem("ServiceProfilerBasic2");

                                    if (scopeProfile != null)
                                    {
                                        foreach (AmbientServiceProfilerAccumulator stats in scopeProfile.ProfilerStatistics)
                                        {
                                            if (string.IsNullOrEmpty(stats.Group))
                                            {
                                                Assert.AreEqual("", stats.Group);
                                                Assert.AreEqual(2, stats.ExecutionCount);
                                                Assert.AreEqual(TimeSpan.FromMilliseconds(113), stats.TimeUsed);
                                            }
                                            else if (stats.Group.EndsWith("1"))
                                            {
                                                Assert.AreEqual("ServiceProfilerBasic1", stats.Group);
                                                Assert.AreEqual(1, stats.ExecutionCount);
                                                Assert.AreEqual(TimeSpan.FromMilliseconds(100), stats.TimeUsed);
                                            }
                                            else if (stats.Group.EndsWith("2"))
                                            {
                                                Assert.AreEqual("ServiceProfilerBasic2", stats.Group);
                                                Assert.AreEqual(1, stats.ExecutionCount);
                                                Assert.AreEqual(TimeSpan.FromMilliseconds(0), stats.TimeUsed);
                                            }
                                        }
                                    }

                                    AmbientClock.SkipAhead(TimeSpan.FromMilliseconds(100));

                                    _ServiceProfiler.Local?.SwitchSystem(null);

                                    AmbientClock.SkipAhead(TimeSpan.FromMilliseconds(113));

                                    _ServiceProfiler.Local?.SwitchSystem("ServiceProfilerBasic3");

                                    if (scopeProfile != null)
                                    {
                                        foreach (AmbientServiceProfilerAccumulator stats in scopeProfile.ProfilerStatistics)
                                        {
                                            if (string.IsNullOrEmpty(stats.Group))
                                            {
                                                Assert.AreEqual("", stats.Group);
                                                Assert.AreEqual(3, stats.ExecutionCount);
                                                Assert.AreEqual(TimeSpan.FromMilliseconds(226), stats.TimeUsed);
                                            }
                                            else if (stats.Group.EndsWith("1"))
                                            {
                                                Assert.AreEqual("ServiceProfilerBasic1", stats.Group);
                                                Assert.AreEqual(1, stats.ExecutionCount);
                                                Assert.AreEqual(TimeSpan.FromMilliseconds(100), stats.TimeUsed);
                                            }
                                            else if (stats.Group.EndsWith("2"))
                                            {
                                                Assert.AreEqual("ServiceProfilerBasic2", stats.Group);
                                                Assert.AreEqual(1, stats.ExecutionCount);
                                                Assert.AreEqual(TimeSpan.FromMilliseconds(100), stats.TimeUsed);
                                            }
                                            else if (stats.Group.EndsWith("3"))
                                            {
                                                Assert.AreEqual("ServiceProfilerBasic3", stats.Group);
                                                Assert.AreEqual(1, stats.ExecutionCount);
                                                Assert.AreEqual(TimeSpan.FromMilliseconds(0), stats.TimeUsed);
                                            }
                                        }
                                    }

                                    _ServiceProfiler.Local?.SwitchSystem(null);
                                }
        }