RestService.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Text;
  5. using System.Threading;
  6. using FrameworkBench;
  7. using Revenj.Api;
  8. using Revenj.DatabasePersistence;
  9. using Revenj.DomainPatterns;
  10. using Revenj.Extensibility;
  11. using Revenj.Http;
  12. using Revenj.Serialization;
  13. using Revenj.Utility;
  14. namespace Revenj.Bench
  15. {
  16. [Controller("bench")]
  17. public class RestService
  18. {
  19. private static readonly ChunkedMemoryStream HelloWorld = ChunkedMemoryStream.Static();
  20. static RestService()
  21. {
  22. var hwText = Encoding.UTF8.GetBytes("Hello, World!");
  23. HelloWorld.Write(hwText, 0, hwText.Length);
  24. HelloWorld.Position = 0;
  25. }
  26. private readonly ThreadLocal<Context> Context;
  27. public RestService(IObjectFactory factory, IDatabaseQueryManager queryManager)
  28. {
  29. this.Context = new ThreadLocal<Context>(() => new Context(factory, queryManager));
  30. }
  31. [Route(HTTP.GET, "/plaintext", false)]
  32. public Stream PlainText(IResponseContext response)
  33. {
  34. response.ContentType = "text/plain";
  35. return HelloWorld;
  36. }
  37. [Route(HTTP.GET, "/json", false)]
  38. public Message JSON()
  39. {
  40. return new Message { message = "Hello, World!" };
  41. }
  42. [Route(HTTP.GET, "/db")]
  43. public World SingleQuery()
  44. {
  45. var ctx = Context.Value;
  46. var id = ctx.Random.Next(10000) + 1;
  47. return ctx.WorldRepository.Find(id);
  48. }
  49. //while IList<World> would work, it would fall back to IEnumerable<IJsonObject> which would create garbage
  50. [Route(HTTP.GET, "/queries/{count}")]
  51. public IList<IJsonObject> MultipleQueries(string count, IResponseContext response)
  52. {
  53. int repeat;
  54. int.TryParse(count, out repeat);
  55. if (repeat < 1) repeat = 1;
  56. else if (repeat > 500) repeat = 500;
  57. var ctx = Context.Value;
  58. ctx.LoadWorldsSlow(repeat, ctx.Worlds);
  59. return new ArraySegment<IJsonObject>(ctx.Worlds, 0, repeat);
  60. }
  61. private static readonly Comparison<World> ASC = (l, r) => l.id - r.id;
  62. [Route(HTTP.GET, "/updates/{count}")]
  63. public World[] Updates(string count)
  64. {
  65. int repeat;
  66. int.TryParse(count, out repeat);
  67. if (repeat < 1) repeat = 1;
  68. else if (repeat > 500) repeat = 500;
  69. var ctx = Context.Value;
  70. var result = new World[repeat];
  71. ctx.LoadWorldsSlow(repeat, result);
  72. for (int i = 0; i < result.Length; i++)
  73. result[i].randomNumber = ctx.Random.Next(10000) + 1;
  74. Array.Sort(result, ASC);
  75. ctx.WorldRepository.Update(result);
  76. return result;
  77. }
  78. private static readonly Comparison<KeyValuePair<int, string>> Comparison = (l, r) => string.Compare(l.Value, r.Value, StringComparison.Ordinal);
  79. [Route(HTTP.GET, "/fortunes")]
  80. public Fortunes Fortunes()
  81. {
  82. var ctx = Context.Value;
  83. var fortunes = ctx.FortuneRepository.Search();
  84. var list = new List<KeyValuePair<int, string>>(fortunes.Length + 1);
  85. foreach (var f in fortunes)
  86. list.Add(new KeyValuePair<int, string>(f.id, f.message));
  87. list.Add(new KeyValuePair<int, string>(0, "Additional fortune added at request time."));
  88. list.Sort(Comparison);
  89. return new Fortunes(list);
  90. }
  91. }
  92. }