VirtualBox

source: vbox/trunk/src/VBox/VMM/testcase/tstCompressionBenchmark.cpp@ 21802

Last change on this file since 21802 was 21802, checked in by vboxsync, 16 years ago

tstCompressionBenchmark: code in progress.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.4 KB
Line 
1/* $Id: tstCompressionBenchmark.cpp 21802 2009-07-26 18:45:55Z vboxsync $ */
2/** @file
3 * Compression Benchmark for SSM.
4 */
5
6/*
7 * Copyright (C) 2009 Sun Microsystems, Inc.
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.215389.xyz. 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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
18 * Clara, CA 95054 USA or visit http://www.sun.com if you need
19 * additional information or have any questions.
20 */
21
22
23/*******************************************************************************
24* Header Files *
25*******************************************************************************/
26#include <iprt/assert.h>
27#include <iprt/ctype.h>
28#include <iprt/err.h>
29#include <iprt/file.h>
30#include <iprt/getopt.h>
31#include <iprt/initterm.h>
32#include <iprt/mem.h>
33#include <iprt/param.h>
34#include <iprt/stream.h>
35#include <iprt/string.h>
36#include <iprt/time.h>
37#include <iprt/zip.h>
38
39
40/*******************************************************************************
41* Global Variables *
42*******************************************************************************/
43static size_t g_cPages = 20*_1M / PAGE_SIZE;
44static uint8_t *g_pabSrc;
45static uint8_t *g_pabDecompr;
46static size_t g_cbCompr;
47static uint8_t *g_pabCompr;
48
49static DECLCALLBACK(int) ComprOutCallback(void *pvUser, const void *pvBuf, size_t cbBuf)
50{
51 return VERR_NOT_IMPLEMENTED;
52}
53
54
55/** Prints an error message and returns 1 for quick return from main use. */
56static int Error(const char *pszMsgFmt, ...)
57{
58 RTStrmPrintf(g_pStdErr, "error: ");
59 va_list va;
60 va_start(va, pszMsgFmt);
61 RTStrmPrintfV(g_pStdErr, pszMsgFmt, va);
62 va_end(va);
63 return 1;
64}
65
66
67int main(int argc, char **argv)
68{
69 RTR3Init();
70
71 /*
72 * Parse arguments.
73 */
74 static const RTGETOPTDEF s_aOptions[] =
75 {
76 { "--interations", 'i', RTGETOPT_REQ_UINT32 },
77 { "--num-pages", 'n', RTGETOPT_REQ_UINT32 },
78 { "--page-file", 'f', RTGETOPT_REQ_STRING },
79 };
80
81 const char *pszPageFile = NULL;
82 uint32_t cIterations = 1;
83 RTGETOPTUNION Val;
84 RTGETOPTSTATE State;
85 int rc = RTGetOptInit(&State, argc, argv, &s_aOptions[0], RT_ELEMENTS(s_aOptions), 1, 0);
86 AssertRCReturn(rc, 1);
87
88 while ((rc = RTGetOpt(&State, &Val)))
89 {
90 switch (rc)
91 {
92 case 'n':
93 g_cPages = Val.u32;
94 if (g_cPages * PAGE_SHIFT * 8 / (PAGE_SIZE * 8) != g_cPages)
95 return Error("The specified page count is too high: %#x\n", g_cPages);
96 if (g_cPages < 1)
97 return Error("The specified page count is too low: %#x\n", g_cPages);
98 break;
99
100 case 'i':
101 cIterations = Val.u32;
102 if (cIterations < 1)
103 return Error("The number of iterations must be 1 or higher\n");
104 break;
105
106 case 'f':
107 pszPageFile = Val.psz;
108 break;
109
110 default:
111 if (rc > 0)
112 {
113 if (RT_C_IS_GRAPH(rc))
114 Error("unhandled option: -%c\n", rc);
115 else
116 Error("unhandled option: %d\n", rc);
117 }
118 else if (rc == VERR_GETOPT_UNKNOWN_OPTION)
119 Error("unknown option: %s\n", Val.psz);
120 else if (rc == VINF_GETOPT_NOT_OPTION)
121 Error("unknown argument: %s\n", Val.psz);
122 else if (Val.pDef)
123 Error("%s: %Rrs\n", Val.pDef->pszLong, rc);
124 else
125 Error("%Rrs\n", rc);
126 return 1;
127 }
128 }
129
130 /*
131 * Gather the test memory.
132 */
133 if (pszPageFile)
134 {
135 size_t cbFile;
136 rc = RTFileReadAllEx(pszPageFile, 0, g_cPages * PAGE_SIZE, RTFILE_RDALL_O_DENY_NONE, (void **)&g_pabSrc, &cbFile);
137 if (RT_FAILURE(rc))
138 return Error("Error reading %zu bytes from %s: %Rrc\n", g_cPages * PAGE_SIZE, pszPageFile, rc);
139 if (cbFile != g_cPages * PAGE_SIZE)
140 return Error("Error reading %zu bytes from %s: got %zu bytes\n", g_cPages * PAGE_SIZE, pszPageFile, cbFile);
141 }
142 else
143 {
144 g_pabSrc = (uint8_t *)RTMemAlloc(g_cPages * PAGE_SIZE);
145 if (g_pabSrc)
146 {
147 /* just fill it with something. */
148 uint8_t *pb = g_pabSrc;
149 uint8_t *pbEnd = &g_pabSrc[g_cPages * PAGE_SIZE];
150 for (; pb != pbEnd; pb += 16)
151 {
152 char szTmp[17];
153 RTStrPrintf(szTmp, sizeof(szTmp), "aaaa%08Xzzzz", (uint32_t)(uintptr_t)pb);
154 memcpy(pb, szTmp, 16);
155 }
156 }
157 }
158
159 g_pabDecompr = (uint8_t *)RTMemAlloc(g_cPages * PAGE_SIZE);
160 g_cbCompr = g_cPages * PAGE_SIZE * 2;
161 g_pabCompr = (uint8_t *)RTMemAlloc(g_cbCompr);
162 if (!g_pabSrc || !g_pabDecompr || !g_pabCompr)
163 return Error("failed to allocate memory buffers (g_cPages=%#x)\n", g_cPages);
164
165 /*
166 * Double loop compressing and uncompressing the data, where the outer does
167 * the specified number of interations while the inner applies the different
168 * compression algorithms.
169 */
170 struct
171 {
172 /** The time spent decompressing. */
173 uint64_t cNanoDecompr;
174 /** The time spent compressing. */
175 uint64_t cNanoCompr;
176 /** The size of the compressed data. */
177 uint64_t cbCompr;
178 /** Number of errrors. */
179 uint32_t cErrors;
180 /** Compresstion type. */
181 RTZIPTYPE enmType;
182 /** Compresison level. */
183 RTZIPLEVEL enmLevel;
184 /** Method name. */
185 const char *pszName;
186 } aTests[] =
187 {
188 { 0, 0, 0, 0, RTZIPTYPE_LZF, RTZIPLEVEL_DEFAULT, "RTZip/LZF" }
189 };
190 for (uint32_t i = 0; i < cIterations; i++)
191 {
192 for (uint32_t j = 0; j < RT_ELEMENTS(aTests); j++)
193 {
194 memset(g_pabCompr, 0, g_cbCompr);
195 memset(g_pabDecompr, 0, g_cPages * PAGE_SIZE);
196
197 /*
198 * Compress it.
199 */
200 PRTZIPCOMP pZip;
201 rc = RTZipCompCreate(&pZip, NULL, ComprOutCallback, aTests[j].enmType, aTests[j].enmLevel);
202 if (RT_FAILURE(rc))
203 {
204 Error("Failed to create compressor for '%s' (#%u): %Rrc\n", aTests[j].pszName, j, rc);
205 aTests[j].cErrors++;
206 continue;
207 }
208
209 uint8_t const *pbPage = &g_pabSrc[0];
210 size_t cLeft = g_cPages;
211 uint64_t NanoTS = RTTimeNanoTS();
212 while (cLeft-- > 0)
213 {
214 pbPage += PAGE_SIZE;
215 }
216 NanoTS = RTTimeNanoTS() - NanoTS;
217
218 }
219 }
220
221
222 /*
223 * Report the results.
224 */
225 RTPrintf("tstCompressionBenchmark: BEGIN RESULTS\n");
226 rc = 0;
227 for (uint32_t j = 0; j < RT_ELEMENTS(aTests); j++)
228 {
229 RTPrintf("\n");
230
231 if (aTests[j].cErrors)
232 rc = 1;
233 }
234 RTPrintf("tstCompressionBenchmark: END RESULTS\n");
235
236 return rc;
237}
238
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette