You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PerformanceTestIndexedContainerTest.java 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package com.vaadin.v7.data.util;
  2. import java.util.ArrayList;
  3. import java.util.Collection;
  4. import java.util.Collections;
  5. import java.util.SortedSet;
  6. import java.util.TreeSet;
  7. import org.junit.Assert;
  8. import org.junit.Test;
  9. public class PerformanceTestIndexedContainerTest {
  10. private static final int REPEATS = 10;
  11. private static final int ITEMS = 50000;
  12. private static final long ADD_ITEM_FAIL_THRESHOLD = 200;
  13. // TODO should improve performance of these methods
  14. private static final long ADD_ITEM_AT_FAIL_THRESHOLD = 5000;
  15. private static final long ADD_ITEM_AFTER_FAIL_THRESHOLD = 5000;
  16. // FIXME: Vaadin 7 compatibility version fails this check with original
  17. // value of 5000
  18. private static final long ADD_ITEM_AFTER_LAST_FAIL_THRESHOLD = 6000;
  19. private static final long ADD_ITEMS_CONSTRUCTOR_FAIL_THRESHOLD = 200;
  20. @Test
  21. public void testAddItemPerformance() {
  22. Collection<Long> times = new ArrayList<Long>();
  23. for (int j = 0; j < REPEATS; ++j) {
  24. IndexedContainer c = new IndexedContainer();
  25. long start = System.currentTimeMillis();
  26. for (int i = 0; i < ITEMS; i++) {
  27. c.addItem();
  28. }
  29. times.add(System.currentTimeMillis() - start);
  30. }
  31. checkMedian(ITEMS, times, "IndexedContainer.addItem()",
  32. ADD_ITEM_FAIL_THRESHOLD);
  33. }
  34. @Test
  35. public void testAddItemAtPerformance() {
  36. Collection<Long> times = new ArrayList<Long>();
  37. for (int j = 0; j < REPEATS; ++j) {
  38. IndexedContainer c = new IndexedContainer();
  39. long start = System.currentTimeMillis();
  40. for (int i = 0; i < ITEMS; i++) {
  41. c.addItemAt(0);
  42. }
  43. times.add(System.currentTimeMillis() - start);
  44. }
  45. checkMedian(ITEMS, times, "IndexedContainer.addItemAt()",
  46. ADD_ITEM_AT_FAIL_THRESHOLD);
  47. }
  48. @Test
  49. public void testAddItemAfterPerformance() {
  50. Object initialId = "Item0";
  51. Collection<Long> times = new ArrayList<Long>();
  52. for (int j = 0; j < REPEATS; ++j) {
  53. IndexedContainer c = new IndexedContainer();
  54. c.addItem(initialId);
  55. long start = System.currentTimeMillis();
  56. for (int i = 0; i < ITEMS; i++) {
  57. c.addItemAfter(initialId);
  58. }
  59. times.add(System.currentTimeMillis() - start);
  60. }
  61. checkMedian(ITEMS, times, "IndexedContainer.addItemAfter()",
  62. ADD_ITEM_AFTER_FAIL_THRESHOLD);
  63. }
  64. @Test
  65. public void testAddItemAfterLastPerformance() {
  66. // TODO running with less items because slow otherwise
  67. Collection<Long> times = new ArrayList<Long>();
  68. for (int j = 0; j < REPEATS; ++j) {
  69. IndexedContainer c = new IndexedContainer();
  70. c.addItem();
  71. long start = System.currentTimeMillis();
  72. for (int i = 0; i < ITEMS / 3; i++) {
  73. c.addItemAfter(c.lastItemId());
  74. }
  75. times.add(System.currentTimeMillis() - start);
  76. }
  77. checkMedian(ITEMS / 3, times, "IndexedContainer.addItemAfter(lastId)",
  78. ADD_ITEM_AFTER_LAST_FAIL_THRESHOLD);
  79. }
  80. @Test
  81. public void testAddItemsConstructorPerformance() {
  82. Collection<Object> items = new ArrayList<Object>(50000);
  83. for (int i = 0; i < ITEMS; ++i) {
  84. items.add(new Object());
  85. }
  86. SortedSet<Long> times = new TreeSet<Long>();
  87. for (int j = 0; j < REPEATS; ++j) {
  88. long start = System.currentTimeMillis();
  89. new IndexedContainer(items);
  90. times.add(System.currentTimeMillis() - start);
  91. }
  92. checkMedian(ITEMS, times, "IndexedContainer(Collection)",
  93. ADD_ITEMS_CONSTRUCTOR_FAIL_THRESHOLD);
  94. }
  95. private void checkMedian(int items, Collection<Long> times,
  96. String methodName, long threshold) {
  97. long median = median(times);
  98. System.out.println(
  99. methodName + " timings (ms) for " + items + " items: " + times);
  100. Assert.assertTrue(methodName + " too slow, median time " + median
  101. + "ms for " + items + " items", median <= threshold);
  102. }
  103. private Long median(Collection<Long> times) {
  104. ArrayList<Long> list = new ArrayList<Long>(times);
  105. Collections.sort(list);
  106. // not exact median in some cases, but good enough
  107. return list.get(list.size() / 2);
  108. }
  109. }