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