From 71c53841c3a88e1253445de691e2cbb3bc8a55db Mon Sep 17 00:00:00 2001 From: Eric Date: Fri, 22 Jul 2016 08:20:55 -0400 Subject: [PATCH] Added benchmark test to test json serialization. Made sure http client would reuse connection. --- plugins/outputs/opentsdb/opentsdb_http.go | 22 ++++------ plugins/outputs/opentsdb/opentsdb_test.go | 53 ++++++++++++++++++++++- 2 files changed, 60 insertions(+), 15 deletions(-) diff --git a/plugins/outputs/opentsdb/opentsdb_http.go b/plugins/outputs/opentsdb/opentsdb_http.go index 8556a4000..0f391abcf 100644 --- a/plugins/outputs/opentsdb/opentsdb_http.go +++ b/plugins/outputs/opentsdb/opentsdb_http.go @@ -3,9 +3,8 @@ package opentsdb import ( "fmt" "encoding/json" - //"os" "io" - //"io/ioutil" + "io/ioutil" "net/http" "net/http/httputil" "net/url" @@ -91,15 +90,9 @@ func (r *requestBody) close() error { return nil } -func (o *openTSDBHttp) startNewRequest() error { - o.body.reset(o.Debug) - - return nil -} - func (o *openTSDBHttp) sendDataPoint(metric *HttpMetric) error { if o.metricCounter == 0 { - o.startNewRequest() + o.body.reset(o.Debug) } if err := o.body.addMetric(metric); err != nil { @@ -139,7 +132,7 @@ func (o *openTSDBHttp) flush() error { if err != nil { 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") if (o.Debug) { @@ -158,14 +151,17 @@ func (o *openTSDBHttp) flush() error { } defer resp.Body.Close() - if o.Debug { + if o.Debug { dump, err := httputil.DumpResponse(resp, true) if err != nil { return fmt.Errorf("Error when dumping response: %s", err.Error()) - } + } 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 == 4 { diff --git a/plugins/outputs/opentsdb/opentsdb_test.go b/plugins/outputs/opentsdb/opentsdb_test.go index 31d4ace1a..700cec007 100644 --- a/plugins/outputs/opentsdb/opentsdb_test.go +++ b/plugins/outputs/opentsdb/opentsdb_test.go @@ -3,8 +3,16 @@ package opentsdb import ( "reflect" "testing" - // "github.com/influxdata/telegraf/testutil" - // "github.com/stretchr/testify/require" + "fmt" + "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) { @@ -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