這裏將告訴您InfluxDB讀寫性能測試,教程操作步驟:
今天進行了InfluxDB和MySQL的對比測試,這裏記錄下結果,也方便我以後查閱。
操作系統: CentOS6.5_x64InfluxDB版本 : v1.1.0MySQL版本:v5.1.73CPU : Intel(R) Core(TM) i5-2320 CPU @ 3.00GHz內存 :12G硬盤 :SSD
一、MySQL讀寫測試 測試準備初始化SQL語句:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
CREATE DATABASE testMysql;
CREATE TABLE `monitorStatus` (
`system_name` VARCHAR(20) NOT NULL,
`site_name` VARCHAR(50) NOT NULL,
`equipment_name` VARCHAR(50) NOT NULL,
`current_value` DOUBLE NOT NULL,
`timestamp` BIGINT(20) NULL DEFAULT NULL,
INDEX `system_name` (`system_name`),
INDEX `site_name` (`site_name`),
INDEX `equipment_name` (`equipment_name`),
INDEX `timestamp` (`timestamp`)
)
ENGINE=InnoDB;
|
單寫測試代碼(insertTest1.c):
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include "mysql/mysql.h"
#define N 100
int
main()
{
MYSQL *conn_ptr;
int
res;
int
t,i,j;
int64_t tstamp = 1486872962;
srand
(
time
(NULL));
t=0;
conn_ptr = mysql_init(NULL);
if
(!conn_ptr)
{
printf
(
"mysql_init failed\n"
);
return
EXIT_FAILURE;
}
conn_ptr = mysql_real_connect(conn_ptr,
"localhost"
,
"root"
,
""
,
"testMysql"
,0,NULL,0);
if
(conn_ptr)
{
for
(i=1;i<= 10000;i++)
{
mysql_query(conn_ptr,
"begin"
);
for
(j=0;j<N;j++,t++)
{
char
query[1024]={0};
sprintf
(query,
"insert into monitorStatus values ('sys_%d','s_%d','e_%d','0.%02d','%lld');"
,
//j%10,(t+i)%10,(t+j)%10,(t+i+j)%100,tstamp);
j%10,(t+i)%10,(t+j)%10,
rand
()%100,tstamp);
//printf("query : %s\n",query);
res = mysql_query(conn_ptr,query);
if
(!res)
{
//printf("Inserted %lu rows\n",(unsigned long)mysql_affected_rows(conn_ptr));
}
else
{
fprintf
(stderr,
"Insert error %d: %sn"
,mysql_errno(conn_ptr),mysql_error(conn_ptr));
}
if
(j%10 == 0) tstamp+=1;
}
mysql_query(conn_ptr,
"commit"
);
//printf("i=%d\n",i);
}
}
else
{
printf
(
"Connection failed\n"
);
}
mysql_close(conn_ptr);
return
EXIT_SUCCESS;
}
|
可根據情況調整測試代碼中的N參數。
單讀測試代碼(queryTest1.c):
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
44
45
46
47
48
49
50
51
52
53
54
55
|
#include <stdio.h>
#include <stdlib.h>
#include "mysql/mysql.h"
int
main()
{
MYSQL *conn_ptr;
MYSQL_RES *res_ptr;
MYSQL_ROW sqlrow;
MYSQL_FIELD *fd;
int
res, i, j;
conn_ptr = mysql_init(NULL);
if
(!conn_ptr)
{
return
EXIT_FAILURE;
}
conn_ptr = mysql_real_connect(conn_ptr,
"localhost"
,
"root"
,
""
,
"testMysql"
, 0, NULL, 0);
if
(conn_ptr)
{
res = mysql_query(conn_ptr,
"select * from `monitorStatus` where system_name='sys_8' and site_name='s_9' and equipment_name='e_6' order by timestamp desc limit 10000;"
);
if
(res)
{
printf
(
"SELECT error:%s\n"
,mysql_error(conn_ptr));
}
else
{
res_ptr = mysql_store_result(conn_ptr);
if
(res_ptr)
{
printf
(
"%lu Rows\n"
,(unsigned
long
)mysql_num_rows(res_ptr));
j = mysql_num_fields(res_ptr);
while
((sqlrow = mysql_fetch_row(res_ptr)))
{
continue
;
for
(i = 0; i < j; i++)
printf
(
"%s\t"
, sqlrow[i]);
printf
(
"\n"
);
}
if
(mysql_errno(conn_ptr))
{
fprintf
(stderr,
"Retrive error:s\n"
,mysql_error(conn_ptr));
}
}
mysql_free_result(res_ptr);
}
}
else
{
printf
(
"Connection failed\n"
);
}
mysql_close(conn_ptr);
return
EXIT_SUCCESS;
}
|
Makefile文件:
1
2
3
4
5
6
7
|
all:
gcc -g insertTest1.c -o insertTest1 -L/usr/lib64/mysql/ -lmysqlclient
gcc -g queryTest1.c -o queryTest1 -L/usr/lib64/mysql/ -lmysqlclient
clean:
rm -rf insertTest1
rm -rf queryTest1
|
磁盤空間佔用查詢:
使用du方式(新數據庫,僅爲測試):
1
|
du -sh /var/lib/mysql
|
查詢特定表:
1
2
|
use information_schema;
select concat(round(sum(DATA_LENGTH/1024/1024), 2),
'MB'
) as data from TABLES where table_schema=
'testMysql'
and table_name=
'monitorStatus'
;
|
測試結果:
100萬條數據
1
2
3
4
5
6
7
8
9
10
11
12
|
real 1m20.645s
user 0m8.238s
sys 0m5.931s
10000 Rows
real 0m0.269s
user 0m0.006s
sys 0m0.002s
|
原始數據 : 28.6Mdu方式 : 279MBsql查詢方式: 57.59MB寫入速度: 12398 / s讀取速度: 37174 / s
1000萬條數據
1
2
3
4
5
6
7
8
9
10
11
12
13
|
real 7m15.003s
user 0m48.187s
sys 0m33.885s
10000 Rows
real 0m6.592s
user 0m0.005s
sys 0m0.002s
|
原始數據 : 286Mdu方式 : 2.4Gsql查詢方式: 572MB寫入速度: 22988 / s讀取速度: 1516 / s
3000萬條數據
1
2
3
4
5
6
7
8
9
10
11
|
real 20m38.235s
user 2m21.459s
sys 1m40.329s
10000 Rows
real 0m4.421s
user 0m0.004s
sys 0m0.004s
|
原始數據 : 858Mdu方式 : 7.1Gsql查詢方式: 1714MB寫入速度: 24228 / s讀取速度: 2261 / s
二、InfluxDB讀寫測試 測試準備需要將InfluxDB的源碼放入 go/src/github.com/influxdata 目錄
單寫測試代碼(write1.go):
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
package main
import (
"log"
"time"
"fmt"
"math/rand"
"github.com/influxdata/influxdb/client/v2"
)
const
(
MyDB =
"testInfluxdb"
username =
"root"
password =
""
)
func queryDB(clnt client.Client, cmd string) (res []client.Result, err error) {
q := client.Query{
Command: cmd,
Database: MyDB,
}
if
response, err := clnt.Query(q); err == nil {
if
response.Error() != nil {
return
res, response.Error()
}
res = response.Results
}
else
{
return
res, err
}
return
res, nil
}
func writePoints(clnt client.Client,num
int
) {
sampleSize := 1 * 10000
rand
.Seed(42)
t := num
bp, _ := client.NewBatchPoints(client.BatchPointsConfig{
Database: MyDB,
Precision:
"us"
,
})
for
i := 0; i < sampleSize; i++ {
t += 1
tags := map[string]string{
"system_name"
: fmt.Sprintf(
"sys_%d"
,i%10),
"site_name"
:fmt.Sprintf(
"s_%d"
, (t+i) % 10),
"equipment_name"
:fmt.Sprintf(
"e_%d"
,t % 10),
}
fields := map[string]interface{}{
"value"
: fmt.Sprintf(
"%d"
,
rand
.Int()),
}
pt, err := client.NewPoint(
"monitorStatus"
, tags, fields,
time
.Now())
if
err != nil {
log
.Fatalln(
"Error: "
, err)
}
bp.AddPoint(pt)
}
err := clnt.Write(bp)
if
err != nil {
log
.Fatal(err)
}
//fmt.Printf("%d task done\n",num)
}
func main() {
// Make client
c, err := client.NewHTTPClient(client.HTTPConfig{
Username: username,
Password: password,
})
if
err != nil {
log
.Fatalln(
"Error: "
, err)
}
_, err = queryDB(c, fmt.Sprintf(
"CREATE DATABASE %s"
, MyDB))
if
err != nil {
log
.Fatal(err)
}
i := 1
for
i <= 10000 {
defer writePoints(c,i)
//fmt.Printf("i=%d\n",i)
|