Program.cs 2.6 KB

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