Skip to content
Snippets Groups Projects
Commit ead1d9a3 authored by Hans Moog's avatar Hans Moog
Browse files

Feat: added spammer

parent 2c6c6d45
Branches
Tags
No related merge requests found
...@@ -7,12 +7,13 @@ import ( ...@@ -7,12 +7,13 @@ import (
"github.com/iotaledger/goshimmer/plugins/cli" "github.com/iotaledger/goshimmer/plugins/cli"
"github.com/iotaledger/goshimmer/plugins/gossip" "github.com/iotaledger/goshimmer/plugins/gossip"
"github.com/iotaledger/goshimmer/plugins/gracefulshutdown" "github.com/iotaledger/goshimmer/plugins/gracefulshutdown"
"github.com/iotaledger/goshimmer/plugins/spammer"
"github.com/iotaledger/goshimmer/plugins/statusscreen" "github.com/iotaledger/goshimmer/plugins/statusscreen"
statusscreen_tps "github.com/iotaledger/goshimmer/plugins/statusscreen-tps"
"github.com/iotaledger/goshimmer/plugins/tangle" "github.com/iotaledger/goshimmer/plugins/tangle"
"github.com/iotaledger/goshimmer/plugins/tipselection" "github.com/iotaledger/goshimmer/plugins/tipselection"
"github.com/iotaledger/goshimmer/plugins/webapi" "github.com/iotaledger/goshimmer/plugins/webapi"
webapi_gtta "github.com/iotaledger/goshimmer/plugins/webapi-gtta" webapi_gtta "github.com/iotaledger/goshimmer/plugins/webapi-gtta"
webapi_spammer "github.com/iotaledger/goshimmer/plugins/webapi-spammer"
) )
func main() { func main() {
...@@ -22,12 +23,14 @@ func main() { ...@@ -22,12 +23,14 @@ func main() {
gossip.PLUGIN, gossip.PLUGIN,
tangle.PLUGIN, tangle.PLUGIN,
analysis.PLUGIN, analysis.PLUGIN,
statusscreen.PLUGIN,
gracefulshutdown.PLUGIN, gracefulshutdown.PLUGIN,
tipselection.PLUGIN, tipselection.PLUGIN,
spammer.PLUGIN,
statusscreen.PLUGIN,
statusscreen_tps.PLUGIN,
webapi.PLUGIN, webapi.PLUGIN,
webapi_gtta.PLUGIN, webapi_gtta.PLUGIN,
webapi_spammer.PLUGIN,
) )
} }
package transactionspammer
import (
"sync"
"time"
"github.com/iotaledger/goshimmer/packages/daemon"
"github.com/iotaledger/goshimmer/packages/model/value_transaction"
"github.com/iotaledger/goshimmer/plugins/gossip"
"github.com/iotaledger/goshimmer/plugins/tipselection"
)
var spamming = false
var startMutex sync.Mutex
var shutdownSignal chan int
func Start(tps int64) {
startMutex.Lock()
if !spamming {
shutdownSignal = make(chan int, 1)
func(shutdownSignal chan int) {
daemon.BackgroundWorker(func() {
for {
start := time.Now()
sentCounter := int64(0)
totalSentCounter := int64(0)
for {
select {
case <-daemon.ShutdownSignal:
return
case <-shutdownSignal:
return
default:
sentCounter++
totalSentCounter++
tx := value_transaction.New()
tx.SetValue(totalSentCounter)
tx.SetBranchTransactionHash(tipselection.GetRandomTip())
tx.SetTrunkTransactionHash(tipselection.GetRandomTip())
gossip.Events.ReceiveTransaction.Trigger(tx.MetaTransaction)
if sentCounter >= tps {
duration := time.Since(start)
if duration < time.Second {
time.Sleep(time.Second - duration)
start = time.Now()
}
sentCounter = 0
}
}
}
}
})
}(shutdownSignal)
spamming = true
}
startMutex.Unlock()
}
func Stop() {
startMutex.Lock()
if spamming {
close(shutdownSignal)
spamming = false
}
startMutex.Unlock()
}
package spammer
import (
"fmt"
"runtime"
"strconv"
"sync/atomic"
"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/model/value_transaction"
"github.com/iotaledger/goshimmer/packages/node"
"github.com/iotaledger/goshimmer/plugins/gossip"
"github.com/iotaledger/goshimmer/plugins/tangle"
"github.com/iotaledger/goshimmer/plugins/tipselection"
)
var PLUGIN = node.NewPlugin("Spammer", configure, run)
func bToMb(b uint64) uint64 {
return b / 1024 / 1024
}
func configure(plugin *node.Plugin) {
tpsCounter := 0
solidCounter := 0
var receivedCounter uint64
gossip.Events.ReceiveTransaction.Attach(events.NewClosure(func(transaction *meta_transaction.MetaTransaction) {
atomic.AddUint64(&receivedCounter, 1)
}))
go func() {
for {
select {
case <-daemon.ShutdownSignal:
return
case <-time.After(1 * time.Second):
fmt.Println("RECEIVED", receivedCounter, " / TPS "+strconv.Itoa(tpsCounter)+" / SOLID "+strconv.Itoa(solidCounter), " / TIPS ", tipselection.GetTipsCount())
fmt.Println(runtime.NumGoroutine())
var m runtime.MemStats
runtime.ReadMemStats(&m)
// For info on each, see: https://golang.org/pkg/runtime/#MemStats
fmt.Printf("Alloc = %v MiB", bToMb(m.Alloc))
fmt.Printf("\tTotalAlloc = %v MiB", bToMb(m.TotalAlloc))
fmt.Printf("\tSys = %v MiB", bToMb(m.Sys))
fmt.Printf("\tNumGC = %v\n", m.NumGC)
tpsCounter = 0
}
}
}()
tangle.Events.TransactionSolid.Attach(events.NewClosure(func(transaction *value_transaction.ValueTransaction) {
solidCounter++
tpsCounter++
}))
}
func run(plugin *node.Plugin) {
daemon.BackgroundWorker(func() {
for {
transactionCount := 100000
for i := 0; i < transactionCount; i++ {
select {
case <-daemon.ShutdownSignal:
return
default:
tx := value_transaction.New()
tx.SetValue(int64(i + 1))
tx.SetBranchTransactionHash(tipselection.GetRandomTip())
tx.SetTrunkTransactionHash(tipselection.GetRandomTip())
gossip.Events.ReceiveTransaction.Trigger(tx.MetaTransaction)
}
}
//time.Sleep(5 * time.Second)
}
})
}
package statusscreen_tps
import (
"strconv"
"sync/atomic"
"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/model/value_transaction"
"github.com/iotaledger/goshimmer/packages/node"
"github.com/iotaledger/goshimmer/plugins/gossip"
"github.com/iotaledger/goshimmer/plugins/statusscreen"
"github.com/iotaledger/goshimmer/plugins/tangle"
)
var receivedTpsCounter uint64
var solidTpsCounter uint64
var receivedTps uint64
var solidTps uint64
var PLUGIN = node.NewPlugin("Statusscreen TPS", func(plugin *node.Plugin) {
gossip.Events.ReceiveTransaction.Attach(events.NewClosure(func(_ *meta_transaction.MetaTransaction) {
atomic.AddUint64(&receivedTpsCounter, 1)
}))
tangle.Events.TransactionSolid.Attach(events.NewClosure(func(_ *value_transaction.ValueTransaction) {
atomic.AddUint64(&solidTpsCounter, 1)
}))
statusscreen.AddHeaderInfo(func() (s string, s2 string) {
return "TPS", strconv.FormatUint(atomic.LoadUint64(&receivedTps), 10) + " received / " + strconv.FormatUint(atomic.LoadUint64(&solidTps), 10) + " new"
})
}, func(plugin *node.Plugin) {
daemon.BackgroundWorker(func() {
ticker := time.NewTicker(time.Second)
for {
select {
case <-daemon.ShutdownSignal:
return
case <-ticker.C:
atomic.StoreUint64(&receivedTps, atomic.LoadUint64(&receivedTpsCounter))
atomic.StoreUint64(&solidTps, atomic.LoadUint64(&solidTpsCounter))
atomic.StoreUint64(&receivedTpsCounter, 0)
atomic.StoreUint64(&solidTpsCounter, 0)
}
}
})
})
...@@ -17,6 +17,12 @@ import ( ...@@ -17,6 +17,12 @@ import (
var start = time.Now() var start = time.Now()
var headerInfos = make([]func() (string, string), 0)
func AddHeaderInfo(generator func() (string, string)) {
headerInfos = append(headerInfos, generator)
}
type UIHeaderBar struct { type UIHeaderBar struct {
Primitive *tview.Grid Primitive *tview.Grid
LogoContainer *tview.TextView LogoContainer *tview.TextView
...@@ -62,9 +68,16 @@ func (headerBar *UIHeaderBar) Update() { ...@@ -62,9 +68,16 @@ func (headerBar *UIHeaderBar) Update() {
fmt.Fprintln(headerBar.InfoContainer) fmt.Fprintln(headerBar.InfoContainer)
fmt.Fprintln(headerBar.InfoContainer, "[::d]COO-LESS IOTA PROTOTYPE - [::b]Status: [green::b]SYNCED ") fmt.Fprintln(headerBar.InfoContainer, "[::d]COO-LESS IOTA PROTOTYPE - [::b]Status: [green::b]SYNCED ")
fmt.Fprintln(headerBar.InfoContainer) for i := 0; i < 3-len(headerInfos); i++ {
fmt.Fprintln(headerBar.InfoContainer) fmt.Fprintln(headerBar.InfoContainer)
fmt.Fprintln(headerBar.InfoContainer) }
for _, infoGenerator := range headerInfos {
fieldName, fieldValue := infoGenerator()
fmt.Fprintf(headerBar.InfoContainer, "[::b]%v: [::d]%40v ", fieldName, fieldValue)
fmt.Fprintln(headerBar.InfoContainer)
}
fmt.Fprintf(headerBar.InfoContainer, "[::b]Node ID: [::d]%40v ", accountability.OwnId().StringIdentifier) fmt.Fprintf(headerBar.InfoContainer, "[::b]Node ID: [::d]%40v ", accountability.OwnId().StringIdentifier)
fmt.Fprintln(headerBar.InfoContainer) fmt.Fprintln(headerBar.InfoContainer)
fmt.Fprintf(headerBar.InfoContainer, "[::b]Neighbors: [::d]%40v ", strconv.Itoa(len(chosenneighbors.INSTANCE.Peers))+" chosen / "+strconv.Itoa(len(acceptedneighbors.INSTANCE.Peers))+" accepted") fmt.Fprintf(headerBar.InfoContainer, "[::b]Neighbors: [::d]%40v ", strconv.Itoa(len(chosenneighbors.INSTANCE.Peers))+" chosen / "+strconv.Itoa(len(acceptedneighbors.INSTANCE.Peers))+" accepted")
......
...@@ -21,14 +21,14 @@ func Handler(c echo.Context) error { ...@@ -21,14 +21,14 @@ func Handler(c echo.Context) error {
branchTransactionHash := tipselection.GetRandomTip() branchTransactionHash := tipselection.GetRandomTip()
trunkTransactionHash := tipselection.GetRandomTip() trunkTransactionHash := tipselection.GetRandomTip()
return c.JSON(http.StatusOK, response{ return c.JSON(http.StatusOK, webResponse{
Duration: time.Since(start).Nanoseconds() / 1e6, Duration: time.Since(start).Nanoseconds() / 1e6,
BranchTransaction: branchTransactionHash, BranchTransaction: branchTransactionHash,
TrunkTransaction: trunkTransactionHash, TrunkTransaction: trunkTransactionHash,
}) })
} }
type response struct { type webResponse struct {
Duration int64 `json:"duration"` Duration int64 `json:"duration"`
BranchTransaction ternary.Trinary `json:"branchTransaction"` BranchTransaction ternary.Trinary `json:"branchTransaction"`
TrunkTransaction ternary.Trinary `json:"trunkTransaction"` TrunkTransaction ternary.Trinary `json:"trunkTransaction"`
......
package webapi_spammer
import (
"net/http"
"time"
"github.com/iotaledger/goshimmer/packages/node"
"github.com/iotaledger/goshimmer/packages/transactionspammer"
"github.com/iotaledger/goshimmer/plugins/webapi"
"github.com/labstack/echo"
)
var PLUGIN = node.NewPlugin("Spammer", configure)
func configure(plugin *node.Plugin) {
webapi.AddEndpoint("spammer", WebApiHandler)
}
func WebApiHandler(c echo.Context) error {
c.Set("requestStartTime", time.Now())
var request webRequest
if err := c.Bind(&request); err != nil {
return requestFailed(c, err.Error())
}
switch request.Cmd {
case "start":
if request.Tps == 0 {
request.Tps = 1000
}
transactionspammer.Stop()
transactionspammer.Start(request.Tps)
return requestSuccessful(c, "started spamming transactions")
case "stop":
transactionspammer.Stop()
return requestSuccessful(c, "stopped spamming transactions")
default:
return requestFailed(c, "invalid cmd in request")
}
}
func requestSuccessful(c echo.Context, message string) error {
return c.JSON(http.StatusOK, webResponse{
Duration: time.Since(c.Get("requestStartTime").(time.Time)).Nanoseconds() / 1e6,
Status: "success",
Message: message,
})
}
func requestFailed(c echo.Context, message string) error {
return c.JSON(http.StatusOK, webResponse{
Duration: time.Since(c.Get("requestStartTime").(time.Time)).Nanoseconds() / 1e6,
Status: "failed",
Message: message,
})
}
type webResponse struct {
Duration int64 `json:"duration"`
Status string `json:"status"`
Message string `json:"message"`
}
type webRequest struct {
Cmd string `json:"cmd"`
Tps int64 `json:"tps"`
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment