Created
September 23, 2023 10:47
-
-
Save cloakd/b2f6f4b25721e6e980d27e7b48f374df to your computer and use it in GitHub Desktop.
Simple go script used to test speed of RPC calls against multiple providers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
ctx "context" | |
"fmt" | |
"github.com/gagliardetto/solana-go/rpc" | |
"log" | |
"time" | |
) | |
type RpcTest struct { | |
Name string | |
Client *rpc.Client | |
Count int64 | |
lastRuntime time.Duration | |
totalRuntime time.Duration | |
} | |
func (rt *RpcTest) AverageRuntime() time.Duration { | |
avg := rt.totalRuntime.Nanoseconds() / rt.Count | |
return time.Duration(avg) | |
} | |
func (rt *RpcTest) Test() error { | |
tn := time.Now() | |
_, err := rt.Client.GetLatestBlockhash(ctx.Background(), "confirmed") | |
if err != nil { | |
return err | |
} | |
tne := time.Now().Sub(tn) | |
rt.lastRuntime = tne | |
rt.totalRuntime += tne | |
log.Printf("%s: %s", rt.Name, tne) | |
return nil | |
} | |
func main() { | |
rpc1 := &RpcTest{ | |
Name: "HelloMoon", | |
Client: rpc.New("https://global.rpc.hellomoon.io/{RPC_KEY}"), | |
} | |
rpc2 := &RpcTest{ | |
Name: "Helius", | |
Client: rpc.New("https://rpc.helius.xyz/?api-key={RPC_KEY}"), | |
} | |
rpc3 := &RpcTest{ | |
Name: "QuickNode", | |
Client: rpc.New("https://methodical-dark-card.solana-mainnet.quiknode.pro/{RPC_KEY}/"), | |
} | |
rpc4 := &RpcTest{ | |
Name: "ExtrNode", | |
Client: rpc.New("https://solana-mainnet.rpc.extrnode.com/"), | |
} | |
rpc5 := &RpcTest{ | |
Name: "Alchemy", | |
Client: rpc.New("https://solana-mainnet.g.alchemy.com/v2/{RPC_KEY}"), | |
} | |
testers := []*RpcTest{rpc1, rpc2, rpc3, rpc4, rpc5} | |
loops := 100 | |
for i := 0; i < loops; i++ { | |
//Test all testers | |
for _, t := range testers { | |
err := t.Test() | |
if err != nil { | |
panic(fmt.Sprintf("%s Failed: %s", t.Name, err)) | |
} | |
t.Count++ | |
} | |
} | |
//Results | |
for _, t := range testers { | |
log.Printf("%s (%v) - Avg: %s", t.Name, t.Count, t.AverageRuntime()) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment