public void UseTracerServiceName(string appName, string framework, string appAssembly)
        {
            var runner = new TestApplicationRunner(appName, framework, appAssembly, _output, enableTracer: true);

            // Set no service name through environment variables to force the tracer to use the value from the datadog.json file
            runner.ServiceName = null;

            using var agent = new MockDatadogAgent(_output);

            var infos = new List <(string ServiceName, string Environment, string Version)>();

            agent.ProfilerRequestReceived += (_, ctx) =>
            {
                infos.Add(ExtractServiceFromProfilerRequest(ctx.Value.Request));
            };

            runner.Run(agent);

            agent.NbCallsOnProfilingEndpoint.Should().BeGreaterThan(0);

            var distinctInfos = infos.Distinct().ToList();

            // There is a possible race condition:
            // if the profiler sends a sample before the tracer is initialized, it will use the wrong service name
            distinctInfos.Count.Should().BeInRange(1, 2);

            infos.Last().Should().Be(("BuggyBitsService", "BuggyBitsEnv", "BuggyBitsVersion"));
        }
        public void GetCpuSamplesIfCpuProfilerIsActivated(string appName, string framework, string appAssembly)
        {
            var runner = new TestApplicationRunner(appName, framework, appAssembly, _output, commandLine: CmdLine);

            runner.Environment.SetVariable(EnvironmentVariables.CpuProfilerEnabled, "1");
            CheckCpuProfiles(runner, true);
        }
Ejemplo n.º 3
0
        public void ThrowExceptionsInParallel(string appName, string framework, string appAssembly)
        {
            StackTrace expectedStack;

            if (framework == "net45")
            {
                expectedStack = new StackTrace(
                    new StackFrame("|lm:Samples.ExceptionGenerator |ns:Samples.ExceptionGenerator |ct:ParallelExceptionsScenario |fn:ThrowExceptions"),
                    new StackFrame("|lm:mscorlib |ns:System.Threading |ct:ThreadHelper |fn:ThreadStart"));
            }
            else
            {
                expectedStack = new StackTrace(
                    new StackFrame("|lm:Samples.ExceptionGenerator |ns:Samples.ExceptionGenerator |ct:ParallelExceptionsScenario |fn:ThrowExceptions"),
                    new StackFrame("|lm:System.Private.CoreLib |ns:System.Threading |ct:ThreadHelper |fn:ThreadStart"));
            }

            var runner = new TestApplicationRunner(appName, framework, appAssembly, _output, commandLine: Scenario2);

            runner.Environment.SetVariable(EnvironmentVariables.ExceptionProfilerEnabled, "1");
            runner.Environment.SetVariable(EnvironmentVariables.ExceptionSampleLimit, "10000");

            using var agent = new MockDatadogAgent(_output);

            runner.Run(agent);

            Assert.True(agent.NbCallsOnProfilingEndpoint > 0);

            var exceptionSamples = ExtractExceptionSamples(runner.Environment.PprofDir).ToArray();

            long total = 0;

            foreach (var sample in exceptionSamples)
            {
                total += sample.Count;
                sample.Type.Should().Be("System.Exception");
                sample.Message.Should().BeEmpty();
                sample.Stacktrace.Should().Be(expectedStack);
            }

            foreach (var file in Directory.GetFiles(runner.Environment.LogDir))
            {
                _output.WriteLine($"Log file: {file}");
            }

            var logFile = Directory.GetFiles(runner.Environment.LogDir)
                          .Single(f => Path.GetFileName(f).StartsWith("DD-DotNet-Profiler-Native-"));

            // Stackwalk will fail if the walltime profiler tries to inspect the thread at the same time as the exception profiler
            // This is expected so we remove those from the expected count
            var missedExceptions = File.ReadLines(logFile)
                                   .Count(l => l.Contains("Failed to walk stack for thrown exception: CORPROF_E_STACKSNAPSHOT_UNSAFE (80131360)"));

            int expectedExceptionCount = (4 * 1000) - missedExceptions;

            expectedExceptionCount.Should().BeGreaterThan(0, "only a few exceptions should be missed");

            total.Should().Be(expectedExceptionCount);
        }
Ejemplo n.º 4
0
        public void GetExceptionSamples(string appName, string framework, string appAssembly)
        {
            var runner = new TestApplicationRunner(appName, framework, appAssembly, _output, commandLine: Scenario1);

            runner.Environment.SetVariable(EnvironmentVariables.ExceptionProfilerEnabled, "1");

            CheckExceptionProfiles(runner);
        }
Ejemplo n.º 5
0
 public SmokeTestRunner(
     string appName,
     string framework,
     string appAssembly,
     string commandLine,
     ITestOutputHelper output)
 {
     _output = output;
     _testApplicationRunner = new TestApplicationRunner(appName, framework, appAssembly, output, commandLine);
 }
Ejemplo n.º 6
0
        public IEnumerable <IXunitTestCase> Discover(ITestFrameworkDiscoveryOptions discoveryOptions, ITestMethod testMethod, IAttributeInfo factAttribute)
        {
            var appName       = factAttribute.GetNamedArgument <string>("AppName");
            var appAssembly   = factAttribute.GetNamedArgument <string>("AppAssembly");
            var appFolderPath = TestApplicationRunner.GetApplicationOutputFolderPath(appName);

            MessageSink.OnMessage(new DiagnosticMessage("Discovering tests case in {0} for application {1}", appFolderPath, appName));

            var results = new List <IXunitTestCase>();

            if (!System.IO.Directory.Exists(appFolderPath))
            {
                results.Add(
                    new ExecutionErrorTestCase(
                        MessageSink,
                        TestMethodDisplay.Method,
                        TestMethodDisplayOptions.None,
                        testMethod,
                        $"Application folder path '{appFolderPath}' does not exist: try compiling application '{appName}' first."));
                return(results);
            }

            foreach (string folder in System.IO.Directory.GetDirectories(appFolderPath))
            {
                results.Add(
                    new XunitTestCase(
                        MessageSink,
                        TestMethodDisplay.Method,
                        TestMethodDisplayOptions.All,
                        testMethod,
                        new object[] { appName, System.IO.Path.GetFileName(folder), appAssembly }));
            }

            if (results.Count == 0)
            {
                results.Add(
                    new ExecutionErrorTestCase(
                        MessageSink,
                        TestMethodDisplay.Method,
                        TestMethodDisplayOptions.None,
                        testMethod,
                        $"Application '{appName}' does not have any test cases: try compiling the application '{appName}' first."));
            }

            return(results);
        }
Ejemplo n.º 7
0
        public void ExplicitlyDisableExceptionProfiler(string appName, string framework, string appAssembly)
        {
            var runner = new TestApplicationRunner(appName, framework, appAssembly, _output, commandLine: Scenario1);

            runner.Environment.SetVariable(EnvironmentVariables.ExceptionProfilerEnabled, "0");

            using var agent = new MockDatadogAgent(_output);

            runner.Run(agent);

            // On alpine, this check is flaky.
            // Disable it on alpine for now
            if (!EnvironmentHelper.IsAlpine)
            {
                Assert.True(agent.NbCallsOnProfilingEndpoint > 0);
            }

            ExtractExceptionSamples(runner.Environment.PprofDir).Should().BeEmpty();
        }
        private void CheckCpuProfiles(TestApplicationRunner runner, bool isEnabled)
        {
            using var agent = new MockDatadogAgent(_output);

            runner.Run(agent);

            Assert.True(agent.NbCallsOnProfilingEndpoint > 0);

            int cpuSamplesCount = 0;

            foreach (var file in Directory.EnumerateFiles(runner.Environment.PprofDir, "*.pprof", SearchOption.AllDirectories))
            {
                cpuSamplesCount += GetCpuSamplesCount(file);
            }

            if (isEnabled)
            {
                Assert.True(cpuSamplesCount > 0);
            }
            else
            {
                Assert.Equal(0, cpuSamplesCount);
            }
        }
        public void NoCpuSampleIfCpuProfilerIsNotActivatedByDefault(string appName, string framework, string appAssembly)
        {
            var runner = new TestApplicationRunner(appName, framework, appAssembly, _output, commandLine: CmdLine);

            CheckCpuProfiles(runner, false);
        }