Warning, file /graphics/krita/libs/image/tiles3/tests/kis_chunk_allocator_test.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002  *  SPDX-FileCopyrightText: 2010 Dmitry Kazakov <dimula73@gmail.com>
0003  *
0004  *  SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #include "kis_chunk_allocator_test.h"
0008 #include <simpletest.h>
0009 
0010 #include "kis_debug.h"
0011 
0012 #include "../swap/kis_chunk_allocator.h"
0013 
0014 
0015 void KisChunkAllocatorTest::testOperations()
0016 {
0017     KisChunkAllocator allocator;
0018 
0019     allocator.getChunk(10);
0020     allocator.getChunk(15);
0021     KisChunk chunk3 = allocator.getChunk(20);
0022     allocator.getChunk(25);
0023     allocator.getChunk(30);
0024 
0025     allocator.freeChunk(chunk3);
0026     chunk3 = allocator.getChunk(20);
0027 
0028     allocator.debugChunks();
0029     allocator.sanityCheck();
0030     QVERIFY(qFuzzyCompare(allocator.debugFragmentation(), 1./6));
0031 }
0032 
0033 
0034 #define NUM_TRANSACTIONS 30
0035 #define NUM_CHUNKS_ALLOC 15000
0036 #define NUM_CHUNKS_FREE 12000
0037 #define CHUNK_AV_SIZE 1024*12
0038 #define CHUNK_DEV_SIZE 1024*4
0039 #define SWAP_SIZE (1ULL * CHUNK_AV_SIZE * NUM_CHUNKS_ALLOC * NUM_TRANSACTIONS)
0040 
0041 quint64 getChunkSize()
0042 {
0043     quint64 deviation = qrand() % (2 * CHUNK_DEV_SIZE);
0044     return CHUNK_AV_SIZE - CHUNK_DEV_SIZE + deviation;
0045 }
0046 
0047 
0048 qreal KisChunkAllocatorTest::measureFragmentation(qint32 transactions,
0049                                                   qint32 chunksAlloc,
0050                                                   qint32 chunksFree,
0051                                                   bool printDetails)
0052 {
0053     KisChunkAllocator allocator(DEFAULT_SLAB_SIZE, SWAP_SIZE);
0054     QList<KisChunk> chunks;
0055 
0056     for(qint32 k = 0; k < transactions; k++) {
0057         if(chunks.size() > 0) {
0058             for(qint32 i = 0; i < chunksFree; i++) {
0059                 qint32 idx = qrand() % chunks.size();
0060                 allocator.freeChunk(chunks.takeAt(idx));
0061             }
0062         }
0063         allocator.sanityCheck();
0064 
0065         for(qint32 i = 0; i < chunksAlloc; i++) {
0066             chunks.append(allocator.getChunk(getChunkSize()));
0067         }
0068         allocator.sanityCheck();
0069     }
0070 
0071     allocator.sanityCheck();
0072     return allocator.debugFragmentation(printDetails);
0073 }
0074 
0075 void KisChunkAllocatorTest::testFragmentation()
0076 {
0077 
0078     qsrand(QTime::currentTime().msec());
0079 
0080     measureFragmentation(NUM_TRANSACTIONS, NUM_CHUNKS_ALLOC,
0081                          NUM_CHUNKS_FREE, true);
0082 
0083     /**
0084      * The following tests are too slow, so we disable them by default
0085      */
0086     return;
0087 
0088     dbgKrita << "fragmentation(transactions)";
0089     for(qint32 t = 1; t < NUM_TRANSACTIONS; t += NUM_TRANSACTIONS/7) {
0090         qreal f = measureFragmentation(t, NUM_CHUNKS_ALLOC,
0091                                        NUM_CHUNKS_FREE, false);
0092         dbgKrita << t << f;
0093     }
0094 
0095     dbgKrita << "fragmentation(alloc)";
0096     for(qint32 t = 1; t < NUM_CHUNKS_ALLOC; t += NUM_CHUNKS_ALLOC/7) {
0097         qreal f = measureFragmentation(NUM_TRANSACTIONS,t,
0098                                        0.8*t, false);
0099         dbgKrita << t << f;
0100     }
0101 
0102     dbgKrita << "fragmentation(free)";
0103     for(qint32 t = NUM_CHUNKS_ALLOC/7; t < NUM_CHUNKS_ALLOC; t += NUM_CHUNKS_ALLOC/15) {
0104         qreal f = measureFragmentation(NUM_TRANSACTIONS,NUM_CHUNKS_ALLOC,
0105                                        t, false);
0106         dbgKrita << t << f;
0107     }
0108 
0109 }
0110 
0111 
0112 SIMPLE_TEST_MAIN(KisChunkAllocatorTest)
0113