MongoDB VS Mysql 簡單測試.

廢話不多說.
MongoDB 使用 MongoDB Object Document Mapper測試.
Mysql 使用 Object Relational Mapper測試.
MongoDB部分:
config.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?php
require_once __DIR__ . '/../../lib/vendor/doctrine-common/lib/Doctrine/Common/ClassLoader.php';
use Doctrine\Common\ClassLoader,
    Doctrine\Common\Annotations\AnnotationReader,
    Doctrine\ODM\MongoDB\Configuration,
    Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver,
    Doctrine\MongoDB\Connection,
    Doctrine\ODM\MongoDB\DocumentManager;
 
$classLoader = new ClassLoader('Doctrine\Common', __DIR__ . '/../../lib/vendor/doctrine-common/lib');
$classLoader->register();
 
$classLoader = new ClassLoader('Doctrine\ODM\MongoDB', __DIR__ . '/../../lib');
$classLoader->register();
 
$classLoader = new ClassLoader('Doctrine\MongoDB', __DIR__ . '/../../lib/vendor/doctrine-mongodb/lib');
$classLoader->register();
 
$classLoader = new ClassLoader('Symfony', __DIR__ . '/../../lib/vendor');
$classLoader->register();
 
$classLoader = new ClassLoader('Documents', __DIR__);
$classLoader->register();
 
$config = new Configuration();
 
$config->setProxyDir(__DIR__ . '/Proxies');
$config->setProxyNamespace('Proxies');
 
$config->setHydratorDir(__DIR__ . '/Hydrators');
$config->setHydratorNamespace('Hydrators');
 
$config->setDefaultDB('doctrine_odm_sandbox');
//使用safe模式
$con = new Connection('localhost',array('safe'=>true, 'w' => 2,'wtimeoutMS' => 2000));
//使用一般模式
//$con = new Connection('localhost');
 
$reader = new AnnotationReader();
$reader->setDefaultAnnotationNamespace('Doctrine\ODM\MongoDB\Mapping\\');
$config->setMetadataDriverImpl(new AnnotationDriver($reader, __DIR__ . '/Documents'));
 
$dm = DocumentManager::create($con, $config);


測試新增用index.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?php
require_once 'config.php';
 
use Documents\User,
    Documents\Address,
    Documents\Phonenumber,
    Documents\Account;
 
// Your code here...
$mtime = explode(' ', microtime());
$starttime = $mtime[1] + $mtime[0];
for($i=1; $i<=1000; $i++) {
    $Account = new Account('Test Account'.rand(1,99999));
	$dm->persist($Account);
	$dm->flush();
}
$mtime = explode(' ', microtime());
$times=number_format(($mtime[1] + $mtime[0] - $starttime), 8);
echo "saft Inster time $times \n";

測試一般寫入1000筆:

1
2
$php index.php
Normal Inster time 14.19290113

測試saft寫入1000筆:

1
2
$ php index.php
saft Inster time 14.25004005

更新測試index.php:
更新七筆資料.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?php
 
require_once 'config.php';
 
use Documents\User,
    Documents\Address,
    Documents\Phonenumber,
    Documents\Account;
 
// Your code here...
$mtime = explode(' ', microtime());
$starttime = $mtime[1] + $mtime[0];
$Account_old = $dm->getRepository('Documents\Account')->find('4e382626e08239cbcf000027');
$Account_old->setName('Test Account'.rand(1,99999));
$dm->persist($Account_old);
$dm->flush();
$Account_old = $dm->getRepository('Documents\Account')->find('4e382626e08239cbcf000026');
$Account_old->setName('Test Account'.rand(1,99999));
$dm->persist($Account_old);
$dm->flush();
$Account_old = $dm->getRepository('Documents\Account')->find('4e382626e08239cbcf000025');
$Account_old->setName('Test Account'.rand(1,99999));
$dm->persist($Account_old);
$dm->flush();
$Account_old = $dm->getRepository('Documents\Account')->find('4e382626e08239cbcf000024');
$Account_old->setName('Test Account'.rand(1,99999));
$dm->persist($Account_old);
$dm->flush();
$Account_old = $dm->getRepository('Documents\Account')->find('4e382626e08239cbcf000023');
$Account_old->setName('Test Account'.rand(1,99999));
$dm->persist($Account_old);
$dm->flush();
$Account_old = $dm->getRepository('Documents\Account')->find('4e382626e08239cbcf000022');
$Account_old->setName('Test Account'.rand(1,99999));
$dm->persist($Account_old);
$dm->flush();
$Account_old = $dm->getRepository('Documents\Account')->find('4e382626e08239cbcf000021');
$Account_old->setName('Test Account'.rand(1,99999));
$dm->persist($Account_old);
$dm->flush();
$mtime = explode(' ', microtime());
$times=number_format(($mtime[1] + $mtime[0] - $starttime), 8);
echo "Update time $times \n";

使用apache ab做測試表示每個連線都對此7筆資料做更新.
saft寫入:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
$ ab -k -c 20 -n 1000 http://localhost/mongodb_odm/tools/sandbox/
....
Server Software:        nginx
Server Hostname:        localhost
Server Port:            80
 
Document Path:          /mongodb_odm/tools/sandbox/
Document Length:        24 bytes
 
Concurrency Level:      20
Time taken for tests:   15.655 seconds
Complete requests:      1000
Failed requests:        0
Write errors:           0
Keep-Alive requests:    0
Total transferred:      187000 bytes
HTML transferred:       24000 bytes
Requests per second:    63.88 [#/sec] (mean)
Time per request:       313.110 [ms] (mean)
Time per request:       15.655 [ms] (mean, across all concurrent requests)
Transfer rate:          11.66 [Kbytes/sec] received
 
Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    2   9.2      0     165
Processing:    28  307 312.6    238    3510
Waiting:       28  306 312.7    236    3510
Total:         29  309 313.2    240    3510

saft更新:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
$ ab -k -c 100 -n 2000 http://localhost/mongodb_odm/tools/sandbox/
........
Server Software:        nginx
Server Hostname:        localhost
Server Port:            80
 
Document Path:          /mongodb_odm/tools/sandbox/
Document Length:        24 bytes
 
Concurrency Level:      100
Time taken for tests:   32.222 seconds
Complete requests:      2000
Failed requests:        345
   (Connect: 0, Receive: 0, Length: 345, Exceptions: 0)
Write errors:           0
Keep-Alive requests:    0
Total transferred:      439550 bytes
HTML transferred:       113550 bytes
Requests per second:    62.07 [#/sec] (mean)
Time per request:       1611.098 [ms] (mean)
Time per request:       16.111 [ms] (mean, across all concurrent requests)
Transfer rate:          13.32 [Kbytes/sec] received
 
Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    3  10.6      0     101
Processing:   502 1580 525.6   1512   10122
Waiting:      502 1579 525.5   1512   10122
Total:        504 1583 525.7   1515   10122

saft暴力更新:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
$ ab -k -c 100 -n 2000 http://localhost/mongodb_odm/tools/sandbox/
........
Server Software:        nginx
Server Hostname:        localhost
Server Port:            80
 
Document Path:          /mongodb_odm/tools/sandbox/
Document Length:        24 bytes
 
Concurrency Level:      200
Time taken for tests:   166.432 seconds
Complete requests:      10000
Failed requests:        2029
   (Connect: 0, Receive: 0, Length: 2029, Exceptions: 0)
Write errors:           0
Keep-Alive requests:    0
Total transferred:      2254187 bytes
HTML transferred:       624187 bytes
Requests per second:    60.08 [#/sec] (mean)
Time per request:       3328.646 [ms] (mean)
Time per request:       16.643 [ms] (mean, across all concurrent requests)
Transfer rate:          13.23 [Kbytes/sec] received
 
Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    8  42.5      0     472
Processing:   471 3295 693.5   3211   17451
Waiting:      471 3292 693.4   3208   17451
Total:        475 3303 687.6   3216   17451

Mysql部分:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// Create EntityManager
$em = EntityManager::create($connectionOptions, $config);
 
## PUT YOUR TEST CODE BELOW
$mtime = explode(' ', microtime());
$starttime = $mtime[1] + $mtime[0];
for($i=1; $i<=10; $i++) {
	$user = new User;
	$user->setName('Test Account'.rand(1,99999));
	$em->persist($user);
	$em->flush();
}
$mtime = explode(' ', microtime());
$times=number_format(($mtime[1] + $mtime[0] - $starttime), 8);
echo "Inster time $times \n";

新增測試:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
$ ab -k -c 20 -n 1000 http://localhost/doctrine2-orm/tools/sandbox/
........
Server Software:        nginx
Server Hostname:        localhost
Server Port:            80
 
Document Path:          /doctrine2-orm/tools/sandbox/
Document Length:        24 bytes
 
Concurrency Level:      20
Time taken for tests:   30.952 seconds
Complete requests:      1000
Failed requests:        0
Write errors:           0
Keep-Alive requests:    0
Total transferred:      187000 bytes
HTML transferred:       24000 bytes
Requests per second:    32.31 [#/sec] (mean)
Time per request:       619.045 [ms] (mean)
Time per request:       30.952 [ms] (mean, across all concurrent requests)
Transfer rate:          5.90 [Kbytes/sec] received
 
Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    1   1.6      0      21
Processing:    62  617 247.8    576    2659
Waiting:       62  617 247.8    576    2659
Total:         62  617 248.0    577    2665

對不起我很懶… mysql新增部分就輸一倍了.
接下去就不測了.

結論:NoSQL粉快
看一下mongodb的log
Wed Aug 3 01:04:30 [conn12232] update doctrine_odm_sandbox.accounts query: { _id: ObjectId(‘4e382626e08239cbcf000022’) } byid 142ms
Wed Aug 3 01:04:27 [conn12035] update doctrine_odm_sandbox.accounts query: { _id: ObjectId(‘4e382626e08239cbcf000023’) } byid 629ms
這竟然是slow query!!!!!! WTF

發表迴響

你的電子郵件位址並不會被公開。 必要欄位標記為 *