Program.cs 2.6 KB

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