aboutsummaryrefslogtreecommitdiffstats
path: root/server/tests/src/com/vaadin/data/util/PerformanceTestIndexedContainer.java
blob: 35335edd232f90c74b6cb3c8a6289526475cb965 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package com.vaadin.data.util;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.SortedSet;
import java.util.TreeSet;

import junit.framework.Assert;
import junit.framework.TestCase;

public class PerformanceTestIndexedContainer extends TestCase {

    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 = 5000;
    private static final long ADD_ITEMS_CONSTRUCTOR_FAIL_THRESHOLD = 200;

    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);
    }

    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);
    }

    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);
    }

    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);
    }

    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);
    }

}