Application.java 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package controllers;
  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import java.util.List;
  5. import java.util.Map;
  6. import java.util.concurrent.ExecutionException;
  7. import java.util.concurrent.ThreadLocalRandom;
  8. import models.World;
  9. import play.db.jpa.JPAPlugin;
  10. import play.jobs.Job;
  11. import play.libs.F.Promise;
  12. import play.mvc.Controller;
  13. public class Application extends Controller {
  14. private static final int TEST_DATABASE_ROWS = 10000;
  15. public static void index() {
  16. render();
  17. }
  18. public static void hello() {
  19. renderText("hello world");
  20. }
  21. public static void json() {
  22. Map<String, String> result = new HashMap<String, String>();
  23. result.put("message", "Hello, World!");
  24. renderJSON(result);
  25. }
  26. /**
  27. * this version is used in the tests. it is the simplest and fastest.
  28. *
  29. * @param queries
  30. */
  31. public static void dbSync(int queries) {
  32. if (queries == 0)
  33. queries = 1;
  34. final List<World> worlds = new ArrayList<World>();
  35. for (int i = 0; i < queries; ++i) {
  36. Long id = Long.valueOf(ThreadLocalRandom.current().nextInt(TEST_DATABASE_ROWS) + 1);
  37. World result = World.findById(id);
  38. worlds.add(result);
  39. }
  40. renderJSON(worlds);
  41. }
  42. @play.db.jpa.NoTransaction
  43. public static void setup() {
  44. JPAPlugin plugin = play.Play.plugin(JPAPlugin.class);
  45. plugin.startTx(true);
  46. // clean out the old
  47. World.deleteAll();
  48. System.out.println("DELETED");
  49. // in with the new
  50. for (long i = 0; i <= TEST_DATABASE_ROWS; i++) {
  51. int randomNumber = ThreadLocalRandom.current().nextInt(TEST_DATABASE_ROWS) + 1;
  52. new World(i, randomNumber).save();
  53. if (i % 100 == 0) {
  54. World.em().flush();
  55. World.em().clear();
  56. System.out.println("FLUSHED : " + i + "/" + TEST_DATABASE_ROWS);
  57. }
  58. }
  59. System.out.println("ADDED");
  60. plugin.closeTx(false);
  61. }
  62. /**
  63. * note this is method is much slower than the synchronous version
  64. */
  65. public static void dbAsyncEachQuery(int queries)
  66. throws InterruptedException, ExecutionException {
  67. if (queries == 0)
  68. queries = 1;
  69. final int queryCount = queries;
  70. List<Promise<World>> promises = new ArrayList<Promise<World>>();
  71. for (int i = 0; i < queryCount; ++i) {
  72. final Long id = Long
  73. .valueOf(ThreadLocalRandom.current().nextInt(TEST_DATABASE_ROWS) + 1);
  74. Job<World> job = new Job<World>() {
  75. public World doJobWithResult() throws Exception {
  76. World result = World.findById(id);
  77. return result;
  78. };
  79. };
  80. promises.add(job.now());
  81. }
  82. List<World> result = await(Promise.waitAll(promises));
  83. renderJSON(result);
  84. }
  85. /**
  86. * note this is method is a bit slower than the synchronous version
  87. */
  88. public static void dbAsyncAllQueries(int queries)
  89. throws InterruptedException, ExecutionException {
  90. if (queries == 0)
  91. queries = 1;
  92. final int queryCount = queries;
  93. final List<World> worlds = new ArrayList<World>();
  94. Job<List<World>> job = new Job<List<World>>() {
  95. public java.util.List<World> doJobWithResult() throws Exception {
  96. for (int i = 0; i < queryCount; ++i) {
  97. Long id = Long
  98. .valueOf(ThreadLocalRandom.current().nextInt(TEST_DATABASE_ROWS) + 1);
  99. World result = World.findById(id);
  100. worlds.add(result);
  101. }
  102. return worlds;
  103. };
  104. };
  105. List<World> result = job.now().get();
  106. renderJSON(result);
  107. }
  108. }