Program.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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.Register(typeof(Program).Assembly);
  60. mApiServer.HttpRequesting += OnRequesting;
  61. mApiServer.Open();
  62. return Task.CompletedTask;
  63. }
  64. public virtual Task StopAsync(CancellationToken cancellationToken)
  65. {
  66. mApiServer.BaseServer.Dispose();
  67. return Task.CompletedTask;
  68. }
  69. }
  70. public class JsonMessage
  71. {
  72. public string message { get; set; }
  73. }
  74. public class SpanJsonResult : ResultBase
  75. {
  76. public SpanJsonResult(object data)
  77. {
  78. Data = data;
  79. }
  80. public object Data { get; set; }
  81. public override IHeaderItem ContentType => ContentTypes.JSON;
  82. public override bool HasBody => true;
  83. public override void Write(PipeStream stream, HttpResponse response)
  84. {
  85. JsonSerializer.NonGeneric.Utf8.SerializeAsync(Data, stream);
  86. }
  87. }
  88. }