-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add msgpack handler for smarter JSON encoding
The msgpack handler correctly retains information about whether the number is a float or an integer, unlike JSON. While the format is not human-readable, it makes a good interchange format for applications that don't necessarily care about a human readable output.
- Loading branch information
1 parent
a30f9b6
commit f6717ae
Showing
7 changed files
with
180 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package httpd_test | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"testing" | ||
"time" | ||
|
||
"github.com/influxdata/influxdb/influxql" | ||
"github.com/influxdata/influxdb/models" | ||
"github.com/influxdata/influxdb/services/httpd" | ||
) | ||
|
||
// discard is an http.ResponseWriter that discards all output. | ||
type discard struct{} | ||
|
||
func (discard) Header() http.Header { return http.Header{} } | ||
func (discard) WriteHeader(int) {} | ||
func (discard) Write(data []byte) (int, error) { return len(data), nil } | ||
|
||
func BenchmarkJSONResponseWriter_1K(b *testing.B) { | ||
benchmarkResponseWriter(b, "application/json", 10, 100) | ||
} | ||
func BenchmarkJSONResponseWriter_100K(b *testing.B) { | ||
benchmarkResponseWriter(b, "application/json", 1000, 100) | ||
} | ||
func BenchmarkJSONResponseWriter_1M(b *testing.B) { | ||
benchmarkResponseWriter(b, "application/json", 10000, 100) | ||
} | ||
|
||
func BenchmarkMsgpackResponseWriter_1K(b *testing.B) { | ||
benchmarkResponseWriter(b, "application/x-msgpack", 10, 100) | ||
} | ||
func BenchmarkMsgpackResponseWriter_100K(b *testing.B) { | ||
benchmarkResponseWriter(b, "application/x-msgpack", 1000, 100) | ||
} | ||
func BenchmarkMsgpackResponseWriter_1M(b *testing.B) { | ||
benchmarkResponseWriter(b, "application/x-msgpack", 10000, 100) | ||
} | ||
|
||
func BenchmarkCSVResponseWriter_1K(b *testing.B) { | ||
benchmarkResponseWriter(b, "text/csv", 10, 100) | ||
} | ||
func BenchmarkCSVResponseWriter_100K(b *testing.B) { | ||
benchmarkResponseWriter(b, "text/csv", 1000, 100) | ||
} | ||
func BenchmarkCSVResponseWriter_1M(b *testing.B) { | ||
benchmarkResponseWriter(b, "text/csv", 10000, 100) | ||
} | ||
|
||
func benchmarkResponseWriter(b *testing.B, contentType string, seriesN, pointsPerSeriesN int) { | ||
r, err := http.NewRequest("POST", "/query", nil) | ||
if err != nil { | ||
b.Fatal(err) | ||
} | ||
r.Header.Set("Accept", contentType) | ||
|
||
// Generate a sample result. | ||
rows := make(models.Rows, 0, seriesN) | ||
for i := 0; i < seriesN; i++ { | ||
row := &models.Row{ | ||
Name: "cpu", | ||
Tags: map[string]string{ | ||
"host": fmt.Sprintf("server-%d", i), | ||
}, | ||
Columns: []string{"time", "value"}, | ||
Values: make([][]interface{}, 0, b.N), | ||
} | ||
|
||
for j := 0; j < pointsPerSeriesN; j++ { | ||
row.Values = append(row.Values, []interface{}{ | ||
time.Unix(int64(10*j), 0), | ||
float64(100), | ||
}) | ||
} | ||
rows = append(rows, row) | ||
} | ||
result := &influxql.Result{Series: rows} | ||
|
||
// Create new ResponseWriter with the underlying ResponseWriter | ||
// being the discard writer so we only benchmark the marshaling. | ||
w := httpd.NewResponseWriter(discard{}, r) | ||
|
||
b.ResetTimer() | ||
b.ReportAllocs() | ||
|
||
for i := 0; i < b.N; i++ { | ||
w.WriteResponse(httpd.Response{ | ||
Results: []*influxql.Result{result}, | ||
}) | ||
} | ||
} |