Startup.cs 6.2 KB

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