HelloHandler.java 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package hello.home.handler;
  2. import hello.*;
  3. import hello.home.entity.*;
  4. import java.util.*;
  5. import java.util.concurrent.*;
  6. import com.techempower.cache.*;
  7. import com.techempower.gemini.*;
  8. import com.techempower.gemini.path.*;
  9. import com.techempower.gemini.path.annotation.*;
  10. /**
  11. * Responds to the framework benchmarking requests for "hello, world" and
  12. * simple database queries.
  13. */
  14. public class HelloHandler
  15. extends BasicPathHandler<GhContext>
  16. {
  17. private static final int DB_ROWS = 10000;
  18. private final EntityStore store;
  19. /**
  20. * Constructor.
  21. */
  22. public HelloHandler(GeminiApplication app)
  23. {
  24. super(app, "hllo");
  25. this.store = app.getStore();
  26. }
  27. /**
  28. * Return "hello world" as a JSON-encoded message.
  29. */
  30. @PathDefault
  31. public boolean helloworld()
  32. {
  33. return message("Hello, World!");
  34. }
  35. /**
  36. * Return a list of World objects as JSON, selected randomly from the World
  37. * table. For consistency, we have assumed the table has 10,000 rows.
  38. */
  39. @PathSegment
  40. public boolean db()
  41. {
  42. final Random random = ThreadLocalRandom.current();
  43. final int queries = context().getInt("queries", 1, 1, 500);
  44. final World[] worlds = new World[queries];
  45. for (int i = 0; i < queries; i++)
  46. {
  47. worlds[i] = store.get(World.class, random.nextInt(DB_ROWS) + 1);
  48. }
  49. return json(worlds);
  50. }
  51. }