Startup.cs 6.2 KB

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