InfluxDB和MySQL的读写对比测试(3)

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方式(新数据库,仅为测试):

du -sh /var/lib/mysql

查询特定表:

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万条数据

[root@localhost mysqlTest]# time ./insertTest1 real 1m20.645s user 0m8.238s sys 0m5.931s [root@localhost mysqlTest]# time ./queryTest1 10000 Rows real 0m0.269s user 0m0.006s sys 0m0.002s 

原始数据 : 28.6M
du方式 : 279MB
sql查询方式: 57.59MB
写入速度: 12398 / s
读取速度: 37174 / s

1000万条数据

root@localhost mysqlTest]# time ./insertTest1 real 7m15.003s user 0m48.187s sys 0m33.885s [root@localhost mysqlTest]# time ./queryTest1 10000 Rows real 0m6.592s user 0m0.005s sys 0m0.002s 

原始数据 : 286M
du方式 : 2.4G
sql查询方式: 572MB
写入速度: 22988 / s
读取速度: 1516 / s

3000万条数据

[root@localhost mysqlTest]# time ./insertTest1 real 20m38.235s user 2m21.459s sys 1m40.329s [root@localhost mysqlTest]# time ./queryTest1 10000 Rows real 0m4.421s user 0m0.004s sys 0m0.004s 

原始数据 : 858M
du方式 : 7.1G
sql查询方式: 1714MB
写入速度: 24228 / s
读取速度: 2261 / s

二、InfluxDB读写测试 测试准备

需要将InfluxDB的源码放入 go/src/github.com/influxdata 目录

单写测试代码(write1.go):

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, cmdstring) (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{
        Addr:
":8086",
        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)
        i += 1
    }
   
//fmt.Printf("task done : i=%d \n",i)

}

View Code

单读测试代码(query1.go):

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/38f0e4cc7cd72550b31343727aaa1cc4.html