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