Startup.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. using System.Buffers;
  2. namespace AipGateway.AIP.Service
  3. {
  4. public class Startup
  5. {
  6. private bool EnableHack = false;
  7. public Startup(IConfiguration configuration)
  8. {
  9. Configuration = configuration;
  10. }
  11. public IConfiguration Configuration { get; }
  12. // This method gets called by the runtime. Use this method to add services to the container.
  13. public void ConfigureServices(IServiceCollection services)
  14. {
  15. services.AddControllers();
  16. services.AddHttpContextAccessor();
  17. }
  18. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
  19. public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
  20. {
  21. if (env.IsDevelopment())
  22. {
  23. app.UseDeveloperExceptionPage();
  24. }
  25. app.UseHttpsRedirection();
  26. app.UseRouting();
  27. app.UseAuthorization();
  28. app.UseEndpoints(endpoints =>
  29. {
  30. endpoints.MapControllers();
  31. endpoints.MapGet("/blobAsync", async context =>
  32. {
  33. // increase StreamPipeWriter buffer using reflection
  34. if (EnableHack)
  35. {
  36. HackPipeWriterBufferSize(context);
  37. }
  38. await WriteAsync(context);
  39. });
  40. endpoints.MapGet("/blobSync", context =>
  41. {
  42. // increase StreamPipeWriter buffer using reflection
  43. if (EnableHack)
  44. {
  45. HackPipeWriterBufferSize(context);
  46. }
  47. return Write(context);
  48. });
  49. });
  50. }
  51. private async Task WriteAsync(HttpContext context)
  52. {
  53. int writeSize = ParseBufferSize(context.Request.Query.TryGetValue("writeSize", out var qv) ? qv[0] : "65536");
  54. byte[] buffer = new byte[writeSize];
  55. var rnd = new Random();
  56. rnd.NextBytes(buffer);
  57. var rom = new ReadOnlyMemory<byte>(buffer);
  58. await context.Response.BodyWriter.WriteAsync(rom);
  59. //return Task.CompletedTask;
  60. }
  61. private Task Write(HttpContext context)
  62. {
  63. int writeSize = ParseBufferSize(context.Request.Query.TryGetValue("writeSize", out var qv) ? qv[0] : "65536");
  64. byte[] buffer = new byte[writeSize];
  65. var rnd = new Random();
  66. rnd.NextBytes(buffer);
  67. var rom = new ReadOnlySpan<byte>(buffer);
  68. context.Response.BodyWriter.Write(rom);
  69. return Task.CompletedTask;
  70. }
  71. private static void HackPipeWriterBufferSize(HttpContext context)
  72. {
  73. var bindingflags = System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic;
  74. var type = context.Response.BodyWriter.GetType();
  75. var field = type.GetField("_minimumBufferSize", bindingflags);
  76. field.SetValue(context.Response.BodyWriter, 65536);
  77. }
  78. public static int ParseBufferSize(string s)
  79. {
  80. var factor = 1;
  81. if (s.EndsWith("k", StringComparison.OrdinalIgnoreCase))
  82. {
  83. s = s.Substring(0, s.Length - 1);
  84. factor = 1024;
  85. }
  86. else if (s.EndsWith("M", StringComparison.OrdinalIgnoreCase))
  87. {
  88. s = s.Substring(0, s.Length - 1);
  89. factor = 1024 * 1024;
  90. }
  91. return Int32.Parse(s) * factor;
  92. }
  93. }
  94. }