HelloHandler.java 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. * Responds to the framework benchmarking requests for "hello, world" and
  11. * simple database queries.
  12. */
  13. public class HelloHandler
  14. extends BasicPathHandler<Context>
  15. {
  16. private static final int DB_ROWS = 10000;
  17. private final EntityStore store;
  18. /**
  19. * Constructor.
  20. */
  21. public HelloHandler(GeminiApplication app)
  22. {
  23. super(app, "hllo");
  24. this.store = app.getStore();
  25. }
  26. /**
  27. * Return "hello world" as a JSON-encoded message.
  28. */
  29. @PathDefault
  30. public boolean helloworld()
  31. {
  32. return message("Hello, World!");
  33. }
  34. /**
  35. * Return a list of World objects as JSON, selected randomly from the World
  36. * table. For consistency, we have assumed the table has 10,000 rows.
  37. */
  38. @PathSegment
  39. public boolean db()
  40. {
  41. final Random random = ThreadLocalRandom.current();
  42. final int queries = context().getInt("queries", 1, 1, 500);
  43. final World[] worlds = new World[queries];
  44. for (int i = 0; i < queries; i++)
  45. {
  46. worlds[i] = store.get(World.class, random.nextInt(DB_ROWS) + 1);
  47. }
  48. return json(worlds);
  49. }
  50. /**
  51. * Fetch the full list of Fortunes from the database, sort them by the
  52. * fortune message text, and then render the results to simple HTML using a
  53. * server-side template.
  54. */
  55. @PathSegment
  56. public boolean fortunes()
  57. {
  58. final List<Fortune> fortunes = store.list(Fortune.class);
  59. fortunes.add(new Fortune().setMessage("Additional fortune added at request time."));
  60. Collections.sort(fortunes);
  61. return mustache("fortunes", fortunes);
  62. }
  63. /**
  64. * Return a list of World objects as JSON, selected randomly from the World
  65. * table. For each row that is retrieved, that row will have it's randomNumber
  66. * field updated and persisted. For consistency, we have assumed the table has 10,000 rows.
  67. */
  68. @PathSegment
  69. public boolean update()
  70. {
  71. final Random random = ThreadLocalRandom.current();
  72. final int queries = context().getInt("queries", 1, 1, 500);
  73. final World[] worlds = new World[queries];
  74. for (int i = 0; i < queries; i++)
  75. {
  76. worlds[i] = store.get(World.class, random.nextInt(DB_ROWS) + 1);
  77. worlds[i].setRandomNumber(random.nextInt(DB_ROWS) + 1);
  78. }
  79. store.putAll(Arrays.asList(worlds));
  80. return json(worlds);
  81. }
  82. }