Program.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using BeetleX.FastHttpApi;
  2. using Microsoft.Extensions.Hosting;
  3. using System.Threading.Tasks;
  4. using Microsoft.Extensions.DependencyInjection;
  5. using System;
  6. using System.Threading;
  7. using System.Text;
  8. using BeetleX.Buffers;
  9. using SpanJson;
  10. namespace Benchmarks
  11. {
  12. [Controller]
  13. class Program
  14. {
  15. public static void Main(string[] args)
  16. {
  17. var builder = new HostBuilder()
  18. .ConfigureServices((hostContext, services) =>
  19. {
  20. services.AddHostedService<BeetleXHttpServer>();
  21. });
  22. builder.Build().Run();
  23. }
  24. }
  25. public class BeetleXHttpServer : IHostedService
  26. {
  27. private static readonly byte[] _helloWorldPayload = Encoding.UTF8.GetBytes("Hello, World!");
  28. public static StringBytes plaintextResult;
  29. private HttpApiServer mApiServer;
  30. public void OnRequesting(object sender, EventHttpRequestArgs e)
  31. {
  32. if (e.Request.BaseUrl == "/plaintext")
  33. {
  34. e.Response.Result(plaintextResult);
  35. }
  36. else if (e.Request.BaseUrl == "/json")
  37. {
  38. var json = new SpanJsonResult(new JsonMessage { message = "Hello, World!" });
  39. e.Response.Result(json);
  40. }
  41. else
  42. {
  43. e.Response.Result(new NotFoundResult("url not found!"));
  44. }
  45. e.Cancel = true;
  46. }
  47. public virtual Task StartAsync(CancellationToken cancellationToken)
  48. {
  49. plaintextResult = new StringBytes(_helloWorldPayload);
  50. mApiServer = new HttpApiServer();
  51. mApiServer.Options.Port = 8080;
  52. mApiServer.Options.BufferPoolMaxMemory = 500;
  53. mApiServer.Options.MaxConnections = 100000;
  54. mApiServer.Options.Statistical = false;
  55. mApiServer.Options.UrlIgnoreCase = false;
  56. mApiServer.Options.LogLevel = BeetleX.EventArgs.LogType.Off;
  57. mApiServer.Options.LogToConsole = true;
  58. mApiServer.Options.PrivateBufferPool = true;
  59. mApiServer.Options.IOQueueEnabled = true;
  60. mApiServer.Register(typeof(Program).Assembly);
  61. mApiServer.HttpRequesting += OnRequesting;
  62. mApiServer.Open();
  63. return Task.CompletedTask;
  64. }
  65. public virtual Task StopAsync(CancellationToken cancellationToken)
  66. {
  67. mApiServer.BaseServer.Dispose();
  68. return Task.CompletedTask;
  69. }
  70. }
  71. public class JsonMessage
  72. {
  73. public string message { get; set; }
  74. }
  75. public class SpanJsonResult : ResultBase
  76. {
  77. public SpanJsonResult(object data)
  78. {
  79. Data = data;
  80. }
  81. public object Data { get; set; }
  82. public override IHeaderItem ContentType => ContentTypes.JSON;
  83. public override bool HasBody => true;
  84. public override void Write(PipeStream stream, HttpResponse response)
  85. {
  86. JsonSerializer.NonGeneric.Utf8.SerializeAsync(Data, stream);
  87. }
  88. }
  89. }