-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathserver.go
66 lines (57 loc) · 1.26 KB
/
server.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
package main
import (
"fmt"
"io"
"log"
"net/http"
"runtime"
"sync/atomic"
"time"
"github.com/lesismal/llib/std/crypto/tls"
"github.com/lesismal/nbio/nbhttp"
)
var (
qps uint64 = 0
total uint64 = 0
)
// visit: https://localhost:8888/echo
func onEcho(w http.ResponseWriter, r *http.Request) {
data, _ := io.ReadAll(r.Body)
if len(data) > 0 {
w.Write(data)
} else {
w.Write([]byte(time.Now().Format("20060102 15:04:05")))
}
atomic.AddUint64(&qps, 1)
}
func main() {
cert, err := tls.LoadX509KeyPair("server.crt", "server.key")
if err != nil {
log.Fatalf("tls.X509KeyPair failed: %v", err)
}
tlsConfig := &tls.Config{
Certificates: []tls.Certificate{cert},
InsecureSkipVerify: true,
}
mux := &http.ServeMux{}
mux.HandleFunc("/echo", onEcho)
svr := nbhttp.NewEngine(nbhttp.Config{
Network: "tcp",
AddrsTLS: []string{"localhost:8888"},
TLSConfig: tlsConfig,
Handler: mux,
})
err = svr.Start()
if err != nil {
fmt.Printf("nbio.Start failed: %v\n", err)
return
}
defer svr.Stop()
ticker := time.NewTicker(time.Second)
for i := 1; true; i++ {
<-ticker.C
n := atomic.SwapUint64(&qps, 0)
total += n
fmt.Printf("running for %v seconds, NumGoroutine: %v, qps: %v, total: %v\n", i, runtime.NumGoroutine(), n, total)
}
}