Program.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. public async Task<object> updates(int queries, IHttpContext context)
  55. {
  56. queries = queries < 1 ? 1 : queries > 500 ? 500 : queries;
  57. var result= await GetDB(context).LoadMultipleUpdatesRows(queries);
  58. return new SpanJsonResult(result);
  59. }
  60. [NotAction]
  61. public void Init(HttpApiServer server, string path)
  62. {
  63. }
  64. }
  65. public class BeetleXHttpServer : IHostedService
  66. {
  67. private static readonly byte[] _helloWorldPayload = Encoding.UTF8.GetBytes("Hello, World!");
  68. public static StringBytes plaintextResult;
  69. private HttpApiServer mApiServer;
  70. public async virtual Task StartAsync(CancellationToken cancellationToken)
  71. {
  72. plaintextResult = new StringBytes(_helloWorldPayload);
  73. mApiServer = new HttpApiServer();
  74. mApiServer.Options.Port = 8080;
  75. mApiServer.Options.BufferPoolMaxMemory = 500;
  76. mApiServer.Options.MaxConnections = 100000;
  77. mApiServer.Options.Statistical = false;
  78. mApiServer.Options.UrlIgnoreCase = false;
  79. mApiServer.Options.LogLevel = BeetleX.EventArgs.LogType.Error;
  80. mApiServer.Options.LogToConsole = true;
  81. mApiServer.Options.PrivateBufferPool = true;
  82. mApiServer.Register(typeof(Program).Assembly);
  83. mApiServer.HttpConnected += (o, e) =>
  84. {
  85. e.Session["DB"] = new RawDb(new ConcurrentRandom(), Npgsql.NpgsqlFactory.Instance);
  86. };
  87. mApiServer.Open();
  88. System.Net.Http.HttpClient client = new System.Net.Http.HttpClient();
  89. var response = await client.GetAsync("http://localhost:8080/json");
  90. mApiServer.BaseServer.Log(LogType.Info, null, $"Get josn {response.StatusCode}");
  91. response = await client.GetAsync("http://localhost:8080/plaintext");
  92. mApiServer.BaseServer.Log(LogType.Info, null, $"Get plaintext {response.StatusCode}");
  93. }
  94. public virtual Task StopAsync(CancellationToken cancellationToken)
  95. {
  96. mApiServer.BaseServer.Dispose();
  97. return Task.CompletedTask;
  98. }
  99. }
  100. public class JsonMessage
  101. {
  102. public string message { get; set; }
  103. }
  104. public class SpanJsonResult : ResultBase
  105. {
  106. public SpanJsonResult(object data)
  107. {
  108. Data = data;
  109. }
  110. public object Data { get; set; }
  111. public override IHeaderItem ContentType => ContentTypes.JSON;
  112. public override bool HasBody => true;
  113. public override void Write(PipeStream stream, HttpResponse response)
  114. {
  115. JsonSerializer.NonGeneric.Utf8.SerializeAsync(Data, stream);
  116. }
  117. }
  118. }