VirtualBox

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

Last change on this file since 32901 was 32901, checked in by vboxsync, 14 years ago

tstRTPrfIO: profile some I/O related APIs.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 9.2 KB
Line 
1/* $Id: tstRTPrfIO.cpp 32901 2010-10-05 10:03:45Z vboxsync $ */
2/** @file
3 * IPRT Testcase - Profile IPRT I/O APIs.
4 */
5
6/*
7 * Copyright (C) 2010 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. */
47static RTTEST g_hTest;
48/** The max number of nanoseconds to benchmark an operation. */
49static uint64_t g_cNsPerOperation = 1000*1000*1000;
50/** The max operation count. */
51static uint32_t g_cMaxOperations = 1000000;
52/** The path to the test directory. */
53static const char *g_pszTestDir = ".";
54
55/** The path to the primary test file. */
56static char g_szTestFile1[RTPATH_MAX];
57/** The path to the primary test directory. */
58static char g_szTestDir1[RTPATH_MAX];
59
60/** The path to a nonexistent file in an existing directory. */
61static char g_szNotExitingFile[RTPATH_MAX];
62/** The path to a nonexistent directory. */
63static char g_szNotExitingDir[RTPATH_MAX];
64/** The path to a nonexistent file in a nonexistent directory. */
65static 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 litteral 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
105static 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
128DECL_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
137static 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
156int main(int argc, char **argv)
157{
158 RTEXITCODE rcExit = RTTestInitAndCreate("tstRTPrfIO", &g_hTest);
159 if (rcExit != RTEXITCODE_SUCCESS)
160 return rcExit;
161 RTTestBanner(g_hTest);
162
163 /*
164 * Parse arguments
165 */
166 static const RTGETOPTDEF s_aOptions[] =
167 {
168 { "--test-dir", 'd', RTGETOPT_REQ_STRING },
169 };
170 bool fFileOpenCloseTest = true;
171 bool fPathQueryInfoTest = true;
172 //bool fFileTests = true;
173 //bool fDirTests = true;
174
175 int ch;
176 RTGETOPTUNION ValueUnion;
177 RTGETOPTSTATE GetState;
178 RTGetOptInit(&GetState, argc, argv, s_aOptions, RT_ELEMENTS(s_aOptions), 1, 0);
179 while ((ch = RTGetOpt(&GetState, &ValueUnion)))
180 {
181 switch (ch)
182 {
183 case 'd':
184 g_pszTestDir = ValueUnion.psz;
185 break;
186
187 case 'V':
188 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "$Revision: 32901 $\n");
189 return RTTestSummaryAndDestroy(g_hTest);
190
191 case 'h':
192 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "usage: testname [-d <testdir>]\n");
193 return RTTestSummaryAndDestroy(g_hTest);
194
195 default:
196 RTTestFailed(g_hTest, "invalid argument");
197 RTGetOptPrintError(ch, &ValueUnion);
198 return RTTestSummaryAndDestroy(g_hTest);
199 }
200 }
201
202 /*
203 * Set up and check the prerequisites.
204 */
205 RTTESTI_CHECK_RC(RTPathJoin(g_szTestFile1, sizeof(g_szTestFile1), g_pszTestDir, "tstRTPrfIO-TestFile1"), VINF_SUCCESS);
206 RTTESTI_CHECK_RC(RTPathJoin(g_szTestDir1, sizeof(g_szTestDir1), g_pszTestDir, "tstRTPrfIO-TestDir1"), VINF_SUCCESS);
207 RTTESTI_CHECK_RC(RTPathJoin(g_szNotExitingFile, sizeof(g_szNotExitingFile), g_pszTestDir, "tstRTPrfIO-nonexistent-file"), VINF_SUCCESS);
208 RTTESTI_CHECK_RC(RTPathJoin(g_szNotExitingDir, sizeof(g_szNotExitingDir), g_pszTestDir, "tstRTPrfIO-nonexistent-dir"), VINF_SUCCESS);
209 RTTESTI_CHECK_RC(RTPathJoin(g_szNotExitingDirFile, sizeof(g_szNotExitingDirFile), g_szNotExitingDir, "nonexistent-file"), VINF_SUCCESS);
210 RTTESTI_CHECK(RTDirExists(g_pszTestDir));
211 if (RTPathExists(g_szTestDir1))
212 RTTestFailed(g_hTest, "The primary test directory (%s) already exist, please remove it", g_szTestDir1);
213 if (RTPathExists(g_szTestFile1))
214 RTTestFailed(g_hTest, "The primary test file (%s) already exist, please remove it", g_szTestFile1);
215 if (RTPathExists(g_szNotExitingFile))
216 RTTestFailed(g_hTest, "'%s' exists, remove it", g_szNotExitingFile);
217 if (RTPathExists(g_szNotExitingDir))
218 RTTestFailed(g_hTest, "'%s' exists, remove it", g_szNotExitingDir);
219 if (RTPathExists(g_szNotExitingDirFile))
220 RTTestFailed(g_hTest, "'%s' exists, remove it", g_szNotExitingDirFile);
221
222 /*
223 * Do the testing.
224 */
225 if (RTTestIErrorCount() == 0)
226 {
227 if (fPathQueryInfoTest)
228 benchmarkPathQueryInfo();
229 if (fFileOpenCloseTest)
230 benchmarkFileOpenClose();
231 //if (fFileTests)
232 // benchmarkFile();
233 //if (fDirTests)
234 // benchmarkDir();
235
236 /*
237 * Cleanup.
238 */
239 RTFileDelete(g_szTestFile1);
240 RTDirRemoveRecursive(g_szTestDir1, 0);
241 RTTESTI_CHECK(RTDirExists(g_pszTestDir));
242 RTTESTI_CHECK(!RTPathExists(g_szTestDir1));
243 RTTESTI_CHECK(!RTPathExists(g_szTestFile1));
244 }
245
246 return RTTestSummaryAndDestroy(g_hTest);
247}
248
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