VirtualBox

Changeset 46211 in vbox for trunk/src/VBox/Runtime/testcase


Ignore:
Timestamp:
May 22, 2013 11:00:02 AM (12 years ago)
Author:
vboxsync
Message:

More strcache testing and some tuning.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Runtime/testcase/tstRTStrCache.cpp

    r46208 r46211  
    3131
    3232#include <iprt/asm.h>
     33#include <iprt/ctype.h>
    3334#include <iprt/err.h>
    3435#include <iprt/initterm.h>
     36#include <iprt/mem.h>
     37#include <iprt/rand.h>
    3538#include <iprt/string.h>
    3639#include <iprt/test.h>
    3740#include <iprt/thread.h>
    38 #include <iprt/rand.h>
     41#include <iprt/time.h>
     42
     43
     44static void tstShowStats(RTSTRCACHE hStrCache)
     45{
     46    size_t   cbStrings;
     47    size_t   cbChunks;
     48    size_t   cbBigEntries;
     49    uint32_t cHashCollisions;
     50    uint32_t cHashCollisions2;
     51    uint32_t cHashInserts;
     52    uint32_t cRehashes;
     53    uint32_t cStrings = RTStrCacheGetStats(hStrCache, &cbStrings, &cbChunks, &cbBigEntries,
     54                                           &cHashCollisions, &cHashCollisions2, &cHashInserts, &cRehashes);
     55    if (cbStrings == UINT32_MAX)
     56    {
     57        RTTESTI_CHECK(!RTStrCacheIsRealImpl());
     58        return;
     59    }
     60
     61    RTTestIValue("Strings", cStrings, RTTESTUNIT_OCCURRENCES);
     62    RTTestIValue("Memory overhead", (uint64_t)(cbChunks + cbBigEntries - cbStrings) * 100 / cbStrings, RTTESTUNIT_PCT);
     63    if (cHashInserts > 0)
     64    {
     65        RTTestIValue("Collisions", (uint64_t)cHashCollisions * 100 / cHashInserts, RTTESTUNIT_PCT);
     66        RTTestIValue("Collisions2", (uint64_t)cHashCollisions2 * 100 / cHashInserts, RTTESTUNIT_PCT);
     67    }
     68    RTTestIPrintf(RTTESTLVL_ALWAYS, "cHashInserts=%u cHashCollisions=%u cHashCollisions2=%u cRehashes=%u\n",
     69                  cHashInserts, cHashCollisions, cHashCollisions2, cRehashes);
     70    RTTestIPrintf(RTTESTLVL_ALWAYS, "cbChunks=%zu cbBigEntries=%zu cbStrings=%zu\n", cbChunks, cbBigEntries, cbStrings);
     71}
     72
     73
     74/**
     75 * Check hash and memory performance.
     76 */
     77static void tst2(void)
     78{
     79    RTTestISub("Hash performance");
     80
     81    /*
     82     * Generate test strings using a specific pseudo random generator.
     83     */
     84    size_t cbStrings = 0;
     85    char  *apszTests[8192];
     86    RTRAND hRand;
     87    RTTESTI_CHECK_RC_RETV(RTRandAdvCreateParkMiller(&hRand), VINF_SUCCESS);
     88    for (uint32_t i = 0; i < 8192; i++)
     89    {
     90        char szBuf[8192];
     91        uint32_t cch = RTRandAdvU32Ex(hRand, 3, sizeof(szBuf) - 1);
     92        RTRandAdvBytes(hRand, szBuf, cch);
     93        szBuf[cch] = '\0';
     94        for (uint32_t off = 0; off < cch; off++)
     95        {
     96            uint8_t b = szBuf[off];
     97            b &= 0x7f;
     98            if (!b || b == 0x7f)
     99                b = ' ';
     100            else if (RTLocCIsCntrl(b) && b != '\n' && b != '\r' && b != '\t')
     101                b += 0x30;
     102            szBuf[off] = b;
     103        }
     104        apszTests[i] = (char *)RTMemDup(szBuf, cch + 1);
     105        RTTESTI_CHECK_RETV(apszTests[i] != NULL);
     106        cbStrings += cch + 1;
     107    }
     108    RTRandAdvDestroy(hRand);
     109    RTTestIValue("Average string", cbStrings / RT_ELEMENTS(apszTests), RTTESTUNIT_BYTES);
     110
     111    /*
     112     * Test new insertion first time around.
     113     */
     114    RTSTRCACHE hStrCache;
     115    RTTESTI_CHECK_RC_RETV(RTStrCacheCreate(&hStrCache, "hash performance"), VINF_SUCCESS);
     116
     117    uint64_t nsTsStart = RTTimeNanoTS();
     118    for (uint32_t i = 0; i < RT_ELEMENTS(apszTests); i++)
     119        RTTESTI_CHECK_RETV(RTStrCacheEnter(hStrCache, apszTests[i]) != NULL);
     120    uint64_t cNsElapsed = RTTimeNanoTS() - nsTsStart;
     121    RTTestIValue("First insert", cNsElapsed / RT_ELEMENTS(apszTests), RTTESTUNIT_NS_PER_CALL);
     122
     123    /*
     124     * Insert existing strings.
     125     */
     126    nsTsStart = RTTimeNanoTS();
     127    for (uint32_t i = 0; i < 8192; i++)
     128        RTTESTI_CHECK(RTStrCacheEnter(hStrCache, apszTests[i]) != NULL);
     129    cNsElapsed = RTTimeNanoTS() - nsTsStart;
     130    RTTestIValue("Duplicate insert", cNsElapsed / RT_ELEMENTS(apszTests), RTTESTUNIT_NS_PER_CALL);
     131
     132    tstShowStats(hStrCache);
     133    RTTESTI_CHECK_RC(RTStrCacheDestroy(hStrCache), VINF_SUCCESS);
     134}
    39135
    40136
     
    177273
    178274    /*
     275     * Cache performance on relatively real world examples.
     276     */
     277    tst2();
     278
     279    /*
    179280     * Summary.
    180281     */
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette