aboutsummaryrefslogtreecommitdiff
path: root/tests/fs-tests/simple/perf.c
blob: aee8226a1199778a47d6cd181088a749cded3800 (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
/*
 * Copyright (C) 2007 Nokia Corporation.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 2 as published by the Free Software Foundation.
 *
 * 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, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 * Author: Adrian Hunter
 */

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/time.h>
#include <time.h>
#include <limits.h>

#include "tests.h"

#define BLOCK_SIZE 32 * 1024

struct timeval tv_start;
struct timeval tv_stop;

static inline void start_timer(void)
{
	CHECK(gettimeofday(&tv_start, NULL) != -1);
}

static inline long long stop_timer(void)
{
	long long usecs;

	CHECK(gettimeofday(&tv_stop, NULL) != -1);
	usecs = (tv_stop.tv_sec - tv_start.tv_sec);
	usecs *= 1000000;
	usecs += tv_stop.tv_usec;
	usecs -= tv_start.tv_usec;
	return usecs;
}

static unsigned speed(size_t bytes, long long usecs)
{
	unsigned long long k;

	k = bytes * 1000000ULL;
	k /= usecs;
	k /= 1024;
	CHECK(k <= UINT_MAX);
	return (unsigned) k;
}

static void perf(void)
{
	pid_t pid;
	int fd, i;
	ssize_t written, readsz;
	size_t remains, block, actual_size;
	char file_name[256];
	unsigned char *buf;
	long long write_time, unmount_time, mount_time, read_time;

	/* Sync all file systems */
	sync();
	/* Make random data to write */
	buf = malloc(BLOCK_SIZE);
	CHECK(buf != NULL);
	pid = getpid();
	srand(pid);
	for (i = 0; i < BLOCK_SIZE; i++)
		buf[i] = rand();
	/* Open file */
	tests_cat_pid(file_name, "perf_test_file_", pid);
	start_timer();
	fd = open(file_name, O_CREAT | O_RDWR | O_TRUNC,
		  S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH);
	CHECK(fd != -1);
	CHECK(tests_size_parameter > 0);
	CHECK(tests_size_parameter <= SIZE_MAX);
	/* Write to file */
	actual_size = 0;
	remains = tests_size_parameter;
	while (remains > 0) {
		if (remains > BLOCK_SIZE)
			block = BLOCK_SIZE;
		else
			block = remains;
		written = write(fd, buf, block);
		if (written <= 0) {
			CHECK(errno == ENOSPC); /* File system full */
			errno = 0;
			break;
		}
		remains -= written;
		actual_size += written;
	}
	CHECK(fsync(fd) != -1);
	CHECK(close(fd) != -1);
	write_time = stop_timer();
	/* Unmount */
	start_timer();
	tests_unmount();
	unmount_time = stop_timer();
	/* Mount */
	start_timer();
	tests_mount();
	mount_time = stop_timer();
	/* Open file, read it, and close it */
	start_timer();
	fd = open(file_name, O_RDONLY);
	CHECK(fd != -1);
	remains = actual_size;
	while (remains > 0) {
		if (remains > BLOCK_SIZE)
			block = BLOCK_SIZE;
		else
			block = remains;
		readsz = read(fd, buf, block);
		CHECK(readsz == block);
		remains -= readsz;
	}
	CHECK(close(fd) != -1);
	read_time = stop_timer();
	CHECK(unlink(file_name) != -1);
	/* Display timings */
	printf("File system read and write speed\n");
	printf("================================\n");
	printf("Specfied file size: %lld\n",
	       (unsigned long long)tests_size_parameter);
	printf("Actual file size: %zu\n", actual_size);
	printf("Write time (us): %lld\n", write_time);
	printf("Unmount time (us): %lld\n", unmount_time);
	printf("Mount time (us): %lld\n", mount_time);
	printf("Read time (us): %lld\n", read_time);
	printf("Write speed (KiB/s): %u\n", speed(actual_size, write_time));
	printf("Read speed (KiB/s): %u\n", speed(actual_size, read_time));
	printf("Test completed\n");
}

/* Title of this test */

static const char *perf_get_title(void)
{
	return "Measure file system read and write speed";
}

/* Description of this test */

static const char *perf_get_description(void)
{
	return
		"Syncs the file system (a newly created empty file system is " \
		"preferable). Creates a file named perf_test_file_pid, where " \
		"pid is the process id. The file is filled with random data. " \
		"The size of the file is given by the -z or --size option, " \
		"otherwise it defaults to 10MiB. Unmounts the file system. " \
		"Mounts the file system. Reads the entire file in 32KiB size " \
		"blocks. Displays the time taken for each activity. Deletes " \
		"the file. Note that the file is synced after writing and " \
		"that time is included in the write time and speed.";
}

int main(int argc, char *argv[])
{
	int run_test;

	/* Set default test file size */
	tests_size_parameter = 10 * 1024 * 1024;

	/* Handle common arguments */
	run_test = tests_get_args(argc, argv, perf_get_title(),
			perf_get_description(), "z");
	if (!run_test)
		return 1;
	/* Change directory to the file system and check it is ok for testing */
	tests_check_test_file_system();
	/* Do the actual test */
	perf();
	return 0;
}