single_upload.sh 1.5 KB

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