Startup.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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.Data.Common;
  5. using System.Text.Encodings.Web;
  6. using System.Text.Unicode;
  7. using Benchmarks.Configuration;
  8. using Benchmarks.Data;
  9. using Benchmarks.Middleware;
  10. using Microsoft.AspNetCore.Builder;
  11. using Microsoft.AspNetCore.Hosting;
  12. using Microsoft.EntityFrameworkCore;
  13. using Microsoft.Extensions.Configuration;
  14. using Microsoft.Extensions.DependencyInjection;
  15. using MySql.Data.MySqlClient;
  16. using Npgsql;
  17. namespace Benchmarks
  18. {
  19. public class Startup
  20. {
  21. public Startup(IHostingEnvironment hostingEnv, Scenarios scenarios)
  22. {
  23. // Set up configuration sources.
  24. var builder = new ConfigurationBuilder()
  25. .SetBasePath(hostingEnv.ContentRootPath)
  26. .AddJsonFile("appsettings.json")
  27. .AddJsonFile($"appsettings.{hostingEnv.EnvironmentName}.json", optional: true)
  28. .AddEnvironmentVariables()
  29. .AddCommandLine(Program.Args)
  30. ;
  31. Configuration = builder.Build();
  32. Scenarios = scenarios;
  33. }
  34. public IConfigurationRoot Configuration { get; set; }
  35. public Scenarios Scenarios { get; }
  36. public void ConfigureServices(IServiceCollection services)
  37. {
  38. services.Configure<AppSettings>(Configuration);
  39. // We re-register the Scenarios as an instance singleton here to avoid it being created again due to the
  40. // registration done in Program.Main
  41. services.AddSingleton(Scenarios);
  42. // Common DB services
  43. services.AddSingleton<IRandom, DefaultRandom>();
  44. var appSettings = Configuration.Get<AppSettings>();
  45. Console.WriteLine($"Database: {appSettings.Database}");
  46. if (appSettings.Database == DatabaseServer.PostgreSql)
  47. {
  48. if (Scenarios.Any("Ef"))
  49. {
  50. services.AddDbContextPool<ApplicationDbContext>(options => options.UseNpgsql(appSettings.ConnectionString));
  51. }
  52. if (Scenarios.Any("Raw") || Scenarios.Any("Dapper"))
  53. {
  54. services.AddSingleton<DbProviderFactory>(NpgsqlFactory.Instance);
  55. }
  56. }
  57. else if (appSettings.Database == DatabaseServer.MySql)
  58. {
  59. if (Scenarios.Any("Raw") || Scenarios.Any("Dapper"))
  60. {
  61. services.AddSingleton<DbProviderFactory>(MySqlClientFactory.Instance);
  62. }
  63. }
  64. if (Scenarios.Any("Ef"))
  65. {
  66. services.AddScoped<EfDb>();
  67. }
  68. if (Scenarios.Any("Raw"))
  69. {
  70. services.AddScoped<RawDb>();
  71. }
  72. if (Scenarios.Any("Fortunes"))
  73. {
  74. var settings = new TextEncoderSettings(UnicodeRanges.BasicLatin, UnicodeRanges.Katakana, UnicodeRanges.Hiragana);
  75. settings.AllowCharacter('\u2014'); // allow EM DASH through
  76. services.AddWebEncoders((options) =>
  77. {
  78. options.TextEncoderSettings = settings;
  79. });
  80. }
  81. if (Scenarios.Any("Mvc"))
  82. {
  83. var mvcBuilder = services
  84. .AddMvcCore()
  85. .AddControllersAsServices();
  86. if (Scenarios.MvcJson || Scenarios.Any("MvcDbSingle") || Scenarios.Any("MvcDbMulti"))
  87. {
  88. mvcBuilder.AddJsonFormatters();
  89. }
  90. if (Scenarios.MvcViews || Scenarios.Any("MvcDbFortunes"))
  91. {
  92. mvcBuilder
  93. .AddViews()
  94. .AddRazorViewEngine();
  95. }
  96. }
  97. }
  98. public void Configure(IApplicationBuilder app)
  99. {
  100. if (Scenarios.Plaintext)
  101. {
  102. app.UsePlainText();
  103. }
  104. if (Scenarios.Json)
  105. {
  106. app.UseJson();
  107. }
  108. // Single query endpoints
  109. if (Scenarios.DbSingleQueryRaw)
  110. {
  111. app.UseSingleQueryRaw();
  112. }
  113. if (Scenarios.DbSingleQueryEf)
  114. {
  115. app.UseSingleQueryEf();
  116. }
  117. // Multiple query endpoints
  118. if (Scenarios.DbMultiQueryRaw)
  119. {
  120. app.UseMultipleQueriesRaw();
  121. }
  122. if (Scenarios.DbMultiQueryEf)
  123. {
  124. app.UseMultipleQueriesEf();
  125. }
  126. // Multiple update endpoints
  127. if (Scenarios.DbMultiUpdateRaw)
  128. {
  129. app.UseMultipleUpdatesRaw();
  130. }
  131. if (Scenarios.DbMultiUpdateEf)
  132. {
  133. app.UseMultipleUpdatesEf();
  134. }
  135. // Fortunes endpoints
  136. if (Scenarios.DbFortunesRaw)
  137. {
  138. app.UseFortunesRaw();
  139. }
  140. if (Scenarios.DbFortunesEf)
  141. {
  142. app.UseFortunesEf();
  143. }
  144. if (Scenarios.Any("Mvc"))
  145. {
  146. app.UseMvc();
  147. }
  148. if (Scenarios.StaticFiles)
  149. {
  150. app.UseStaticFiles();
  151. }
  152. }
  153. }
  154. }