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.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 final static 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. private static final long ADD_ITEM_AFTER_LAST_FAIL_THRESHOLD = 6000;
  17. private static final long ADD_ITEMS_CONSTRUCTOR_FAIL_THRESHOLD = 200;
  18. @Test
  19. public void testAddItemPerformance() {
  20. Collection<Long> times = new ArrayList<Long>();
  21. for (int j = 0; j < REPEATS; ++j) {
  22. IndexedContainer c = new IndexedContainer();
  23. long start = System.currentTimeMillis();
  24. for (int i = 0; i < ITEMS; i++) {
  25. c.addItem();
  26. }
  27. times.add(System.currentTimeMillis() - start);
  28. }
  29. checkMedian(ITEMS, times, "IndexedContainer.addItem()",
  30. ADD_ITEM_FAIL_THRESHOLD);
  31. }
  32. @Test
  33. public void testAddItemAtPerformance() {
  34. Collection<Long> times = new ArrayList<Long>();
  35. for (int j = 0; j < REPEATS; ++j) {
  36. IndexedContainer c = new IndexedContainer();
  37. long start = System.currentTimeMillis();
  38. for (int i = 0; i < ITEMS; i++) {
  39. c.addItemAt(0);
  40. }
  41. times.add(System.currentTimeMillis() - start);
  42. }
  43. checkMedian(ITEMS, times, "IndexedContainer.addItemAt()",
  44. ADD_ITEM_AT_FAIL_THRESHOLD);
  45. }
  46. @Test
  47. public void testAddItemAfterPerformance() {
  48. Object initialId = "Item0";
  49. Collection<Long> times = new ArrayList<Long>();
  50. for (int j = 0; j < REPEATS; ++j) {
  51. IndexedContainer c = new IndexedContainer();
  52. c.addItem(initialId);
  53. long start = System.currentTimeMillis();
  54. for (int i = 0; i < ITEMS; i++) {
  55. c.addItemAfter(initialId);
  56. }
  57. times.add(System.currentTimeMillis() - start);
  58. }
  59. checkMedian(ITEMS, times, "IndexedContainer.addItemAfter()",
  60. ADD_ITEM_AFTER_FAIL_THRESHOLD);
  61. }
  62. @Test
  63. public void testAddItemAfterLastPerformance() {
  64. // TODO running with less items because slow otherwise
  65. Collection<Long> times = new ArrayList<Long>();
  66. for (int j = 0; j < REPEATS; ++j) {
  67. IndexedContainer c = new IndexedContainer();
  68. c.addItem();
  69. long start = System.currentTimeMillis();
  70. for (int i = 0; i < ITEMS / 3; i++) {
  71. c.addItemAfter(c.lastItemId());
  72. }
  73. times.add(System.currentTimeMillis() - start);
  74. }
  75. checkMedian(ITEMS / 3, times, "IndexedContainer.addItemAfter(lastId)",
  76. ADD_ITEM_AFTER_LAST_FAIL_THRESHOLD);
  77. }
  78. @Test
  79. public void testAddItemsConstructorPerformance() {
  80. Collection<Object> items = new ArrayList<Object>(50000);
  81. for (int i = 0; i < ITEMS; ++i) {
  82. items.add(new Object());
  83. }
  84. SortedSet<Long> times = new TreeSet<Long>();
  85. for (int j = 0; j < REPEATS; ++j) {
  86. long start = System.currentTimeMillis();
  87. new IndexedContainer(items);
  88. times.add(System.currentTimeMillis() - start);
  89. }
  90. checkMedian(ITEMS, times, "IndexedContainer(Collection)",
  91. ADD_ITEMS_CONSTRUCTOR_FAIL_THRESHOLD);
  92. }
  93. private void checkMedian(int items, Collection<Long> times,
  94. String methodName, long threshold) {
  95. long median = median(times);
  96. System.out.println(
  97. methodName + " timings (ms) for " + items + " items: " + times);
  98. Assert.assertTrue(methodName + " too slow, median time " + median
  99. + "ms for " + items + " items", median <= threshold);
  100. }
  101. private Long median(Collection<Long> times) {
  102. ArrayList<Long> list = new ArrayList<Long>(times);
  103. Collections.sort(list);
  104. // not exact median in some cases, but good enough
  105. return list.get(list.size() / 2);
  106. }
  107. }