single_upload.sh 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #!/bin/bash
  2. set -eu
  3. # single_upload.sh <nb-of-files> <size-of-files>
  4. export KB=${KB:-100}
  5. export MB=${MB:-$((KB*1000))}
  6. export NB=$1
  7. export SIZE=$2
  8. export CONCURRENCY=${CONCURRENCY:-1}
  9. export BANDWIDTH=${BANDWIDTH:-$((100*MB/CONCURRENCY))}
  10. export USER="admin"
  11. export PASS="password"
  12. export SERVER="nextcloud.test"
  13. export UPLOAD_ID="single_$(openssl rand --hex 8)"
  14. export LOCAL_FOLDER="/tmp/single_upload/${UPLOAD_ID}_${NB}_${SIZE}"
  15. export REMOTE_FOLDER="/single_upload/${UPLOAD_ID}_${NB}_${SIZE}"
  16. mkdir --parent "$LOCAL_FOLDER"
  17. curl \
  18. -X MKCOL \
  19. -k \
  20. "https://$USER:$PASS@$SERVER/remote.php/dav/files/$USER/bulk_upload" > /dev/null
  21. curl \
  22. -X MKCOL \
  23. -k \
  24. --cookie "XDEBUG_SESSION=true;path=/;" \
  25. "https://$USER:$PASS@$SERVER/remote.php/dav/files/$USER/$REMOTE_FOLDER"
  26. upload_file() {
  27. file_name=$(openssl rand --hex 8)
  28. file_local_path="$LOCAL_FOLDER/$file_name.txt"
  29. file_remote_path="$REMOTE_FOLDER/$file_name.txt"
  30. head -c "$SIZE" /dev/urandom > "$file_local_path"
  31. curl \
  32. -X PUT \
  33. -k \
  34. --limit-rate "${BANDWIDTH}k" \
  35. --data-binary @"$file_local_path" "https://$USER:$PASS@$SERVER/remote.php/webdav/$file_remote_path"
  36. }
  37. export -f upload_file
  38. file_list=''
  39. for ((i=1; i<"$NB"; i++))
  40. do
  41. file_list+="$i "
  42. done
  43. file_list+=$NB
  44. echo "$file_list" | xargs -d ' ' -P "$((CONCURRENCY/5))" -I{} bash -c "upload_file {}"
  45. printf "\n"
  46. rm -rf "${LOCAL_FOLDER:?}"/*