HttpServer.cs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. using BeetleX.Light;
  2. using BeetleX.Light.Logs;
  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. private static NetServer<HttpNetApplication, HttpHandler> _apiServer;
  14. public virtual Task StartAsync(CancellationToken cancellationToken)
  15. {
  16. ThreadPool.SetMinThreads(Environment.ProcessorCount * 2, Environment.ProcessorCount * 2);
  17. Constants.MemorySegmentMinSize = 1024 * 8;
  18. Constants.MemorySegmentMaxSize = 1024 * 8;
  19. Constants.InitMemoryBlock();
  20. ArraySegment<byte> date = GMTDate.Default.DATE;
  21. _apiServer = new NetServer<HttpNetApplication, HttpHandler>();
  22. _apiServer.Options.LogLevel = BeetleX.Light.Logs.LogLevel.Error;
  23. _apiServer.Options.AddLogOutputHandler<LogOutputToConsole>();
  24. _apiServer.Options.SetDefaultListen(o =>
  25. {
  26. o.Port = 8080;
  27. });
  28. _apiServer.Start();
  29. if (!Program.UpDB)
  30. {
  31. RawDb._connectionString = "Server=tfb-database;Database=hello_world;User Id=benchmarkdbuser;Password=benchmarkdbpass;SSL Mode=Disable;Maximum Pool Size=64;NoResetOnClose=true;Enlist=false;Max Auto Prepare=4;Multiplexing=true;Write Coalescing Buffer Threshold Bytes=1000";
  32. //RawDb._connectionString = "Server=127.0.0.1;Database=hello_world;User Id=benchmarkdbuser;Password=benchmarkdbpass;Maximum Pool Size=256;NoResetOnClose=true;Enlist=false;Max Auto Prepare=3";
  33. }
  34. else
  35. {
  36. RawDb._connectionString = "Server=tfb-database;Database=hello_world;User Id=benchmarkdbuser;Password=benchmarkdbpass;SSL Mode=Disable;Maximum Pool Size=64;NoResetOnClose=true;Enlist=false;Max Auto Prepare=4;Multiplexing=true;Write Coalescing Buffer Threshold Bytes=1000";
  37. //RawDb._connectionString = "Server=127.0.0.1;Database=hello_world;User Id=benchmarkdbuser;Password=benchmarkdbpass;Maximum Pool Size=64;NoResetOnClose=true;Enlist=false;Max Auto Prepare=3";
  38. }
  39. // ApiServer.Log(LogType.Info, null, $"Debug mode [{Program.Debug}]");
  40. return Task.CompletedTask;
  41. }
  42. public virtual Task StopAsync(CancellationToken cancellationToken)
  43. {
  44. return Task.CompletedTask;
  45. }
  46. }
  47. }