~ubuntu-branches/ubuntu/oneiric/koffice/oneiric-updates

« back to all changes in this revision

Viewing changes to krita/image/tiles3/tests/kis_memory_pool_test.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Alessandro Ghersi
  • Date: 2010-10-27 17:52:57 UTC
  • mfrom: (0.12.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20101027175257-s04zqqk5bs8ckm9o
Tags: 1:2.2.83-0ubuntu1
* Merge with Debian git remaining changes:
 - Add build-deps on librcps-dev, opengtl-dev, libqtgtl-dev, freetds-dev,
   create-resources, libspnav-dev
 - Remove needless build-dep on libwv2-dev
 - koffice-libs recommends create-resources
 - krita recommends pstoedit
 - Keep our patches
* New upstream release 2.3 beta 3
  - Remove debian/patches fixed by upstream
  - Update install files

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  Copyright (c) 2010 Dmitry Kazakov <dimula73@gmail.com>
 
3
 *
 
4
 *  This program is free software; you can redistribute it and/or modify
 
5
 *  it under the terms of the GNU General Public License as published by
 
6
 *  the Free Software Foundation; either version 2 of the License, or
 
7
 *  (at your option) any later version.
 
8
 *
 
9
 *  This program is distributed in the hope that it will be useful,
 
10
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
 *  GNU General Public License for more details.
 
13
 *
 
14
 *  You should have received a copy of the GNU General Public License
 
15
 *  along with this program; if not, write to the Free Software
 
16
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 
17
 */
 
18
 
 
19
#include "kis_memory_pool_test.h"
 
20
#include <qtest_kde.h>
 
21
 
 
22
#include "kis_debug.h"
 
23
 
 
24
#include "tiles3/kis_memory_pool.h"
 
25
 
 
26
#define OBJECT_SIZE 16384 //bytes (size of a usual tile data)
 
27
#define POOL_SIZE 32
 
28
#define NUM_THREADS 2
 
29
#define NUM_CYCLES 100000
 
30
#define NUM_OBJECTS 64 // for tiles - 64 == area of 512x512px
 
31
 
 
32
class TestingClass
 
33
{
 
34
private:
 
35
    quint8 m_data[OBJECT_SIZE];
 
36
};
 
37
 
 
38
typedef KisMemoryPool<TestingClass, POOL_SIZE> TestingMemoryPool;
 
39
 
 
40
 
 
41
class KisPoolStressJob : public QRunnable
 
42
{
 
43
public:
 
44
    KisPoolStressJob(TestingMemoryPool &pool)
 
45
        : m_pool(pool)
 
46
    {
 
47
    }
 
48
 
 
49
    void run() {
 
50
        qsrand(QTime::currentTime().msec());
 
51
        for(qint32 i = 0; i < NUM_CYCLES; i++) {
 
52
            qint32 type = i % 2;
 
53
 
 
54
            switch(type) {
 
55
            case 0:
 
56
                for(qint32 j = 0; j < NUM_OBJECTS; j++) {
 
57
                    m_pointer[j] = m_pool.pop();
 
58
                    Q_ASSERT(m_pointer[j]);
 
59
                    // check for sigsegv ;)
 
60
                    ((quint8*)m_pointer[j])[0] = i;
 
61
                }
 
62
                break;
 
63
            case 1:
 
64
                for(qint32 j = 0; j < NUM_OBJECTS; j++) {
 
65
                    // are we the only writers here?
 
66
                    Q_ASSERT(((quint8*)m_pointer[j])[0] == (i-1) % 256);
 
67
                    m_pool.push(m_pointer[j]);
 
68
                }
 
69
                break;
 
70
            }
 
71
        }
 
72
    }
 
73
 
 
74
private:
 
75
    void* m_pointer[NUM_OBJECTS];
 
76
    TestingMemoryPool &m_pool;
 
77
};
 
78
 
 
79
void KisMemoryPoolTest::benchmarkMemoryPool()
 
80
{
 
81
    TestingMemoryPool memoryPool;
 
82
 
 
83
    QThreadPool pool;
 
84
    pool.setMaxThreadCount(NUM_THREADS);
 
85
 
 
86
    QBENCHMARK {
 
87
        for(qint32 i = 0; i < NUM_THREADS; i++) {
 
88
            KisPoolStressJob *job = new KisPoolStressJob(memoryPool);
 
89
            pool.start(job);
 
90
        }
 
91
 
 
92
        pool.waitForDone();
 
93
    }
 
94
}
 
95
 
 
96
 
 
97
class KisAllocStressJob : public QRunnable
 
98
{
 
99
public:
 
100
    KisAllocStressJob()
 
101
    {
 
102
    }
 
103
 
 
104
    void run() {
 
105
        qsrand(QTime::currentTime().msec());
 
106
        for(qint32 i = 0; i < NUM_CYCLES; i++) {
 
107
            qint32 type = i % 2;
 
108
 
 
109
            switch(type) {
 
110
            case 0:
 
111
                for(qint32 j = 0; j < NUM_OBJECTS; j++) {
 
112
                    m_pointer[j] = new TestingClass;
 
113
                    Q_ASSERT(m_pointer[j]);
 
114
                    // check for sigsegv ;)
 
115
                    ((quint8*)m_pointer[j])[0] = i;
 
116
                }
 
117
                break;
 
118
            case 1:
 
119
                for(qint32 j = 0; j < NUM_OBJECTS; j++) {
 
120
                    // are we the only writers here?
 
121
                    Q_ASSERT(((quint8*)m_pointer[j])[0] == (i-1) % 256);
 
122
                    delete (TestingClass*)m_pointer[j];
 
123
                }
 
124
                break;
 
125
            }
 
126
        }
 
127
    }
 
128
 
 
129
private:
 
130
    void* m_pointer[NUM_OBJECTS];
 
131
};
 
132
 
 
133
void KisMemoryPoolTest::benchmarkAlloc()
 
134
{
 
135
    QThreadPool pool;
 
136
    pool.setMaxThreadCount(NUM_THREADS);
 
137
 
 
138
    QBENCHMARK {
 
139
        for(qint32 i = 0; i < NUM_THREADS; i++) {
 
140
            KisAllocStressJob *job = new KisAllocStressJob();
 
141
            pool.start(job);
 
142
        }
 
143
 
 
144
        pool.waitForDone();
 
145
    }
 
146
}
 
147
 
 
148
 
 
149
QTEST_KDEMAIN(KisMemoryPoolTest, NoGUI)
 
150
#include "kis_memory_pool_test.moc"
 
151