-
Notifications
You must be signed in to change notification settings - Fork 114
/
main.go
116 lines (97 loc) · 2.49 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
package main
import (
"context"
"crypto/ed25519"
"fmt"
"github.com/xssnick/tonutils-go/adnl/address"
rldphttp "github.com/xssnick/tonutils-go/adnl/rldp/http"
"io"
"log"
"net"
"net/http"
"runtime"
"sync/atomic"
"time"
)
func main() {
srvPub, srvKey, err := ed25519.GenerateKey(nil)
if err != nil {
panic(err)
}
mx := http.NewServeMux()
mx.HandleFunc("/hello", func(writer http.ResponseWriter, request *http.Request) {
// writer.Header().Set("Transfer-Encoding", "chunked")
_, _ = writer.Write([]byte("ADNL ID: " + request.Header.Get("X-Adnl-Id")))
_, _ = writer.Write([]byte("\nADNL IP: " + request.Header.Get("X-Adnl-Ip")))
})
dht := &MockDHT{
ip: "127.0.0.1",
port: 9056,
pub: srvPub,
}
s := rldphttp.NewServer(srvKey, dht, mx)
go func() {
if err = s.ListenAndServe("127.0.0.1:9056"); err != nil {
panic(err)
}
}()
var x int64
numClients := 150
// 150 clients, parallel requests
for i := 0; i < numClients; i++ {
go func() {
_, clientKey, _ := ed25519.GenerateKey(nil)
// it uses fake DHT, but real adnl and rldp through network
client := &http.Client{
Transport: rldphttp.NewTransport(dht, nil, clientKey),
}
for {
resp, err := client.Get("http://ucvasyg57dnvgez2pct5v4z7ul5n3a6w7noenmxoh74uuo4djj5dfzv.adnl/hello")
if err != nil {
continue
}
dat, err := io.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
log.Println("err body", err.Error())
continue
}
_ = dat
atomic.AddInt64(&x, 1)
}
}()
}
xOld := int64(0)
// stats
for {
time.Sleep(100 * time.Millisecond)
var m runtime.MemStats
runtime.ReadMemStats(&m)
// those stats include client's resources usage too, not so accurate
fmt.Println("RPS:", (x-xOld)*10, "| Requests num:", x, "| Goroutines:", runtime.NumGoroutine()-(numClients*2), "| RAM Usage:", m.Alloc/1024/1024, "MB")
xOld = x
}
}
type MockDHT struct {
ip string
port int
pub ed25519.PublicKey
}
func (m *MockDHT) StoreAddress(ctx context.Context, addresses address.List, ttl time.Duration, ownerKey ed25519.PrivateKey, copies int) (int, []byte, error) {
return 0, nil, nil
}
func (m *MockDHT) Close() {}
func (m *MockDHT) FindAddresses(ctx context.Context, key []byte) (*address.List, ed25519.PublicKey, error) {
return &address.List{
Addresses: []*address.UDP{
{
IP: net.ParseIP(m.ip),
Port: int32(m.port),
},
},
Version: 0,
ReinitDate: 0,
Priority: 0,
ExpireAt: int32(time.Now().Add(1 * time.Hour).Unix()),
}, m.pub, nil
}