Skip to content

Commit 0b433d9

Browse files
init
1 parent 63a059e commit 0b433d9

9 files changed

+208
-1
lines changed

.idea/.gitignore

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/testrpc.iml

+9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,9 @@
1-
# testrpc
1+
# testrpc
2+
3+
## Usage
4+
5+
```usage
6+
./testrpc {threads} {apikey}
7+
8+
./testrpc 100 25febb1b5a003e935e16c0cd9662099a27ebec5e
9+
```

go.mod

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module testrpc
2+
3+
go 1.18
4+
5+
require github.com/valyala/fasthttp v1.36.0
6+
7+
require (
8+
github.com/andybalholm/brotli v1.0.4 // indirect
9+
github.com/klauspost/compress v1.15.0 // indirect
10+
github.com/valyala/bytebufferpool v1.0.0 // indirect
11+
)

go.sum

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
github.com/andybalholm/brotli v1.0.4 h1:V7DdXeJtZscaqfNuAdSRuRFzuiKlHSC/Zh3zl9qY3JY=
2+
github.com/andybalholm/brotli v1.0.4/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig=
3+
github.com/klauspost/compress v1.15.0 h1:xqfchp4whNFxn5A4XFyyYtitiWI8Hy5EW59jEwcyL6U=
4+
github.com/klauspost/compress v1.15.0/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk=
5+
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
6+
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
7+
github.com/valyala/fasthttp v1.36.0 h1:NhqfO/cB7Ajn1czkKnWkMHyPYr5nyND14ZGPk23g0/c=
8+
github.com/valyala/fasthttp v1.36.0/go.mod h1:t/G+3rLek+CyY9bnIE+YlMRddxVAAGjhxndDB4i4C0I=
9+
github.com/valyala/tcplisten v1.0.0/go.mod h1:T0xQ8SeCZGxckz9qRXTfG43PvQ/mcWh7FwZEA7Ioqkc=
10+
golang.org/x/crypto v0.0.0-20220214200702-86341886e292/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
11+
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
12+
golang.org/x/net v0.0.0-20220225172249-27dd8689420f/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
13+
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
14+
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
15+
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
16+
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
17+
golang.org/x/sys v0.0.0-20220227234510-4e6760a101f9/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
18+
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
19+
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
20+
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
21+
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
22+
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=

main.go

+131
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"github.com/valyala/fasthttp"
6+
"math"
7+
"math/rand"
8+
"os"
9+
"strconv"
10+
"strings"
11+
"sync/atomic"
12+
"time"
13+
)
14+
15+
var _version = "1.0.3"
16+
17+
var (
18+
_defaultThreads = 100
19+
_maxThreads = 50000
20+
_threads = 100
21+
_apiKey = "25febb1b5a003e935e16c0cd9662099a27ebec5e"
22+
_addrLen = len("50679ea8C67095AF6bFF156f6dAFB82Dfc005Bfb")
23+
)
24+
25+
var (
26+
success int64
27+
failed int64
28+
timeout int64
29+
hostError int64
30+
31+
rpcFailed int64
32+
33+
traffics int64
34+
)
35+
36+
func main() {
37+
println("testrpc version:", _version)
38+
args := os.Args
39+
if len(args) > 1 {
40+
threads, err := strconv.Atoi(args[1])
41+
if err != nil {
42+
println(fmt.Sprintf("threads error, use default %d threads, max threads is %d", _defaultThreads, _maxThreads))
43+
}
44+
if threads > 0 {
45+
_threads = int(math.Min(float64(threads), float64(_maxThreads)))
46+
} else {
47+
_threads = _defaultThreads
48+
}
49+
}
50+
if len(args) > 2 {
51+
apiKey := args[2]
52+
if apiKey != "" {
53+
_apiKey = apiKey
54+
}
55+
}
56+
57+
url := ""
58+
if strings.HasPrefix(_apiKey, "http://") || strings.HasPrefix(_apiKey, "https://") {
59+
url = _apiKey
60+
} else {
61+
url = fmt.Sprintf("http://klaytn.testnet.blockpi.net/v1/rpc/%s", _apiKey)
62+
}
63+
64+
println(fmt.Sprintf("Threads:\t%d \nAPI Endpoint:\t%s", _threads, url))
65+
66+
reqFmt := "{\"jsonrpc\": \"2.0\",\"id\": \"1\",\"method\": \"eth_getBalance\",\"params\": [\"%s\",\"latest\"]}"
67+
for i := 0; i < _threads; i++ {
68+
go func() {
69+
for {
70+
req := fmt.Sprintf(reqFmt, randomAddr())
71+
PostJson(url, req)
72+
time.Sleep(time.Second * 1)
73+
}
74+
}()
75+
}
76+
77+
for {
78+
time.Sleep(time.Second * 10)
79+
println(fmt.Sprintf("-------------------\ntime:\t%s\nsuccess:\t%d\nfailed:\t\t%d\ntimeout:\t%d\nhostError:\t%d\nrpcFailed:\t%d\ntraffics:\t%d", time.Now().String(), success, failed, timeout, hostError, rpcFailed, traffics))
80+
}
81+
}
82+
83+
func PostJson(url string, jsonData string) {
84+
req := fasthttp.AcquireRequest()
85+
defer fasthttp.ReleaseRequest(req)
86+
87+
req.Header.SetContentType("application/json")
88+
req.Header.SetMethod("POST")
89+
90+
req.SetRequestURI(url)
91+
92+
req.SetBodyString(jsonData)
93+
94+
resp := fasthttp.AcquireResponse()
95+
defer fasthttp.ReleaseResponse(resp)
96+
97+
client := fasthttp.Client{}
98+
99+
if err := client.DoTimeout(req, resp, time.Second*10); err != nil {
100+
if err == fasthttp.ErrTimeout {
101+
atomic.AddInt64(&timeout, 1)
102+
} else if err == fasthttp.ErrNoFreeConns {
103+
atomic.AddInt64(&hostError, 1)
104+
} else {
105+
atomic.AddInt64(&hostError, 1)
106+
//println(err.Error())
107+
}
108+
atomic.AddInt64(&failed, 1)
109+
return
110+
}
111+
112+
response := string(resp.Body())
113+
114+
traffics += int64(len(jsonData) + len(response))
115+
116+
if strings.Contains(response, "\"error\"") {
117+
atomic.AddInt64(&rpcFailed, 1)
118+
println(response)
119+
} else {
120+
atomic.AddInt64(&success, 1)
121+
}
122+
}
123+
124+
func randomAddr() string {
125+
chars := "0123456789abcdef"
126+
addr := "0x"
127+
for i := 0; i < _addrLen; i++ {
128+
addr += string(chars[rand.Intn(16)])
129+
}
130+
return addr
131+
}

testrpc.bat

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
::argv1 - threads
2+
::argv2 - API KEY
3+
4+
testrpc.exe 100 25febb1b5a003e935e16c0cd9662099a27ebec5e

0 commit comments

Comments
 (0)