Startup.cs 5.4 KB

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