summaryrefslogtreecommitdiff
path: root/tests/libsqfs/xattr_benchmark.c
blob: 918a636586a322e09c53caf49f7fa9fd574923c5 (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
/* SPDX-License-Identifier: GPL-3.0-or-later */
/*
 * xattr_benchmark.c
 *
 * Copyright (C) 2021 David Oberhollenzer <goliath@infraroot.at>
 */
#include "config.h"
#include "compat.h"
#include "common.h"

#include "sqfs/xattr_writer.h"
#include "sqfs/xattr.h"

#include <stdlib.h>
#include <getopt.h>
#include <string.h>
#include <stdio.h>

static struct option long_opts[] = {
	{ "block-count", required_argument, NULL, 'b' },
	{ "groups-size", required_argument, NULL, 'g' },
	{ "version", no_argument, NULL, 'V' },
	{ "help", no_argument, NULL, 'h' },
	{ NULL, 0, NULL, 0 },
};

static const char *short_opts = "g:b:hV";

static const char *help_string =
"Usage: xattr_benchmark [OPTIONS...]\n"
"\n"
"Possible options:\n"
"\n"
"  --block-count, -b <count>  How many unique xattr blocks to generate.\n"
"  --group-size, -g <count>   Number of key-value pairs to generate for each\n"
"                             xattr block.\n"
"\n";

int main(int argc, char **argv)
{
	long blkidx, grpidx, block_count = 0, group_size = 0;
	sqfs_xattr_writer_t *xwr;
	sqfs_u32 id;
	int ret;

	for (;;) {
		int i = getopt_long(argc, argv, short_opts, long_opts, NULL);
		if (i == -1)
			break;

		switch (i) {
		case 'b':
			block_count = strtol(optarg, NULL, 0);
			break;
		case 'g':
			group_size = strtol(optarg, NULL, 0);
			break;
		case 'h':
			fputs(help_string, stdout);
			return EXIT_SUCCESS;
		case 'V':
			print_version("xattr_benchmark");
			return EXIT_SUCCESS;
		default:
			goto fail_arg;
		}
	}

	if (block_count <= 0) {
		fputs("A block count > 0 must be specified.\n", stderr);
		goto fail_arg;
	}

	if (group_size <= 0) {
		fputs("A group size > 0 must be specified.\n", stderr);
		goto fail_arg;
	}

	/* setup writer */
	xwr = sqfs_xattr_writer_create(0);

	/* generate blocks */
	for (blkidx = 0; blkidx < block_count; ++blkidx) {
		ret = sqfs_xattr_writer_begin(xwr, 0);
		if (ret < 0) {
			sqfs_perror(NULL, "begin xattr block", ret);
			goto fail;
		}

		for (grpidx = 0; grpidx < group_size; ++grpidx) {
			char key[64], value[64];

			snprintf(key, sizeof(key), "user.group%ld.key%ld",
				 blkidx, grpidx);

			snprintf(value, sizeof(value), "group%ld/value%ld",
				 blkidx, grpidx);

			ret = sqfs_xattr_writer_add(xwr, key, value,
						    strlen(value));

			if (ret < 0) {
				sqfs_perror(NULL, "add to xattr block", ret);
				goto fail;
			}
		}

		ret = sqfs_xattr_writer_end(xwr, &id);
		if (ret < 0) {
			sqfs_perror(NULL, "end xattr block", ret);
			goto fail;
		}
	}

	/* cleanup */
	sqfs_destroy(xwr);
	return EXIT_SUCCESS;
fail:
	sqfs_destroy(xwr);
	return EXIT_FAILURE;
fail_arg:
	fputs("Try `xattr_benchmark --help' for more information.\n", stderr);
	return EXIT_FAILURE;
}