Created
September 24, 2022 18:19
-
-
Save poundifdef/e748c467d354662ed034b5f647729e18 to your computer and use it in GitHub Desktop.
Brief example showing the read performance difference between random vs linear disk reads
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <sys/mman.h> | |
#include <unistd.h> | |
#include <sys/stat.h> | |
#include <fcntl.h> | |
#include <sys/types.h> | |
const unsigned int num_records = 100000000; | |
struct inventory | |
{ | |
unsigned int seq; | |
unsigned int amount; | |
}; | |
void create_data() | |
{ | |
struct inventory record; | |
FILE *fd; | |
fd = fopen("test.bin", "wb"); | |
for (unsigned int i = 0; i < num_records; i++) | |
{ | |
memset(&record, 0, sizeof(struct inventory)); | |
record.seq = i; | |
record.amount = rand() % 3; | |
fwrite(&record, sizeof(struct inventory), 1, fd); | |
} | |
fclose(fd); | |
} | |
void mmap_read() | |
{ | |
size_t pagesize = getpagesize(); | |
printf("System page size: %zu bytes\n", pagesize); | |
int fdin; | |
char *src, *dst; | |
struct stat statbuf; | |
fdin = open("test.bin", O_RDONLY); | |
fstat(fdin, &statbuf); | |
src = mmap(0, statbuf.st_size, PROT_READ, MAP_SHARED, fdin, 0); | |
struct inventory record; | |
unsigned int recordno; | |
for (unsigned int i = 0; i < 100000; i++) | |
{ | |
// Use this to select random records from the file | |
recordno = rand() % num_records; | |
// Use this to read data linerally from the file | |
// recordno = i; | |
memcpy(&record, src + (sizeof(struct inventory) * recordno), sizeof(struct inventory)); | |
printf("%d %d\n", record.seq, record.amount); | |
} | |
} | |
int main(int argc, char **argv) | |
{ | |
// Make sure to run `sync && sudo purge` between runs to flush the OS disk cache | |
// https://stackoverflow.com/questions/28845524/echo-3-proc-sys-vm-drop-caches-on-mac-osx | |
// Run this first to create the example data | |
// create_data(); | |
// Run this to read 100k items from that file | |
mmap_read(); | |
printf("hello world\n"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment