Application.java 3.2 KB

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