HttpServer.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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 static string _connectionString;
  15. public virtual async Task StartAsync(CancellationToken cancellationToken)
  16. {
  17. _connectionString = "Server=tfb-database;Database=hello_world;User Id=benchmarkdbuser;Password=benchmarkdbpass;SSL Mode=Disable;Maximum Pool Size=16;NoResetOnClose=true;Enlist=false;Max Auto Prepare=4;Multiplexing=true;Write Coalescing Buffer Threshold Bytes=1000";
  18. //_connectionString = "Server=localhost;Database=hello_world;User Id=benchmarkdbuser;Password=benchmarkdbpass;SSL Mode=Disable;Maximum Pool Size=16;NoResetOnClose=true;Enlist=false;Max Auto Prepare=4;Multiplexing=true;Write Coalescing Buffer Threshold Bytes=1000";
  19. ThreadPool.SetMinThreads(Environment.ProcessorCount * 2, Environment.ProcessorCount * 2);
  20. Constants.MemorySegmentMinSize = 1024 * 16;
  21. Constants.MemorySegmentMaxSize = 1024 * 16;
  22. Constants.InitMemoryBlock();
  23. var date = GMTDate.Default.DATE;
  24. HttpHandler.DB = new RawDb(new ConcurrentRandom(), HttpServer._connectionString);
  25. await HttpHandler.DB.PopulateCache();
  26. _apiServer = new NetServer<HttpNetApplication, HttpHandler>();
  27. _apiServer.Options.LogLevel = BeetleX.Light.Logs.LogLevel.Error;
  28. _apiServer.Options.AddLogOutputHandler<LogOutputToConsole>();
  29. _apiServer.Options.SetDefaultListen(o =>
  30. {
  31. o.Port = 8080;
  32. });
  33. _apiServer.Start();
  34. }
  35. public virtual Task StopAsync(CancellationToken cancellationToken)
  36. {
  37. return Task.CompletedTask;
  38. }
  39. }
  40. }