VirtualBox

source: vbox/trunk/src/VBox/Runtime/testcase/tstRTPrfIO.cpp@ 96781

Last change on this file since 96781 was 96407, checked in by vboxsync, 2 years ago

scm copyright and license note update

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 10.9 KB
Line 
1/* $Id: tstRTPrfIO.cpp 96407 2022-08-22 17:43:14Z vboxsync $ */
2/** @file
3 * IPRT Testcase - Profile IPRT I/O APIs.
4 */
5
6/*
7 * Copyright (C) 2010-2022 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/file.h>
42#include <iprt/dir.h>
43#include <iprt/fs.h>
44
45#include <iprt/err.h>
46#include <iprt/getopt.h>
47#include <iprt/param.h>
48#include <iprt/path.h>
49#include <iprt/string.h>
50#include <iprt/test.h>
51#include <iprt/time.h>
52
53
54/*********************************************************************************************************************************
55* Global Variables *
56*********************************************************************************************************************************/
57/** The test instance handle. */
58static RTTEST g_hTest;
59/** The max number of nanoseconds to benchmark an operation. */
60static uint64_t g_cNsPerOperation = 1000*1000*1000;
61/** The max operation count. */
62static uint32_t g_cMaxOperations = 1000000;
63/** The path to the test directory. */
64static const char *g_pszTestDir = ".";
65
66/** The path to the primary test file. */
67static char g_szTestFile1[RTPATH_MAX];
68/** The path to the primary test directory. */
69static char g_szTestDir1[RTPATH_MAX];
70
71/** The path to a nonexistent file in an existing directory. */
72static char g_szNotExitingFile[RTPATH_MAX];
73/** The path to a nonexistent directory. */
74static char g_szNotExitingDir[RTPATH_MAX];
75/** The path to a nonexistent file in a nonexistent directory. */
76static char g_szNotExitingDirFile[RTPATH_MAX];
77
78
79/*********************************************************************************************************************************
80* Defined Constants And Macros *
81*********************************************************************************************************************************/
82
83/**
84 * Benchmark an operation.
85 * @param stmt Statement to benchmark.
86 * @param what String literal describing what's being benchmarked..
87 */
88#define TIME_OP(stmt, what) \
89 do \
90 { \
91 /* warm-up */ \
92 stmt; \
93 stmt; \
94 \
95 /* the real thing */ \
96 uint32_t cOps = 0; \
97 uint64_t cNsElapsed = 0; \
98 uint64_t u64StartTS = RTTimeNanoTS(); \
99 for (;;) \
100 { \
101 stmt; \
102 cOps++; \
103 if ((cOps & 127) == 127) \
104 { \
105 cNsElapsed = RTTimeNanoTS() - u64StartTS; \
106 if (cNsElapsed >= g_cNsPerOperation || cOps >= g_cMaxOperations) \
107 break; \
108 } \
109 } \
110 RTTestValue(g_hTest, what, cNsElapsed / cOps, RTTESTUNIT_NS_PER_CALL); \
111 RTTestValue(g_hTest, what " cps", UINT64_C(10000000000) / (cNsElapsed * 10 / cOps), RTTESTUNIT_CALLS_PER_SEC); \
112 } while (0)
113
114
115
116static void benchmarkPathQueryInfo(void)
117{
118 RTTestSub(g_hTest, "RTPathQueryInfo");
119
120 RTFSOBJINFO ObjInfo;
121
122 RTTESTI_CHECK_RC_RETV(RTPathQueryInfo(g_szNotExitingFile, &ObjInfo, RTFSOBJATTRADD_NOTHING), VERR_FILE_NOT_FOUND);
123 TIME_OP(RTPathQueryInfo(g_szNotExitingFile, &ObjInfo, RTFSOBJATTRADD_NOTHING), "RTPathQueryInfo(g_szNotExitingFile)");
124
125 int rc = RTPathQueryInfo(g_szNotExitingDirFile, &ObjInfo, RTFSOBJATTRADD_NOTHING);
126 RTTESTI_CHECK_RETV(rc == VERR_PATH_NOT_FOUND || VERR_FILE_NOT_FOUND);
127 TIME_OP(RTPathQueryInfo(g_szNotExitingDirFile, &ObjInfo, RTFSOBJATTRADD_NOTHING), "RTPathQueryInfo(g_szNotExitingDirFile)");
128
129 RTTESTI_CHECK_RC_RETV(RTPathQueryInfo(g_pszTestDir, &ObjInfo, RTFSOBJATTRADD_NOTHING), VINF_SUCCESS);
130 TIME_OP(RTPathQueryInfo(g_pszTestDir, &ObjInfo, RTFSOBJATTRADD_NOTHING), "RTPathQueryInfo(g_pszTestDir)");
131
132 RTTESTI_CHECK_RC_RETV(RTPathQueryInfo(g_pszTestDir, &ObjInfo, RTFSOBJATTRADD_UNIX), VINF_SUCCESS);
133 TIME_OP(RTPathQueryInfo(g_pszTestDir, &ObjInfo, RTFSOBJATTRADD_UNIX), "RTPathQueryInfo(g_pszTestDir,UNIX)");
134
135 RTTestSubDone(g_hTest);
136}
137
138
139DECL_FORCE_INLINE(int) benchmarkFileOpenCloseOp(const char *pszFilename)
140{
141 RTFILE hFile;
142 int rc = RTFileOpen(&hFile, pszFilename, RTFILE_O_READ | RTFILE_O_DENY_NONE | RTFILE_O_OPEN);
143 if (RT_SUCCESS(rc))
144 rc = RTFileClose(hFile);
145 return rc;
146}
147
148static void benchmarkFileOpenClose(void)
149{
150 RTTestSub(g_hTest, "RTFileOpen + RTFileClose");
151
152 RTTESTI_CHECK_RC_RETV(benchmarkFileOpenCloseOp(g_szNotExitingFile), VERR_FILE_NOT_FOUND);
153 TIME_OP(benchmarkFileOpenCloseOp(g_szNotExitingFile), "RTFileOpen(g_szNotExitingFile)");
154
155 RTTESTI_CHECK_RC_RETV(benchmarkFileOpenCloseOp(g_szNotExitingFile), VERR_FILE_NOT_FOUND);
156 TIME_OP(benchmarkFileOpenCloseOp(g_szNotExitingFile), "RTFileOpen(g_szNotExitingFile)");
157
158 int rc = benchmarkFileOpenCloseOp(g_szNotExitingDirFile);
159 RTTESTI_CHECK_RETV(rc == VERR_PATH_NOT_FOUND || VERR_FILE_NOT_FOUND);
160 TIME_OP(benchmarkFileOpenCloseOp(g_szNotExitingDirFile), "RTFileOpen(g_szNotExitingDirFile)");
161
162 RTTestSubDone(g_hTest);
163}
164
165
166static void benchmarkFileWriteByte(void)
167{
168 RTTestSub(g_hTest, "RTFileWrite(byte)");
169
170 RTFILE hFile;
171
172 RTTESTI_CHECK_RC_RETV(RTFileOpen(&hFile, g_szTestFile1,
173 RTFILE_O_WRITE | RTFILE_O_DENY_NONE | RTFILE_O_CREATE_REPLACE
174 | (0655 << RTFILE_O_CREATE_MODE_SHIFT)),
175 VINF_SUCCESS);
176 static const char s_szContent[] = "0123456789abcdef";
177 uint32_t offContent = 0;
178 int rc;;
179 RTTESTI_CHECK_RC(rc = RTFileWrite(hFile, &s_szContent[offContent++ % RT_ELEMENTS(s_szContent)], 1, NULL), VINF_SUCCESS);
180 if (RT_SUCCESS(rc))
181 {
182 TIME_OP(RTFileWrite(hFile, &s_szContent[offContent++ % RT_ELEMENTS(s_szContent)], 1, NULL), "RTFileWrite(byte)");
183 }
184 RTTESTI_CHECK_RC(RTFileClose(hFile), VINF_SUCCESS);
185
186 RTTestSubDone(g_hTest);
187}
188
189
190
191int main(int argc, char **argv)
192{
193 RTEXITCODE rcExit = RTTestInitAndCreate("tstRTPrfIO", &g_hTest);
194 if (rcExit != RTEXITCODE_SUCCESS)
195 return rcExit;
196 RTTestBanner(g_hTest);
197
198 /*
199 * Parse arguments
200 */
201 static const RTGETOPTDEF s_aOptions[] =
202 {
203 { "--test-dir", 'd', RTGETOPT_REQ_STRING },
204 };
205 bool fFileOpenCloseTest = true;
206 bool fFileWriteByteTest = true;
207 bool fPathQueryInfoTest = true;
208 //bool fFileTests = true;
209 //bool fDirTests = true;
210
211 int ch;
212 RTGETOPTUNION ValueUnion;
213 RTGETOPTSTATE GetState;
214 RTGetOptInit(&GetState, argc, argv, s_aOptions, RT_ELEMENTS(s_aOptions), 1, 0);
215 while ((ch = RTGetOpt(&GetState, &ValueUnion)))
216 {
217 switch (ch)
218 {
219 case 'd':
220 g_pszTestDir = ValueUnion.psz;
221 break;
222
223 case 'V':
224 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "$Revision: 96407 $\n");
225 return RTTestSummaryAndDestroy(g_hTest);
226
227 case 'h':
228 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "usage: testname [-d <testdir>]\n");
229 return RTTestSummaryAndDestroy(g_hTest);
230
231 default:
232 RTTestFailed(g_hTest, "invalid argument");
233 RTGetOptPrintError(ch, &ValueUnion);
234 return RTTestSummaryAndDestroy(g_hTest);
235 }
236 }
237
238 /*
239 * Set up and check the prerequisites.
240 */
241 RTTESTI_CHECK_RC(RTPathJoin(g_szTestFile1, sizeof(g_szTestFile1), g_pszTestDir, "tstRTPrfIO-TestFile1"), VINF_SUCCESS);
242 RTTESTI_CHECK_RC(RTPathJoin(g_szTestDir1, sizeof(g_szTestDir1), g_pszTestDir, "tstRTPrfIO-TestDir1"), VINF_SUCCESS);
243 RTTESTI_CHECK_RC(RTPathJoin(g_szNotExitingFile, sizeof(g_szNotExitingFile), g_pszTestDir, "tstRTPrfIO-nonexistent-file"), VINF_SUCCESS);
244 RTTESTI_CHECK_RC(RTPathJoin(g_szNotExitingDir, sizeof(g_szNotExitingDir), g_pszTestDir, "tstRTPrfIO-nonexistent-dir"), VINF_SUCCESS);
245 RTTESTI_CHECK_RC(RTPathJoin(g_szNotExitingDirFile, sizeof(g_szNotExitingDirFile), g_szNotExitingDir, "nonexistent-file"), VINF_SUCCESS);
246 RTTESTI_CHECK(RTDirExists(g_pszTestDir));
247 if (RTPathExists(g_szTestDir1))
248 RTTestFailed(g_hTest, "The primary test directory (%s) already exist, please remove it", g_szTestDir1);
249 if (RTPathExists(g_szTestFile1))
250 RTTestFailed(g_hTest, "The primary test file (%s) already exist, please remove it", g_szTestFile1);
251 if (RTPathExists(g_szNotExitingFile))
252 RTTestFailed(g_hTest, "'%s' exists, remove it", g_szNotExitingFile);
253 if (RTPathExists(g_szNotExitingDir))
254 RTTestFailed(g_hTest, "'%s' exists, remove it", g_szNotExitingDir);
255 if (RTPathExists(g_szNotExitingDirFile))
256 RTTestFailed(g_hTest, "'%s' exists, remove it", g_szNotExitingDirFile);
257
258 /*
259 * Do the testing.
260 */
261 if (RTTestIErrorCount() == 0)
262 {
263#if 1
264 if (fPathQueryInfoTest)
265 benchmarkPathQueryInfo();
266 if (fFileOpenCloseTest)
267 benchmarkFileOpenClose();
268#endif
269 if (fFileWriteByteTest)
270 benchmarkFileWriteByte();
271 //if (fFileTests)
272 // benchmarkFile();
273 //if (fDirTests)
274 // benchmarkDir();
275
276 /*
277 * Cleanup.
278 */
279 RTFileDelete(g_szTestFile1);
280 RTDirRemoveRecursive(g_szTestDir1, 0);
281 RTTESTI_CHECK(RTDirExists(g_pszTestDir));
282 RTTESTI_CHECK(!RTPathExists(g_szTestDir1));
283 RTTESTI_CHECK(!RTPathExists(g_szTestFile1));
284 }
285
286 return RTTestSummaryAndDestroy(g_hTest);
287}
288
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