123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- package com.vaadin.v7.data.util;
-
- import java.util.ArrayList;
- import java.util.Collection;
- import java.util.Collections;
- import java.util.SortedSet;
- import java.util.TreeSet;
-
- import org.junit.Assert;
- import org.junit.Test;
-
- public class PerformanceTestIndexedContainerTest {
-
- private static final int REPEATS = 10;
- private final static int ITEMS = 50000;
- private static final long ADD_ITEM_FAIL_THRESHOLD = 200;
- // TODO should improve performance of these methods
- private static final long ADD_ITEM_AT_FAIL_THRESHOLD = 5000;
- private static final long ADD_ITEM_AFTER_FAIL_THRESHOLD = 5000;
- private static final long ADD_ITEM_AFTER_LAST_FAIL_THRESHOLD = 6000;
- private static final long ADD_ITEMS_CONSTRUCTOR_FAIL_THRESHOLD = 200;
-
- @Test
- public void testAddItemPerformance() {
- Collection<Long> times = new ArrayList<Long>();
- for (int j = 0; j < REPEATS; ++j) {
- IndexedContainer c = new IndexedContainer();
- long start = System.currentTimeMillis();
- for (int i = 0; i < ITEMS; i++) {
- c.addItem();
- }
- times.add(System.currentTimeMillis() - start);
- }
- checkMedian(ITEMS, times, "IndexedContainer.addItem()",
- ADD_ITEM_FAIL_THRESHOLD);
- }
-
- @Test
- public void testAddItemAtPerformance() {
- Collection<Long> times = new ArrayList<Long>();
- for (int j = 0; j < REPEATS; ++j) {
- IndexedContainer c = new IndexedContainer();
- long start = System.currentTimeMillis();
- for (int i = 0; i < ITEMS; i++) {
- c.addItemAt(0);
- }
- times.add(System.currentTimeMillis() - start);
- }
- checkMedian(ITEMS, times, "IndexedContainer.addItemAt()",
- ADD_ITEM_AT_FAIL_THRESHOLD);
- }
-
- @Test
- public void testAddItemAfterPerformance() {
- Object initialId = "Item0";
- Collection<Long> times = new ArrayList<Long>();
- for (int j = 0; j < REPEATS; ++j) {
- IndexedContainer c = new IndexedContainer();
- c.addItem(initialId);
- long start = System.currentTimeMillis();
- for (int i = 0; i < ITEMS; i++) {
- c.addItemAfter(initialId);
- }
- times.add(System.currentTimeMillis() - start);
- }
- checkMedian(ITEMS, times, "IndexedContainer.addItemAfter()",
- ADD_ITEM_AFTER_FAIL_THRESHOLD);
- }
-
- @Test
- public void testAddItemAfterLastPerformance() {
- // TODO running with less items because slow otherwise
- Collection<Long> times = new ArrayList<Long>();
- for (int j = 0; j < REPEATS; ++j) {
- IndexedContainer c = new IndexedContainer();
- c.addItem();
- long start = System.currentTimeMillis();
- for (int i = 0; i < ITEMS / 3; i++) {
- c.addItemAfter(c.lastItemId());
- }
- times.add(System.currentTimeMillis() - start);
- }
- checkMedian(ITEMS / 3, times, "IndexedContainer.addItemAfter(lastId)",
- ADD_ITEM_AFTER_LAST_FAIL_THRESHOLD);
- }
-
- @Test
- public void testAddItemsConstructorPerformance() {
- Collection<Object> items = new ArrayList<Object>(50000);
- for (int i = 0; i < ITEMS; ++i) {
- items.add(new Object());
- }
-
- SortedSet<Long> times = new TreeSet<Long>();
- for (int j = 0; j < REPEATS; ++j) {
- long start = System.currentTimeMillis();
- new IndexedContainer(items);
- times.add(System.currentTimeMillis() - start);
- }
- checkMedian(ITEMS, times, "IndexedContainer(Collection)",
- ADD_ITEMS_CONSTRUCTOR_FAIL_THRESHOLD);
- }
-
- private void checkMedian(int items, Collection<Long> times,
- String methodName, long threshold) {
- long median = median(times);
- System.out.println(
- methodName + " timings (ms) for " + items + " items: " + times);
- Assert.assertTrue(methodName + " too slow, median time " + median
- + "ms for " + items + " items", median <= threshold);
- }
-
- private Long median(Collection<Long> times) {
- ArrayList<Long> list = new ArrayList<Long>(times);
- Collections.sort(list);
- // not exact median in some cases, but good enough
- return list.get(list.size() / 2);
- }
-
- }
|