Program.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. using System.Collections.Generic;
  11. namespace Benchmarks
  12. {
  13. [Controller]
  14. class Program : IController
  15. {
  16. public static void Main(string[] args)
  17. {
  18. var builder = new HostBuilder()
  19. .ConfigureServices((hostContext, services) =>
  20. {
  21. services.AddHostedService<BeetleXHttpServer>();
  22. });
  23. builder.Build().Run();
  24. }
  25. public object plaintext()
  26. {
  27. return BeetleXHttpServer.plaintextResult;
  28. }
  29. public object json()
  30. {
  31. return new SpanJsonResult(new JsonMessage { message = "Hello, World!" });
  32. }
  33. public async Task<object> queries(int queries, IHttpContext context)
  34. {
  35. queries = queries < 1 ? 1 : queries > 500 ? 500 : queries;
  36. var result = await GetDB(context).LoadMultipleQueriesRows(queries);
  37. return new SpanJsonResult(result);
  38. }
  39. public RawDb GetDB(IHttpContext context)
  40. {
  41. return (RawDb)context.Session["DB"];
  42. }
  43. public async Task<object> db(IHttpContext context)
  44. {
  45. var result = await GetDB(context).LoadSingleQueryRow();
  46. return new SpanJsonResult(result);
  47. }
  48. public async Task<object> fortunes(IHttpContext context)
  49. {
  50. var data = await GetDB(context).LoadFortunesRows();
  51. return new FortuneView(data);
  52. }
  53. [NotAction]
  54. public void Init(HttpApiServer server, string path)
  55. {
  56. }
  57. }
  58. public class BeetleXHttpServer : IHostedService
  59. {
  60. private static readonly byte[] _helloWorldPayload = Encoding.UTF8.GetBytes("Hello, World!");
  61. public static StringBytes plaintextResult;
  62. private HttpApiServer mApiServer;
  63. public virtual Task StartAsync(CancellationToken cancellationToken)
  64. {
  65. plaintextResult = new StringBytes(_helloWorldPayload);
  66. mApiServer = new HttpApiServer();
  67. mApiServer.Options.Port = 8080;
  68. mApiServer.Options.BufferPoolMaxMemory = 500;
  69. mApiServer.Options.MaxConnections = 100000;
  70. mApiServer.Options.Statistical = false;
  71. mApiServer.Options.UrlIgnoreCase = false;
  72. mApiServer.Options.LogLevel = BeetleX.EventArgs.LogType.Error;
  73. mApiServer.Options.LogToConsole = true;
  74. mApiServer.Options.PrivateBufferPool = true;
  75. mApiServer.Register(typeof(Program).Assembly);
  76. mApiServer.HttpConnected += (o, e) => {
  77. e.Session["DB"] = new RawDb(new ConcurrentRandom(), Npgsql.NpgsqlFactory.Instance);
  78. };
  79. mApiServer.Open();
  80. return Task.CompletedTask;
  81. }
  82. public virtual Task StopAsync(CancellationToken cancellationToken)
  83. {
  84. mApiServer.BaseServer.Dispose();
  85. return Task.CompletedTask;
  86. }
  87. }
  88. public class JsonMessage
  89. {
  90. public string message { get; set; }
  91. }
  92. public class SpanJsonResult : ResultBase
  93. {
  94. public SpanJsonResult(object data)
  95. {
  96. Data = data;
  97. }
  98. public object Data { get; set; }
  99. public override IHeaderItem ContentType => ContentTypes.JSON;
  100. public override bool HasBody => true;
  101. public override void Write(PipeStream stream, HttpResponse response)
  102. {
  103. JsonSerializer.NonGeneric.Utf8.SerializeAsync(Data, stream);
  104. }
  105. }
  106. }