From f39ae0997a5e80b36841de4a812200974107d074 Mon Sep 17 00:00:00 2001 From: Steve MacLean Date: Thu, 29 Aug 2019 18:52:50 -0400 Subject: [PATCH] Add DiagnosticScenarios Add sample debug target for use in C# Guide diagnostics scenarios. --- .../Controllers/DiagnosticScenarios.cs | 162 ++++++++++++++++++ .../Controllers/ValuesController.cs | 45 +++++ .../DiagnosticScenarios.csproj | 11 ++ .../DiagnosticScenarios/Program.cs | 26 +++ .../Properties/launchSettings.json | 30 ++++ .../DiagnosticScenarios/Startup.cs | 57 ++++++ .../appsettings.Development.json | 9 + .../DiagnosticScenarios/appsettings.json | 10 ++ 8 files changed, 350 insertions(+) create mode 100644 core/diagnostics/DiagnosticScenarios/Controllers/DiagnosticScenarios.cs create mode 100644 core/diagnostics/DiagnosticScenarios/Controllers/ValuesController.cs create mode 100644 core/diagnostics/DiagnosticScenarios/DiagnosticScenarios.csproj create mode 100644 core/diagnostics/DiagnosticScenarios/Program.cs create mode 100644 core/diagnostics/DiagnosticScenarios/Properties/launchSettings.json create mode 100644 core/diagnostics/DiagnosticScenarios/Startup.cs create mode 100644 core/diagnostics/DiagnosticScenarios/appsettings.Development.json create mode 100644 core/diagnostics/DiagnosticScenarios/appsettings.json diff --git a/core/diagnostics/DiagnosticScenarios/Controllers/DiagnosticScenarios.cs b/core/diagnostics/DiagnosticScenarios/Controllers/DiagnosticScenarios.cs new file mode 100644 index 00000000000..472e8bf69ac --- /dev/null +++ b/core/diagnostics/DiagnosticScenarios/Controllers/DiagnosticScenarios.cs @@ -0,0 +1,162 @@ +using System; +using System.Diagnostics; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Mvc; +using System.Threading; + +namespace testwebapi.Controllers +{ + [Route("api/[controller]")] + [ApiController] + public class DiagScenarioController : ControllerBase + { + object o1 = new object(); + object o2 = new object(); + + private static Processor p = new Processor(); + + [HttpGet] + [Route("deadlock/")] + public ActionResult deadlock() + { + (new System.Threading.Thread(() => { + DeadlockFunc(); + })).Start(); + + Thread.Sleep(5000); + + Thread[] threads = new Thread[300]; + for(int i=0; i<300;i++) + { + (threads[i] = new Thread(() => { + lock (o1) {Thread.Sleep(100);} + })).Start(); + } + + foreach(Thread thread in threads) + { + thread.Join(); + } + + return "success:deadlock"; + } + + private void DeadlockFunc() + { + lock (o1) + { + (new Thread(() => { + lock (o2) { Monitor.Enter(o1); } + })).Start(); + + Thread.Sleep(2000); + Monitor.Enter(o2); + } + } + + [HttpGet] + [Route("memspike/{seconds}")] + public ActionResult memspike(int seconds) + { + Stopwatch watch=new Stopwatch(); + watch.Start(); + + while(true) + { + p = new Processor(); + watch.Stop(); + if(watch.ElapsedMilliseconds > seconds*1000) + break; + watch.Start(); + + int it = (200000*1000) / 100; + for(int i=0; i memleak(int kb) + { + int it = (kb*1000) / 100; + for(int i=0; i exception() + { + throw new Exception("bad, bad code"); + } + + + [HttpGet] + [Route("highcpu/{milliseconds}")] + public ActionResult highcpu(int milliseconds) + { + Stopwatch watch=new Stopwatch(); + watch.Start(); + + while (true) + { + watch.Stop(); + if(watch.ElapsedMilliseconds > milliseconds) + break; + watch.Start(); + } + + return "success:highcpu"; + } + + } + + class Customer + { + private string id; + + public Customer(string id) + { + this.id = id; + } + } + + class CustomerCache + { + private List cache = new List(); + + public void AddCustomer(Customer c) + { + cache.Add(c); + } + } + + class Processor + { + private CustomerCache cache = new CustomerCache(); + + public void ProcessTransaction(Customer customer) + { + cache.AddCustomer(customer); + } + } +} diff --git a/core/diagnostics/DiagnosticScenarios/Controllers/ValuesController.cs b/core/diagnostics/DiagnosticScenarios/Controllers/ValuesController.cs new file mode 100644 index 00000000000..c38be03dc86 --- /dev/null +++ b/core/diagnostics/DiagnosticScenarios/Controllers/ValuesController.cs @@ -0,0 +1,45 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Mvc; + +namespace DiagnosticScenarios.Controllers +{ + [Route("api/[controller]")] + [ApiController] + public class ValuesController : ControllerBase + { + // GET api/values + [HttpGet] + public ActionResult> Get() + { + return new string[] { "value1", "value2" }; + } + + // GET api/values/5 + [HttpGet("{id}")] + public ActionResult Get(int id) + { + return "value"; + } + + // POST api/values + [HttpPost] + public void Post([FromBody] string value) + { + } + + // PUT api/values/5 + [HttpPut("{id}")] + public void Put(int id, [FromBody] string value) + { + } + + // DELETE api/values/5 + [HttpDelete("{id}")] + public void Delete(int id) + { + } + } +} diff --git a/core/diagnostics/DiagnosticScenarios/DiagnosticScenarios.csproj b/core/diagnostics/DiagnosticScenarios/DiagnosticScenarios.csproj new file mode 100644 index 00000000000..4fa8377c237 --- /dev/null +++ b/core/diagnostics/DiagnosticScenarios/DiagnosticScenarios.csproj @@ -0,0 +1,11 @@ + + + + netcoreapp3.0 + + + + + + + diff --git a/core/diagnostics/DiagnosticScenarios/Program.cs b/core/diagnostics/DiagnosticScenarios/Program.cs new file mode 100644 index 00000000000..9269655239d --- /dev/null +++ b/core/diagnostics/DiagnosticScenarios/Program.cs @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; + +namespace DiagnosticScenarios +{ + public class Program + { + public static void Main(string[] args) + { + CreateHostBuilder(args).Build().Run(); + } + + public static IHostBuilder CreateHostBuilder(string[] args) => + Host.CreateDefaultBuilder(args) + .ConfigureWebHostDefaults(webBuilder => + { + webBuilder.UseStartup(); + }); + } +} diff --git a/core/diagnostics/DiagnosticScenarios/Properties/launchSettings.json b/core/diagnostics/DiagnosticScenarios/Properties/launchSettings.json new file mode 100644 index 00000000000..ce8acd42589 --- /dev/null +++ b/core/diagnostics/DiagnosticScenarios/Properties/launchSettings.json @@ -0,0 +1,30 @@ +{ + "$schema": "http://json.schemastore.org/launchsettings.json", + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:26127", + "sslPort": 44359 + } + }, + "profiles": { + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "launchUrl": "api/values", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "DiagnosticScenarios": { + "commandName": "Project", + "launchBrowser": true, + "launchUrl": "api/values", + "applicationUrl": "https://localhost:5001;http://localhost:5000", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} \ No newline at end of file diff --git a/core/diagnostics/DiagnosticScenarios/Startup.cs b/core/diagnostics/DiagnosticScenarios/Startup.cs new file mode 100644 index 00000000000..3fdcd61484e --- /dev/null +++ b/core/diagnostics/DiagnosticScenarios/Startup.cs @@ -0,0 +1,57 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.HttpsPolicy; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; + +namespace DiagnosticScenarios +{ + public class Startup + { + public Startup(IConfiguration configuration) + { + Configuration = configuration; + } + + public IConfiguration Configuration { get; } + + // This method gets called by the runtime. Use this method to add services to the container. + public void ConfigureServices(IServiceCollection services) + { + services.AddControllers() + .AddNewtonsoftJson(); + } + + // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. + public void Configure(IApplicationBuilder app, IWebHostEnvironment env) + { + if (env.IsDevelopment()) + { + app.UseDeveloperExceptionPage(); + } + else + { + // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. + app.UseHsts(); + } + + app.UseHttpsRedirection(); + + app.UseRouting(); + + app.UseAuthorization(); + + app.UseEndpoints(endpoints => + { + endpoints.MapControllers(); + }); + } + } +} diff --git a/core/diagnostics/DiagnosticScenarios/appsettings.Development.json b/core/diagnostics/DiagnosticScenarios/appsettings.Development.json new file mode 100644 index 00000000000..e203e9407e7 --- /dev/null +++ b/core/diagnostics/DiagnosticScenarios/appsettings.Development.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Debug", + "System": "Information", + "Microsoft": "Information" + } + } +} diff --git a/core/diagnostics/DiagnosticScenarios/appsettings.json b/core/diagnostics/DiagnosticScenarios/appsettings.json new file mode 100644 index 00000000000..d9d9a9bff6f --- /dev/null +++ b/core/diagnostics/DiagnosticScenarios/appsettings.json @@ -0,0 +1,10 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft": "Warning", + "Microsoft.Hosting.Lifetime": "Information" + } + }, + "AllowedHosts": "*" +}