Program.cs 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // Copyright (c) .NET Foundation. All rights reserved.
  2. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
  3. using System;
  4. using System.IO;
  5. using System.Reflection;
  6. using System.Runtime;
  7. using System.Threading;
  8. using Benchmarks.Configuration;
  9. using Microsoft.AspNetCore.Hosting;
  10. using Microsoft.Extensions.Configuration;
  11. using Microsoft.Extensions.DependencyInjection;
  12. using Microsoft.Extensions.Hosting;
  13. using Microsoft.Extensions.Logging;
  14. namespace Benchmarks
  15. {
  16. public class Program
  17. {
  18. public static string[] Args;
  19. public static void Main(string[] args)
  20. {
  21. Args = args;
  22. Console.WriteLine();
  23. Console.WriteLine("ASP.NET Core Benchmarks");
  24. Console.WriteLine("-----------------------");
  25. bool isMono = typeof(object).Assembly.GetType("Mono.RuntimeStructs") != null;
  26. Console.WriteLine("Runtime " + (isMono ? "Mono" : "CoreCLR"));
  27. Console.WriteLine(typeof(object).Assembly.FullName);
  28. Console.WriteLine(System.Reflection.Assembly.GetEntryAssembly());
  29. Console.WriteLine(System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription);
  30. Console.WriteLine($"Current directory: {Directory.GetCurrentDirectory()}");
  31. Console.WriteLine($"WebHostBuilder loading from: {typeof(WebHostBuilder).GetTypeInfo().Assembly.Location}");
  32. var config = new ConfigurationBuilder()
  33. .AddJsonFile("hosting.json", optional: true)
  34. .AddEnvironmentVariables(prefix: "ASPNETCORE_")
  35. .AddCommandLine(args)
  36. .Build();
  37. var webHostBuilder = new WebHostBuilder()
  38. .UseContentRoot(Directory.GetCurrentDirectory())
  39. .UseConfiguration(config)
  40. .UseStartup<Startup>()
  41. .ConfigureServices(services => services
  42. .AddSingleton(new ConsoleArgs(args))
  43. .AddSingleton<IScenariosConfiguration, ConsoleHostScenariosConfiguration>()
  44. .AddSingleton<Scenarios>()
  45. )
  46. .UseDefaultServiceProvider(
  47. (context, options) => options.ValidateScopes = context.HostingEnvironment.IsDevelopment())
  48. .UseKestrel();
  49. var threadCount = GetThreadCount(config);
  50. webHostBuilder.UseSockets(x =>
  51. {
  52. if (threadCount > 0)
  53. {
  54. x.IOQueueCount = threadCount;
  55. }
  56. Console.WriteLine($"Using Sockets with {x.IOQueueCount} threads");
  57. });
  58. var webHost = webHostBuilder.Build();
  59. Console.WriteLine($"Server GC is currently {(GCSettings.IsServerGC ? "ENABLED" : "DISABLED")}");
  60. webHost.Run();
  61. }
  62. private static int GetThreadCount(IConfigurationRoot config)
  63. {
  64. var threadCountValue = config["threadCount"];
  65. return threadCountValue == null ? -1 : int.Parse(threadCountValue);
  66. }
  67. }
  68. }