VirtualBox

source: vbox/trunk/src/VBox/Runtime/testcase/tstRTStrCache.cpp@ 100762

Last change on this file since 100762 was 98103, checked in by vboxsync, 2 years ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 11.7 KB
Line 
1/* $Id: tstRTStrCache.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
2/** @file
3 * IPRT Testcase - StrCache.
4 */
5
6/*
7 * Copyright (C) 2009-2023 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * The contents of this file may alternatively be used under the terms
26 * of the Common Development and Distribution License Version 1.0
27 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
28 * in the VirtualBox distribution, in which case the provisions of the
29 * CDDL are applicable instead of those of the GPL.
30 *
31 * You may elect to license modified versions of this file under the
32 * terms and conditions of either the GPL or the CDDL or both.
33 *
34 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
35 */
36
37
38/*********************************************************************************************************************************
39* Header Files *
40*********************************************************************************************************************************/
41#include <iprt/strcache.h>
42
43#include <iprt/asm.h>
44#include <iprt/ctype.h>
45#include <iprt/errcore.h>
46#include <iprt/initterm.h>
47#include <iprt/mem.h>
48#include <iprt/rand.h>
49#include <iprt/string.h>
50#include <iprt/test.h>
51#include <iprt/thread.h>
52#include <iprt/time.h>
53
54
55static void tstShowStats(RTSTRCACHE hStrCache)
56{
57 size_t cbStrings;
58 size_t cbChunks;
59 size_t cbBigEntries;
60 uint32_t cHashCollisions;
61 uint32_t cHashCollisions2;
62 uint32_t cHashInserts;
63 uint32_t cRehashes;
64 uint32_t cStrings = RTStrCacheGetStats(hStrCache, &cbStrings, &cbChunks, &cbBigEntries,
65 &cHashCollisions, &cHashCollisions2, &cHashInserts, &cRehashes);
66 if (cbStrings == UINT32_MAX)
67 {
68 RTTESTI_CHECK(!RTStrCacheIsRealImpl());
69 return;
70 }
71
72 RTTestIValue("Strings", cStrings, RTTESTUNIT_OCCURRENCES);
73 RTTestIValue("Memory overhead", (uint64_t)(cbChunks + cbBigEntries - cbStrings) * 100 / cbStrings, RTTESTUNIT_PCT);
74 if (cHashInserts > 0)
75 {
76 RTTestIValue("Collisions", (uint64_t)cHashCollisions * 100 / cHashInserts, RTTESTUNIT_PCT);
77 RTTestIValue("Collisions2", (uint64_t)cHashCollisions2 * 100 / cHashInserts, RTTESTUNIT_PCT);
78 }
79 RTTestIPrintf(RTTESTLVL_ALWAYS, "cHashInserts=%u cHashCollisions=%u cHashCollisions2=%u cRehashes=%u\n",
80 cHashInserts, cHashCollisions, cHashCollisions2, cRehashes);
81 RTTestIPrintf(RTTESTLVL_ALWAYS, "cbChunks=%zu cbBigEntries=%zu cbStrings=%zu\n", cbChunks, cbBigEntries, cbStrings);
82}
83
84
85/**
86 * Check hash and memory performance.
87 */
88static void tst2(void)
89{
90 RTTestISub("Hash performance");
91
92 /*
93 * Generate test strings using a specific pseudo random generator.
94 */
95 size_t cbStrings = 0;
96 char *apszTests[8192];
97 RTRAND hRand;
98 RTTESTI_CHECK_RC_RETV(RTRandAdvCreateParkMiller(&hRand), VINF_SUCCESS);
99 for (uint32_t i = 0; i < 8192; i++)
100 {
101 char szBuf[8192];
102 uint32_t cch = RTRandAdvU32Ex(hRand, 3, sizeof(szBuf) - 1);
103 RTRandAdvBytes(hRand, szBuf, cch);
104 szBuf[cch] = '\0';
105 for (uint32_t off = 0; off < cch; off++)
106 {
107 uint8_t b = szBuf[off];
108 b &= 0x7f;
109 if (!b || b == 0x7f)
110 b = ' ';
111 else if (RTLocCIsCntrl(b) && b != '\n' && b != '\r' && b != '\t')
112 b += 0x30;
113 szBuf[off] = b;
114 }
115 apszTests[i] = (char *)RTMemDup(szBuf, cch + 1);
116 RTTESTI_CHECK_RETV(apszTests[i] != NULL);
117 cbStrings += cch + 1;
118 }
119 RTRandAdvDestroy(hRand);
120 RTTestIValue("Average string", cbStrings / RT_ELEMENTS(apszTests), RTTESTUNIT_BYTES);
121
122 /*
123 * Test new insertion first time around.
124 */
125 RTSTRCACHE hStrCache;
126 RTTESTI_CHECK_RC_RETV(RTStrCacheCreate(&hStrCache, "hash performance"), VINF_SUCCESS);
127
128 uint64_t nsTsStart = RTTimeNanoTS();
129 for (uint32_t i = 0; i < RT_ELEMENTS(apszTests); i++)
130 RTTESTI_CHECK_RETV(RTStrCacheEnter(hStrCache, apszTests[i]) != NULL);
131 uint64_t cNsElapsed = RTTimeNanoTS() - nsTsStart;
132 RTTestIValue("First insert", cNsElapsed / RT_ELEMENTS(apszTests), RTTESTUNIT_NS_PER_CALL);
133
134 /*
135 * Insert existing strings.
136 */
137 nsTsStart = RTTimeNanoTS();
138 for (uint32_t i = 0; i < 8192; i++)
139 RTTESTI_CHECK(RTStrCacheEnter(hStrCache, apszTests[i]) != NULL);
140 cNsElapsed = RTTimeNanoTS() - nsTsStart;
141 RTTestIValue("Duplicate insert", cNsElapsed / RT_ELEMENTS(apszTests), RTTESTUNIT_NS_PER_CALL);
142
143 tstShowStats(hStrCache);
144 RTTESTI_CHECK_RC(RTStrCacheDestroy(hStrCache), VINF_SUCCESS);
145
146 for (uint32_t i = 0; i < 8192; i++)
147 RTMemFree(apszTests[i]);
148}
149
150
151/**
152 * Basic API checks.
153 * We'll return if any of these fails.
154 */
155static void tst1(RTSTRCACHE hStrCache)
156{
157 const char *psz;
158
159 /* Simple string entering and length. */
160 RTTESTI_CHECK_RETV(psz = RTStrCacheEnter(hStrCache, "abcdefgh"));
161 RTTESTI_CHECK_RETV(strcmp(psz, "abcdefgh") == 0);
162 RTTESTI_CHECK_RETV(RTStrCacheLength(psz) == strlen("abcdefgh"));
163 RTTESTI_CHECK_RETV(RTStrCacheRelease(hStrCache, psz) == 0);
164
165 RTTESTI_CHECK_RETV(psz = RTStrCacheEnter(hStrCache, "abcdefghijklmnopqrstuvwxyz"));
166 RTTESTI_CHECK_RETV(strcmp(psz, "abcdefghijklmnopqrstuvwxyz") == 0);
167 RTTESTI_CHECK_RETV(RTStrCacheLength(psz) == strlen("abcdefghijklmnopqrstuvwxyz"));
168 RTTESTI_CHECK_RETV(RTStrCacheRelease(hStrCache, psz) == 0);
169
170 /* Unterminated strings. */
171 RTTESTI_CHECK_RETV(psz = RTStrCacheEnterN(hStrCache, "0123456789", 3));
172 RTTESTI_CHECK_RETV(strcmp(psz, "012") == 0);
173 RTTESTI_CHECK_RETV(RTStrCacheLength(psz) == strlen("012"));
174 RTTESTI_CHECK_RETV(RTStrCacheRelease(hStrCache, psz) == 0);
175
176 RTTESTI_CHECK_RETV(psz = RTStrCacheEnterN(hStrCache, "0123456789abcdefghijklmnopqrstuvwxyz", 16));
177 RTTESTI_CHECK_RETV(strcmp(psz, "0123456789abcdef") == 0);
178 RTTESTI_CHECK_RETV(RTStrCacheLength(psz) == strlen("0123456789abcdef"));
179 RTTESTI_CHECK_RETV(RTStrCacheRelease(hStrCache, psz) == 0);
180
181 /* String referencing. */
182 char szTest[4096+16];
183 memset(szTest, 'a', sizeof(szTest));
184 char szTest2[4096+16];
185 memset(szTest2, 'f', sizeof(szTest));
186 for (int32_t i = 4096; i > 3; i /= 3)
187 {
188 void *pv2;
189 RTTESTI_CHECK_RETV(psz = RTStrCacheEnterN(hStrCache, szTest, i));
190 RTTESTI_CHECK_MSG_RETV((pv2 = ASMMemFirstMismatchingU8(psz, i, 'a')) == NULL && !psz[i], ("i=%#x psz=%p off=%#x\n", i, psz, (uintptr_t)pv2 - (uintptr_t)psz));
191 RTTESTI_CHECK(RTStrCacheRetain(psz) == 2);
192 RTTESTI_CHECK(RTStrCacheRetain(psz) == 3);
193 RTTESTI_CHECK(RTStrCacheRetain(psz) == 4);
194 RTTESTI_CHECK_MSG_RETV((pv2 = ASMMemFirstMismatchingU8(psz, i, 'a')) == NULL && !psz[i], ("i=%#x psz=%p off=%#x\n", i, psz, (uintptr_t)pv2 - (uintptr_t)psz));
195 RTTESTI_CHECK(RTStrCacheRelease(hStrCache, psz) == 3);
196 RTTESTI_CHECK_MSG_RETV((pv2 = ASMMemFirstMismatchingU8(psz, i, 'a')) == NULL && !psz[i], ("i=%#x psz=%p off=%#x\n", i, psz, (uintptr_t)pv2 - (uintptr_t)psz));
197 RTTESTI_CHECK(RTStrCacheRetain(psz) == 4);
198 RTTESTI_CHECK(RTStrCacheRetain(psz) == 5);
199 RTTESTI_CHECK(RTStrCacheRetain(psz) == 6);
200 RTTESTI_CHECK(RTStrCacheRelease(hStrCache, psz) == 5);
201 RTTESTI_CHECK(RTStrCacheRelease(hStrCache, psz) == 4);
202 RTTESTI_CHECK_MSG_RETV((pv2 = ASMMemFirstMismatchingU8(psz, i, 'a')) == NULL && !psz[i], ("i=%#x psz=%p off=%#x\n", i, psz, (uintptr_t)pv2 - (uintptr_t)psz));
203
204 for (uint32_t cRefs = 3;; cRefs--)
205 {
206 RTTESTI_CHECK(RTStrCacheRelease(hStrCache, psz) == cRefs);
207 if (cRefs == 0)
208 break;
209 RTTESTI_CHECK_MSG_RETV((pv2 = ASMMemFirstMismatchingU8(psz, i, 'a')) == NULL && !psz[i], ("i=%#x psz=%p off=%#x cRefs=%d\n", i, psz, (uintptr_t)pv2 - (uintptr_t)psz, cRefs));
210 for (uint32_t j = 0; j < 42; j++)
211 {
212 const char *psz2;
213 RTTESTI_CHECK_RETV(psz2 = RTStrCacheEnterN(hStrCache, szTest2, i));
214 RTTESTI_CHECK_RETV(psz2 != psz);
215 RTTESTI_CHECK(RTStrCacheRelease(hStrCache, psz2) == 0);
216 RTTESTI_CHECK_MSG_RETV((pv2 = ASMMemFirstMismatchingU8(psz, i, 'a')) == NULL && !psz[i], ("i=%#x psz=%p off=%#x cRefs=%d\n", i, psz, (uintptr_t)pv2 - (uintptr_t)psz, cRefs));
217 }
218 }
219 }
220
221 /* Lots of allocations. */
222 memset(szTest, 'b', sizeof(szTest));
223 memset(szTest2, 'e', sizeof(szTest));
224 const char *pszTest1Rets[4096 + 16];
225 const char *pszTest2Rets[4096 + 16];
226 for (uint32_t i = 1; i < RT_ELEMENTS(pszTest1Rets); i++)
227 {
228 RTTESTI_CHECK(pszTest1Rets[i] = RTStrCacheEnterN(hStrCache, szTest, i));
229 RTTESTI_CHECK(strlen(pszTest1Rets[i]) == i);
230 RTTESTI_CHECK(pszTest2Rets[i] = RTStrCacheEnterN(hStrCache, szTest2, i));
231 RTTESTI_CHECK(strlen(pszTest2Rets[i]) == i);
232 }
233
234 if (RTStrCacheIsRealImpl())
235 {
236 for (uint32_t i = 1; i < RT_ELEMENTS(pszTest1Rets); i++)
237 {
238 uint32_t cRefs;
239 const char *psz1, *psz2;
240 RTTESTI_CHECK((psz1 = RTStrCacheEnterN(hStrCache, szTest, i)) == pszTest1Rets[i]);
241 RTTESTI_CHECK((psz2 = RTStrCacheEnterN(hStrCache, szTest2, i)) == pszTest2Rets[i]);
242 RTTESTI_CHECK_MSG((cRefs = RTStrCacheRelease(hStrCache, psz1)) == 1, ("cRefs=%#x i=%#x\n", cRefs, i));
243 RTTESTI_CHECK_MSG((cRefs = RTStrCacheRelease(hStrCache, psz2)) == 1, ("cRefs=%#x i=%#x\n", cRefs, i));
244 }
245 }
246
247 for (uint32_t i = 1; i < RT_ELEMENTS(pszTest1Rets); i++)
248 {
249 uint32_t cRefs;
250 RTTESTI_CHECK(strlen(pszTest1Rets[i]) == i);
251 RTTESTI_CHECK_MSG((cRefs = RTStrCacheRelease(hStrCache, pszTest1Rets[i])) == 0, ("cRefs=%#x i=%#x\n", cRefs, i));
252 RTTESTI_CHECK(strlen(pszTest2Rets[i]) == i);
253 RTTESTI_CHECK_MSG((cRefs = RTStrCacheRelease(hStrCache, pszTest2Rets[i])) == 0, ("cRefs=%#x i=%#x\n", cRefs, i));
254 }
255}
256
257
258int main()
259{
260 RTTEST hTest;
261 int rc = RTTestInitAndCreate("tstRTStrCache", &hTest);
262 if (rc)
263 return rc;
264 RTTestBanner(hTest);
265
266 /*
267 * Smoke tests using first the default and then a custom pool.
268 */
269 RTTestSub(hTest, "Smoke test on default cache");
270 tst1(RTSTRCACHE_DEFAULT);
271
272 RTTestSub(hTest, "Smoke test on custom cache");
273 RTSTRCACHE hStrCache;
274 RTTESTI_CHECK_RC(rc = RTStrCacheCreate(&hStrCache, "test 2a"), VINF_SUCCESS);
275 if (RT_SUCCESS(rc))
276 RTTESTI_CHECK_RC(rc = RTStrCacheDestroy(hStrCache), VINF_SUCCESS);
277 RTTESTI_CHECK_RC(rc = RTStrCacheDestroy(NIL_RTSTRCACHE), VINF_SUCCESS);
278 RTTESTI_CHECK_RC(rc = RTStrCacheDestroy(RTSTRCACHE_DEFAULT), VINF_SUCCESS);
279 RTTESTI_CHECK_RC(rc = RTStrCacheDestroy(RTSTRCACHE_DEFAULT), VINF_SUCCESS);
280
281 RTTESTI_CHECK_RC(rc = RTStrCacheCreate(&hStrCache, "test 2b"), VINF_SUCCESS);
282 if (RT_SUCCESS(rc))
283 {
284 tst1(hStrCache);
285 RTTESTI_CHECK_RC(rc = RTStrCacheDestroy(hStrCache), VINF_SUCCESS);
286 }
287
288 /*
289 * Cache performance on relatively real world examples.
290 */
291 tst2();
292
293 /*
294 * Summary.
295 */
296 return RTTestSummaryAndDestroy(hTest);
297}
298
Note: See TracBrowser for help on using the repository browser.

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