HttpServer.cs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. using BeetleX;
  2. using BeetleX.EventArgs;
  3. using Microsoft.Extensions.Hosting;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Text;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. namespace PlatformBenchmarks
  10. {
  11. public class HttpServer : IHostedService
  12. {
  13. public static IServer ApiServer;
  14. public virtual Task StartAsync(CancellationToken cancellationToken)
  15. {
  16. ArraySegment<byte> date = GMTDate.Default.DATE;
  17. ServerOptions serverOptions = new ServerOptions();
  18. serverOptions.LogLevel = LogType.Error;
  19. serverOptions.DefaultListen.Port = 8080;
  20. serverOptions.Statistical = false;
  21. serverOptions.BufferSize = 1024 * 8;
  22. serverOptions.BufferPoolMaxMemory = 1000;
  23. serverOptions.BufferPoolSize = 1024 * 10;
  24. if (Program.Debug)
  25. {
  26. serverOptions.IOQueueEnabled = true;
  27. serverOptions.IOQueues = System.Math.Min(Environment.ProcessorCount, 16);
  28. serverOptions.SyncAccept = false;
  29. }
  30. ApiServer = SocketFactory.CreateTcpServer<HttpHandler>(serverOptions);
  31. ApiServer.Open();
  32. if (!Program.UpDB)
  33. {
  34. 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";
  35. //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";
  36. }
  37. else
  38. {
  39. // RawDb._connectionString = "Server=192.168.2.19;Database=hello_world;User Id=benchmarkdbuser;Password=benchmarkdbpass;Maximum Pool Size=64;NoResetOnClose=true;Enlist=false;Max Auto Prepare=3";
  40. RawDb._connectionString = "Server=tfb-database;Database=hello_world;User Id=benchmarkdbuser;Password=benchmarkdbpass;Maximum Pool Size=64;NoResetOnClose=true;Enlist=false;Max Auto Prepare=3;Multiplexing=true;Write Coalescing Delay Us=500;Write Coalescing Buffer Threshold Bytes=1000";
  41. }
  42. ApiServer.Log(LogType.Info, null, $"Debug mode [{Program.Debug}]");
  43. return Task.CompletedTask;
  44. }
  45. public virtual Task StopAsync(CancellationToken cancellationToken)
  46. {
  47. ApiServer.Dispose();
  48. return Task.CompletedTask;
  49. }
  50. }
  51. }