Program.cs 3.3 KB

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