VirtualBox

source: vbox/trunk/src/VBox/Main/testcase/tstVBoxAPIPerf.cpp@ 107866

Last change on this file since 107866 was 107748, checked in by vboxsync, 5 weeks ago

Main/testcase/tstVBoxAPIPerf.cpp: Missing error check, bugref:3409

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 8.0 KB
Line 
1/* $Id: tstVBoxAPIPerf.cpp 107748 2025-01-14 10:24:35Z vboxsync $ */
2/** @file
3 * tstVBoxAPIPerf - Checks the performance of the COM / XPOM API.
4 */
5
6/*
7 * Copyright (C) 2006-2024 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 * SPDX-License-Identifier: GPL-3.0-only
26 */
27
28
29/*********************************************************************************************************************************
30* Header Files *
31*********************************************************************************************************************************/
32#include <VBox/com/com.h>
33#include <VBox/com/string.h>
34#include <VBox/com/array.h>
35#include <VBox/com/Guid.h>
36#include <VBox/com/ErrorInfo.h>
37#include <VBox/com/VirtualBox.h>
38#include <VBox/sup.h>
39
40#include <iprt/test.h>
41#include <iprt/time.h>
42
43
44
45/*********************************************************************************************************************************
46* Global Variables *
47*********************************************************************************************************************************/
48static RTTEST g_hTest;
49
50
51/** Worker fro TST_COM_EXPR(). */
52static HRESULT tstComExpr(HRESULT hrc, const char *pszOperation, int iLine)
53{
54 if (FAILED(hrc))
55 RTTestFailed(g_hTest, "%s failed on line %u with hrc=%Rhrc", pszOperation, iLine, hrc);
56 return hrc;
57}
58
59/** Macro that executes the given expression and report any failure.
60 * The expression must return a HRESULT. */
61#define TST_COM_EXPR(expr) tstComExpr(expr, #expr, __LINE__)
62
63
64
65static void tstApiPrf1(IVirtualBox *pVBox)
66{
67 RTTestSub(g_hTest, "IVirtualBox::Revision performance");
68
69 uint32_t const cCalls = 65536;
70 uint32_t cLeft = cCalls;
71 uint64_t uStartTS = RTTimeNanoTS();
72 while (cLeft-- > 0)
73 {
74 ULONG uRev;
75 HRESULT hrc = pVBox->COMGETTER(Revision)(&uRev);
76 if (FAILED(hrc))
77 {
78 tstComExpr(hrc, "IVirtualBox::Revision", __LINE__);
79 return;
80 }
81 }
82 uint64_t uElapsed = RTTimeNanoTS() - uStartTS;
83 RTTestValue(g_hTest, "IVirtualBox::Revision average", uElapsed / cCalls, RTTESTUNIT_NS_PER_CALL);
84 RTTestSubDone(g_hTest);
85}
86
87
88static void tstApiPrf2(IVirtualBox *pVBox)
89{
90 RTTestSub(g_hTest, "IVirtualBox::Version performance");
91
92 uint32_t const cCalls = 65536;
93 uint32_t cLeft = cCalls;
94 uint64_t uStartTS = RTTimeNanoTS();
95 while (cLeft-- > 0)
96 {
97 com::Bstr bstrVersion;
98 HRESULT hrc = pVBox->COMGETTER(Version)(bstrVersion.asOutParam());
99 if (FAILED(hrc))
100 {
101 tstComExpr(hrc, "IVirtualBox::Version", __LINE__);
102 return;
103 }
104 }
105 uint64_t uElapsed = RTTimeNanoTS() - uStartTS;
106 RTTestValue(g_hTest, "IVirtualBox::Version average", uElapsed / cCalls, RTTESTUNIT_NS_PER_CALL);
107 RTTestSubDone(g_hTest);
108}
109
110
111static void tstApiPrf3(IVirtualBox *pVBox)
112{
113 RTTestSub(g_hTest, "IVirtualBox::Host performance");
114
115 /* The first call. */
116 uint64_t uStartTS = RTTimeNanoTS();
117 IHost *pHost = NULL;
118 HRESULT hrc = pVBox->COMGETTER(Host)(&pHost);
119 if (FAILED(hrc))
120 {
121 tstComExpr(hrc, "IVirtualBox::Host", __LINE__);
122 return;
123 }
124 pHost->Release();
125 uint64_t uElapsed = RTTimeNanoTS() - uStartTS;
126 RTTestValue(g_hTest, "IVirtualBox::Host first", uElapsed, RTTESTUNIT_NS);
127
128 /* Subsequent calls. */
129 uint32_t const cCalls1 = 4096;
130 uint32_t cLeft = cCalls1;
131 uStartTS = RTTimeNanoTS();
132 while (cLeft-- > 0)
133 {
134 IHost *pHost2 = NULL;
135 hrc = pVBox->COMGETTER(Host)(&pHost2);
136 if (FAILED(hrc))
137 {
138 tstComExpr(hrc, "IVirtualBox::Host", __LINE__);
139 return;
140 }
141 pHost2->Release();
142 }
143 uElapsed = RTTimeNanoTS() - uStartTS;
144 RTTestValue(g_hTest, "IVirtualBox::Host average", uElapsed / cCalls1, RTTESTUNIT_NS_PER_CALL);
145
146 /* Keep a reference around and see how that changes things.
147 Note! VBoxSVC is not creating and destroying Host(). */
148 pHost = NULL;
149 hrc = pVBox->COMGETTER(Host)(&pHost);
150 if (FAILED(hrc))
151 {
152 tstComExpr(hrc, "IVirtualBox::Host", __LINE__);
153 return;
154 }
155
156 uint32_t const cCalls2 = 16384;
157 cLeft = cCalls2;
158 uStartTS = RTTimeNanoTS();
159 while (cLeft-- > 0)
160 {
161 IHost *pHost2 = NULL;
162 hrc = pVBox->COMGETTER(Host)(&pHost2);
163 if (FAILED(hrc))
164 {
165 tstComExpr(hrc, "IVirtualBox::Host", __LINE__);
166 pHost->Release();
167 return;
168 }
169 pHost2->Release();
170 }
171 uElapsed = RTTimeNanoTS() - uStartTS;
172 RTTestValue(g_hTest, "IVirtualBox::Host 2nd ref", uElapsed / cCalls2, RTTESTUNIT_NS_PER_CALL);
173 pHost->Release();
174
175 RTTestSubDone(g_hTest);
176}
177
178
179static void tstApiPrf4(IVirtualBox *pVBox)
180{
181 RTTestSub(g_hTest, "IHost::GetProcessorFeature performance");
182
183 IHost *pHost = NULL;
184 HRESULT hrc = pVBox->COMGETTER(Host)(&pHost);
185 if (FAILED(hrc))
186 {
187 tstComExpr(hrc, "IVirtualBox::Host", __LINE__);
188 return;
189 }
190
191 uint32_t const cCalls = 65536;
192 uint32_t cLeft = cCalls;
193 uint64_t uStartTS = RTTimeNanoTS();
194 while (cLeft-- > 0)
195 {
196 BOOL fSupported;
197 hrc = pHost->GetProcessorFeature(ProcessorFeature_PAE, &fSupported);
198 if (FAILED(hrc))
199 {
200 tstComExpr(hrc, "IHost::GetProcessorFeature", __LINE__);
201 pHost->Release();
202 return;
203 }
204 }
205 uint64_t uElapsed = RTTimeNanoTS() - uStartTS;
206 RTTestValue(g_hTest, "IHost::GetProcessorFeature average", uElapsed / cCalls, RTTESTUNIT_NS_PER_CALL);
207 pHost->Release();
208 RTTestSubDone(g_hTest);
209}
210
211
212
213int main()
214{
215 /*
216 * Initialization.
217 */
218 RTEXITCODE rcExit = RTTestInitAndCreate("tstVBoxAPIPerf", &g_hTest);
219 if (rcExit != RTEXITCODE_SUCCESS)
220 return rcExit;
221 SUPR3Init(NULL); /* Better time support. */
222 RTTestBanner(g_hTest);
223
224 RTTestSub(g_hTest, "Initializing COM and singletons");
225 HRESULT hrc = com::Initialize();
226 if (SUCCEEDED(hrc))
227 {
228 ComPtr<IVirtualBoxClient> ptrVBoxClient;
229 ComPtr<IVirtualBox> ptrVBox;
230 hrc = TST_COM_EXPR(ptrVBoxClient.createInprocObject(CLSID_VirtualBoxClient));
231 if (SUCCEEDED(hrc))
232 hrc = TST_COM_EXPR(ptrVBoxClient->COMGETTER(VirtualBox)(ptrVBox.asOutParam()));
233 if (SUCCEEDED(hrc))
234 {
235 ComPtr<ISession> ptrSession;
236 hrc = TST_COM_EXPR(ptrSession.createInprocObject(CLSID_Session));
237 if (SUCCEEDED(hrc))
238 {
239 RTTestSubDone(g_hTest);
240
241 /*
242 * Call test functions.
243 */
244 tstApiPrf1(ptrVBox);
245 tstApiPrf2(ptrVBox);
246 tstApiPrf3(ptrVBox);
247
248 /** @todo Find something that returns a 2nd instance of an interface and see
249 * how if wrapper stuff is reused in any way. */
250 tstApiPrf4(ptrVBox);
251 }
252 }
253
254 ptrVBox.setNull();
255 ptrVBoxClient.setNull();
256 com::Shutdown();
257 }
258 else
259 RTTestIFailed("com::Initialize failed with hrc=%Rhrc", hrc);
260 return RTTestSummaryAndDestroy(g_hTest);
261}
262
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