微博上kevin_prajna提了一個問題:“求Linux下一打包工具,需求:能把兩個10G的文件打包成一個文件,時間在1分鍾之內能接受!”。 暫且作答一下吧。首先問題是求解工具,那么我們忽略IO問題,采用內存盤來解決, 在公司一台128G內存的機器上: mkdir /mnt/test mount -t ramfs none /mnt/test cd /mnt/test 生成一個小腳本,生成兩個10G的文件: #!/bin/bash for (( i = 0; i < 2; i++)); do echo $i dd if=/dev/zero of=file$i.bin bs=1M count=10000 done; 生成測試文件: time ./test.sh <<< 0 10000+0 records in 10000+0 records out 10485760000 bytes (10 GB) copied, 4.78903 s, 2.2 GB/s 1 10000+0 records in 10000+0 records out 10485760000 bytes (10 GB) copied, 4.92947 s, 2.1 GB/s ./test.sh 0.00s user 9.68s system 99% cpu 9.731 total 測試結果,生成兩個10G文件,消耗了9.731秒 采用tar打包工具測試: time tar cvf out.bin file* <<< file0.bin file1.bin tar cvf out.bin file* 0.40s user 13.90s system 99% cpu 14.353 total 采用 tar打包這兩個文件,並且寫入 out.bin文件,消耗了 14.353秒, 完全滿足kevin_prajna的要求。 然后我們用cpio來測試,由於cpio對10G這樣的文件打包有bug,會報錯,所以我們用20個1G文件測試: #!/bin/bash for (( i = 0; i < 20; i++)); do echo $i dd if=/dev/zero of=file$i.bin bs=1M count=1000 done; 生成20個1G測試文件,用了 9.806秒 使用tar對這20個1G文件打包,用了13.800 秒 cpio的測試結果: # time ls file*|cpio -o > out.bin <<< 40960002 blocks ls --color=tty file* 0.00s user 0.00s system 0% cpu 0.002 total cpio -o > out.bin 6.31s user 37.61s system 99% cpu 44.029 total cpio打包這20個1G文件消耗了44.029秒,速度相對tar,還是慢了好多。 測試環境: Dell R710, 2*Xeon E5620, 128G RAM OS: Ubuntu 12.04 x86_64 從上面也可以看出,現在CPU和工具是很強悍的,弱爆的是磁盤IO,這是要大把花銀子的。