visual_regression.sh 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. This is probably too low, but
  43. # a good first pass.
  44. THRESHOLD=0.01
  45. # Set up Directories
  46. # 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)
  47. BUILDFOLDER=./visual_regression
  48. if [ "$1" != "" ]
  49. then
  50. BUILDFOLDER=$1
  51. fi
  52. BLESSED=$BUILDFOLDER/blessed
  53. CURRENT=$BUILDFOLDER/current
  54. DIFF=$BUILDFOLDER/diff
  55. # diff also acts as the temp folder here, unlike in Vexflow, where it is current.
  56. # 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
  57. # All results are stored here.
  58. RESULTS=$DIFF/results.txt
  59. WARNINGS=$DIFF/warnings.txt
  60. # If no prefix is provided, test all images.
  61. if [ "$2" == "" ]
  62. then
  63. files=*.png
  64. else
  65. files=$2*.png
  66. echo "image filter (shell): $files"
  67. fi
  68. ## Sanity checks: some simple checks that the script can run correctly (doesn't validate pngs)
  69. folderWarningStringMsg="Exiting without running visual regression tests."
  70. totalCurrentImages=`ls -1 $CURRENT/$files | wc -l | xargs` # xargs trims space
  71. if [ $? -ne 0 ] || [ "$totalCurrentImages" -lt 1 ] # $? returns the exit code of the previous command (ls). (0 is success)
  72. then
  73. echo Missing images in $CURRENT.
  74. echo Please run \"npm run generate:current\"
  75. exit 1
  76. fi
  77. totalBlessedImages=`ls -1 $BLESSED/$files | wc -l | xargs`
  78. if [ $? -ne 0 ] || [ "$totalBlessedImages" -lt 1 ]
  79. then
  80. echo Missing images in $BLESSED.
  81. echo Please run \"npm run generate:blessed\"
  82. exit 1
  83. fi
  84. # check that #currentImages == #blessedImages (will continue anyways)
  85. if [ ! "$totalCurrentImages" -eq "$totalBlessedImages" ]
  86. then
  87. echo "Warning: Number of current images ($totalCurrentImages) is not the same as blessed images ($totalBlessedImages). Continuing anyways.\n"
  88. else
  89. echo "Found $totalCurrentImages current and $totalBlessedImages blessed png files (not tested if valid). Continuing.\n"
  90. fi
  91. # ----------------- end of sanity checks -----------------
  92. mkdir -p $DIFF
  93. if [ -e "$RESULTS" ]
  94. then
  95. rm $DIFF/*
  96. fi
  97. touch $RESULTS
  98. touch $RESULTS.fails
  99. # 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).
  100. touch $WARNINGS
  101. # Number of simultaneous jobs
  102. nproc=$(sysctl -n hw.physicalcpu 2> /dev/null || nproc)
  103. if [ -n "$NPROC" ]; then
  104. nproc=$NPROC
  105. fi
  106. total=`ls -l $BLESSED/$files | wc -l | sed 's/[[:space:]]//g'`
  107. echo "Running $total tests with threshold $THRESHOLD (nproc=$nproc)..."
  108. function ProgressBar {
  109. let _progress=(${1}*100/${2}*100)/100
  110. let _done=(${_progress}*4)/10
  111. let _left=40-$_done
  112. _fill=$(printf "%${_done}s")
  113. _empty=$(printf "%${_left}s")
  114. printf "\rProgress : [${_fill// /#}${_empty// /-}] ${_progress}%%"
  115. }
  116. function diff_image() {
  117. local image=$1
  118. local name=`basename $image .png`
  119. local blessed=$BLESSED/$name.png
  120. local current=$CURRENT/$name.png
  121. local diff=$DIFF/$name.png-temp
  122. if [ ! -e "$current" ]
  123. then
  124. echo "Warning: $name.png missing in $CURRENT." >$diff.warn
  125. return
  126. fi
  127. if [ ! -e "$blessed" ]
  128. then
  129. return
  130. fi
  131. cp $blessed $diff-a.png
  132. cp $current $diff-b.png
  133. # Calculate the difference metric and store the composite diff image.
  134. local hash=`compare -metric PHASH -highlight-color '#ff000050' $diff-b.png $diff-a.png $diff-diff.png 2>&1`
  135. local isGT=`echo "$hash > $THRESHOLD" | bc -l`
  136. if [ "$isGT" == "1" ]
  137. then
  138. # Add the result to results.txt
  139. echo $name $hash >$diff.fail
  140. # Threshold exceeded, save the diff and the original, current
  141. cp $diff-diff.png $DIFF/$name.png
  142. cp $diff-a.png $DIFF/$name'_'Blessed.png
  143. cp $diff-b.png $DIFF/$name'_'Current.png
  144. echo
  145. echo "Test: $name"
  146. echo " PHASH value exceeds threshold: $hash > $THRESHOLD"
  147. echo " Image diff stored in $DIFF/$name.png"
  148. # $VIEWER "$diff-diff.png" "$diff-a.png" "$diff-b.png"
  149. # echo 'Hit return to process next image...'
  150. # read
  151. else
  152. echo $name $hash >$diff.pass
  153. fi
  154. rm -f $diff-a.png $diff-b.png $diff-diff.png
  155. }
  156. function wait_jobs () {
  157. local n=$1
  158. while [[ "$(jobs -r | wc -l)" -ge "$n" ]] ; do
  159. # echo ===================================== && jobs -lr
  160. # wait the oldest job.
  161. local pid_to_wait=`jobs -rp | head -1`
  162. # echo wait $pid_to_wait
  163. wait $pid_to_wait &> /dev/null
  164. done
  165. }
  166. count=0
  167. for image in $CURRENT/$files
  168. do
  169. count=$((count + 1))
  170. ProgressBar ${count} ${total}
  171. wait_jobs $nproc
  172. diff_image $image &
  173. done
  174. wait
  175. cat $DIFF/*.warn 1>$WARNINGS 2>/dev/null
  176. rm -f $DIFF/*.warn
  177. ## Check for files newly built that are not yet blessed.
  178. for image in $CURRENT/$files
  179. do
  180. name=`basename $image .png`
  181. blessed=$BLESSED/$name.png
  182. current=$CURRENT/$name.png
  183. if [ ! -e "$blessed" ]
  184. then
  185. echo " Warning: $name.png missing in $BLESSED." >>$WARNINGS
  186. fi
  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