Program.cs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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.Reflection;
  4. using System.Runtime;
  5. using Benchmarks.Configuration;
  6. namespace Benchmarks;
  7. public class Program
  8. {
  9. public static string[] Args;
  10. public static void Main(string[] args)
  11. {
  12. Args = args;
  13. Console.WriteLine();
  14. Console.WriteLine("ASP.NET Core Benchmarks");
  15. Console.WriteLine("-----------------------");
  16. Console.WriteLine($"Current directory: {Directory.GetCurrentDirectory()}");
  17. Console.WriteLine($"WebHostBuilder loading from: {typeof(WebHostBuilder).GetTypeInfo().Assembly.Location}");
  18. var config = new ConfigurationBuilder()
  19. .AddJsonFile("hosting.json", optional: true)
  20. .AddEnvironmentVariables(prefix: "ASPNETCORE_")
  21. .AddCommandLine(args)
  22. .Build();
  23. var webHostBuilder = new WebHostBuilder()
  24. .UseContentRoot(Directory.GetCurrentDirectory())
  25. .UseConfiguration(config)
  26. .UseStartup<Startup>()
  27. .ConfigureServices(services => services
  28. .AddSingleton(new ConsoleArgs(args))
  29. .AddSingleton<IScenariosConfiguration, ConsoleHostScenariosConfiguration>()
  30. .AddSingleton<Scenarios>()
  31. )
  32. .UseDefaultServiceProvider(
  33. (context, options) => options.ValidateScopes = context.HostingEnvironment.IsDevelopment())
  34. .UseKestrel();
  35. var threadCount = GetThreadCount(config);
  36. webHostBuilder.UseSockets(x =>
  37. {
  38. if (threadCount > 0)
  39. {
  40. x.IOQueueCount = threadCount;
  41. }
  42. Console.WriteLine($"Using Sockets with {x.IOQueueCount} threads");
  43. });
  44. var webHost = webHostBuilder.Build();
  45. Console.WriteLine($"Server GC is currently {(GCSettings.IsServerGC ? "ENABLED" : "DISABLED")}");
  46. webHost.Run();
  47. }
  48. private static int GetThreadCount(IConfigurationRoot config)
  49. {
  50. var threadCountValue = config["threadCount"];
  51. return threadCountValue == null ? -1 : int.Parse(threadCountValue);
  52. }
  53. }