|
1 #!/bin/sh |
|
2 # |
|
3 # This Source Code Form is subject to the terms of the Mozilla Public |
|
4 # License, v. 2.0. If a copy of the MPL was not distributed with this |
|
5 # file, You can obtain one at http://mozilla.org/MPL/2.0/. |
|
6 |
|
7 # histogram-pretty.sh [-c <count>] [-w <width>] <file> |
|
8 # |
|
9 # Pretty-print the histogram in file <file>, displaying at most |
|
10 # <count> rows. |
|
11 |
|
12 # How many rows are we gonna show? |
|
13 COUNT=20 |
|
14 WIDTH=22 |
|
15 |
|
16 # Read arguments |
|
17 while [ $# -gt 0 ]; do |
|
18 case "$1" in |
|
19 -c) COUNT=$2 |
|
20 shift 2 |
|
21 ;; |
|
22 -w) WIDTH=$2 |
|
23 shift 2 |
|
24 ;; |
|
25 *) break |
|
26 ;; |
|
27 esac |
|
28 done |
|
29 |
|
30 FILE=$1 |
|
31 |
|
32 # The first `awk' script computes a `TOTAL' row. Then, we sort by the |
|
33 # larges delta in bytes. |
|
34 awk '{ tobj += $2; tbytes += $3; } END { print "TOTAL", tobj, tbytes; }' ${FILE} > /tmp/$$.sorted |
|
35 |
|
36 sort -nr +2 ${FILE} >> /tmp/$$.sorted |
|
37 |
|
38 # Pretty-print, including percentages |
|
39 cat <<EOF > /tmp/$$.awk |
|
40 BEGIN { |
|
41 printf "%-${WIDTH}s Count Bytes %Total %Cov\n", "Type"; |
|
42 } |
|
43 \$1 == "TOTAL" { |
|
44 tbytes = \$3; |
|
45 } |
|
46 NR <= $COUNT { |
|
47 if (\$1 != "TOTAL") { |
|
48 covered += \$3; |
|
49 } |
|
50 printf "%-${WIDTH}s %6d %8d %6.2lf %6.2lf\n", \$1, \$2, \$3, 100.0 * \$3 / tbytes, 100.0 * covered / tbytes; |
|
51 } |
|
52 NR > $COUNT { |
|
53 oobjs += \$2; obytes += \$3; covered += \$3; |
|
54 } |
|
55 END { |
|
56 printf "%-${WIDTH}s %6d %8d %6.2lf %6.2lf\n", "OTHER", oobjs, obytes, obytes * 100.0 / tbytes, covered * 100.0 / tbytes; |
|
57 } |
|
58 EOF |
|
59 |
|
60 # Now pretty print the file, and spit it out on stdout. |
|
61 awk -f /tmp/$$.awk /tmp/$$.sorted |
|
62 |
|
63 rm -f /tmp/$$.awk /tmp/$$.sorted |