Skip to content

Commit

Permalink
Benchmark API response parsing code (#17046)
Browse files Browse the repository at this point in the history
* Benchmark API response parsing code

* Fixing contents

* Removing unnecessary line
  • Loading branch information
ycombinator authored Mar 17, 2020
1 parent 2b8279b commit 99fd13a
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.

Large diffs are not rendered by default.

7 changes: 5 additions & 2 deletions metricbeat/module/elasticsearch/index/data_xpack.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,7 @@ var (

func eventsMappingXPack(r mb.ReporterV2, m *MetricSet, info elasticsearch.Info, content []byte) error {
var indicesStruct IndicesStruct
err := json.Unmarshal(content, &indicesStruct)
if err != nil {
if err := parseAPIResponse(content, &indicesStruct); err != nil {
return errors.Wrap(err, "failure parsing Indices Stats Elasticsearch API response")
}

Expand Down Expand Up @@ -138,6 +137,10 @@ func eventsMappingXPack(r mb.ReporterV2, m *MetricSet, info elasticsearch.Info,
return errs.Err()
}

func parseAPIResponse(content []byte, indicesStruct *IndicesStruct) error {
return json.Unmarshal(content, indicesStruct)
}

// Fields added here are based on same fields being added by internal collection in
// https://github.com/elastic/elasticsearch/blob/master/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/collector/indices/IndexStatsMonitoringDoc.java#L62-L124
func addClusterStateFields(indexName string, indexStats, clusterState common.MapStr) error {
Expand Down
41 changes: 41 additions & 0 deletions metricbeat/module/elasticsearch/index/data_xpack_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

// +build !integration

package index

import (
"io/ioutil"
"testing"

"github.com/stretchr/testify/require"
)

func BenchmarkParseAPIResponse(b *testing.B) {
// Read in large stats API response fixture
content, err := ioutil.ReadFile("_meta/test/stats.800.bench.json")
require.NoError(b, err)

var indicesStruct IndicesStruct

for i := 0; i < b.N; i++ {
err = parseAPIResponse(content, &indicesStruct)
require.NoError(b, err)
}

}

0 comments on commit 99fd13a

Please sign in to comment.