测试磁盘读写速度


#!/bin/bash

i=1
while [ 1 -eq 1 ]
do
dd if=/dev/zero of=$i.img bs=15M count=1
i=$((i+1))
[ $i -gt 20 ] && exit 0
done


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用gettimeofday函数测量磁盘读写速度。以下是一个示例代码: ```c #include <stdio.h> #include <stdlib.h> #include <sys/time.h> #define BUFFER_SIZE (1 << 20) // 1MB int main() { char *buffer = malloc(BUFFER_SIZE); if (buffer == NULL) { printf("Failed to allocate memory.\n"); return 1; } FILE *file = fopen("test.txt", "wb"); // 以二进制写入方式打开文件 if (file == NULL) { printf("Failed to open file.\n"); return 1; } struct timeval start, end; gettimeofday(&start, NULL); fwrite(buffer, BUFFER_SIZE, 1, file); // 写入1MB数据 fflush(file); // 强制刷新缓冲区 gettimeofday(&end, NULL); fclose(file); free(buffer); long seconds = end.tv_sec - start.tv_sec; long useconds = end.tv_usec - start.tv_usec; double elapsed_time = seconds + useconds / 1000000.0; printf("Write speed: %.2f MB/s.\n", BUFFER_SIZE / elapsed_time / (1 << 20)); buffer = malloc(BUFFER_SIZE); if (buffer == NULL) { printf("Failed to allocate memory.\n"); return 1; } file = fopen("test.txt", "rb"); // 以二进制读取方式打开文件 if (file == NULL) { printf("Failed to open file.\n"); return 1; } gettimeofday(&start, NULL); fread(buffer, BUFFER_SIZE, 1, file); // 读取1MB数据 gettimeofday(&end, NULL); fclose(file); free(buffer); seconds = end.tv_sec - start.tv_sec; useconds = end.tv_usec - start.tv_usec; elapsed_time = seconds + useconds / 1000000.0; printf("Read speed: %.2f MB/s.\n", BUFFER_SIZE / elapsed_time / (1 << 20)); return 0; } ``` 在上面的代码中,我们使用gettimeofday函数分别测量了磁盘写入和读取的速度。首先,我们分配1MB内存作为缓冲区,然后以二进制写入方式打开文件,写入1MB数据,并强制刷新缓冲区。接着,我们测量写入所需时间,并通过写入数据量除以时间获得写入速度。然后,我们以二进制读取方式打开文件,读取1MB数据,测量读取所需时间,并通过读取数据量除以时间获得读取速度。最后,我们释放内存并关闭文件。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值