Program.cs 4.1 KB

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