HelloHandler.java 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package hello.home.handler;
  2. import hello.home.entity.*;
  3. import java.util.*;
  4. import java.util.concurrent.*;
  5. import com.techempower.cache.*;
  6. import com.techempower.gemini.*;
  7. import com.techempower.gemini.path.*;
  8. import com.techempower.gemini.path.annotation.*;
  9. /**
  10. * Handles the various framework benchmark request types.
  11. */
  12. public class HelloHandler
  13. extends MethodPathHandler<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. /**
  26. * Return "hello world" as a JSON-encoded message.
  27. */
  28. @PathSegment("json")
  29. @PathDefault
  30. public boolean helloworld()
  31. {
  32. return message("Hello, World!");
  33. }
  34. /**
  35. * Return a single World objects as JSON, selected randomly from the World
  36. * table. Assume the table has 10,000 rows.
  37. */
  38. @PathSegment
  39. public boolean db()
  40. {
  41. return json(store.get(World.class, ThreadLocalRandom.current().nextInt(DB_ROWS) + 1));
  42. }
  43. /**
  44. * Return a list of World objects as JSON, selected randomly from the World
  45. * table. Assume the table has 10,000 rows.
  46. */
  47. @PathSegment("query")
  48. public boolean multipleQueries()
  49. {
  50. final Random random = ThreadLocalRandom.current();
  51. final int queries = query().getInt("queries", 1, 1, 500);
  52. final World[] worlds = new World[queries];
  53. for (int i = 0; i < queries; i++)
  54. {
  55. worlds[i] = store.get(World.class, random.nextInt(DB_ROWS) + 1);
  56. }
  57. return json(worlds);
  58. }
  59. /**
  60. * Fetch the full list of Fortunes from the database, sort them by the
  61. * fortune message text, and then render the results to simple HTML using a
  62. * server-side template.
  63. */
  64. @PathSegment
  65. public boolean fortunes()
  66. {
  67. final List<Fortune> fortunes = store.list(Fortune.class);
  68. fortunes.add(new Fortune().setMessage("Additional fortune added at request time."));
  69. Collections.sort(fortunes);
  70. return mustache("fortunes", fortunes);
  71. }
  72. /**
  73. * Return a list of World objects as JSON, selected randomly from the World
  74. * table. For each row that is retrieved, that row will have its
  75. * randomNumber field updated and then the row will be persisted. We
  76. * assume the table has 10,000 rows.
  77. */
  78. @PathSegment
  79. public boolean update()
  80. {
  81. final Random random = ThreadLocalRandom.current();
  82. final int queries = query().getInt("queries", 1, 1, 500);
  83. final World[] worlds = new World[queries];
  84. for (int i = 0; i < queries; i++)
  85. {
  86. worlds[i] = store.get(World.class, random.nextInt(DB_ROWS) + 1);
  87. worlds[i].setRandomNumber(random.nextInt(DB_ROWS) + 1);
  88. }
  89. store.putAll(Arrays.asList(worlds));
  90. return json(worlds);
  91. }
  92. /**
  93. * Responds with a plaintext "Hello, World!"
  94. */
  95. @PathSegment
  96. public boolean plaintext()
  97. {
  98. return text("Hello, World!");
  99. }
  100. }