Startup.cs 6.2 KB

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