Skip to content

Commit

Permalink
Move test metrics server to testutil package
Browse files Browse the repository at this point in the history
  • Loading branch information
aalda committed Apr 5, 2019
1 parent 8a53278 commit 0aca63e
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 48 deletions.
43 changes: 43 additions & 0 deletions balloon/hyper/metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
Copyright 2018-2019 Banco Bilbao Vizcaya Argentaria, S.A.
Licensed 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.
*/

package hyper

import (
"github.com/prometheus/client_golang/prometheus"
)

const namespace = "qed"
const subSystem = "hyper"

var (
AddTotal = prometheus.NewCounter(
prometheus.CounterOpts{
Namespace: namespace,
Subsystem: subSystem,
Name: "add_total",
Help: "Number of the events added to the hyper tree.",
},
)
MembershipTotal = prometheus.NewCounter(
prometheus.CounterOpts{
Namespace: namespace,
Subsystem: subSystem,
Name: "membership_total",
Help: "Number of membership queries",
},
)
)
6 changes: 2 additions & 4 deletions balloon/hyper/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ import (
)

const (
CacheSize int = (1118481) * ((31 * 33) + 34) // (2^0+2^4 + 2^8 + 2^12 + 2^16 + 2^20) batches * batchSize (31 nodes * 33 bytes + 34 bytes from key)
//CacheSize int = (1118481) * ((31 * 33) + 34) // (2^0+2^4 + 2^8 + 2^12 + 2^16 + 2^20) batches * batchSize (31 nodes * 33 bytes + 34 bytes from key)
CacheSize int = (2000000) * ((31 * 33) + 34)
)

type HyperTree struct {
Expand Down Expand Up @@ -75,9 +76,6 @@ func (t *HyperTree) Add(eventDigest hashing.Digest, version uint64) (hashing.Dig
t.Lock()
defer t.Unlock()

// metrics
//metrics.QedHyperAddTotal.Inc()

//log.Debugf("Adding new event digest %x with version %d", eventDigest, version)

versionAsBytes := util.Uint64AsBytes(version)
Expand Down
34 changes: 5 additions & 29 deletions balloon/hyper/tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,17 @@
package hyper

import (
"context"
"encoding/binary"
"net/http"
"testing"
"time"

"github.com/bbva/qed/balloon/cache"
"github.com/bbva/qed/hashing"
"github.com/bbva/qed/log"
"github.com/bbva/qed/metrics"
"github.com/bbva/qed/storage"
"github.com/bbva/qed/storage/rocks"
metrics_utils "github.com/bbva/qed/testutils/metrics"
"github.com/bbva/qed/testutils/rand"
storage_utils "github.com/bbva/qed/testutils/storage"
"github.com/bbva/qed/util"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -272,40 +266,22 @@ func BenchmarkAdd(b *testing.B) {

hasher := hashing.NewSha256Hasher()
freeCache := cache.NewFreeCache(CacheSize)

tree := NewHyperTree(hashing.NewSha256Hasher, store, freeCache)

srvCloseF := startMetricsServer(store)
hyperMetrics := metrics_utils.CustomRegister(AddTotal)
srvCloseF := metrics_utils.StartMetricsServer(hyperMetrics, store)
defer srvCloseF()

b.ResetTimer()
b.N = 1000000
b.N = 2000000
for i := 0; i < b.N; i++ {
index := make([]byte, 8)
binary.LittleEndian.PutUint64(index, uint64(i))
elem := append(rand.Bytes(32), index...)
_, mutations, err := tree.Add(hasher.Do(elem), uint64(i))
require.NoError(b, err)
require.NoError(b, store.Mutate(mutations))
metrics.QedHyperAddTotal.Inc()
AddTotal.Inc()
}

}

func startMetricsServer(store *rocks.RocksDBStore) func() {
reg := prometheus.NewRegistry()
reg.Register(metrics.QedHyperAddTotal)
store.RegisterMetrics(reg)
mux := http.NewServeMux()
mux.Handle("/metrics", promhttp.HandlerFor(reg, promhttp.HandlerOpts{}))
srv := &http.Server{Addr: ":2112", Handler: mux}
go srv.ListenAndServe()
closeF := func() {
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
defer cancel()
if err := srv.Shutdown(ctx); err != nil {
log.Fatal(err)
}
}
return closeF
}
15 changes: 0 additions & 15 deletions metrics/definitions.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,21 +82,6 @@ var (
},
)

// HYPER TREE

QedHyperAddTotal = prometheus.NewCounter(
prometheus.CounterOpts{
Name: "qed_hyper_add_total",
Help: "Number of the events added to the hyper tree.",
},
)
QedHyperMembershipTotal = prometheus.NewCounter(
prometheus.CounterOpts{
Name: "qed_hyper_membership_total",
Help: "Number of membership queries",
},
)

// HISTORY TREE

QedHistoryAddTotal = prometheus.NewCounter(
Expand Down
65 changes: 65 additions & 0 deletions testutils/metrics/metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
Copyright 2018-2019 Banco Bilbao Vizcaya Argentaria, S.A.
Licensed 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.
*/

package metrics

import (
"context"
"log"
"net/http"
"time"

"github.com/bbva/qed/metrics"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)

func StartMetricsServer(registerers ...metrics.Registerer) func() {
reg := prometheus.NewRegistry()
for _, r := range registerers {
r.RegisterMetrics(reg)
}

mux := http.NewServeMux()
mux.Handle("/metrics", promhttp.HandlerFor(reg, promhttp.HandlerOpts{}))
srv := &http.Server{Addr: ":2112", Handler: mux}
go srv.ListenAndServe()

closeF := func() {
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
defer cancel()
if err := srv.Shutdown(ctx); err != nil {
log.Fatal(err)
}
}
return closeF
}

type customMetrics struct {
registerMetrics func(metrics.Registry)
}

func (cm customMetrics) RegisterMetrics(r metrics.Registry) {
cm.registerMetrics(r)
}

func CustomRegister(c ...prometheus.Collector) metrics.Registerer {
m := struct{ customMetrics }{}
m.registerMetrics = func(r metrics.Registry) {
r.MustRegister(c...)
}
return m
}

0 comments on commit 0aca63e

Please sign in to comment.