Startup.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. namespace Benchmarks
  18. {
  19. public class Startup
  20. {
  21. public Startup(IWebHostEnvironment 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. services.AddEntityFrameworkSqlServer();
  45. var appSettings = Configuration.Get<AppSettings>();
  46. BatchUpdateString.DatabaseServer = appSettings.Database;
  47. Console.WriteLine($"Database: {appSettings.Database}");
  48. if (appSettings.Database == DatabaseServer.PostgreSql)
  49. {
  50. if (Scenarios.Any("Ef"))
  51. {
  52. services.AddDbContextPool<ApplicationDbContext>(options => options.UseNpgsql(appSettings.ConnectionString));
  53. }
  54. if (Scenarios.Any("Raw") || Scenarios.Any("Dapper"))
  55. {
  56. services.AddSingleton<DbProviderFactory>(NpgsqlFactory.Instance);
  57. }
  58. }
  59. else if (appSettings.Database == DatabaseServer.MySql)
  60. {
  61. if (Scenarios.Any("Raw") || Scenarios.Any("Dapper"))
  62. {
  63. services.AddSingleton<DbProviderFactory>(MySqlConnectorFactory.Instance);
  64. }
  65. }
  66. if (Scenarios.Any("Ef"))
  67. {
  68. services.AddScoped<EfDb>();
  69. }
  70. if (Scenarios.Any("Raw"))
  71. {
  72. services.AddScoped<RawDb>();
  73. }
  74. if (Scenarios.Any("Dapper"))
  75. {
  76. services.AddScoped<DapperDb>();
  77. }
  78. if (Scenarios.Any("Fortunes"))
  79. {
  80. var settings = new TextEncoderSettings(UnicodeRanges.BasicLatin, UnicodeRanges.Katakana, UnicodeRanges.Hiragana);
  81. settings.AllowCharacter('\u2014'); // allow EM DASH through
  82. services.AddWebEncoders((options) =>
  83. {
  84. options.TextEncoderSettings = settings;
  85. });
  86. }
  87. if (Scenarios.Any("Mvc"))
  88. {
  89. var mvcBuilder = services.AddMvcCore();
  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. // Fortunes endpoints
  109. if (Scenarios.DbFortunesRaw)
  110. {
  111. app.UseFortunesRaw();
  112. }
  113. if (Scenarios.DbFortunesDapper)
  114. {
  115. app.UseFortunesDapper();
  116. }
  117. if (Scenarios.DbFortunesEf)
  118. {
  119. app.UseFortunesEf();
  120. }
  121. // Single query endpoints
  122. if (Scenarios.DbSingleQueryRaw)
  123. {
  124. app.UseSingleQueryRaw();
  125. }
  126. if (Scenarios.DbSingleQueryDapper)
  127. {
  128. app.UseSingleQueryDapper();
  129. }
  130. if (Scenarios.DbSingleQueryEf)
  131. {
  132. app.UseSingleQueryEf();
  133. }
  134. // Multiple query endpoints
  135. if (Scenarios.DbMultiQueryRaw)
  136. {
  137. app.UseMultipleQueriesRaw();
  138. }
  139. if (Scenarios.DbMultiQueryDapper)
  140. {
  141. app.UseMultipleQueriesDapper();
  142. }
  143. if (Scenarios.DbMultiQueryEf)
  144. {
  145. app.UseMultipleQueriesEf();
  146. }
  147. // Multiple update endpoints
  148. if (Scenarios.DbMultiUpdateRaw)
  149. {
  150. app.UseMultipleUpdatesRaw();
  151. }
  152. if (Scenarios.DbMultiUpdateDapper)
  153. {
  154. app.UseMultipleUpdatesDapper();
  155. }
  156. if (Scenarios.DbMultiUpdateEf)
  157. {
  158. app.UseMultipleUpdatesEf();
  159. }
  160. if (Scenarios.Any("Mvc"))
  161. {
  162. app.UseRouting();
  163. app.UseEndpoints(endpoints =>
  164. {
  165. endpoints.MapControllers();
  166. });
  167. }
  168. if (Scenarios.StaticFiles)
  169. {
  170. app.UseStaticFiles();
  171. }
  172. }
  173. }
  174. }