Startup.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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(IHostingEnvironment 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. Console.WriteLine($"Database: {appSettings.Database}");
  49. if (appSettings.Database == DatabaseServer.PostgreSql)
  50. {
  51. if (Scenarios.Any("Ef"))
  52. {
  53. services.AddDbContextPool<ApplicationDbContext>(options => options.UseNpgsql(appSettings.ConnectionString));
  54. }
  55. if (Scenarios.Any("Raw") || Scenarios.Any("Dapper"))
  56. {
  57. services.AddSingleton<DbProviderFactory>(NpgsqlFactory.Instance);
  58. }
  59. }
  60. else if (appSettings.Database == DatabaseServer.MySql)
  61. {
  62. if (Scenarios.Any("Raw") || Scenarios.Any("Dapper"))
  63. {
  64. services.AddSingleton<DbProviderFactory>(MySqlClientFactory.Instance);
  65. }
  66. }
  67. if (Scenarios.Any("Ef"))
  68. {
  69. services.AddScoped<EfDb>();
  70. }
  71. if (Scenarios.Any("Raw"))
  72. {
  73. services.AddScoped<RawDb>();
  74. }
  75. if (Scenarios.Any("Dapper"))
  76. {
  77. services.AddScoped<DapperDb>();
  78. }
  79. if (Scenarios.Any("Update"))
  80. {
  81. BatchUpdateString.Initalize();
  82. }
  83. if (Scenarios.Any("Fortunes"))
  84. {
  85. var settings = new TextEncoderSettings(UnicodeRanges.BasicLatin, UnicodeRanges.Katakana, UnicodeRanges.Hiragana);
  86. settings.AllowCharacter('\u2014'); // allow EM DASH through
  87. services.AddWebEncoders((options) =>
  88. {
  89. options.TextEncoderSettings = settings;
  90. });
  91. }
  92. if (Scenarios.Any("Mvc"))
  93. {
  94. var mvcBuilder = services
  95. .AddMvcCore()
  96. .SetCompatibilityVersion(CompatibilityVersion.Latest)
  97. ;
  98. if (Scenarios.MvcJson || Scenarios.Any("MvcDbSingle") || Scenarios.Any("MvcDbMulti"))
  99. {
  100. mvcBuilder.AddJsonFormatters();
  101. }
  102. if (Scenarios.MvcViews || Scenarios.Any("MvcDbFortunes"))
  103. {
  104. mvcBuilder
  105. .AddViews()
  106. .AddRazorViewEngine();
  107. }
  108. }
  109. }
  110. public void Configure(IApplicationBuilder app)
  111. {
  112. if (Scenarios.Plaintext)
  113. {
  114. app.UsePlainText();
  115. }
  116. if (Scenarios.Json)
  117. {
  118. app.UseJson();
  119. }
  120. if (Scenarios.Utf8Json)
  121. {
  122. app.UseUtf8Json();
  123. }
  124. if (Scenarios.SpanJson)
  125. {
  126. app.UseSpanJson();
  127. }
  128. // Fortunes endpoints
  129. if (Scenarios.DbFortunesRaw)
  130. {
  131. app.UseFortunesRaw();
  132. }
  133. if (Scenarios.DbFortunesDapper)
  134. {
  135. app.UseFortunesDapper();
  136. }
  137. if (Scenarios.DbFortunesEf)
  138. {
  139. app.UseFortunesEf();
  140. }
  141. // Single query endpoints
  142. if (Scenarios.DbSingleQueryRaw)
  143. {
  144. app.UseSingleQueryRaw();
  145. }
  146. if (Scenarios.DbSingleQueryDapper)
  147. {
  148. app.UseSingleQueryDapper();
  149. }
  150. if (Scenarios.DbSingleQueryEf)
  151. {
  152. app.UseSingleQueryEf();
  153. }
  154. // Multiple query endpoints
  155. if (Scenarios.DbMultiQueryRaw)
  156. {
  157. app.UseMultipleQueriesRaw();
  158. }
  159. if (Scenarios.DbMultiQueryDapper)
  160. {
  161. app.UseMultipleQueriesDapper();
  162. }
  163. if (Scenarios.DbMultiQueryEf)
  164. {
  165. app.UseMultipleQueriesEf();
  166. }
  167. // Multiple update endpoints
  168. if (Scenarios.DbMultiUpdateRaw)
  169. {
  170. app.UseMultipleUpdatesRaw();
  171. }
  172. if (Scenarios.DbMultiUpdateDapper)
  173. {
  174. app.UseMultipleUpdatesDapper();
  175. }
  176. if (Scenarios.DbMultiUpdateEf)
  177. {
  178. app.UseMultipleUpdatesEf();
  179. }
  180. if (Scenarios.Any("Mvc"))
  181. {
  182. app.UseMvc();
  183. }
  184. if (Scenarios.StaticFiles)
  185. {
  186. app.UseStaticFiles();
  187. }
  188. }
  189. }
  190. }