influx測試——單條讀性能很差,大約400條/s,批量寫性能很高,7萬條/s,總體說來適合IOT數據批量存,根據tag查和過濾場景,按照時間順序返回


測試准備

需要將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, 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{
        Addr: "http://localhost: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)

}

 

單機讀:

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 main() {
    // Make client
    c, err := client.NewHTTPClient(client.HTTPConfig{
        Addr: "http://localhost:8086",
        Username: username,
        Password: password,
    })

    if err != nil {
        log.Fatalln("Error: ", err)
    }
    q := fmt.Sprintf("select * from monitorStatus where system_name='sys_5' and site_name='s_1' and equipment_name='e_6' order by time desc limit 10000 ;")
    res, err2 := queryDB(c, q)
    if err2 != nil {
        log.Fatal(err)
    }
    count := len(res[0].Series[0].Values)
    log.Printf("Found a total of %v records\n", count)

}

代碼摘自:http://www.cnblogs.com/MikeZhang/p/InfluxDBTest20170212.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM