visual_regression.sh 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. #!/bin/bash
  2. # This script runs a visual regression test on all the images
  3. # generated from OSMD samples (npm run generate:current and npm run generate:blessed)
  4. #
  5. # inspired by and adapted from Vexflow's visual regression tests.
  6. #
  7. # Prerequisites: ImageMagick
  8. #
  9. # On OSX: $ brew install imagemagick
  10. # On Linux: $ apt-get install imagemagick
  11. #
  12. # Usage:
  13. #
  14. #
  15. # First generate the known good or previous state PNG images you want to compare to, e.g. the develop branch or last release:
  16. # (Server has to be running for this: npm start)
  17. #
  18. # npm run generate:blessed
  19. #
  20. # Make changes in OSMD, then generate your new images:
  21. #
  22. # npm run generate:current
  23. #
  24. # Run the regression tests against the blessed images in visual_regression/blessed.
  25. #
  26. # npm run test:visual
  27. # # npm will navigate to the base folder automatically
  28. #
  29. # # or: (this should be done from the main OSMD folder)
  30. # # sh test/Util/visual_regression.sh [imageBaseFolder] [sampleShellPrefix]
  31. # # example: sh test/Util/visual_regression.sh ./visual_regression OSMD_function_test_
  32. # # will run visual regression tests for all images matching OSMD_function_test_*.png.
  33. #
  34. # Check visual_regression/diff/results.txt for results. This file is sorted
  35. # by PHASH difference (most different files on top.) The composite diff
  36. # images for failed tests (i.e., PHASH > 1.0) are stored in visual_regression/diff.
  37. #
  38. # (If you are satisfied with the differences, copy *.png from visual_regression/current
  39. # into visual_regression/blessed, and submit your change (TODO))
  40. # PNG viewer on OSX. Switch this to whatever your system uses.
  41. # VIEWER=open
  42. # Show images over this PHASH threshold.
  43. # 0.01 is probably too low, but a good first pass.
  44. # 0.0001 catches for example a repetition ending not having a down line at the end (see Saltarello bar 10) (0.001 doesn't catch this)
  45. THRESHOLD=0.0001
  46. # Set up Directories
  47. # It does not matter where this script is executed, as long as these folders are given correctly (and blessed/current have png images set up correctly)
  48. BUILDFOLDER=./visual_regression
  49. if [ "$1" != "" ]
  50. then
  51. BUILDFOLDER=$1
  52. fi
  53. BLESSED=$BUILDFOLDER/blessed
  54. CURRENT=$BUILDFOLDER/current
  55. DIFF=$BUILDFOLDER/diff
  56. # diff also acts as the temp folder here, unlike in Vexflow, where it is current.
  57. # it would be nice to have a tmp folder (for temporary files), but we'd want to delete the folder entirely, and we'd better not risk using rm -rf in a script
  58. # All results are stored here.
  59. RESULTS=$DIFF/results.txt
  60. WARNINGS=$DIFF/warnings.txt
  61. # If no prefix is provided, test all images.
  62. if [ "$2" == "" ]
  63. then
  64. files=*.png
  65. else
  66. files=$2*.png
  67. echo "image filter (shell): $files"
  68. fi
  69. ## Sanity checks: some simple checks that the script can run correctly (doesn't validate pngs)
  70. folderWarningStringMsg="Exiting without running visual regression tests."
  71. totalCurrentImages=`ls -1 $CURRENT/$files | wc -l | xargs` # xargs trims space
  72. if [ $? -ne 0 ] || [ "$totalCurrentImages" -lt 1 ] # $? returns the exit code of the previous command (ls). (0 is success)
  73. then
  74. echo Missing images in $CURRENT.
  75. echo Please run \"npm run generate:current\"
  76. exit 1
  77. fi
  78. totalBlessedImages=`ls -1 $BLESSED/$files | wc -l | xargs`
  79. if [ $? -ne 0 ] || [ "$totalBlessedImages" -lt 1 ]
  80. then
  81. echo Missing images in $BLESSED.
  82. echo Please run \"npm run generate:blessed\"
  83. exit 1
  84. fi
  85. # check that #currentImages == #blessedImages (will continue anyways)
  86. if [ ! "$totalCurrentImages" -eq "$totalBlessedImages" ]
  87. then
  88. echo "Warning: Number of current images ($totalCurrentImages) is not the same as blessed images ($totalBlessedImages). Continuing anyways.\n"
  89. else
  90. echo "Found $totalCurrentImages current and $totalBlessedImages blessed png files (not tested if valid). Continuing.\n"
  91. fi
  92. # ----------------- end of sanity checks -----------------
  93. mkdir -p $DIFF
  94. if [ -e "$RESULTS" ]
  95. then
  96. rm $DIFF/*
  97. fi
  98. touch $RESULTS
  99. touch $RESULTS.fails
  100. # this shouldn't be named .fail because we have a *.fail shell match further below, which will loop endlessly if files are in the same folder (diff).
  101. touch $WARNINGS
  102. # Number of simultaneous jobs
  103. nproc=$(sysctl -n hw.physicalcpu 2> /dev/null || nproc)
  104. if [ -n "$NPROC" ]; then
  105. nproc=$NPROC
  106. fi
  107. total=`ls -l $BLESSED/$files | wc -l | sed 's/[[:space:]]//g'`
  108. echo "Running $total tests with threshold $THRESHOLD (nproc=$nproc)..."
  109. function ProgressBar {
  110. let _progress=(${1}*100/${2}*100)/100
  111. let _done=(${_progress}*4)/10
  112. let _left=40-$_done
  113. _fill=$(printf "%${_done}s")
  114. _empty=$(printf "%${_left}s")
  115. printf "\rProgress : [${_fill// /#}${_empty// /-}] ${_progress}%%"
  116. }
  117. function diff_image() {
  118. local image=$1
  119. local name=`basename $image .png`
  120. local blessed=$BLESSED/$name.png
  121. local current=$CURRENT/$name.png
  122. local diff=$DIFF/$name.png-temp
  123. if [ ! -e "$current" ]
  124. then
  125. echo " Warning: $name.png missing in $CURRENT. Skipped." >$diff.warn
  126. #((total--))
  127. return
  128. fi
  129. if [ ! -e "$blessed" ]
  130. then
  131. echo " Warning: $name.png doesn't exist in $BLESSED. Skipped." >$diff.warn
  132. #((total--))
  133. return
  134. fi
  135. cp $blessed $diff-a.png
  136. cp $current $diff-b.png
  137. # Calculate the difference metric and store the composite diff image.
  138. local hash=`compare -metric PHASH -highlight-color '#ff000050' $diff-b.png $diff-a.png $diff-diff.png 2>&1`
  139. local isGT=`echo "$hash > $THRESHOLD" | bc -l`
  140. if [ "$isGT" == "1" ]
  141. then
  142. # Add the result to results.txt
  143. echo $name $hash >$diff.fail
  144. # Threshold exceeded, save the diff and the original, current
  145. cp $diff-diff.png $DIFF/$name.png
  146. cp $diff-a.png $DIFF/$name'_'Blessed.png
  147. cp $diff-b.png $DIFF/$name'_'Current.png
  148. echo
  149. echo "Test: $name"
  150. echo " PHASH value exceeds threshold: $hash > $THRESHOLD"
  151. echo " Image diff stored in $DIFF/$name.png"
  152. # $VIEWER "$diff-diff.png" "$diff-a.png" "$diff-b.png"
  153. # echo 'Hit return to process next image...'
  154. # read
  155. else
  156. echo $name $hash >$diff.pass
  157. fi
  158. rm -f $diff-a.png $diff-b.png $diff-diff.png
  159. }
  160. function wait_jobs () {
  161. local n=$1
  162. while [[ "$(jobs -r | wc -l)" -ge "$n" ]] ; do
  163. # echo ===================================== && jobs -lr
  164. # wait the oldest job.
  165. local pid_to_wait=`jobs -rp | head -1`
  166. # echo wait $pid_to_wait
  167. wait $pid_to_wait &> /dev/null
  168. done
  169. }
  170. count=0
  171. for image in $CURRENT/$files
  172. do
  173. count=$((count + 1))
  174. ProgressBar ${count} ${total}
  175. wait_jobs $nproc
  176. diff_image $image &
  177. done
  178. wait
  179. cat $DIFF/*.warn 1>$WARNINGS 2>/dev/null
  180. rm -f $DIFF/*.warn
  181. ## Check for files newly built that are not yet blessed.
  182. for image in $CURRENT/$files
  183. do
  184. name=`basename $image .png`
  185. blessed=$BLESSED/$name.png
  186. current=$CURRENT/$name.png
  187. done
  188. num_warnings=`cat $WARNINGS | wc -l`
  189. cat $DIFF/*.fail 1>$RESULTS.fails 2>/dev/null
  190. num_fails=`cat $RESULTS.fails | wc -l`
  191. rm -f $DIFF/*.fail
  192. # Sort results by PHASH
  193. sort -r -n -k 2 $RESULTS.fails >$RESULTS
  194. sort -r -n -k 2 $DIFF/*.pass 1>>$RESULTS 2>/dev/null
  195. rm -f $DIFF/*.pass $RESULTS.fails
  196. echo
  197. echo Results stored in $DIFF/results.txt
  198. echo All images with a difference over threshold, $THRESHOLD, are
  199. echo available in $DIFF, sorted by perceptual hash.
  200. echo
  201. if [ "$num_warnings" -gt 0 ]
  202. then
  203. echo
  204. echo "You have $num_warnings warning(s):"
  205. cat $WARNINGS
  206. fi
  207. if [ "$num_fails" -gt 0 ]
  208. then
  209. echo "You have $num_fails fail(s):"
  210. head -n $num_fails $RESULTS
  211. else
  212. echo "Success - All diffs under threshold!"
  213. fi