tcp.sh 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #!/bin/bash
  2. # tar comparison program
  3. # 2007-10-25 Jan Psota
  4. n=3 # number of repetitions
  5. TAR="bsdtar gnutar star" # Tape archivers to compare
  6. OPT=("" "--seek" "-no-fsync")
  7. pax="--format=pax" # comment out for defaults
  8. OPN=(create list extract compare) # operations
  9. version="2007-10-25"
  10. TIMEFORMAT=$'%R\t%U\t%S\t%P'
  11. LC_ALL=C
  12. test $# -ge 2 || {
  13. echo -e "usage:\t$0 source_dir where_to_place_archive
  14. [where_to_extract_it]
  15. TCP, version $version
  16. TCP stands for Tar Comparison Program here.
  17. It currently compares: BSD tar (bsdtar), GNU tar (gnutar) and star in archive
  18. creation, listing, extraction and archive-to-extracted comparison.
  19. Tcp prints out best time of n=$n repetitions.
  20. Tcp creates temporary archive named tcp.tar with $pax and some native
  21. (--seek/-no-fsync) options and extracts it to [\$3]/tcptmp/.
  22. If unset, third argument defaults to [\$2].
  23. After normal exit tcp removes tarball and extracted files.
  24. Tcp does not check filesystems destination directories are on for free space,
  25. so make sure there is enough space (a bit more than source_dir uses) for both:
  26. archive and extracted files.
  27. Do not use white space in arguments.
  28. Jan Psota, $version"
  29. exit 0
  30. }
  31. src=$1
  32. dst=$2/tcp.tar
  33. dst_path=${3:-$2}/tcptmp
  34. test -e $dst -o -e /tmp/tcp \
  35. && { echo "$dst or /tmp/tcp exists, exiting"; exit 1; }
  36. mkdir $dst_path || exit 2
  37. use_times ()
  38. {
  39. awk -F"\t" -vN=$n -vL="`du -k $dst`" -vOFS="\t" -vORS="" '
  40. { if (NF==4) { printf "\t%s\t%10.1d KB/s\n", $0, ($1+0>0 ?
  41. (L+0)/($1+0) : 0) } }' \
  42. /tmp/tcp | sort | head -1
  43. > /tmp/tcp
  44. }
  45. test -d $src || { echo "'$src' is not a directory"; exit 3; }
  46. # system information: type, release, memory, cpu(s), compiler and flags
  47. echo -e "TCP, version $version\n"`uname -sr`" / "`head -1 /etc/*-release`
  48. free -m | awk '/^Mem/ { printf "%dMB of memory, ", $2 }'
  49. test -e /proc/cpuinfo \
  50. && awk -F: '/name|cache size|MHz|mips/ { if (!a) b=b $2 }
  51. /^$/ { a++ } END { print a" x"b" bmips" }' /proc/cpuinfo
  52. test -e /etc/gentoo-release \
  53. && gcc --version | head -1 && grep ^CFLAGS /etc/make.conf
  54. # tar versions
  55. t=
  56. echo
  57. for tar in $TAR; do
  58. if which $tar &> /dev/null; then
  59. t="$t $tar";
  60. echo -ne "$tar:\t"; $tar --version | head -1;
  61. fi
  62. done
  63. TAR="$t"
  64. echo -e "\nbest time of $n repetitions,\n"\
  65. " src=$src, "\
  66. `du -sh $src | awk '{print $1}'`" in "`find $src | wc -l`" files, "\
  67. "avg "$((`du -sk $src | awk '{print $1}'`/`find $src -type f | wc -l`))"KB/file,\n"\
  68. " archive=$dst, extract to $dst_path"
  69. echo -e "program\toperation\treal\tuser\tsystem\t%CPU\t speed"
  70. > /tmp/tcp
  71. let op_num=0
  72. for op in "cf $dst $pax -C $src ." "tf $dst" "xf $dst -C $dst_path" \
  73. "f $dst -C $dst_path --diff"; do
  74. let tar_num=0
  75. for tar in $TAR; do
  76. echo -en "$tar\t${OPN[op_num]}\t"
  77. for ((i=1; i<=$n; i++)); do
  78. echo $op | grep -q ^cf && rm -f $dst
  79. echo $op | grep -q ^xf &&
  80. { chmod -R u+w $dst_path
  81. rm -rf $dst_path; mkdir $dst_path; }
  82. sync
  83. if echo $op | grep -q ^f; then # op == compare
  84. time $tar $op ${OPT[$tar_num]} > /dev/null
  85. else # op in (create | list | extract)
  86. time $tar $op ${OPT[$tar_num]} > /dev/null \
  87. || break 3
  88. fi 2>> /tmp/tcp
  89. done
  90. use_times
  91. let tar_num++
  92. done
  93. let op_num++
  94. echo
  95. done
  96. rm -rf $dst_path $dst
  97. echo
  98. cat /tmp/tcp
  99. rm -f /tmp/tcp