Program.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. HeaderTypeFactory.SERVAR_HEADER_BYTES = Encoding.ASCII.GetBytes("Server: TFB\r\n");
  84. mApiServer.HttpConnected += (o, e) =>
  85. {
  86. e.Session["DB"] = new RawDb(new ConcurrentRandom(), Npgsql.NpgsqlFactory.Instance);
  87. };
  88. mApiServer.Open();
  89. RawDb._connectionString = "Server=tfb-database;Database=hello_world;User Id=benchmarkdbuser;Password=benchmarkdbpass;Maximum Pool Size=256;NoResetOnClose=true;Enlist=false;Max Auto Prepare=3";
  90. //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";
  91. System.Net.Http.HttpClient client = new System.Net.Http.HttpClient();
  92. var response = await client.GetAsync("http://localhost:8080/json");
  93. mApiServer.BaseServer.Log(LogType.Info, null, $"Get josn {response.StatusCode}");
  94. response = await client.GetAsync("http://localhost:8080/plaintext");
  95. mApiServer.BaseServer.Log(LogType.Info, null, $"Get plaintext {response.StatusCode}");
  96. }
  97. public virtual Task StopAsync(CancellationToken cancellationToken)
  98. {
  99. mApiServer.BaseServer.Dispose();
  100. return Task.CompletedTask;
  101. }
  102. }
  103. public class JsonMessage
  104. {
  105. public string message { get; set; }
  106. }
  107. public class SpanJsonResult : ResultBase
  108. {
  109. public SpanJsonResult(object data)
  110. {
  111. Data = data;
  112. }
  113. public object Data { get; set; }
  114. public override IHeaderItem ContentType => ContentTypes.JSON;
  115. public override bool HasBody => true;
  116. public override void Write(PipeStream stream, HttpResponse response)
  117. {
  118. JsonSerializer.NonGeneric.Utf8.SerializeAsync(Data, stream);
  119. }
  120. }
  121. }