Program.cs 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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.Runtime.InteropServices;
  5. using System.Threading.Tasks;
  6. using Microsoft.AspNetCore.Builder;
  7. using Microsoft.AspNetCore.Hosting;
  8. using Microsoft.Extensions.Configuration;
  9. #if DATABASE
  10. using Npgsql;
  11. #endif
  12. using PlatformExtensions;
  13. namespace PlatformBenchmarks
  14. {
  15. public class Program
  16. {
  17. public static async Task Main(string[] args)
  18. {
  19. Console.WriteLine("ASP.NET Core Platform Level");
  20. foreach (var path in BenchmarkApplication.Paths.Enabled)
  21. {
  22. Console.WriteLine(path);
  23. }
  24. var host = BuildWebHost(args);
  25. var config = (IConfiguration)host.Services.GetService(typeof(IConfiguration));
  26. BatchUpdateString.Database = config.Get<AppSettings>().Database;
  27. #if DATABASE
  28. await BenchmarkApplication.Db.PopulateCache();
  29. #endif
  30. await host.RunAsync();
  31. }
  32. public static IWebHost BuildWebHost(string[] args)
  33. {
  34. var config = new ConfigurationBuilder()
  35. .AddJsonFile("appsettings.json")
  36. .AddEnvironmentVariables()
  37. .AddEnvironmentVariables(prefix: "ASPNETCORE_")
  38. .AddCommandLine(args)
  39. .Build();
  40. var appSettings = config.Get<AppSettings>();
  41. #if DATABASE
  42. Console.WriteLine($"Database: {appSettings.Database}");
  43. Console.WriteLine($"ConnectionString: {appSettings.ConnectionString}");
  44. if (appSettings.Database == DatabaseServer.PostgreSql)
  45. {
  46. BenchmarkApplication.Db = new RawDb(new ConcurrentRandom(), appSettings);
  47. }
  48. else
  49. {
  50. throw new NotSupportedException($"{appSettings.Database} is not supported");
  51. }
  52. #endif
  53. var host = new WebHostBuilder()
  54. .UseSockets(options =>
  55. {
  56. options.WaitForDataBeforeAllocatingBuffer = false;
  57. if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
  58. {
  59. options.UnsafePreferInlineScheduling = Environment.GetEnvironmentVariable("DOTNET_SYSTEM_NET_SOCKETS_INLINE_COMPLETIONS") == "1";
  60. }
  61. })
  62. .UseKestrel((context, options) =>
  63. {
  64. var endPoint = context.Configuration.CreateIPEndPoint();
  65. options.Listen(endPoint, builder =>
  66. {
  67. builder.UseHttpApplication<BenchmarkApplication>();
  68. });
  69. })
  70. .UseStartup<Program>()
  71. .Build();
  72. return host;
  73. }
  74. public void Configure(IApplicationBuilder app)
  75. {
  76. }
  77. }
  78. }