| 1 | #!/bin/sh |
| 2 | |
| 3 | # Measures impact of a Git commit (or multiple commits) on generated parsers' |
| 4 | # speed and size. Makes sense to use only on PEG.js Git repository checkout. |
| 5 | |
| 6 | set -e |
| 7 | export LC_ALL=C |
| 8 | |
| 9 | # Measurement |
| 10 | |
| 11 | prepare() { |
| 12 | git checkout --quiet "$1" |
| 13 | } |
| 14 | |
| 15 | run_benchmark() { |
| 16 | echo $(make benchmark | awk 'BEGIN { FS = " *│ *" } /Total/ { split($5, a, " "); print a[1] }') |
| 17 | } |
| 18 | |
| 19 | measure_speed() { |
| 20 | bc <<-EOT |
| 21 | scale = 2 |
| 22 | ($(run_benchmark) + $(run_benchmark) + $(run_benchmark) + $(run_benchmark) + $(run_benchmark)) / 5 |
| 23 | EOT |
| 24 | } |
| 25 | |
| 26 | measure_size() { |
| 27 | for file in examples/*.pegjs; do |
| 28 | bin/pegjs "$file" |
| 29 | done |
| 30 | |
| 31 | echo $(cat examples/*.js | wc -c) |
| 32 | |
| 33 | rm examples/*.js |
| 34 | } |
| 35 | |
| 36 | difference() { |
| 37 | bc <<-EOT |
| 38 | scale = 4 |
| 39 | ($2 / $1 - 1) * 100 |
| 40 | EOT |
| 41 | } |
| 42 | |
| 43 | # Helpers |
| 44 | |
| 45 | print_results() { |
| 46 | echo |
| 47 | |
| 48 | echo "Speed impact" |
| 49 | echo "------------" |
| 50 | echo "Before: $1 kB/s" |
| 51 | echo "After: $2 kB/s" |
| 52 | printf "Difference: %0.2f%%\n" $(difference $1 $2) |
| 53 | |
| 54 | echo |
| 55 | |
| 56 | echo "Size impact" |
| 57 | echo "-----------" |
| 58 | echo "Before: $3 b" |
| 59 | echo "After: $4 b" |
| 60 | printf "Difference: %0.2f%%\n" $(difference $3 $4) |
| 61 | |
| 62 | echo |
| 63 | |
| 64 | echo "(Measured by /tools/impact with Node.js $(node --version) on $(uname -mrs).)" |
| 65 | } |
| 66 | |
| 67 | print_usage() { |
| 68 | echo "Usage:" |
| 69 | echo " $0 <commit>" |
| 70 | echo " $0 <commit_before> <commit_after>" |
| 71 | echo |
| 72 | echo "Measures impact of a Git commit (or multiple commits) on generated parsers'" |
| 73 | echo "speed and size. Makes sense to use only on PEG.js Git repository checkout." |
| 74 | } |
| 75 | |
| 76 | cd_to_root() { |
| 77 | if [ -L "$0" ]; then |
| 78 | THIS_FILE=$(readlink "$0") |
| 79 | else |
| 80 | THIS_FILE="$0" |
| 81 | fi |
| 82 | cd "$(dirname "$THIS_FILE")/.." |
| 83 | } |
| 84 | |
| 85 | exit_failure() { |
| 86 | exit 1 |
| 87 | } |
| 88 | |
| 89 | # Main |
| 90 | |
| 91 | if [ $# -eq 1 ]; then |
| 92 | commit_before="$1~1" |
| 93 | commit_after="$1" |
| 94 | elif [ $# -eq 2 ]; then |
| 95 | commit_before="$1" |
| 96 | commit_after="$2" |
| 97 | else |
| 98 | print_usage |
| 99 | exit_failure |
| 100 | fi |
| 101 | |
| 102 | cd_to_root |
| 103 | |
| 104 | printf "Measuring commit %s..." "$commit_before" |
| 105 | prepare "$commit_before" |
| 106 | speed1=$(measure_speed) |
| 107 | size1=$(measure_size) |
| 108 | echo " OK" |
| 109 | |
| 110 | printf "Measuring commit %s..." "$commit_after" |
| 111 | prepare "$commit_after" |
| 112 | speed2=$(measure_speed) |
| 113 | size2=$(measure_size) |
| 114 | echo " OK" |
| 115 | |
| 116 | print_results $speed1 $speed2 $size1 $size2 |