Skip to content

Instantly share code, notes, and snippets.

@bochsdbg
Last active December 23, 2018 16:37
Show Gist options
  • Save bochsdbg/f3f5fc1d17fb56868118075304e1d177 to your computer and use it in GitHub Desktop.
Save bochsdbg/f3f5fc1d17fb56868118075304e1d177 to your computer and use it in GitHub Desktop.
small hdd benchmark
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <time.h>
#include <sys/stat.h>
#include <memory.h>
static inline int64_t get_current_time() {
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC_RAW, &ts);
return (1000 * 1000 * 1000 * (int64_t)ts.tv_sec) + (int64_t)ts.tv_nsec;
}
int main(int argc, char *argv[])
{
if (argc != 4) {
printf("Usage: %s <block_size> <iterations> <path_to_file>\n", argv[0]);
return 1;
}
const char *file_name = argv[3];
struct stat st;
if (stat(file_name, &st) < 0) {
perror("stat()");
return 1;
}
off_t file_size = st.st_size;
printf("Testing '%s', size: %ld\n", file_name, file_size);
int fd = open(file_name, O_RDONLY | O_DIRECT);
if (fd < 0) {
perror("open()");
return 1;
}
size_t block_size = (size_t)atol(argv[1]);
// currently unused.
// unsigned n_iterations = (unsigned)atol(argv[2]);
char *buf = aligned_alloc(block_size, block_size);
srand48(time(NULL));
int64_t start_time = get_current_time();
ssize_t total_read = 0;
while (get_current_time() - start_time < 2 * 1000 * 1000 * 1000) {
// for (unsigned i = 0; i < n_iterations; ++i) {
off_t offset = lrand48() % (file_size - (off_t)block_size);
offset &= ~(block_size - 1); // align by block_size
ssize_t result = pread(fd, buf, block_size, offset);
if (result < 0) {
perror("pread()");
free(buf);
return 1;
}
total_read += result;
// printf("%10ld %10ld %10ld\n", offset, block_size, result);
}
int64_t end_time = get_current_time();
double secs_elapsed = (double)(end_time - start_time) / 1e9;
printf("Total time: %lf, Total read: %ld bytes\nSpeed: %lf KBytes/sec\n", secs_elapsed, total_read, ((double)total_read) / secs_elapsed / 1024.0);
free(buf);
return 0;
}
Total time: 10.087583, Total read: 104857600 bytes
Speed: 10151.093271 KBytes/sec
Command being timed: "/lib/ld-musl-x86_64.so.1 hdd-bench 1048576 100 /tmp/test.dat"
User time (seconds): 0.00
System time (seconds): 0.02
Percent of CPU this job got: 0%
Elapsed (wall clock) time (h:mm:ss or m:ss): 10.08s
Average shared text size (kbytes): 0
Average unshared data size (kbytes): 0
Average stack size (kbytes): 0
Average total size (kbytes): 0
Maximum resident set size (kbytes): 4816
Average resident set size (kbytes): 0
Major (requiring I/O) page faults: 0
Minor (reclaiming a frame) page faults: 331
Voluntary context switches: 113
Involuntary context switches: 2
Swaps: 0
File system inputs: 204800
File system outputs: 0
Socket messages sent: 0
Socket messages received: 0
Signals delivered: 0
Page size (bytes): 4096
Exit status: 0
Total time: 13.978887, Total read: 81920000 bytes
Speed: 5722.916461 KBytes/sec
Command being timed: "/lib/ld-musl-x86_64.so.1 hdd-bench 8192 10000 /tmp/test.dat"
User time (seconds): 0.02
System time (seconds): 0.51
Percent of CPU this job got: 3%
Elapsed (wall clock) time (h:mm:ss or m:ss): 13.98s
Average shared text size (kbytes): 0
Average unshared data size (kbytes): 0
Average stack size (kbytes): 0
Average total size (kbytes): 0
Maximum resident set size (kbytes): 1152
Average resident set size (kbytes): 0
Major (requiring I/O) page faults: 0
Minor (reclaiming a frame) page faults: 78
Voluntary context switches: 10197
Involuntary context switches: 2
Swaps: 0
File system inputs: 160000
File system outputs: 0
Socket messages sent: 0
Socket messages received: 0
Signals delivered: 0
Page size (bytes): 4096
Exit status: 0
Total time: 0.075575, Total read: 1638400 bytes
Speed: 21170.939805 KBytes/sec
Command being timed: "/lib/ld-musl-x86_64.so.1 hdd-bench 8192 200 /tmp/test.dat"
User time (seconds): 0.00
System time (seconds): 0.01
Percent of CPU this job got: 18%
Elapsed (wall clock) time (h:mm:ss or m:ss): 0.07s
Average shared text size (kbytes): 0
Average unshared data size (kbytes): 0
Average stack size (kbytes): 0
Average total size (kbytes): 0
Maximum resident set size (kbytes): 1184
Average resident set size (kbytes): 0
Major (requiring I/O) page faults: 0
Minor (reclaiming a frame) page faults: 79
Voluntary context switches: 223
Involuntary context switches: 1
Swaps: 0
File system inputs: 3200
File system outputs: 0
Socket messages sent: 0
Socket messages received: 0
Signals delivered: 0
Page size (bytes): 4096
Exit status: 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment