Skip to content

Commit 0546103

Browse files
committed
add performance tests
1 parent 050f53e commit 0546103

File tree

4 files changed

+94
-10
lines changed

4 files changed

+94
-10
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
- [User-defined classes plugin](#user-defined-classes-plugin)
2121
- [Annotation plugin](#annotation-plugin)
2222
- [Internals](#internals)
23+
- [Performance](#performance)
2324

2425
## Installation
2526
The recommended way to install the library is through [Composer](http://getcomposer.org):
@@ -399,3 +400,16 @@ $mapper->getRepository('person')->findOne(['name' => 'Dmitry']);
399400
// you can flush cache manually
400401
$mapper->getRepository('person')->flushCache();
401402
```
403+
404+
## Performance
405+
Mapper overhead depends on amount of rows and operation type.
406+
Table contains overhead in **milliseconds** per entity. In some cases, overhead can't be calculated due float precision.
407+
408+
| Operation | 100 | 1000 | 10 000 | 100 000 |
409+
| --- | --- | --- | --- | --- |
410+
| create entity one by one | 0.017 | 0.022 | 0.023 | 0.024 |
411+
| select entity one by one | - | 0.015 | 0.016 | 0.018 |
412+
| one select for all entites | - | - | 0.002 | 0.006 |
413+
414+
Perfomance test was made on (intel i5-6400), bash for windows 10 using php 7.0.18.
415+
For example, when select will create 10 000 entites overhead will be 19ms.

src/Client.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,15 @@ private function log($start, $class, $request = [], $response = [])
3434
}
3535
}
3636

37+
public function getTimeSummary()
38+
{
39+
$summary = 0;
40+
foreach($this->log as $request) {
41+
$summary += $request[0];
42+
}
43+
return $summary;
44+
}
45+
3746
public function setLogging($logging)
3847
{
3948
$this->logging = $logging;
@@ -44,4 +53,4 @@ public function getLog()
4453
return $this->log;
4554
}
4655

47-
}
56+
}

src/Repository.php

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,10 @@ public function findOne($params = [])
8989

9090
public function find($params = [], $one = false)
9191
{
92-
$cacheIndex = array_search([$params, $one], $this->cache);
93-
if($cacheIndex !== false) {
94-
return $this->results[$cacheIndex];
92+
$cacheKey = json_encode(func_get_args());
93+
94+
if(array_key_exists($cacheKey, $this->results)) {
95+
return $this->results[$cacheKey];
9596
}
9697

9798
if(!is_array($params)) {
@@ -122,9 +123,6 @@ public function find($params = [], $one = false)
122123
throw new Exception("No index for params ".json_encode($params));
123124
}
124125

125-
$cacheIndex = count($this->cache);
126-
$this->cache[] = [$params, $one];
127-
128126
$client = $this->space->getMapper()->getClient();
129127
$values = $this->space->getIndexValues($index, $params);
130128

@@ -134,16 +132,16 @@ public function find($params = [], $one = false)
134132
foreach($data as $tuple) {
135133
$instance = $this->getInstance($tuple);
136134
if($one) {
137-
return $this->results[$cacheIndex] = $instance;
135+
return $this->results[$cacheKey] = $instance;
138136
}
139137
$result[] = $instance;
140138
}
141139

142140
if($one) {
143-
return $this->results[$cacheIndex] = null;
141+
return $this->results[$cacheKey] = null;
144142
}
145143

146-
return $this->results[$cacheIndex] = $result;
144+
return $this->results[$cacheKey] = $result;
147145
}
148146

149147
private function getInstance($tuple)

tests/PerformanceTest.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
use Tarantool\Mapper\Client;
4+
use Tarantool\Mapper\Mapper;
5+
use Tarantool\Mapper\Schema;
6+
use Tarantool\Mapper\Plugins\Sequence;
7+
8+
class PerformanceTest extends TestCase
9+
{
10+
public $counter = 10000;
11+
12+
public function test()
13+
{
14+
$mapper = $this->createMapper();
15+
$this->clean($mapper);
16+
17+
$mapper->getSchema()
18+
->createSpace('tester', [
19+
'id' => 'unsigned',
20+
'text' => 'str',
21+
])
22+
->addIndex('id');
23+
24+
25+
$this->exec('create', 2000, function(Mapper $mapper) {
26+
foreach(range(1, $this->counter) as $id) {
27+
$mapper->create('tester', [$id, "text for $id"]);
28+
}
29+
});
30+
31+
$this->exec('read ony by one', 20000, function(Mapper $mapper) {
32+
foreach(range(1, $this->counter) as $id) {
33+
$mapper->findOne('tester', $id);
34+
}
35+
});
36+
37+
$this->exec('mass read', 200000, function(Mapper $mapper) {
38+
$mapper->find('tester');
39+
});
40+
}
41+
42+
private function exec($label, $value, Callable $runner)
43+
{
44+
$startTime = microtime(1);
45+
46+
$mapper = $this->createMapper();
47+
$mapper->getClient()->setLogging(true);
48+
$runner($mapper);
49+
50+
$totalTime = microtime(1) - $startTime;
51+
52+
$cleanTime = $totalTime - $mapper->getClient()->getTimeSummary();
53+
if($cleanTime <= 0) {
54+
return [$label, [$totalTime, $mapper->getClient()->getTimeSummary()]];
55+
}
56+
57+
$mappingPerSecond = $this->counter / $cleanTime;
58+
$this->assertGreaterThan($value, $mappingPerSecond, "exec: $label");
59+
60+
// output overhead in milliseconds per entity
61+
// var_dump($label.": ".(1000 * $cleanTime / $this->counter). ' ' .$cleanTime);
62+
}
63+
}

0 commit comments

Comments
 (0)