QueryResource.cs 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading.Tasks;
  4. using Microsoft.EntityFrameworkCore;
  5. using Benchmarks.Model;
  6. using GenHTTP.Modules.Webservices;
  7. namespace Benchmarks.Tests
  8. {
  9. public sealed class QueryResource
  10. {
  11. private static Random _Random = new Random();
  12. [ResourceMethod(":queries")]
  13. public ValueTask<List<World>> GetWorldsFromPath(string queries) => GetWorlds(queries);
  14. [ResourceMethod]
  15. public async ValueTask<List<World>> GetWorlds(string queries)
  16. {
  17. var count = 1;
  18. int.TryParse(queries, out count);
  19. if (count < 1) count = 1;
  20. else if (count > 500) count = 500;
  21. var result = new List<World>(count);
  22. using var context = DatabaseContext.CreateNoTracking();
  23. for (int _ = 0; _ < count; _++)
  24. {
  25. var id = _Random.Next(1, 10001);
  26. result.Add(await context.World.FirstOrDefaultAsync(w => w.Id == id));
  27. }
  28. return result;
  29. }
  30. }
  31. }