Program.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. private System.Threading.Tasks.TaskCompletionSource<object> mComplete = new TaskCompletionSource<object>();
  71. public async virtual Task StartAsync(CancellationToken cancellationToken)
  72. {
  73. plaintextResult = new StringBytes(_helloWorldPayload);
  74. mApiServer = new HttpApiServer();
  75. mApiServer.Options.Port = 8080;
  76. mApiServer.Options.BufferPoolMaxMemory = 500;
  77. mApiServer.Options.MaxConnections = 100000;
  78. mApiServer.Options.Statistical = false;
  79. mApiServer.Options.LogLevel = BeetleX.EventArgs.LogType.Error;
  80. mApiServer.Options.LogToConsole = true;
  81. mApiServer.Register(typeof(Program).Assembly);
  82. HeaderTypeFactory.SERVAR_HEADER_BYTES = Encoding.ASCII.GetBytes("Server: TFB\r\n");
  83. mApiServer.HttpConnected += (o, e) =>
  84. {
  85. e.Session["DB"] = new RawDb(new ConcurrentRandom(), Npgsql.NpgsqlFactory.Instance);
  86. };
  87. mApiServer.Started += (o, e) =>
  88. {
  89. mComplete.TrySetResult(new object());
  90. };
  91. mApiServer.Open();
  92. RawDb._connectionString = "Server=tfb-database;Database=hello_world;User Id=benchmarkdbuser;Password=benchmarkdbpass;Maximum Pool Size=256;NoResetOnClose=true;Enlist=false;Max Auto Prepare=4;Multiplexing=true;Write Coalescing Delay Us=500;Write Coalescing Buffer Threshold Bytes=1000";
  93. //RawDb._connectionString = "Server=192.168.2.19;Database=hello_world;User Id=benchmarkdbuser;Password=benchmarkdbpass;Maximum Pool Size=256;NoResetOnClose=true;Enlist=false;Max Auto Prepare=3";
  94. await mComplete.Task;
  95. }
  96. public virtual Task StopAsync(CancellationToken cancellationToken)
  97. {
  98. mApiServer.BaseServer.Dispose();
  99. return Task.CompletedTask;
  100. }
  101. }
  102. public class JsonMessage
  103. {
  104. public string message { get; set; }
  105. }
  106. public class SpanJsonResult : ResultBase
  107. {
  108. public SpanJsonResult(object data)
  109. {
  110. Data = data;
  111. }
  112. public object Data { get; set; }
  113. public override IHeaderItem ContentType => ContentTypes.JSON;
  114. public override bool HasBody => true;
  115. public override void Write(PipeStream stream, HttpResponse response)
  116. {
  117. JsonSerializer.NonGeneric.Utf8.SerializeAsync(Data, stream);
  118. }
  119. }
  120. }