Skip to content

Commit

Permalink
Merge pull request #23 from maticnetwork/jhilliard/loadtest-upgrades
Browse files Browse the repository at this point in the history
Load Tests for ERC20/721 and Metrics Summarization
  • Loading branch information
praetoriansentry authored Dec 12, 2022
2 parents e74346a + a0942aa commit 6012615
Show file tree
Hide file tree
Showing 22 changed files with 2,648 additions and 127 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: goreleaser
name: polycli-releaser

on:
push:
Expand All @@ -22,6 +22,7 @@ jobs:
- name: perform cross build and compress binaries
shell: bash
run: |
sudo apt-get update
sudo apt-get install -y gcc-aarch64-linux-gnu
pwd
Expand Down
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,23 @@ $(BUILD_DIR): ## Create the binary folder.
mkdir -p $(BUILD_DIR)

.PHONY: run
run: lint ## Run the go program.
run: ## Run the go program.
go run main.go

.PHONY: build
build: lint $(BUILD_DIR) ## Build go binary.
build: $(BUILD_DIR) ## Build go binary.
go build -o $(BUILD_DIR)/$(BIN_NAME) main.go

.PHONY: cross
cross: $(BUILD_DIR) lint ## Cross-compile go binaries using CGO.
cross: $(BUILD_DIR) ## Cross-compile go binaries using CGO.
env CC=aarch64-linux-gnu-gcc CGO_ENABLED=1 GOOS=linux GOARCH=arm64 go build -ldflags "$(STATIC_LD_FLAGS)" -tags netgo -o $(BUILD_DIR)/linux-arm64-$(BIN_NAME) main.go
env CGO_ENABLED=1 GOOS=linux GOARCH=amd64 go build -ldflags "$(STATIC_LD_FLAGS)" -tags netgo -o $(BUILD_DIR)/linux-amd64-$(BIN_NAME) main.go
# mac builds - this will be functional but will still have secp issues
env GOOS=darwin GOARCH=arm64 go build -ldflags "$(LD_FLAGS)" -tags netgo -o $(BUILD_DIR)/darwin-arm64-$(BIN_NAME) main.go
env GOOS=darwin GOARCH=amd64 go build -ldflags "$(LD_FLAGS)" -tags netgo -o $(BUILD_DIR)/darwin-amd64-$(BIN_NAME) main.go

.PHONY: simplecross
simplecross: $(BUILD_DIR) lint ## Cross-compile go binaries without using CGO.
simplecross: $(BUILD_DIR) ## Cross-compile go binaries without using CGO.
env GOOS=linux GOARCH=arm64 go build -o $(BUILD_DIR)/linux-arm64-$(BIN_NAME) main.go
env GOOS=darwin GOARCH=arm64 go build -o $(BUILD_DIR)/darwin-arm64-$(BIN_NAME) main.go
env GOOS=linux GOARCH=amd64 go build -o $(BUILD_DIR)/linux-amd64-$(BIN_NAME) main.go
Expand Down
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,18 @@ The `--mode` flag is important for this command:
- `c` Will call random functions in our load test contract
- `f` will call a specific function on the load test contract. The
function is specified using the `-f` flag
- `2` Will run an ERC20 transfer test. It starts out by minting
a large amount of an ERC20 contract then transferring it in small
amounts
- `7` Will run an ERC721 test which will mint an NFT over and over
again
- `i` Will call the increment function repeatedly on the load test
contract. It's a minimal example of a contract call that will
require an update to a contract's storage.
- `r` Will call any of th eother modes randomly
- `s` Is used for Avail / Eth to store random data in large amounts
- `l` Will call a smart contract function that runs as long as it can
(based on the block limit)

This example is very simple. It runs 1000 requests at a max rate of 1
request per second against the http rpc endpoint on localhost. t's
Expand Down
40 changes: 24 additions & 16 deletions cmd/dumpblocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ import (
type (
dumpblocksArgs struct {
URL string
Start int64
End int64
Start uint64
End uint64
BatchSize uint64
Threads *uint
}
Expand Down Expand Up @@ -67,36 +67,36 @@ var dumpblocksCmd = &cobra.Command{

for start < end {
rangeStart := start
rangeEnd := rangeStart + int64(inputDumpblocks.BatchSize)
rangeEnd := rangeStart + inputDumpblocks.BatchSize

if rangeEnd > end {
rangeEnd = end
}

pool <- true
wg.Add(1)
log.Info().Int64("start", rangeStart).Int64("end", rangeEnd).Msg("getting range")
log.Info().Uint64("start", rangeStart).Uint64("end", rangeEnd).Msg("getting range")
go func() {
defer wg.Done()
for {
failCount := 0
blocks, err := getBlockRange(ctx, rangeStart, rangeEnd, ec, inputDumpblocks.URL)
blocks, err := getBlockRange(ctx, rangeStart, rangeEnd, ec)
if err != nil {
failCount = failCount + 1
if failCount > 5 {
log.Error().Int64("rangeStart", rangeStart).Int64("rangeEnd", rangeEnd).Msg("unable to fetch blocks")
log.Error().Uint64("rangeStart", rangeStart).Uint64("rangeEnd", rangeEnd).Msg("unable to fetch blocks")
break
}
time.Sleep(5 * time.Second)
continue
}

failCount = 0
receipts, err := getReceipts(ctx, blocks, ec, inputDumpblocks.URL)
receipts, err := getReceipts(ctx, blocks, ec)
if err != nil {
failCount = failCount + 1
if failCount > 5 {
log.Error().Int64("rangeStart", rangeStart).Int64("rangeEnd", rangeEnd).Msg("unable to fetch receipts")
log.Error().Uint64("rangeStart", rangeStart).Uint64("rangeEnd", rangeEnd).Msg("unable to fetch receipts")
break
}
time.Sleep(5 * time.Second)
Expand Down Expand Up @@ -149,8 +149,8 @@ var dumpblocksCmd = &cobra.Command{
start, end = end, start
}
inputDumpblocks.URL = args[0]
inputDumpblocks.Start = start
inputDumpblocks.End = end
inputDumpblocks.Start = uint64(start)
inputDumpblocks.End = uint64(end)
// realistically, this probably shoudln't be bigger than 999. Most Providers seem to cap at 1000
inputDumpblocks.BatchSize = 150

Expand All @@ -165,18 +165,20 @@ func init() {

}

func getBlockRange(ctx context.Context, from, to int64, c *ethrpc.Client, url string) ([]*json.RawMessage, error) {
func getBlockRange(ctx context.Context, from, to uint64, c *ethrpc.Client) ([]*json.RawMessage, error) {
blms := make([]ethrpc.BatchElem, 0)
for i := from; i <= to; i = i + 1 {
r := new(json.RawMessage)
var err error
blms = append(blms, ethrpc.BatchElem{
Method: "eth_getBlockByNumber",
Args: []interface{}{"0x" + strconv.FormatInt(i, 16), true},
Args: []interface{}{"0x" + strconv.FormatUint(i, 16), true},
Result: r,
Error: err,
})
}
log.Trace().Uint64("start", from).Uint64("end", to).Msg("Fetching block range")

err := c.BatchCallContext(ctx, blms)
if err != nil {
log.Error().Err(err).Msg("rpc issue fetching blocks")
Expand All @@ -199,12 +201,14 @@ type (
Hash string `json:"hash"`
}
simpleRPCBlock struct {
Number string `json:"number"`
Transactions []simpleRPCTransaction `json:"transactions"`
}
)

func getReceipts(ctx context.Context, rawBlocks []*json.RawMessage, c *ethrpc.Client, url string) ([]*json.RawMessage, error) {
func getReceipts(ctx context.Context, rawBlocks []*json.RawMessage, c *ethrpc.Client) ([]*json.RawMessage, error) {
txHashes := make([]string, 0)
txHashMap := make(map[string]string, 0)
for _, rb := range rawBlocks {
var sb simpleRPCBlock
err := json.Unmarshal(*rb, &sb)
Expand All @@ -214,6 +218,7 @@ func getReceipts(ctx context.Context, rawBlocks []*json.RawMessage, c *ethrpc.Cl
}
for _, tx := range sb.Transactions {
txHashes = append(txHashes, tx.Hash)
txHashMap[tx.Hash] = sb.Number
}

}
Expand All @@ -222,7 +227,8 @@ func getReceipts(ctx context.Context, rawBlocks []*json.RawMessage, c *ethrpc.Cl
}

blms := make([]ethrpc.BatchElem, 0)
for _, tx := range txHashes {
blmsBlockMap := make(map[int]string, 0)
for i, tx := range txHashes {
r := new(json.RawMessage)
var err error
blms = append(blms, ethrpc.BatchElem{
Expand All @@ -231,17 +237,19 @@ func getReceipts(ctx context.Context, rawBlocks []*json.RawMessage, c *ethrpc.Cl
Result: r,
Error: err,
})
blmsBlockMap[i] = txHashMap[tx]
}

var start uint64 = 0
for {
last := false
end := start + inputDumpblocks.BatchSize
if int(end) >= len(blms) {
if int(end) > len(blms) {
last = true
end = uint64(len(blms) - 1)
end = uint64(len(blms))
}

log.Trace().Str("startblock", blmsBlockMap[int(start)]).Uint64("start", start).Uint64("end", end).Msg("Fetching tx receipt range")
// json: cannot unmarshal object into Go value of type []rpc.jsonrpcMessage
// The error occurs when we call batchcallcontext with a single transaction for some reason.
// polycli dumpblocks -c 1 http://127.0.0.1:9209/ 34457958 34458108
Expand Down
Loading

0 comments on commit 6012615

Please sign in to comment.