Added benchmark test to test json serialization. Made sure http client would reuse connection.

This commit is contained in:
Eric 2016-07-22 08:20:55 -04:00
parent 6f63fbcf8e
commit 71c53841c3
2 changed files with 60 additions and 15 deletions

View File

@ -3,9 +3,8 @@ package opentsdb
import ( import (
"fmt" "fmt"
"encoding/json" "encoding/json"
//"os"
"io" "io"
//"io/ioutil" "io/ioutil"
"net/http" "net/http"
"net/http/httputil" "net/http/httputil"
"net/url" "net/url"
@ -91,15 +90,9 @@ func (r *requestBody) close() error {
return nil return nil
} }
func (o *openTSDBHttp) startNewRequest() error {
o.body.reset(o.Debug)
return nil
}
func (o *openTSDBHttp) sendDataPoint(metric *HttpMetric) error { func (o *openTSDBHttp) sendDataPoint(metric *HttpMetric) error {
if o.metricCounter == 0 { if o.metricCounter == 0 {
o.startNewRequest() o.body.reset(o.Debug)
} }
if err := o.body.addMetric(metric); err != nil { if err := o.body.addMetric(metric); err != nil {
@ -139,7 +132,7 @@ func (o *openTSDBHttp) flush() error {
if err != nil { if err != nil {
return fmt.Errorf("Error when building request: %s", err.Error()) return fmt.Errorf("Error when building request: %s", err.Error())
} }
req.Header.Add("Content-Type", "applicaton/json") req.Header.Set("Content-Type", "applicaton/json")
req.Header.Set("Content-Encoding", "gzip") req.Header.Set("Content-Encoding", "gzip")
if (o.Debug) { if (o.Debug) {
@ -165,6 +158,9 @@ func (o *openTSDBHttp) flush() error {
} }
fmt.Printf("Received response\n%s\n\n", dump) fmt.Printf("Received response\n%s\n\n", dump)
} else {
// Important so http client reuse connection for next request if need be.
io.Copy(ioutil.Discard, resp.Body)
} }
if resp.StatusCode/100 != 2 { if resp.StatusCode/100 != 2 {

View File

@ -3,8 +3,16 @@ package opentsdb
import ( import (
"reflect" "reflect"
"testing" "testing"
// "github.com/influxdata/telegraf/testutil" "fmt"
// "github.com/stretchr/testify/require" "net"
"net/http"
"net/http/httptest"
"net/url"
"strconv"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/testutil"
//"github.com/stretchr/testify/require"
) )
func TestCleanTags(t *testing.T) { func TestCleanTags(t *testing.T) {
@ -67,6 +75,47 @@ func TestBuildTagsTelnet(t *testing.T) {
} }
} }
func BenchmarkHttpSend(b *testing.B) {
const BatchSize = 50
const MetricsCount = 4*BatchSize
metrics := make([]telegraf.Metric, MetricsCount)
for i:=0; i<MetricsCount; i++ {
metrics[i] = testutil.TestMetric(1.0)
}
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
fmt.Fprintln(w, "{}")
}))
defer ts.Close()
u, err := url.Parse(ts.URL)
if err != nil {
panic(err)
}
host, p, _ := net.SplitHostPort(u.Host)
port, err := strconv.Atoi(p)
if err != nil {
panic(err)
}
o := &OpenTSDB{
Host: host,
Port: port,
Prefix: "",
UseHttp: true,
BatchSize: BatchSize,
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
o.Write(metrics)
}
}
// func TestWrite(t *testing.T) { // func TestWrite(t *testing.T) {
// if testing.Short() { // if testing.Short() {
// t.Skip("Skipping integration test in short mode") // t.Skip("Skipping integration test in short mode")