#define MAX_BUFFER (1024*1024)
#define MAX_FILESIZE (300*1024*1024)
#define MAX_BUFFSIZE (1024*1024*1024)
enum bool{ false, true };
const char* filepathram[30] = {
"/root/myram/test1.txt", "/root/myram/test2.txt",
"/root/myram/test3.txt","/root/myram/test4.txt",
"/root/myram/test5.txt","/root/myram/test6.txt",
"/root/myram/test7.txt","/root/myram/test8.txt",
"/root/myram/test9.txt","/root/myram/test10.txt",
"/root/myram/test11.txt", "/root/myram/test12.txt",
"/root/myram/test13.txt","/root/myram/test14.txt",
"/root/myram/test15.txt","/root/myram/test16.txt",
const char* filepathdisk[30] = {
"/usr/test1.txt", "/usr/test2.txt",
"/usr/test3.txt", "/usr/test4.txt",
"/usr/test5.txt", "/usr/test6.txt",
"/usr/test7.txt", "/usr/test8.txt",
"/usr/test9.txt", "/usr/test10.txt",
"/usr/test11.txt", "/usr/test12.txt",
"/usr/test13.txt", "/usr/test14.txt",
"/usr/test15.txt", "/usr/test16.txt",
char text[MAX_BUFFER] = "11111111";
write_file(int blocksize, bool isrand, char* filepath) {
int fd = open(filepath, O_RDWR | O_CREAT | O_SYNC, 0755);
if (fd == -1) perror("Open");
for (int i = 0;i < MAX_ITER;i++) {
if (write(fd, text, blocksize) != blocksize)
lseek(fd, (MAX_ITER - 1) * (rand() % blocksize), SEEK_SET);
read_file(int blocksize, bool isrand, char* filepath) {
int fd = open(filepath, O_RDWR | O_CREAT | O_SYNC, 0755);
if (fd == -1) perror("Open");
for (int i = 0;i < MAX_ITER;i++) {
if (read(fd, buff, blocksize) != blocksize)
lseek(fd, (MAX_ITER - 1) * (rand() % blocksize), SEEK_SET);
calc_time(struct timeval t1, struct timeval t2) {
return (double)(t2.tv_sec - t1.tv_sec) * 1000 + (t2.tv_usec - t1.tv_usec) / 1000;
main(int argc, char* argv[]) {
printf("BlockSize(KB),FileSize(MB),Speed(MB/s)\n");
srand((unsigned)time(NULL));
for (int i = 0;i < MAX_BUFFER;i += 16) {
strcat(text, "1111111111111111");
for (int blocksize = 64;blocksize <= 1024 * 32;blocksize = blocksize * 2) {
for (int i = 0;i < proc_num;i++) {
if (strcmp(argv[1], "W") == 0) {
if (strcmp(argv[2], "R") == 0) {
if (strcmp(argv[3], "S") == 0)
write_file(blocksize, false, filepathram[i]);
write_file(blocksize, true, filepathram[i]);
if (strcmp(argv[3], "S") == 0)
write_file(blocksize, false, filepathdisk[i]);
write_file(blocksize, true, filepathdisk[i]);
if (strcmp(argv[3], "R") == 0) {
if (strcmp(argv[3], "S") == 0)
read_file(blocksize, false, filepathram[i]);
read_file(blocksize, true, filepathram[i]);
if (strcmp(argv[3], "S") == 0)
read_file(blocksize, false, filepathdisk[i]);
read_file(blocksize, true, filepathdisk[i]);
while (wait(NULL) != -1);
t = calc_time(t1, t2) / 1000.0;
int total_size = blocksize * proc_num * MAX_ITER;
printf("%lf,%lf,%lf\n", 1.0 * blocksize / 1024,
1.0 * total_size / 1024 / 1024, 1.0 * total_size / t / 1024 / 1024);
// printf("blocksize_KB=%.4fKB,filesize_MB=%.4fMB,speed=%fMB/s\n",
// (double)blocksize / 1024.0,
// (double)total_size / 1024.0 / 1024.0,
// (double)total_size / t / 1024.0 / 1024.0);