Program.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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)
  34. {
  35. queries = queries < 1 ? 1 : queries > 500 ? 500 : queries;
  36. var result = await mPgsql.LoadMultipleQueriesRows(queries);
  37. return new SpanJsonResult(result);
  38. }
  39. public async Task<object> db()
  40. {
  41. var result = await mPgsql.LoadSingleQueryRow();
  42. return new SpanJsonResult(result);
  43. }
  44. public async Task<object> fortunes()
  45. {
  46. var data = await mPgsql.LoadFortunesRows();
  47. return new FortuneView(data);
  48. }
  49. private RawDb mPgsql;
  50. [NotAction]
  51. public void Init(HttpApiServer server, string path)
  52. {
  53. mPgsql = new RawDb(new ConcurrentRandom(), Npgsql.NpgsqlFactory.Instance);
  54. }
  55. }
  56. public class BeetleXHttpServer : IHostedService
  57. {
  58. private static readonly byte[] _helloWorldPayload = Encoding.UTF8.GetBytes("Hello, World!");
  59. public static StringBytes plaintextResult;
  60. private HttpApiServer mApiServer;
  61. public virtual Task StartAsync(CancellationToken cancellationToken)
  62. {
  63. plaintextResult = new StringBytes(_helloWorldPayload);
  64. mApiServer = new HttpApiServer();
  65. mApiServer.Options.Port = 8080;
  66. mApiServer.Options.BufferPoolMaxMemory = 500;
  67. mApiServer.Options.MaxConnections = 100000;
  68. mApiServer.Options.Statistical = false;
  69. mApiServer.Options.UrlIgnoreCase = false;
  70. mApiServer.Options.LogLevel = BeetleX.EventArgs.LogType.Error;
  71. mApiServer.Options.LogToConsole = true;
  72. mApiServer.Options.PrivateBufferPool = true;
  73. mApiServer.Register(typeof(Program).Assembly);
  74. mApiServer.Open();
  75. return Task.CompletedTask;
  76. }
  77. public virtual Task StopAsync(CancellationToken cancellationToken)
  78. {
  79. mApiServer.BaseServer.Dispose();
  80. return Task.CompletedTask;
  81. }
  82. }
  83. public class JsonMessage
  84. {
  85. public string message { get; set; }
  86. }
  87. public class SpanJsonResult : ResultBase
  88. {
  89. public SpanJsonResult(object data)
  90. {
  91. Data = data;
  92. }
  93. public object Data { get; set; }
  94. public override IHeaderItem ContentType => ContentTypes.JSON;
  95. public override bool HasBody => true;
  96. public override void Write(PipeStream stream, HttpResponse response)
  97. {
  98. JsonSerializer.NonGeneric.Utf8.SerializeAsync(Data, stream);
  99. }
  100. }
  101. }