Application.java 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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() {
  32. Long id = Long.valueOf(ThreadLocalRandom.current().nextInt(TEST_DATABASE_ROWS) + 1);
  33. World result = World.findById(id);
  34. renderJSON(result);
  35. }
  36. public static void dbQueries(int queries) {
  37. if (queries == 0) {
  38. queries = 1;
  39. } else if (queries > 500) {
  40. queries = 500;
  41. }
  42. final List<World> worlds = new ArrayList<World>();
  43. for (int i = 0; i < queries; ++i) {
  44. Long id = Long.valueOf(ThreadLocalRandom.current().nextInt(TEST_DATABASE_ROWS) + 1);
  45. World result = World.findById(id);
  46. worlds.add(result);
  47. }
  48. renderJSON(worlds);
  49. }
  50. @play.db.jpa.NoTransaction
  51. public static void setup() {
  52. JPAPlugin plugin = play.Play.plugin(JPAPlugin.class);
  53. plugin.startTx(true);
  54. // clean out the old
  55. World.deleteAll();
  56. System.out.println("DELETED");
  57. // in with the new
  58. for (long i = 0; i <= TEST_DATABASE_ROWS; i++) {
  59. int randomNumber = ThreadLocalRandom.current().nextInt(TEST_DATABASE_ROWS) + 1;
  60. new World(i, randomNumber).save();
  61. if (i % 100 == 0) {
  62. World.em().flush();
  63. World.em().clear();
  64. System.out.println("FLUSHED : " + i + "/" + TEST_DATABASE_ROWS);
  65. }
  66. }
  67. System.out.println("ADDED");
  68. plugin.closeTx(false);
  69. }
  70. /**
  71. * note this is method is much slower than the synchronous version
  72. */
  73. public static void dbAsyncEachQuery(int queries)
  74. throws InterruptedException, ExecutionException {
  75. if (queries == 0)
  76. queries = 1;
  77. final int queryCount = queries;
  78. List<Promise<World>> promises = new ArrayList<Promise<World>>();
  79. for (int i = 0; i < queryCount; ++i) {
  80. final Long id = Long
  81. .valueOf(ThreadLocalRandom.current().nextInt(TEST_DATABASE_ROWS) + 1);
  82. Job<World> job = new Job<World>() {
  83. public World doJobWithResult() throws Exception {
  84. World result = World.findById(id);
  85. return result;
  86. };
  87. };
  88. promises.add(job.now());
  89. }
  90. List<World> result = await(Promise.waitAll(promises));
  91. renderJSON(result);
  92. }
  93. /**
  94. * note this is method is a bit slower than the synchronous version
  95. */
  96. public static void dbAsyncAllQueries(int queries)
  97. throws InterruptedException, ExecutionException {
  98. if (queries == 0)
  99. queries = 1;
  100. final int queryCount = queries;
  101. final List<World> worlds = new ArrayList<World>();
  102. Job<List<World>> job = new Job<List<World>>() {
  103. public java.util.List<World> doJobWithResult() throws Exception {
  104. for (int i = 0; i < queryCount; ++i) {
  105. Long id = Long
  106. .valueOf(ThreadLocalRandom.current().nextInt(TEST_DATABASE_ROWS) + 1);
  107. World result = World.findById(id);
  108. worlds.add(result);
  109. }
  110. return worlds;
  111. };
  112. };
  113. List<World> result = job.now().get();
  114. renderJSON(result);
  115. }
  116. }