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