From edce00448695f708fac28b0c22a6c55b8e071a2b Mon Sep 17 00:00:00 2001 From: Hans Moog <hm@mkjc.net> Date: Mon, 8 Jul 2019 11:47:04 +0200 Subject: [PATCH] Feat: metrics plugin stub (for web based metrics) --- main.go | 2 ++ packages/curl/batch_hasher_test.go | 4 +-- .../bundleprocessor/bundleprocessor_test.go | 4 +-- plugins/metrics/events.go | 20 +++++++++++ plugins/metrics/plugin.go | 27 ++++++++++++++ plugins/metrics/tps.go | 36 +++++++++++++++++++ plugins/validator/plugin.go | 6 ++-- 7 files changed, 92 insertions(+), 7 deletions(-) create mode 100644 plugins/metrics/events.go create mode 100644 plugins/metrics/plugin.go create mode 100644 plugins/metrics/tps.go diff --git a/main.go b/main.go index 7ab4b3ab..9bf4f875 100644 --- a/main.go +++ b/main.go @@ -9,6 +9,7 @@ import ( "github.com/iotaledger/goshimmer/plugins/gossip" gossip_on_solidification "github.com/iotaledger/goshimmer/plugins/gossip-on-solidification" "github.com/iotaledger/goshimmer/plugins/gracefulshutdown" + "github.com/iotaledger/goshimmer/plugins/metrics" "github.com/iotaledger/goshimmer/plugins/statusscreen" statusscreen_tps "github.com/iotaledger/goshimmer/plugins/statusscreen-tps" "github.com/iotaledger/goshimmer/plugins/tangle" @@ -31,6 +32,7 @@ func main() { gracefulshutdown.PLUGIN, tipselection.PLUGIN, zeromq.PLUGIN, + metrics.PLUGIN, statusscreen.PLUGIN, statusscreen_tps.PLUGIN, diff --git a/packages/curl/batch_hasher_test.go b/packages/curl/batch_hasher_test.go index 9c9c3eec..009cc6bf 100644 --- a/packages/curl/batch_hasher_test.go +++ b/packages/curl/batch_hasher_test.go @@ -4,12 +4,12 @@ import ( "sync" "testing" - "github.com/iotaledger/goshimmer/packages/ternary" + "github.com/iotaledger/iota.go/trinary" ) func BenchmarkBatchHasher_Hash(b *testing.B) { batchHasher := NewBatchHasher(243, 81) - tritsToHash := ternary.Trytes("A999999FF").ToTrits() + tritsToHash := trinary.MustTrytesToTrits(trinary.Trytes("A999999FF")) b.ResetTimer() diff --git a/plugins/bundleprocessor/bundleprocessor_test.go b/plugins/bundleprocessor/bundleprocessor_test.go index f2132dc9..5da9cc4d 100644 --- a/plugins/bundleprocessor/bundleprocessor_test.go +++ b/plugins/bundleprocessor/bundleprocessor_test.go @@ -7,8 +7,8 @@ import ( "github.com/iotaledger/goshimmer/packages/events" "github.com/iotaledger/goshimmer/packages/model/bundle" "github.com/iotaledger/goshimmer/packages/model/value_transaction" - "github.com/iotaledger/goshimmer/packages/ternary" "github.com/iotaledger/goshimmer/plugins/tangle" + "github.com/iotaledger/iota.go/trinary" "github.com/magiconair/properties/assert" ) @@ -35,7 +35,7 @@ func TestProcessSolidBundleHead(t *testing.T) { if err != nil { t.Error(err) } else { - assert.Equal(t, result.GetHash(), ternary.Trytes("UFWJYEWKMEQDNSQUCUWBGOFRHVBGHVVYEZCLCGRDTRQSMAFALTIPMJEEYFDPMQCNJWLXUWFMBZGHQRO99"), "invalid bundle hash") + assert.Equal(t, result.GetHash(), trinary.Trytes("UFWJYEWKMEQDNSQUCUWBGOFRHVBGHVVYEZCLCGRDTRQSMAFALTIPMJEEYFDPMQCNJWLXUWFMBZGHQRO99"), "invalid bundle hash") assert.Equal(t, result.IsValueBundle(), true, "invalid value bundle status") } } diff --git a/plugins/metrics/events.go b/plugins/metrics/events.go new file mode 100644 index 00000000..1bcab96e --- /dev/null +++ b/plugins/metrics/events.go @@ -0,0 +1,20 @@ +package metrics + +import ( + "github.com/iotaledger/goshimmer/packages/events" +) + +// define a struct with the available events +type pluginEvents struct { + ReceivedTPSUpdated *events.Event +} + +// define a function that maps our event parameters to the correct type: uint64 (GO has no generics) +func uint64EventCaller(handler interface{}, params ...interface{}) { + handler.(func(uint64))(params[0].(uint64)) +} + +// expose our event API +var Events = pluginEvents{ + ReceivedTPSUpdated: events.NewEvent(uint64EventCaller), +} diff --git a/plugins/metrics/plugin.go b/plugins/metrics/plugin.go new file mode 100644 index 00000000..837a7bfe --- /dev/null +++ b/plugins/metrics/plugin.go @@ -0,0 +1,27 @@ +package metrics + +import ( + "time" + + "github.com/iotaledger/goshimmer/packages/daemon" + "github.com/iotaledger/goshimmer/packages/events" + "github.com/iotaledger/goshimmer/packages/model/meta_transaction" + "github.com/iotaledger/goshimmer/packages/node" + "github.com/iotaledger/goshimmer/packages/timeutil" + "github.com/iotaledger/goshimmer/plugins/gossip" +) + +// create configure handler (get's called when the PLUGIN is "loaded" by the node) +func configure(plugin *node.Plugin) { + // increase received TPS counter whenever we receive a new transaction + gossip.Events.ReceiveTransaction.Attach(events.NewClosure(func(_ *meta_transaction.MetaTransaction) { increaseReceivedTPSCounter() })) +} + +// create run handler (get's called when the PLUGIN is "executed" by the node) +func run(plugin *node.Plugin) { + // create a background worker that "measures" the TPS value every second + daemon.BackgroundWorker("Metrics TPS Updater", func() { timeutil.Ticker(measureReceivedTPS, 1*time.Second) }) +} + +// export plugin +var PLUGIN = node.NewPlugin("Metrics", configure, run) diff --git a/plugins/metrics/tps.go b/plugins/metrics/tps.go new file mode 100644 index 00000000..4acdd176 --- /dev/null +++ b/plugins/metrics/tps.go @@ -0,0 +1,36 @@ +package metrics + +import ( + "sync/atomic" +) + +// public api method to proactively retrieve the received TPS value +func GetReceivedTPS() uint64 { + return atomic.LoadUint64(&measuredReceivedTPS) +} + +// counter for the received TPS +var tpsReceivedSinceLastMeasurement uint64 + +// measured value of the received TPS +var measuredReceivedTPS uint64 + +// increases the received TPS counter +func increaseReceivedTPSCounter() { + atomic.AddUint64(&tpsReceivedSinceLastMeasurement, 1) +} + +// measures the received TPS value +func measureReceivedTPS() { + // sample the current counter value into a measured TPS value + sampledTPS := atomic.LoadUint64(&tpsReceivedSinceLastMeasurement) + + // store the measured value + atomic.StoreUint64(&measuredReceivedTPS, sampledTPS) + + // reset the counter + atomic.StoreUint64(&tpsReceivedSinceLastMeasurement, 0) + + // trigger events for outside listeners + Events.ReceivedTPSUpdated.Trigger(sampledTPS) +} diff --git a/plugins/validator/plugin.go b/plugins/validator/plugin.go index 9b898d79..dce2129b 100644 --- a/plugins/validator/plugin.go +++ b/plugins/validator/plugin.go @@ -8,6 +8,7 @@ import ( "github.com/iotaledger/goshimmer/plugins/bundleprocessor" "github.com/iotaledger/iota.go/address" . "github.com/iotaledger/iota.go/consts" + "github.com/iotaledger/iota.go/kerl" "github.com/iotaledger/iota.go/signing" . "github.com/iotaledger/iota.go/trinary" ) @@ -17,7 +18,6 @@ var PLUGIN = node.NewPlugin("Validator", configure, run) // Creates bundle signature fragments and the corresponding address to validate against. // Each signature fragment after the first must go into its own meta transaction with value = 0. func demoSign(seed Trytes, index uint64, sec SecurityLevel, bundleHash Hash) (Hash, []Trytes) { - addr, _ := address.GenerateAddress(seed, index, sec) // compute seed based on address index @@ -63,7 +63,7 @@ func validateSignatures(bundleHash Hash, txs []*value_transaction.ValueTransacti } // validate all the fragments against the address using Kerl - valid, err := signing.ValidateSignatures(address, fragments, bundleHash, signing.NewKerl) + valid, err := signing.ValidateSignatures(address, fragments, bundleHash, kerl.NewKerl()) if err != nil { return false, err } @@ -77,7 +77,7 @@ func validateSignatures(bundleHash Hash, txs []*value_transaction.ValueTransacti func configure(plugin *node.Plugin) { - bundleprocessor.Events.ValueBundleReceived.Attach(events.NewClosure(func(b *bundle.Bundle, txs []*value_transaction.ValueTransaction) { + bundleprocessor.Events.BundleSolid.Attach(events.NewClosure(func(b *bundle.Bundle, txs []*value_transaction.ValueTransaction) { // signature are verified against the bundle hash valid, _ := validateSignatures(b.GetBundleEssenceHash(), txs) if !valid { -- GitLab