HelloHandler.java 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. package hello.home.handler;
  2. import java.util.*;
  3. import java.util.concurrent.*;
  4. import com.techempower.cache.*;
  5. import com.techempower.gemini.*;
  6. import com.techempower.gemini.path.*;
  7. import com.techempower.gemini.path.annotation.*;
  8. import hello.home.entity.*;
  9. /**
  10. * Handles the various framework benchmark request types.
  11. */
  12. public class HelloHandler
  13. extends MethodSegmentHandler<Context>
  14. {
  15. private static final int DB_ROWS = 10000;
  16. private final EntityStore store;
  17. /**
  18. * Constructor.
  19. */
  20. public HelloHandler(GeminiApplication app)
  21. {
  22. super(app, "hllo");
  23. this.store = app.getStore();
  24. }
  25. @PathDefault
  26. public boolean test()
  27. {
  28. return false;
  29. }
  30. /**
  31. * Return "hello world" as a JSON-encoded message.
  32. */
  33. @PathSegment("json")
  34. public boolean helloworld()
  35. {
  36. final Map<String,String> resp = new HashMap<>(1);
  37. resp.put(GeminiConstants.GEMINI_MESSAGE, "Hello, World!");
  38. return json(resp);
  39. }
  40. /**
  41. * Return a single World objects as JSON, selected randomly from the World
  42. * table. Assume the table has 10,000 rows.
  43. */
  44. @PathSegment
  45. public boolean db()
  46. {
  47. return json(store.get(World.class, ThreadLocalRandom.current().nextInt(DB_ROWS) + 1));
  48. }
  49. /**
  50. * Return a list of World objects as JSON, selected randomly from the World
  51. * table. Assume the table has 10,000 rows.
  52. */
  53. @PathSegment("query")
  54. public boolean multipleQueries()
  55. {
  56. final Random random = ThreadLocalRandom.current();
  57. final int queries = query().getInt("queries", 1, 1, 500);
  58. final World[] worlds = new World[queries];
  59. for (int i = 0; i < queries; i++)
  60. {
  61. worlds[i] = store.get(World.class, random.nextInt(DB_ROWS) + 1);
  62. }
  63. return json(worlds);
  64. }
  65. /**
  66. * Return a list of World objects as JSON, selected randomly from the World
  67. * table. Assume the table has 10,000 rows.
  68. */
  69. @PathSegment("cached_query")
  70. public boolean multipleCachedQueries()
  71. {
  72. final Random random = ThreadLocalRandom.current();
  73. final int queries = query().getInt("queries", 1, 1, 500);
  74. final CachedWorld[] worlds = new CachedWorld[queries];
  75. for (int i = 0; i < queries; i++)
  76. {
  77. worlds[i] = store.get(CachedWorld.class, random.nextInt(DB_ROWS) + 1);
  78. }
  79. return json(worlds);
  80. }
  81. /**
  82. * Fetch the full list of Fortunes from the database, sort them by the
  83. * fortune message text, and then render the results to simple HTML using a
  84. * server-side template.
  85. */
  86. @PathSegment
  87. public boolean fortunes()
  88. {
  89. final List<Fortune> fortunes = store.list(Fortune.class);
  90. fortunes.add(new Fortune().setMessage("Additional fortune added at request time."));
  91. Collections.sort(fortunes);
  92. return mustache("fortunes", fortunes);
  93. }
  94. /**
  95. * Return a list of World objects as JSON, selected randomly from the World
  96. * table. For each row that is retrieved, that row will have its
  97. * randomNumber field updated and then the row will be persisted. We
  98. * assume the table has 10,000 rows.
  99. */
  100. @PathSegment
  101. public boolean update()
  102. {
  103. final Random random = ThreadLocalRandom.current();
  104. final int queries = query().getInt("queries", 1, 1, 500);
  105. final World[] worlds = new World[queries];
  106. for (int i = 0; i < queries; i++)
  107. {
  108. worlds[i] = store.get(World.class, random.nextInt(DB_ROWS) + 1);
  109. worlds[i].setRandomNumber(random.nextInt(DB_ROWS) + 1);
  110. }
  111. store.putAll(Arrays.asList(worlds));
  112. return json(worlds);
  113. }
  114. /**
  115. * Responds with a plaintext "Hello, World!"
  116. */
  117. @PathSegment
  118. public boolean plaintext()
  119. {
  120. return text("Hello, World!");
  121. }
  122. }