-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathserver.go
55 lines (47 loc) · 1001 Bytes
/
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
package main
import (
"fmt"
"io"
"net/http"
_ "net/http/pprof"
"runtime"
"sync/atomic"
"time"
"github.com/lesismal/nbio/nbhttp"
)
var (
qps uint64 = 0
total uint64 = 0
)
func onEcho(w http.ResponseWriter, r *http.Request) {
// time.Sleep(time.Second * 5)
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() {
mux := &http.ServeMux{}
mux.HandleFunc("/echo", onEcho)
svr := nbhttp.NewEngine(nbhttp.Config{
Network: "tcp",
Addrs: []string{"localhost:8888"},
Handler: mux,
}) // pool.Go)
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)
}
}