chunkperf.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Thomas Müller <thomas.mueller@tmit.eu>
  6. *
  7. * @license AGPL-3.0
  8. *
  9. * This code is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License, version 3,
  11. * as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License, version 3,
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>
  20. *
  21. */
  22. require '../../../../3rdparty/autoload.php';
  23. if ($argc !== 6) {
  24. echo "Invalid number of arguments" . PHP_EOL;
  25. exit;
  26. }
  27. /**
  28. * @param \Sabre\DAV\Client $client
  29. * @param $uploadUrl
  30. * @return mixed
  31. */
  32. function request($client, $method, $uploadUrl, $data = null, $headers = []) {
  33. echo "$method $uploadUrl ... ";
  34. $t0 = microtime(true);
  35. $result = $client->request($method, $uploadUrl, $data, $headers);
  36. $t1 = microtime(true);
  37. echo $result['statusCode'] . " - " . ($t1 - $t0) . ' seconds' . PHP_EOL;
  38. if (!in_array($result['statusCode'], [200, 201])) {
  39. echo $result['body'] . PHP_EOL;
  40. }
  41. return $result;
  42. }
  43. $baseUri = $argv[1];
  44. $userName = $argv[2];
  45. $password = $argv[3];
  46. $file = $argv[4];
  47. $chunkSize = $argv[5] * 1024 * 1024;
  48. $client = new \Sabre\DAV\Client([
  49. 'baseUri' => $baseUri,
  50. 'userName' => $userName,
  51. 'password' => $password
  52. ]);
  53. $transfer = uniqid('transfer', true);
  54. $uploadUrl = "$baseUri/uploads/$userName/$transfer";
  55. request($client, 'MKCOL', $uploadUrl);
  56. $size = filesize($file);
  57. $stream = fopen($file, 'r');
  58. $index = 0;
  59. while(!feof($stream)) {
  60. request($client, 'PUT', "$uploadUrl/$index", fread($stream, $chunkSize));
  61. $index++;
  62. }
  63. $destination = pathinfo($file, PATHINFO_BASENAME);
  64. //echo "Moving $uploadUrl/.file to it's final destination $baseUri/files/$userName/$destination" . PHP_EOL;
  65. request($client, 'MOVE', "$uploadUrl/.file", null, [
  66. 'Destination' => "$baseUri/files/$userName/$destination"
  67. ]);