Program.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. Console.WriteLine($"Current directory: {Directory.GetCurrentDirectory()}");
  26. Console.WriteLine($"WebHostBuilder loading from: {typeof(WebHostBuilder).GetTypeInfo().Assembly.Location}");
  27. var config = new ConfigurationBuilder()
  28. .AddJsonFile("hosting.json", optional: true)
  29. .AddEnvironmentVariables(prefix: "ASPNETCORE_")
  30. .AddCommandLine(args)
  31. .Build();
  32. var webHostBuilder = new WebHostBuilder()
  33. .UseContentRoot(Directory.GetCurrentDirectory())
  34. .UseConfiguration(config)
  35. .UseStartup<Startup>()
  36. .ConfigureServices(services => services
  37. .AddSingleton(new ConsoleArgs(args))
  38. .AddSingleton<IScenariosConfiguration, ConsoleHostScenariosConfiguration>()
  39. .AddSingleton<Scenarios>()
  40. )
  41. .UseDefaultServiceProvider(
  42. (context, options) => options.ValidateScopes = context.HostingEnvironment.IsDevelopment())
  43. .UseKestrel();
  44. var threadCount = GetThreadCount(config);
  45. webHostBuilder.UseSockets(x =>
  46. {
  47. if (threadCount > 0)
  48. {
  49. x.IOQueueCount = threadCount;
  50. }
  51. Console.WriteLine($"Using Sockets with {x.IOQueueCount} threads");
  52. });
  53. var webHost = webHostBuilder.Build();
  54. Console.WriteLine($"Server GC is currently {(GCSettings.IsServerGC ? "ENABLED" : "DISABLED")}");
  55. webHost.Run();
  56. }
  57. private static int GetThreadCount(IConfigurationRoot config)
  58. {
  59. var threadCountValue = config["threadCount"];
  60. return threadCountValue == null ? -1 : int.Parse(threadCountValue);
  61. }
  62. }
  63. }