Startup.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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 MySql.Data.MySqlClient;
  17. using Npgsql;
  18. namespace Benchmarks
  19. {
  20. public class Startup
  21. {
  22. public Startup(IHostingEnvironment 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. 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>(MySqlClientFactory.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("Update"))
  79. {
  80. BatchUpdateString.Initalize();
  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
  94. .AddMvcCore()
  95. .AddControllersAsServices();
  96. if (Scenarios.MvcJson || Scenarios.Any("MvcDbSingle") || Scenarios.Any("MvcDbMulti"))
  97. {
  98. mvcBuilder.AddJsonFormatters();
  99. }
  100. if (Scenarios.MvcViews || Scenarios.Any("MvcDbFortunes"))
  101. {
  102. mvcBuilder
  103. .AddViews()
  104. .AddRazorViewEngine();
  105. }
  106. }
  107. }
  108. public void Configure(IApplicationBuilder app)
  109. {
  110. if (Scenarios.Plaintext)
  111. {
  112. app.UsePlainText();
  113. }
  114. if (Scenarios.Json)
  115. {
  116. app.UseJson();
  117. }
  118. if (Scenarios.Utf8Json)
  119. {
  120. app.UseUtf8Json();
  121. }
  122. // Single query endpoints
  123. if (Scenarios.DbSingleQueryRaw)
  124. {
  125. app.UseSingleQueryRaw();
  126. }
  127. if (Scenarios.DbSingleQueryDapper)
  128. {
  129. app.UseSingleQueryDapper();
  130. }
  131. if (Scenarios.DbSingleQueryEf)
  132. {
  133. app.UseSingleQueryEf();
  134. }
  135. // Multiple query endpoints
  136. if (Scenarios.DbMultiQueryRaw)
  137. {
  138. app.UseMultipleQueriesRaw();
  139. }
  140. if (Scenarios.DbMultiQueryDapper)
  141. {
  142. app.UseMultipleQueriesDapper();
  143. }
  144. if (Scenarios.DbMultiQueryEf)
  145. {
  146. app.UseMultipleQueriesEf();
  147. }
  148. // Multiple update endpoints
  149. if (Scenarios.DbMultiUpdateRaw)
  150. {
  151. app.UseMultipleUpdatesRaw();
  152. }
  153. if (Scenarios.DbMultiUpdateDapper)
  154. {
  155. app.UseMultipleUpdatesDapper();
  156. }
  157. if (Scenarios.DbMultiUpdateEf)
  158. {
  159. app.UseMultipleUpdatesEf();
  160. }
  161. // Fortunes endpoints
  162. if (Scenarios.DbFortunesRaw)
  163. {
  164. app.UseFortunesRaw();
  165. }
  166. if (Scenarios.DbFortunesDapper)
  167. {
  168. app.UseFortunesDapper();
  169. }
  170. if (Scenarios.DbFortunesEf)
  171. {
  172. app.UseFortunesEf();
  173. }
  174. if (Scenarios.Any("Mvc"))
  175. {
  176. app.UseMvc();
  177. }
  178. if (Scenarios.StaticFiles)
  179. {
  180. app.UseStaticFiles();
  181. }
  182. }
  183. }
  184. }