summaryrefslogtreecommitdiff
path: root/source4/torture/raw/lookuprate.c
blob: f7ba004ae50def429d030398380e726155f6ef08 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/*
   File lookup rate test.

   Copyright (C) James Peach 2006

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "includes.h"
#include "system/filesys.h"
#include "torture/smbtorture.h"
#include "torture/basic/proto.h"
#include "libcli/libcli.h"
#include "torture/util.h"
#include "lib/cmdline/popt_common.h"
#include "auth/credentials/credentials.h"

#define BASEDIR "\\lookuprate"
#define MISSINGNAME BASEDIR "\\foo"

#define FUZZ_PERCENT 10

#define usec_to_sec(s) ((s) / 1000000)
#define sec_to_usec(s) ((s) * 1000000)

struct rate_record
{
    unsigned	dirent_count;
    unsigned	querypath_persec;
    unsigned	findfirst_persec;
};

static struct rate_record records[] =
{
    { 0, 0, 0 },	/* Base (optimal) lookup rate. */
    { 100, 0, 0},
    { 1000, 0, 0},
    { 10000, 0, 0},
    { 100000, 0, 0}
};

typedef NTSTATUS lookup_function(struct smbcli_tree *tree, const char * path);

/* Test whether rhs is within fuzz% of lhs. */
static bool fuzzily_equal(unsigned lhs, unsigned rhs, int percent)
{
	double fuzz = (double)lhs * (double)percent/100.0;

	if (((double)rhs >= ((double)lhs - fuzz)) &&
	    ((double)rhs <= ((double)lhs + fuzz))) {
		return true;
	}

	return false;

}

static NTSTATUS fill_directory(struct smbcli_tree *tree,
	    const char * path, unsigned count)
{
	NTSTATUS	status;
	char		*fname = NULL;
	unsigned	i;
	unsigned	current;

	struct timeval start;
	struct timeval now;

	status = smbcli_mkdir(tree, path);
	if (!NT_STATUS_IS_OK(status)) {
		return status;
	}

	printf("filling directory %s with %u files... ", path, count);
	fflush(stdout);

	current = random();
	start = timeval_current();

	for (i = 0; i < count; ++i) {
		int fnum;

		++current;
		fname = talloc_asprintf(NULL, "%s\\fill%u",
				    path, current);

		fnum = smbcli_open(tree, fname, O_RDONLY|O_CREAT,
				OPENX_MODE_DENY_NONE);
		if (fnum < 0) {
			talloc_free(fname);
			return smbcli_nt_error(tree);
		}

		smbcli_close(tree, fnum);
		talloc_free(fname);
	}

	if (count) {
		double rate;
		now = timeval_current();
		rate = (double)count / usec_to_sec((double)usec_time_diff(&now, &start));
		printf("%u/sec\n", (unsigned)rate);
	} else {
		printf("done\n");
	}

	return NT_STATUS_OK;
}

static NTSTATUS squash_lookup_error(NTSTATUS status)
{
	if (NT_STATUS_IS_OK(status)) {
		return NT_STATUS_OK;
	}

	/* We don't care if the file isn't there. */
	if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_PATH_NOT_FOUND)) {
		return NT_STATUS_OK;
	}

	if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
		return NT_STATUS_OK;
	}

	if (NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_FILE)) {
		return NT_STATUS_OK;
	}

	return status;
}

/* Look up a pathname using TRANS2_QUERY_PATH_INFORMATION. */
static NTSTATUS querypath_lookup(struct smbcli_tree *tree, const char * path)
{
	NTSTATUS	status;
	time_t		ftimes[3];
	size_t		fsize;
	uint16_t	fmode;

	status = smbcli_qpathinfo(tree, path, &ftimes[0], &ftimes[1], &ftimes[2],
			&fsize, &fmode);

	return squash_lookup_error(status);
}

/* Look up a pathname using TRANS2_FIND_FIRST2. */
static NTSTATUS findfirst_lookup(struct smbcli_tree *tree, const char * path)
{
	NTSTATUS status = NT_STATUS_OK;

	if (smbcli_list(tree, path, 0, NULL, NULL) < 0) {
		status = smbcli_nt_error(tree);
	}

	return squash_lookup_error(status);
}

static NTSTATUS lookup_rate_convert(struct smbcli_tree *tree,
	lookup_function lookup, const char * path, unsigned * rate)
{
	NTSTATUS	status;

	struct timeval	start;
	struct timeval	now;
	unsigned	count = 0;
	int64_t		elapsed = 0;

#define LOOKUP_PERIOD_SEC (2)

	start = timeval_current();
	while (elapsed < sec_to_usec(LOOKUP_PERIOD_SEC)) {

		status = lookup(tree, path);
		if (!NT_STATUS_IS_OK(status)) {
			return status;
		}

		++count;
		now = timeval_current();
		elapsed = usec_time_diff(&now, &start);
	}

#undef LOOKUP_PERIOD_SEC

	*rate = (unsigned)((double)count / (double)usec_to_sec(elapsed));
	return NT_STATUS_OK;
}

static bool remove_working_directory(struct smbcli_tree *tree,
		const char * path)
{
	int tries;

	/* Using smbcli_deltree to delete a very large number of files
	 * doesn't work against all servers. Work around this by
	 * retrying.
	 */
	for (tries = 0; tries < 5; ) {
		int ret;

		ret = smbcli_deltree(tree, BASEDIR);
		if (ret == -1) {
			tries++;
			printf("(%s) failed to deltree %s: %s\n",
				__location__, BASEDIR,
				smbcli_errstr(tree));
			continue;
		}

		return true;
	}

	return false;

}

/* Verify that looking up a file name takes constant time.
 *
 * This test samples the lookup rate for a non-existant filename in a
 * directory, while varying the number of files in the directory. The
 * lookup rate should continue to approximate the lookup rate for the
 * empty directory case.
 */
bool torture_bench_lookup(struct torture_context *torture)
{
	NTSTATUS	status;
	bool		result = false;

	int i, tries;
	struct smbcli_state *cli = NULL;

	if (!torture_open_connection(&cli, torture, 0)) {
		goto done;
	}

	remove_working_directory(cli->tree, BASEDIR);

	for (i = 0; i < ARRAY_SIZE(records); ++i) {
		printf("testing lookup rate with %u directory entries\n",
				records[i].dirent_count);

		status = fill_directory(cli->tree, BASEDIR,
				records[i].dirent_count);
		if (!NT_STATUS_IS_OK(status)) {
			printf("failed to fill directory: %s\n", nt_errstr(status));
			goto done;
		}

		status = lookup_rate_convert(cli->tree, querypath_lookup,
			MISSINGNAME, &records[i].querypath_persec);
		if (!NT_STATUS_IS_OK(status)) {
			printf("querypathinfo of %s failed: %s\n",
				MISSINGNAME, nt_errstr(status));
			goto done;
		}

		status = lookup_rate_convert(cli->tree, findfirst_lookup,
			MISSINGNAME, &records[i].findfirst_persec);
		if (!NT_STATUS_IS_OK(status)) {
			printf("findfirst of %s failed: %s\n",
				MISSINGNAME, nt_errstr(status));
			goto done;
		}

		printf("entries = %u, querypath = %u/sec, findfirst = %u/sec\n",
				records[i].dirent_count,
				records[i].querypath_persec,
				records[i].findfirst_persec);

		if (!remove_working_directory(cli->tree, BASEDIR)) {
			goto done;
		}
	}

	/* Ok. We have run all our tests. Walk through the records we
	 * accumulated and figure out whether the lookups took constant
	 * time of not.
	 */
	for (i = 0; i < ARRAY_SIZE(records); ++i) {
		if (!fuzzily_equal(records[0].querypath_persec,
				    records[i].querypath_persec,
				    FUZZ_PERCENT)) {
			printf("querypath rate for %d entries differed by "
				"more than %d%% from base rate\n",
				records[i].dirent_count, FUZZ_PERCENT);
			result = false;
		}

		if (!fuzzily_equal(records[0].findfirst_persec,
				    records[i].findfirst_persec,
				    FUZZ_PERCENT)) {
			printf("findfirst rate for %d entries differed by "
				"more than %d%% from base rate\n",
				records[i].dirent_count, FUZZ_PERCENT);
			result = false;
		}
	}

done:
	if (cli) {
		remove_working_directory(cli->tree, BASEDIR);
		talloc_free(cli);
	}

	return result;
}

/* vim: set sts=8 sw=8 : */