summaryrefslogtreecommitdiffstats
path: root/src/VBox/Main/testcase/tstVBoxAPIPerf.cpp
blob: 0553c7eebd3c0c6d16468356882502a441a0cf83 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/* $Id: tstVBoxAPIPerf.cpp $ */
/** @file
 * tstVBoxAPIPerf - Checks the performance of the COM / XPOM API.
 */

/*
 * Copyright (C) 2006-2023 Oracle and/or its affiliates.
 *
 * This file is part of VirtualBox base platform packages, as
 * available from https://www.virtualbox.org.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation, in version 3 of the
 * License.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <https://www.gnu.org/licenses>.
 *
 * SPDX-License-Identifier: GPL-3.0-only
 */


/*********************************************************************************************************************************
*   Header Files                                                                                                                 *
*********************************************************************************************************************************/
#include <VBox/com/com.h>
#include <VBox/com/string.h>
#include <VBox/com/array.h>
#include <VBox/com/Guid.h>
#include <VBox/com/ErrorInfo.h>
#include <VBox/com/VirtualBox.h>
#include <VBox/sup.h>

#include <iprt/test.h>
#include <iprt/time.h>



/*********************************************************************************************************************************
*   Global Variables                                                                                                             *
*********************************************************************************************************************************/
static RTTEST g_hTest;


/** Worker fro TST_COM_EXPR(). */
static HRESULT tstComExpr(HRESULT hrc, const char *pszOperation, int iLine)
{
    if (FAILED(hrc))
        RTTestFailed(g_hTest, "%s failed on line %u with hrc=%Rhrc", pszOperation, iLine, hrc);
    return hrc;
}

/** Macro that executes the given expression and report any failure.
 *  The expression must return a HRESULT. */
#define TST_COM_EXPR(expr) tstComExpr(expr, #expr, __LINE__)



static void tstApiPrf1(IVirtualBox *pVBox)
{
    RTTestSub(g_hTest, "IVirtualBox::Revision performance");

    uint32_t const cCalls   = 65536;
    uint32_t       cLeft    = cCalls;
    uint64_t       uStartTS = RTTimeNanoTS();
    while (cLeft-- > 0)
    {
        ULONG uRev;
        HRESULT hrc = pVBox->COMGETTER(Revision)(&uRev);
        if (FAILED(hrc))
        {
            tstComExpr(hrc, "IVirtualBox::Revision", __LINE__);
            return;
        }
    }
    uint64_t uElapsed = RTTimeNanoTS() - uStartTS;
    RTTestValue(g_hTest, "IVirtualBox::Revision average", uElapsed / cCalls, RTTESTUNIT_NS_PER_CALL);
    RTTestSubDone(g_hTest);
}


static void tstApiPrf2(IVirtualBox *pVBox)
{
    RTTestSub(g_hTest, "IVirtualBox::Version performance");

    uint32_t const cCalls   = 65536;
    uint32_t       cLeft    = cCalls;
    uint64_t       uStartTS = RTTimeNanoTS();
    while (cLeft-- > 0)
    {
        com::Bstr bstrVersion;
        HRESULT hrc = pVBox->COMGETTER(Version)(bstrVersion.asOutParam());
        if (FAILED(hrc))
        {
            tstComExpr(hrc, "IVirtualBox::Version", __LINE__);
            return;
        }
    }
    uint64_t uElapsed = RTTimeNanoTS() - uStartTS;
    RTTestValue(g_hTest, "IVirtualBox::Version average", uElapsed / cCalls, RTTESTUNIT_NS_PER_CALL);
    RTTestSubDone(g_hTest);
}


static void tstApiPrf3(IVirtualBox *pVBox)
{
    RTTestSub(g_hTest, "IVirtualBox::Host performance");

    /* The first call. */
    uint64_t    uStartTS = RTTimeNanoTS();
    IHost      *pHost = NULL;
    HRESULT     hrc = pVBox->COMGETTER(Host)(&pHost);
    if (FAILED(hrc))
    {
        tstComExpr(hrc, "IVirtualBox::Host", __LINE__);
        return;
    }
    pHost->Release();
    uint64_t uElapsed = RTTimeNanoTS() - uStartTS;
    RTTestValue(g_hTest, "IVirtualBox::Host first", uElapsed, RTTESTUNIT_NS);

    /* Subsequent calls. */
    uint32_t const cCalls1  = 4096;
    uint32_t       cLeft    = cCalls1;
    uStartTS = RTTimeNanoTS();
    while (cLeft-- > 0)
    {
        IHost *pHost2 = NULL;
        hrc = pVBox->COMGETTER(Host)(&pHost2);
        if (FAILED(hrc))
        {
            tstComExpr(hrc, "IVirtualBox::Host", __LINE__);
            return;
        }
        pHost2->Release();
    }
    uElapsed = RTTimeNanoTS() - uStartTS;
    RTTestValue(g_hTest, "IVirtualBox::Host average", uElapsed / cCalls1, RTTESTUNIT_NS_PER_CALL);

    /* Keep a reference around and see how that changes things.
       Note! VBoxSVC is not creating and destroying Host().  */
    pHost = NULL;
    hrc = pVBox->COMGETTER(Host)(&pHost);

    uint32_t const cCalls2  = 16384;
    cLeft    = cCalls2;
    uStartTS = RTTimeNanoTS();
    while (cLeft-- > 0)
    {
        IHost *pHost2 = NULL;
        hrc = pVBox->COMGETTER(Host)(&pHost2);
        if (FAILED(hrc))
        {
            tstComExpr(hrc, "IVirtualBox::Host", __LINE__);
            pHost->Release();
            return;
        }
        pHost2->Release();
    }
    uElapsed = RTTimeNanoTS() - uStartTS;
    RTTestValue(g_hTest, "IVirtualBox::Host 2nd ref", uElapsed / cCalls2, RTTESTUNIT_NS_PER_CALL);
    pHost->Release();

    RTTestSubDone(g_hTest);
}


static void tstApiPrf4(IVirtualBox *pVBox)
{
    RTTestSub(g_hTest, "IHost::GetProcessorFeature performance");

    IHost      *pHost = NULL;
    HRESULT     hrc = pVBox->COMGETTER(Host)(&pHost);
    if (FAILED(hrc))
    {
        tstComExpr(hrc, "IVirtualBox::Host", __LINE__);
        return;
    }

    uint32_t const  cCalls   = 65536;
    uint32_t        cLeft    = cCalls;
    uint64_t        uStartTS = RTTimeNanoTS();
    while (cLeft-- > 0)
    {
        BOOL fSupported;
        hrc = pHost->GetProcessorFeature(ProcessorFeature_PAE, &fSupported);
        if (FAILED(hrc))
        {
            tstComExpr(hrc, "IHost::GetProcessorFeature", __LINE__);
            pHost->Release();
            return;
        }
    }
    uint64_t uElapsed = RTTimeNanoTS() - uStartTS;
    RTTestValue(g_hTest, "IHost::GetProcessorFeature average", uElapsed / cCalls, RTTESTUNIT_NS_PER_CALL);
    pHost->Release();
    RTTestSubDone(g_hTest);
}



int main()
{
    /*
     * Initialization.
     */
    RTEXITCODE rcExit = RTTestInitAndCreate("tstVBoxAPIPerf", &g_hTest);
    if (rcExit != RTEXITCODE_SUCCESS)
        return rcExit;
    SUPR3Init(NULL); /* Better time support. */
    RTTestBanner(g_hTest);

    RTTestSub(g_hTest, "Initializing COM and singletons");
    HRESULT hrc = com::Initialize();
    if (SUCCEEDED(hrc))
    {
        ComPtr<IVirtualBoxClient> ptrVBoxClient;
        ComPtr<IVirtualBox> ptrVBox;
        hrc = TST_COM_EXPR(ptrVBoxClient.createInprocObject(CLSID_VirtualBoxClient));
        if (SUCCEEDED(hrc))
            hrc = TST_COM_EXPR(ptrVBoxClient->COMGETTER(VirtualBox)(ptrVBox.asOutParam()));
        if (SUCCEEDED(hrc))
        {
            ComPtr<ISession> ptrSession;
            hrc = TST_COM_EXPR(ptrSession.createInprocObject(CLSID_Session));
            if (SUCCEEDED(hrc))
            {
                RTTestSubDone(g_hTest);

                /*
                 * Call test functions.
                 */
                tstApiPrf1(ptrVBox);
                tstApiPrf2(ptrVBox);
                tstApiPrf3(ptrVBox);

                /** @todo Find something that returns a 2nd instance of an interface and see
                 *        how if wrapper stuff is reused in any way. */
                tstApiPrf4(ptrVBox);
            }
        }

        ptrVBox.setNull();
        ptrVBoxClient.setNull();
        com::Shutdown();
    }
    else
        RTTestIFailed("com::Initialize failed with hrc=%Rhrc", hrc);
    return RTTestSummaryAndDestroy(g_hTest);
}