1 | /* $Id: tstRTPrfIO.cpp 76553 2019-01-01 01:45:53Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT Testcase - Profile IPRT I/O APIs.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2010-2019 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | */
|
---|
26 |
|
---|
27 |
|
---|
28 | /*********************************************************************************************************************************
|
---|
29 | * Header Files *
|
---|
30 | *********************************************************************************************************************************/
|
---|
31 | #include <iprt/file.h>
|
---|
32 | #include <iprt/dir.h>
|
---|
33 | #include <iprt/fs.h>
|
---|
34 |
|
---|
35 | #include <iprt/err.h>
|
---|
36 | #include <iprt/getopt.h>
|
---|
37 | #include <iprt/param.h>
|
---|
38 | #include <iprt/path.h>
|
---|
39 | #include <iprt/string.h>
|
---|
40 | #include <iprt/test.h>
|
---|
41 | #include <iprt/time.h>
|
---|
42 |
|
---|
43 |
|
---|
44 | /*********************************************************************************************************************************
|
---|
45 | * Global Variables *
|
---|
46 | *********************************************************************************************************************************/
|
---|
47 | /** The test instance handle. */
|
---|
48 | static RTTEST g_hTest;
|
---|
49 | /** The max number of nanoseconds to benchmark an operation. */
|
---|
50 | static uint64_t g_cNsPerOperation = 1000*1000*1000;
|
---|
51 | /** The max operation count. */
|
---|
52 | static uint32_t g_cMaxOperations = 1000000;
|
---|
53 | /** The path to the test directory. */
|
---|
54 | static const char *g_pszTestDir = ".";
|
---|
55 |
|
---|
56 | /** The path to the primary test file. */
|
---|
57 | static char g_szTestFile1[RTPATH_MAX];
|
---|
58 | /** The path to the primary test directory. */
|
---|
59 | static char g_szTestDir1[RTPATH_MAX];
|
---|
60 |
|
---|
61 | /** The path to a nonexistent file in an existing directory. */
|
---|
62 | static char g_szNotExitingFile[RTPATH_MAX];
|
---|
63 | /** The path to a nonexistent directory. */
|
---|
64 | static char g_szNotExitingDir[RTPATH_MAX];
|
---|
65 | /** The path to a nonexistent file in a nonexistent directory. */
|
---|
66 | static char g_szNotExitingDirFile[RTPATH_MAX];
|
---|
67 |
|
---|
68 |
|
---|
69 | /*********************************************************************************************************************************
|
---|
70 | * Defined Constants And Macros *
|
---|
71 | *********************************************************************************************************************************/
|
---|
72 |
|
---|
73 | /**
|
---|
74 | * Benchmark an operation.
|
---|
75 | * @param stmt Statement to benchmark.
|
---|
76 | * @param what String literal describing what's being benchmarked..
|
---|
77 | */
|
---|
78 | #define TIME_OP(stmt, what) \
|
---|
79 | do \
|
---|
80 | { \
|
---|
81 | /* warm-up */ \
|
---|
82 | stmt; \
|
---|
83 | stmt; \
|
---|
84 | \
|
---|
85 | /* the real thing */ \
|
---|
86 | uint32_t cOps = 0; \
|
---|
87 | uint64_t cNsElapsed = 0; \
|
---|
88 | uint64_t u64StartTS = RTTimeNanoTS(); \
|
---|
89 | for (;;) \
|
---|
90 | { \
|
---|
91 | stmt; \
|
---|
92 | cOps++; \
|
---|
93 | if ((cOps & 127) == 127) \
|
---|
94 | { \
|
---|
95 | cNsElapsed = RTTimeNanoTS() - u64StartTS; \
|
---|
96 | if (cNsElapsed >= g_cNsPerOperation || cOps >= g_cMaxOperations) \
|
---|
97 | break; \
|
---|
98 | } \
|
---|
99 | } \
|
---|
100 | RTTestValue(g_hTest, what, cNsElapsed / cOps, RTTESTUNIT_NS_PER_CALL); \
|
---|
101 | RTTestValue(g_hTest, what " cps", UINT64_C(10000000000) / (cNsElapsed * 10 / cOps), RTTESTUNIT_CALLS_PER_SEC); \
|
---|
102 | } while (0)
|
---|
103 |
|
---|
104 |
|
---|
105 |
|
---|
106 | static void benchmarkPathQueryInfo(void)
|
---|
107 | {
|
---|
108 | RTTestSub(g_hTest, "RTPathQueryInfo");
|
---|
109 |
|
---|
110 | RTFSOBJINFO ObjInfo;
|
---|
111 |
|
---|
112 | RTTESTI_CHECK_RC_RETV(RTPathQueryInfo(g_szNotExitingFile, &ObjInfo, RTFSOBJATTRADD_NOTHING), VERR_FILE_NOT_FOUND);
|
---|
113 | TIME_OP(RTPathQueryInfo(g_szNotExitingFile, &ObjInfo, RTFSOBJATTRADD_NOTHING), "RTPathQueryInfo(g_szNotExitingFile)");
|
---|
114 |
|
---|
115 | int rc = RTPathQueryInfo(g_szNotExitingDirFile, &ObjInfo, RTFSOBJATTRADD_NOTHING);
|
---|
116 | RTTESTI_CHECK_RETV(rc == VERR_PATH_NOT_FOUND || VERR_FILE_NOT_FOUND);
|
---|
117 | TIME_OP(RTPathQueryInfo(g_szNotExitingDirFile, &ObjInfo, RTFSOBJATTRADD_NOTHING), "RTPathQueryInfo(g_szNotExitingDirFile)");
|
---|
118 |
|
---|
119 | RTTESTI_CHECK_RC_RETV(RTPathQueryInfo(g_pszTestDir, &ObjInfo, RTFSOBJATTRADD_NOTHING), VINF_SUCCESS);
|
---|
120 | TIME_OP(RTPathQueryInfo(g_pszTestDir, &ObjInfo, RTFSOBJATTRADD_NOTHING), "RTPathQueryInfo(g_pszTestDir)");
|
---|
121 |
|
---|
122 | RTTESTI_CHECK_RC_RETV(RTPathQueryInfo(g_pszTestDir, &ObjInfo, RTFSOBJATTRADD_UNIX), VINF_SUCCESS);
|
---|
123 | TIME_OP(RTPathQueryInfo(g_pszTestDir, &ObjInfo, RTFSOBJATTRADD_UNIX), "RTPathQueryInfo(g_pszTestDir,UNIX)");
|
---|
124 |
|
---|
125 | RTTestSubDone(g_hTest);
|
---|
126 | }
|
---|
127 |
|
---|
128 |
|
---|
129 | DECL_FORCE_INLINE(int) benchmarkFileOpenCloseOp(const char *pszFilename)
|
---|
130 | {
|
---|
131 | RTFILE hFile;
|
---|
132 | int rc = RTFileOpen(&hFile, pszFilename, RTFILE_O_READ | RTFILE_O_DENY_NONE | RTFILE_O_OPEN);
|
---|
133 | if (RT_SUCCESS(rc))
|
---|
134 | rc = RTFileClose(hFile);
|
---|
135 | return rc;
|
---|
136 | }
|
---|
137 |
|
---|
138 | static void benchmarkFileOpenClose(void)
|
---|
139 | {
|
---|
140 | RTTestSub(g_hTest, "RTFileOpen + RTFileClose");
|
---|
141 |
|
---|
142 | RTTESTI_CHECK_RC_RETV(benchmarkFileOpenCloseOp(g_szNotExitingFile), VERR_FILE_NOT_FOUND);
|
---|
143 | TIME_OP(benchmarkFileOpenCloseOp(g_szNotExitingFile), "RTFileOpen(g_szNotExitingFile)");
|
---|
144 |
|
---|
145 | RTTESTI_CHECK_RC_RETV(benchmarkFileOpenCloseOp(g_szNotExitingFile), VERR_FILE_NOT_FOUND);
|
---|
146 | TIME_OP(benchmarkFileOpenCloseOp(g_szNotExitingFile), "RTFileOpen(g_szNotExitingFile)");
|
---|
147 |
|
---|
148 | int rc = benchmarkFileOpenCloseOp(g_szNotExitingDirFile);
|
---|
149 | RTTESTI_CHECK_RETV(rc == VERR_PATH_NOT_FOUND || VERR_FILE_NOT_FOUND);
|
---|
150 | TIME_OP(benchmarkFileOpenCloseOp(g_szNotExitingDirFile), "RTFileOpen(g_szNotExitingDirFile)");
|
---|
151 |
|
---|
152 | RTTestSubDone(g_hTest);
|
---|
153 | }
|
---|
154 |
|
---|
155 |
|
---|
156 | static void benchmarkFileWriteByte(void)
|
---|
157 | {
|
---|
158 | RTTestSub(g_hTest, "RTFileWrite(byte)");
|
---|
159 |
|
---|
160 | RTFILE hFile;
|
---|
161 |
|
---|
162 | RTTESTI_CHECK_RC_RETV(RTFileOpen(&hFile, g_szTestFile1,
|
---|
163 | RTFILE_O_WRITE | RTFILE_O_DENY_NONE | RTFILE_O_CREATE_REPLACE
|
---|
164 | | (0655 << RTFILE_O_CREATE_MODE_SHIFT)),
|
---|
165 | VINF_SUCCESS);
|
---|
166 | static const char s_szContent[] = "0123456789abcdef";
|
---|
167 | uint32_t offContent = 0;
|
---|
168 | int rc;;
|
---|
169 | RTTESTI_CHECK_RC(rc = RTFileWrite(hFile, &s_szContent[offContent++ % RT_ELEMENTS(s_szContent)], 1, NULL), VINF_SUCCESS);
|
---|
170 | if (RT_SUCCESS(rc))
|
---|
171 | {
|
---|
172 | TIME_OP(RTFileWrite(hFile, &s_szContent[offContent++ % RT_ELEMENTS(s_szContent)], 1, NULL), "RTFileWrite(byte)");
|
---|
173 | }
|
---|
174 | RTTESTI_CHECK_RC(RTFileClose(hFile), VINF_SUCCESS);
|
---|
175 |
|
---|
176 | RTTestSubDone(g_hTest);
|
---|
177 | }
|
---|
178 |
|
---|
179 |
|
---|
180 |
|
---|
181 | int main(int argc, char **argv)
|
---|
182 | {
|
---|
183 | RTEXITCODE rcExit = RTTestInitAndCreate("tstRTPrfIO", &g_hTest);
|
---|
184 | if (rcExit != RTEXITCODE_SUCCESS)
|
---|
185 | return rcExit;
|
---|
186 | RTTestBanner(g_hTest);
|
---|
187 |
|
---|
188 | /*
|
---|
189 | * Parse arguments
|
---|
190 | */
|
---|
191 | static const RTGETOPTDEF s_aOptions[] =
|
---|
192 | {
|
---|
193 | { "--test-dir", 'd', RTGETOPT_REQ_STRING },
|
---|
194 | };
|
---|
195 | bool fFileOpenCloseTest = true;
|
---|
196 | bool fFileWriteByteTest = true;
|
---|
197 | bool fPathQueryInfoTest = true;
|
---|
198 | //bool fFileTests = true;
|
---|
199 | //bool fDirTests = true;
|
---|
200 |
|
---|
201 | int ch;
|
---|
202 | RTGETOPTUNION ValueUnion;
|
---|
203 | RTGETOPTSTATE GetState;
|
---|
204 | RTGetOptInit(&GetState, argc, argv, s_aOptions, RT_ELEMENTS(s_aOptions), 1, 0);
|
---|
205 | while ((ch = RTGetOpt(&GetState, &ValueUnion)))
|
---|
206 | {
|
---|
207 | switch (ch)
|
---|
208 | {
|
---|
209 | case 'd':
|
---|
210 | g_pszTestDir = ValueUnion.psz;
|
---|
211 | break;
|
---|
212 |
|
---|
213 | case 'V':
|
---|
214 | RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "$Revision: 76553 $\n");
|
---|
215 | return RTTestSummaryAndDestroy(g_hTest);
|
---|
216 |
|
---|
217 | case 'h':
|
---|
218 | RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "usage: testname [-d <testdir>]\n");
|
---|
219 | return RTTestSummaryAndDestroy(g_hTest);
|
---|
220 |
|
---|
221 | default:
|
---|
222 | RTTestFailed(g_hTest, "invalid argument");
|
---|
223 | RTGetOptPrintError(ch, &ValueUnion);
|
---|
224 | return RTTestSummaryAndDestroy(g_hTest);
|
---|
225 | }
|
---|
226 | }
|
---|
227 |
|
---|
228 | /*
|
---|
229 | * Set up and check the prerequisites.
|
---|
230 | */
|
---|
231 | RTTESTI_CHECK_RC(RTPathJoin(g_szTestFile1, sizeof(g_szTestFile1), g_pszTestDir, "tstRTPrfIO-TestFile1"), VINF_SUCCESS);
|
---|
232 | RTTESTI_CHECK_RC(RTPathJoin(g_szTestDir1, sizeof(g_szTestDir1), g_pszTestDir, "tstRTPrfIO-TestDir1"), VINF_SUCCESS);
|
---|
233 | RTTESTI_CHECK_RC(RTPathJoin(g_szNotExitingFile, sizeof(g_szNotExitingFile), g_pszTestDir, "tstRTPrfIO-nonexistent-file"), VINF_SUCCESS);
|
---|
234 | RTTESTI_CHECK_RC(RTPathJoin(g_szNotExitingDir, sizeof(g_szNotExitingDir), g_pszTestDir, "tstRTPrfIO-nonexistent-dir"), VINF_SUCCESS);
|
---|
235 | RTTESTI_CHECK_RC(RTPathJoin(g_szNotExitingDirFile, sizeof(g_szNotExitingDirFile), g_szNotExitingDir, "nonexistent-file"), VINF_SUCCESS);
|
---|
236 | RTTESTI_CHECK(RTDirExists(g_pszTestDir));
|
---|
237 | if (RTPathExists(g_szTestDir1))
|
---|
238 | RTTestFailed(g_hTest, "The primary test directory (%s) already exist, please remove it", g_szTestDir1);
|
---|
239 | if (RTPathExists(g_szTestFile1))
|
---|
240 | RTTestFailed(g_hTest, "The primary test file (%s) already exist, please remove it", g_szTestFile1);
|
---|
241 | if (RTPathExists(g_szNotExitingFile))
|
---|
242 | RTTestFailed(g_hTest, "'%s' exists, remove it", g_szNotExitingFile);
|
---|
243 | if (RTPathExists(g_szNotExitingDir))
|
---|
244 | RTTestFailed(g_hTest, "'%s' exists, remove it", g_szNotExitingDir);
|
---|
245 | if (RTPathExists(g_szNotExitingDirFile))
|
---|
246 | RTTestFailed(g_hTest, "'%s' exists, remove it", g_szNotExitingDirFile);
|
---|
247 |
|
---|
248 | /*
|
---|
249 | * Do the testing.
|
---|
250 | */
|
---|
251 | if (RTTestIErrorCount() == 0)
|
---|
252 | {
|
---|
253 | #if 1
|
---|
254 | if (fPathQueryInfoTest)
|
---|
255 | benchmarkPathQueryInfo();
|
---|
256 | if (fFileOpenCloseTest)
|
---|
257 | benchmarkFileOpenClose();
|
---|
258 | #endif
|
---|
259 | if (fFileWriteByteTest)
|
---|
260 | benchmarkFileWriteByte();
|
---|
261 | //if (fFileTests)
|
---|
262 | // benchmarkFile();
|
---|
263 | //if (fDirTests)
|
---|
264 | // benchmarkDir();
|
---|
265 |
|
---|
266 | /*
|
---|
267 | * Cleanup.
|
---|
268 | */
|
---|
269 | RTFileDelete(g_szTestFile1);
|
---|
270 | RTDirRemoveRecursive(g_szTestDir1, 0);
|
---|
271 | RTTESTI_CHECK(RTDirExists(g_pszTestDir));
|
---|
272 | RTTESTI_CHECK(!RTPathExists(g_szTestDir1));
|
---|
273 | RTTESTI_CHECK(!RTPathExists(g_szTestFile1));
|
---|
274 | }
|
---|
275 |
|
---|
276 | return RTTestSummaryAndDestroy(g_hTest);
|
---|
277 | }
|
---|
278 |
|
---|