Skip to content

Commit

Permalink
Refactoring of rpctest to reduce copy-paste and enable error recording (
Browse files Browse the repository at this point in the history
erigontech#2401)

* Refactoring of rpctest to reduce copy-paste and enable error recording

* Reduction

Co-authored-by: Alexey Sharp <alexeysharp@Alexeys-iMac.local>
  • Loading branch information
AlexeyAkhunov and Alexey Sharp authored Jul 19, 2021
1 parent d085bf9 commit 1c24b93
Show file tree
Hide file tree
Showing 11 changed files with 192 additions and 488 deletions.
34 changes: 17 additions & 17 deletions cmd/rpctest/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func main() {
cmd.Flags().StringVar(&recordFile, "recordFile", "", "File where to record requests and responses to")
}
withErrorFile := func(cmd *cobra.Command) {
cmd.Flags().StringVar(&recordFile, "errorFile", "", "File where to record errors (when responses do not match)")
cmd.Flags().StringVar(&errorFile, "errorFile", "", "File where to record errors (when responses do not match)")
}
with := func(cmd *cobra.Command, opts ...func(*cobra.Command)) {
for i := range opts {
Expand Down Expand Up @@ -123,10 +123,10 @@ func main() {
Short: "",
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
rpctest.BenchEthGetLogs(erigonURL, gethURL, needCompare, blockFrom, blockTo, recordFile)
rpctest.BenchEthGetLogs(erigonURL, gethURL, needCompare, blockFrom, blockTo, recordFile, errorFile)
},
}
with(benchEthGetLogsCmd, withErigonUrl, withGethUrl, withNeedCompare, withBlockNum, withRecord)
with(benchEthGetLogsCmd, withErigonUrl, withGethUrl, withNeedCompare, withBlockNum, withRecord, withErrorFile)

var bench9Cmd = &cobra.Command{
Use: "bench9",
Expand All @@ -143,10 +143,10 @@ func main() {
Short: "",
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
rpctest.BenchTraceTransaction(erigonURL, gethURL, needCompare, blockFrom, blockTo, recordFile)
rpctest.BenchTraceTransaction(erigonURL, gethURL, needCompare, blockFrom, blockTo, recordFile, errorFile)
},
}
with(benchTraceTransactionCmd, withGethUrl, withErigonUrl, withNeedCompare, withBlockNum, withRecord)
with(benchTraceTransactionCmd, withGethUrl, withErigonUrl, withNeedCompare, withBlockNum, withRecord, withErrorFile)

var benchTraceCallCmd = &cobra.Command{
Use: "benchTraceCall",
Expand All @@ -163,60 +163,60 @@ func main() {
Short: "",
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
rpctest.BenchDebugTraceCall(erigonURL, gethURL, needCompare, blockFrom, blockTo, recordFile)
rpctest.BenchDebugTraceCall(erigonURL, gethURL, needCompare, blockFrom, blockTo, recordFile, errorFile)
},
}
with(benchDebugTraceCallCmd, withGethUrl, withErigonUrl, withNeedCompare, withBlockNum, withRecord)
with(benchDebugTraceCallCmd, withGethUrl, withErigonUrl, withNeedCompare, withBlockNum, withRecord, withErrorFile)

var benchTraceCallManyCmd = &cobra.Command{
Use: "benchTraceCallMany",
Short: "",
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
rpctest.BenchTraceCallMany(erigonURL, gethURL, needCompare, blockFrom, blockTo, recordFile)
rpctest.BenchTraceCallMany(erigonURL, gethURL, needCompare, blockFrom, blockTo, recordFile, errorFile)
},
}
with(benchTraceCallManyCmd, withGethUrl, withErigonUrl, withNeedCompare, withBlockNum, withRecord)
with(benchTraceCallManyCmd, withGethUrl, withErigonUrl, withNeedCompare, withBlockNum, withRecord, withErrorFile)

var benchTraceBlockCmd = &cobra.Command{
Use: "benchTraceBlock",
Short: "",
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
rpctest.BenchTraceBlock(erigonURL, gethURL, needCompare, blockFrom, blockTo, recordFile)
rpctest.BenchTraceBlock(erigonURL, gethURL, needCompare, blockFrom, blockTo, recordFile, errorFile)
},
}
with(benchTraceBlockCmd, withGethUrl, withErigonUrl, withNeedCompare, withBlockNum, withRecord)
with(benchTraceBlockCmd, withGethUrl, withErigonUrl, withNeedCompare, withBlockNum, withRecord, withErrorFile)

var benchTraceFilterCmd = &cobra.Command{
Use: "benchTraceFilter",
Short: "",
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
rpctest.BenchTraceFilter(erigonURL, gethURL, needCompare, blockFrom, blockTo, recordFile)
rpctest.BenchTraceFilter(erigonURL, gethURL, needCompare, blockFrom, blockTo, recordFile, errorFile)
},
}
with(benchTraceFilterCmd, withGethUrl, withErigonUrl, withNeedCompare, withBlockNum, withRecord)
with(benchTraceFilterCmd, withGethUrl, withErigonUrl, withNeedCompare, withBlockNum, withRecord, withErrorFile)

var benchTxReceiptCmd = &cobra.Command{
Use: "benchTxReceipt",
Short: "",
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
rpctest.BenchTxReceipt(erigonURL, gethURL, needCompare, blockFrom, blockTo, recordFile)
rpctest.BenchTxReceipt(erigonURL, gethURL, needCompare, blockFrom, blockTo, recordFile, errorFile)
},
}
with(benchTxReceiptCmd, withGethUrl, withErigonUrl, withNeedCompare, withBlockNum, withRecord)
with(benchTxReceiptCmd, withGethUrl, withErigonUrl, withNeedCompare, withBlockNum, withRecord, withErrorFile)

var benchTraceReplayTransactionCmd = &cobra.Command{
Use: "benchTraceReplayTransaction",
Short: "",
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
rpctest.BenchTraceReplayTransaction(erigonURL, gethURL, needCompare, blockFrom, blockTo, recordFile)
rpctest.BenchTraceReplayTransaction(erigonURL, gethURL, needCompare, blockFrom, blockTo, recordFile, errorFile)
},
}
with(benchTraceReplayTransactionCmd, withGethUrl, withErigonUrl, withNeedCompare, withBlockNum, withRecord)
with(benchTraceReplayTransactionCmd, withGethUrl, withErigonUrl, withNeedCompare, withBlockNum, withRecord, withErrorFile)

var replayCmd = &cobra.Command{
Use: "replay",
Expand Down
48 changes: 15 additions & 33 deletions cmd/rpctest/rpctest/bench_debugtracecall.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
// parameters:
// needCompare - if false - doesn't call Erigon and doesn't compare responses
// use false value - to generate vegeta files, it's faster but we can generate vegeta files for Geth and Erigon
func BenchDebugTraceCall(erigonURL, gethURL string, needCompare bool, blockFrom uint64, blockTo uint64, recordFile string) {
func BenchDebugTraceCall(erigonURL, gethURL string, needCompare bool, blockFrom uint64, blockTo uint64, recordFile string, errorFile string) {
setRoutes(erigonURL, gethURL)
var client = &http.Client{
Timeout: time.Second * 600,
Expand All @@ -29,6 +29,17 @@ func BenchDebugTraceCall(erigonURL, gethURL string, needCompare bool, blockFrom
rec = bufio.NewWriter(f)
defer rec.Flush()
}
var errs *bufio.Writer
if errorFile != "" {
ferr, err := os.Create(errorFile)
if err != nil {
fmt.Printf("Cannot create file %s for error output: %v\n", errorFile, err)
return
}
defer ferr.Close()
errs = bufio.NewWriter(ferr)
defer errs.Flush()
}

var res CallResult
reqGen := &RequestGenerator{
Expand Down Expand Up @@ -82,40 +93,11 @@ func BenchDebugTraceCall(erigonURL, gethURL string, needCompare bool, blockFrom
reqGen.reqID++

request := reqGen.debugTraceCall(tx.From, tx.To, &tx.Gas, &tx.GasPrice, &tx.Value, tx.Input, bn-1)
recording := rec != nil // This flag will be set to false if recording is not to be performed
res := reqGen.Erigon2("debug_traceCall", request)
if res.Err != nil {
fmt.Printf("Could not debug traceCall (Erigon) %d: %v\n", bn, res.Err)
errCtx := fmt.Sprintf("block %d tx %s", bn, tx.Hash)
if err := requestAndCompare(request, "debug_traceCall", errCtx, reqGen, needCompare, rec, errs); err != nil {
fmt.Println(err)
return
}
if errVal := res.Result.Get("error"); errVal != nil {
fmt.Printf("Error debugging call (Erigon): %d %s\n", errVal.GetInt("code"), errVal.GetStringBytes("message"))
return
}

if needCompare {
resg := reqGen.Geth2("debug_traceCall", request)
if resg.Err != nil {
fmt.Printf("Could not debug traceCall (geth) %d: %v\n", bn, res.Err)
return
}
if errVal := resg.Result.Get("error"); errVal != nil {
fmt.Printf("Error debugging call (geth): %d %s\n", errVal.GetInt("code"), errVal.GetStringBytes("message"))
return
}
if resg.Err == nil && resg.Result.Get("error") == nil {
recording = false
if err := compareResults(res.Result, resg.Result); err != nil {
fmt.Printf("Different debug traceCall block %d, tx %x: %v\n", bn, tx.Hash, err)
fmt.Printf("\n\nTG response=================================\n%s\n", res.Response)
fmt.Printf("\n\nG response=================================\n%s\n", resg.Response)
return
}
}
}
if recording {
fmt.Fprintf(rec, "%s\n%s\n\n", request, res.Response)
}
}
}
}
123 changes: 21 additions & 102 deletions cmd/rpctest/rpctest/bench_ethgetlogs.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@ import (
"net/http"
"os"
"time"

"github.com/ledgerwatch/erigon/log"
)

func BenchEthGetLogs(erigonURL, gethURL string, needCompare bool, blockFrom uint64, blockTo uint64, recordFile string) {
func BenchEthGetLogs(erigonURL, gethURL string, needCompare bool, blockFrom uint64, blockTo uint64, recordFile string, errorFile string) {
setRoutes(erigonURL, gethURL)
var client = &http.Client{
Timeout: time.Second * 600,
Expand All @@ -28,6 +26,17 @@ func BenchEthGetLogs(erigonURL, gethURL string, needCompare bool, blockFrom uint
rec = bufio.NewWriter(f)
defer rec.Flush()
}
var errs *bufio.Writer
if errorFile != "" {
ferr, err := os.Create(errorFile)
if err != nil {
fmt.Printf("Cannot create file %s for error output: %v\n", errorFile, err)
return
}
defer ferr.Close()
errs = bufio.NewWriter(ferr)
defer errs.Flush()
}
resultsCh := make(chan CallResult, 1000)
defer close(resultsCh)
go vegetaWrite(false, []string{"debug_getModifiedAccountsByNumber", "eth_getLogs"}, resultsCh)
Expand Down Expand Up @@ -70,85 +79,22 @@ func BenchEthGetLogs(erigonURL, gethURL string, needCompare bool, blockFrom uint
accountSet := extractAccountMap(&mag)
for account := range accountSet {
reqGen.reqID++
startErigon := time.Now()
request := reqGen.getLogs(prevBn, bn, account)
recording := rec != nil // This flag will be set to false if recording is not to be performed
res = reqGen.Erigon2("eth_getLogs", request)
durationErigon := time.Since(startErigon).Seconds()
if res.Err != nil {
fmt.Printf("Could not get logs for account (Erigon) %x: %v\n", account, res.Err)
errCtx := fmt.Sprintf("account %x blocks %d-%d", account, prevBn, bn)
if err := requestAndCompare(request, "eth_getLogs", errCtx, reqGen, needCompare, rec, errs); err != nil {
fmt.Println(err)
return
}
if errVal := res.Result.Get("error"); errVal != nil {
fmt.Printf("Error getting logs for account (Erigon) %x: %d %s\n", account, errVal.GetInt("code"), errVal.GetStringBytes("message"))
return
}
var durationG float64
if needCompare {
startG := time.Now()
resg := reqGen.Geth2("eth_getLogs", request)
durationG = time.Since(startG).Seconds()
resultsCh <- res
if resg.Err != nil {
fmt.Printf("Could not get logs for account (geth) %x: %v\n", account, resg.Err)
recording = false
} else if errValg := resg.Result.Get("error"); errValg != nil {
fmt.Printf("Error getting logs for account (geth) %x: %d %s\n", account, errValg.GetInt("code"), errValg.GetStringBytes("message"))
recording = false
} else {
if err := compareResults(res.Result, resg.Result); err != nil {
fmt.Printf("Different logs for account %x and block %d-%d\n", account, prevBn, bn)
fmt.Printf("\n\nTG response=================================\n%s\n", res.Response)
fmt.Printf("\n\nG response=================================\n%s\n", resg.Response)
return
}
}
}
log.Info("Results", "count", len(res.Result.GetArray("result")), "durationErigon", durationErigon, "durationG", durationG)
if recording {
fmt.Fprintf(rec, "%s\n%s\n\n", request, res.Response)
}
topics := getTopics(res.Result)
// All combination of account and one topic
for _, topic := range topics {
reqGen.reqID++
startErigon := time.Now()
request = reqGen.getLogs1(prevBn, bn+10000, account, topic)
recording = rec != nil
res = reqGen.Erigon2("eth_getLogs", request)
durationErigon := time.Since(startErigon).Seconds()
if res.Err != nil {
fmt.Printf("Could not get logs for account (Erigon) %x %x: %v\n", account, topic, res.Err)
return
}
if errVal := res.Result.Get("error"); errVal != nil {
fmt.Printf("Error getting logs for account (Erigon) %x %x: %d %s\n", account, topic, errVal.GetInt("code"), errVal.GetStringBytes("message"))
errCtx := fmt.Sprintf("account %x topic %x blocks %d-%d", account, topic, prevBn, bn)
if err := requestAndCompare(request, "eth_getLogs", errCtx, reqGen, needCompare, rec, errs); err != nil {
fmt.Println(err)
return
}
if needCompare {
startG := time.Now()
resg := reqGen.Geth2("eth_getLogs", request)
durationG = time.Since(startG).Seconds()
resultsCh <- res
if resg.Err != nil {
fmt.Printf("Could not get logs for account (geth) %x %x: %v\n", account, topic, resg.Err)
recording = false
} else if errValg := resg.Result.Get("error"); errValg != nil {
fmt.Printf("Error getting logs for account (geth) %x %x: %d %s\n", account, topic, errValg.GetInt("code"), errValg.GetStringBytes("message"))
recording = false
} else {
if err := compareResults(res.Result, resg.Result); err != nil {
fmt.Printf("Different logs for account %x %x and block %d-%d\n", account, topic, prevBn, bn)
fmt.Printf("\n\nTG response=================================\n%s\n", res.Response)
fmt.Printf("\n\nG response=================================\n%s\n", resg.Response)
return
}
}
}
if recording {
fmt.Fprintf(rec, "%s\n%s\n\n", request, res.Response)
}
log.Info("Results", "count", len(res.Result.GetArray("result")), "durationErigon", durationErigon, "durationG", durationG)
}
// Random combinations of two topics
if len(topics) >= 2 {
Expand All @@ -159,38 +105,11 @@ func BenchEthGetLogs(erigonURL, gethURL string, needCompare bool, blockFrom uint
}
reqGen.reqID++
request = reqGen.getLogs2(prevBn, bn+100000, account, topics[idx1], topics[idx2])
recording = rec != nil
res = reqGen.Erigon2("eth_getLogs", request)
if res.Err != nil {
fmt.Printf("Could not get logs for account (Erigon) %x %x %x: %v\n", account, topics[idx1], topics[idx2], res.Err)
errCtx := fmt.Sprintf("account %x topic1 %x topic2 %x blocks %d-%d", account, topics[idx1], topics[idx2], prevBn, bn)
if err := requestAndCompare(request, "eth_getLogs", errCtx, reqGen, needCompare, rec, errs); err != nil {
fmt.Println(err)
return
}
if errVal := res.Result.Get("error"); errVal != nil {
fmt.Printf("Error getting logs for account (Erigon) %x %x %x: %d %s\n", account, topics[idx1], topics[idx2], errVal.GetInt("code"), errVal.GetStringBytes("message"))
return
}
if needCompare {
resg := reqGen.Geth2("eth_getLogs", request)
resultsCh <- res
if resg.Err != nil {
fmt.Printf("Could not get logs for account (geth) %x %x %x: %v\n", account, topics[idx1], topics[idx2], res.Err)
recording = false
} else if errValg := resg.Result.Get("error"); errValg != nil {
fmt.Printf("Error getting logs for account (geth) %x %x %x: %d %s\n", account, topics[idx1], topics[idx2], errValg.GetInt("code"), errValg.GetStringBytes("message"))
recording = false
} else {
if err := compareResults(res.Result, resg.Result); err != nil {
fmt.Printf("Different logs for account %x %x %x and block %d-%d\n", account, topics[idx1], topics[idx2], prevBn, bn)
fmt.Printf("\n\nTG response=================================\n%s\n", res.Response)
fmt.Printf("\n\nG response=================================\n%s\n", resg.Response)
return
}
}
}
if recording {
fmt.Fprintf(rec, "%s\n%s\n\n", request, res.Response)
}
log.Info("Results", "count", len(res.Result.GetArray("result")))
}
}
}
Expand Down
Loading

0 comments on commit 1c24b93

Please sign in to comment.