Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[uc] Improve chunk upload AssemblyStream performance #1370

Merged
merged 1 commit into from
Sep 13, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 31 additions & 8 deletions apps/dav/lib/Upload/AssemblyStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ class AssemblyStream implements \Icewind\Streams\File {
/** @var int */
private $size;

/** @var resource */
private $currentStream = null;

/**
* @param string $path
* @param string $mode
Expand Down Expand Up @@ -102,16 +105,36 @@ public function stream_tell() {
* @return string
*/
public function stream_read($count) {
do {
if ($this->currentStream === null) {
list($node, $posInNode) = $this->getNodeForPosition($this->pos);
if (is_null($node)) {
// reached last node, no more data
return '';
}
$this->currentStream = $this->getStream($node);
fseek($this->currentStream, $posInNode);
}

list($node, $posInNode) = $this->getNodeForPosition($this->pos);
if (is_null($node)) {
return null;
}
$stream = $this->getStream($node);
$data = fread($this->currentStream, $count);
// isset is faster than strlen
if (isset($data[$count - 1])) {
// we read the full count
$read = $count;
} else {
// reaching end of stream, which happens less often so strlen is ok
$read = strlen($data);
}

fseek($stream, $posInNode);
$data = fread($stream, $count);
$read = strlen($data);
if (feof($this->currentStream)) {
fclose($this->currentStream);
$this->currentNode = null;
$this->currentStream = null;
}
// if no data read, try again with the next node because
// returning empty data can make the caller think there is no more
// data left to read
} while ($read === 0);

// update position
$this->pos += $read;
Expand Down
66 changes: 63 additions & 3 deletions apps/dav/tests/unit/Upload/AssemblyStreamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,78 @@ public function testGetContents($expected, $nodes) {
$this->assertEquals($expected, $content);
}

/**
* @dataProvider providesNodes()
*/
public function testGetContentsFread($expected, $nodes) {
$stream = \OCA\DAV\Upload\AssemblyStream::wrap($nodes);

$content = '';
while (!feof($stream)) {
$content .= fread($stream, 3);
}

$this->assertEquals($expected, $content);
}

function providesNodes() {
$data8k = $this->makeData(8192);
$dataLess8k = $this->makeData(8191);
return[
'one node only' => ['1234567890', [
'one node zero bytes' => [
'', [
$this->buildNode('0', '')
]],
'one node only' => [
'1234567890', [
$this->buildNode('0', '1234567890')
]],
'two nodes' => ['1234567890', [
'one node buffer boundary' => [
$data8k, [
$this->buildNode('0', $data8k)
]],
'two nodes' => [
'1234567890', [
$this->buildNode('1', '67890'),
$this->buildNode('0', '12345')
]]
]],
'two nodes end on buffer boundary' => [
$data8k . $data8k, [
$this->buildNode('1', $data8k),
$this->buildNode('0', $data8k)
]],
'two nodes with one on buffer boundary' => [
$data8k . $dataLess8k, [
$this->buildNode('1', $dataLess8k),
$this->buildNode('0', $data8k)
]],
'two nodes on buffer boundary plus one byte' => [
$data8k . 'X' . $data8k, [
$this->buildNode('1', $data8k),
$this->buildNode('0', $data8k . 'X')
]],
'two nodes on buffer boundary plus one byte at the end' => [
$data8k . $data8k . 'X', [
$this->buildNode('1', $data8k . 'X'),
$this->buildNode('0', $data8k)
]],
];
}

private function makeData($count) {
$data = '';
$base = '1234567890';
$j = 0;
for ($i = 0; $i < $count; $i++) {
$data .= $base[$j];
$j++;
if (!isset($base[$j])) {
$j = 0;
}
}
return $data;
}

private function buildNode($name, $data) {
$node = $this->getMockBuilder('\Sabre\DAV\File')
->setMethods(['getName', 'get', 'getSize'])
Expand Down