Skip to content
Snippets Groups Projects
Unverified Commit 6e06e9a4 authored by Levente Pap's avatar Levente Pap
Browse files

Measure tips in message tangle

parent cde6e35e
Branches
No related tags found
No related merge requests found
......@@ -12,6 +12,7 @@ type CollectionEvents struct {
MemUsage *events.Event
Synced *events.Event
ValueTips *events.Event
MessageTips *events.Event
}
func uint64Caller(handler interface{}, params ...interface{}) {
......
......@@ -19,6 +19,7 @@ func new() *CollectionEvents {
events.NewEvent(uint64Caller),
events.NewEvent(boolCaller),
events.NewEvent(uint64Caller),
events.NewEvent(uint64Caller),
}
}
......
......@@ -2,10 +2,13 @@ package metrics
import (
"sync/atomic"
"github.com/iotaledger/goshimmer/packages/metrics"
"github.com/iotaledger/goshimmer/plugins/messagelayer"
)
// ReceivedMessagesPerSecond retrieves the current messages per second number.
func ReceivedMessagesPerSecond() uint64 {
// MPS retrieves the current messages per second number.
func MPS() uint64 {
return atomic.LoadUint64(&measuredReceivedMPS)
}
......@@ -15,6 +18,9 @@ var mpsReceivedSinceLastMeasurement uint64
// measured value of the received MPS
var measuredReceivedMPS uint64
// current number of message tips
var messageTips uint64
// increases the received MPS counter
func increaseReceivedMPSCounter() {
atomic.AddUint64(&mpsReceivedSinceLastMeasurement, 1)
......@@ -34,3 +40,12 @@ func measureReceivedMPS() {
// trigger events for outside listeners
Events.ReceivedMPSUpdated.Trigger(sampledMPS)
}
func measureMessageTips() {
metrics.Events().MessageTips.Trigger((uint64)(messagelayer.TipSelector.TipCount()))
}
// MessageTips returns the actual number of tips in the message tangle.
func MessageTips() uint64 {
return atomic.LoadUint64(&messageTips)
}
package metrics
import (
"sync"
"sync/atomic"
"testing"
"github.com/iotaledger/goshimmer/packages/binary/messagelayer/tipselector"
"github.com/iotaledger/goshimmer/packages/metrics"
"github.com/iotaledger/goshimmer/plugins/messagelayer"
"github.com/iotaledger/hive.go/events"
"github.com/magiconair/properties/assert"
)
func TestReceivedMessagesPerSecond(t *testing.T) {
// simulate attaching 10 value payloads in 0s < t < 1s
for i := 0; i < 10; i++ {
increaseReceivedMPSCounter()
}
// first measurement happens at t=1s
measureReceivedMPS()
// simulate 5 TPS for 1s < t < 2s
for i := 0; i < 5; i++ {
increaseReceivedMPSCounter()
}
assert.Equal(t, MPS(), (uint64)(10))
// measure at t=2s
measureReceivedMPS()
assert.Equal(t, MPS(), (uint64)(5))
// measure at t=3s
measureReceivedMPS()
assert.Equal(t, MPS(), (uint64)(0))
}
func TestReceivedMPSUpdatedEvent(t *testing.T) {
var wg sync.WaitGroup
Events.ReceivedMPSUpdated.Attach(events.NewClosure(func(mps uint64) {
assert.Equal(t, mps, (uint64)(10))
wg.Done()
}))
// simulate attaching 10 value payloads in 0s < t < 1s
for i := 0; i < 10; i++ {
increaseReceivedMPSCounter()
}
wg.Add(1)
measureReceivedMPS()
wg.Wait()
}
func TestMessageTips(t *testing.T) {
var wg sync.WaitGroup
// messagelayer TipSelector not configured here, so to avoid nil pointer panic, we instantiate it
messagelayer.TipSelector = tipselector.New()
metrics.Events().MessageTips.Attach(events.NewClosure(func(tips uint64) {
atomic.StoreUint64(&messageTips, tips)
wg.Done()
}))
wg.Add(1)
measureMessageTips()
wg.Wait()
assert.Equal(t, MessageTips(), (uint64)(0))
}
......@@ -7,6 +7,7 @@ const (
MPSMeasurementInterval = 1 * time.Second
TPSMeasurementInterval = 1 * time.Second
// can be adjusted as wished
MessageTipsMeasurementInterval = 1 * time.Second
ValueTipsMeasurementInterval = 1 * time.Second
CPUUsageMeasurementInterval = 1 * time.Second
MemUsageMeasurementInterval = 1 * time.Second
......
......@@ -34,6 +34,7 @@ func configure(_ *node.Plugin) {
// increase received MPS counter whenever we attached a message
messagelayer.Tangle.Events.MessageAttached.Attach(events.NewClosure(func(cachedMessage *message.CachedMessage, cachedMessageMetadata *tangle.CachedMessageMetadata) {
// TODO: maintain mps figures per payload type
cachedMessage.Release()
cachedMessageMetadata.Release()
increaseReceivedMPSCounter()
......@@ -69,6 +70,9 @@ func configure(_ *node.Plugin) {
defer syncLock.Unlock()
isSynced = synced
}))
metrics.Events().MessageTips.Attach(events.NewClosure(func(tipsCount uint64) {
atomic.StoreUint64(&messageTips, tipsCount)
}))
metrics.Events().ValueTips.Attach(events.NewClosure(func(tipsCount uint64) {
atomic.StoreUint64(&valueTips, tipsCount)
}))
......@@ -78,6 +82,7 @@ func run(_ *node.Plugin) {
// create a background worker that "measures" the MPS value every second
if err := daemon.BackgroundWorker("Metrics Updater", func(shutdownSignal <-chan struct{}) {
timeutil.Ticker(measureReceivedMPS, MPSMeasurementInterval, shutdownSignal)
timeutil.Ticker(measureMessageTips, MessageTipsMeasurementInterval, shutdownSignal)
timeutil.Ticker(measureReceivedTPS, TPSMeasurementInterval, shutdownSignal)
timeutil.Ticker(measureValueTips, ValueTipsMeasurementInterval, shutdownSignal)
timeutil.Ticker(measureCPUUsage, CPUUsageMeasurementInterval, shutdownSignal)
......
......@@ -60,5 +60,4 @@ func TestValueTips(t *testing.T) {
measureValueTips()
wg.Wait()
assert.Equal(t, ValueTips(), (uint64)(0))
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment