我是用SATA磁盤對Linux機箱上的同步讀取性能進行基準測試。使用單線程讀取,奇怪的是更高的QPS(50)在讀取300個條目後給出了12ms的平均讀取時間,而在讀取相同的300個條目之後較低的QPS(1)給出了63ms。有沒有解釋?爲什麼更頻繁地讀取磁盤會使Linux上的每個讀取操作更快? QPS1與50
代碼和數據如下:
struct Request{
unsigned long long len;
unsigned long long offset;
int fd;
};
int read_request(Request* request){
char* buf = (char*)malloc(request->len);
off_t of = lseek(request->fd,request->offset,SEEK_SET);
assert(of == request->offset);
int len = read(request->fd,buf,request->len);
assert(len == request->len);
free(buf);
return 0;
}
int read_with_qps(Request* request,int request_num,Files* f,int mode,int qps){
int interval = 1000/qps;
struct timeval start,end;
for(int i = 0 ; i < request_num ; i++){
gettimeofday(&start,NULL);
int ret = read_request(&request[i]);
gettimeofday(&end,NULL);
int time_used = (end.tv_sec - start.tv_sec) * 1000 + (end.tv_usec - start.tv_usec)/1000;
fprintf(stderr,"%lld,offset=%lld,len=%lld, read time:%d,ret=%d,mode=%d\n",
end.tv_sec,request[i].offset,request[i].len,time_used,ret,mode);
if(time_used < interval){
usleep((interval - time_used) * 1000);
}
}
return 0;
}
隨着QPS = 50時,輸出的採樣的樣子(忽略時間,這被認爲是計算平均時間時,擊中頁面緩存< 4ms的):
1332233329,offset=1052299215,len=13186, read time:13,ret=0,mode=1
1332233329,offset=2319646140,len=1612, read time:10,ret=0,mode=1
1332233330,offset=1319250005,len=5654, read time:12,ret=0,mode=1
1332233330,offset=2520376009,len=2676, read time:12,ret=0,mode=1
1332233330,offset=2197548522,len=17236, read time:10,ret=0,mode=1
1332233330,offset=1363242083,len=13734, read time:11,ret=0,mode=1
1332233330,offset=4242210521,len=2003, read time:17,ret=0,mode=1
1332233330,offset=1666337117,len=2207, read time:10,ret=0,mode=1
1332233330,offset=797722662,len=5480, read time:18,ret=0,mode=1
1332233330,offset=1129310678,len=2265, read time:10,ret=0,mode=1
QPS = 1,smaple的相同提取物:
1332300410,offset=1052299215,len=13186, read time:19,ret=0,mode=1
1332300411,offset=2319646140,len=1612, read time:40,ret=0,mode=1
1332300412,offset=1319250005,len=5654, read time:141,ret=0,mode=1
1332300413,offset=2520376009,len=2676, read time:15,ret=0,mode=1
1332300414,offset=2197548522,len=17236, read time:21,ret=0,mode=1
1332300415,offset=1363242083,len=13734, read time:13,ret=0,mode=1
1332300416,offset=4242210521,len=2003, read time:43,ret=0,mode=1
1332300417,offset=1666337117,len=2207, read time:18,ret=0,mode=1
1332300418,offset=797722662,len=5480, read time:67,ret=0,mode=1
1332300419,offset=1129310678,len=2265, read time:12,ret=0,mode=1
內核版本我S:2.6.18-194.el5 SMP x86_64的
$ cat /sys/block/sda/queue/scheduler
noop anticipatory deadline [cfq]
感謝您的回覆
但是讀取請求是同步的,即下一次讀取在最後一次讀取返回之前不會發出。 – Utoah 2012-03-21 03:55:25
對於偏移量,請求的高度隨機性如示例輸出所示。 – Utoah 2012-03-21 03:58:05