-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_server_benchmark.go
67 lines (65 loc) · 2.01 KB
/
http_server_benchmark.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
package main
import (
"fmt"
"log"
"net/http"
"sync"
"time"
)
var (
sleepDuration = 1000 * time.Millisecond
)
var byteCount = 2_000_000 // 2907082
func main() {
// Single thread http server
var mu sync.Mutex
bytes := make([]byte, byteCount, byteCount)
for i :=0; i< byteCount; i++ {
bytes[i] = 'a'
}
http.HandleFunc("/test", func(w http.ResponseWriter, r *http.Request) {
mu.Lock()
defer mu.Unlock()
//time.Sleep(sleepDuration)
n, err := w.Write(bytes)
fmt.Printf("n %v err %v\n", n, err)
//fmt.Fprintf(w, "%v", bytes)
})
log.Fatal(http.ListenAndServe(":80", nil))
}
// package main
// import (
// "fmt"
// "log"
// "net/http"
// "sync"
// )
// var byteCount = 1_000_000 // 2907082
// func main() {
// // Single thread http server
// var mu sync.Mutex
// bytes := make([]byte, byteCount, byteCount)
// for i :=0; i< byteCount; i++ {
// bytes[i] = 'a'
// }
// http.HandleFunc("/test", func(w http.ResponseWriter, r *http.Request) {
// w.Header().Set("Transfer-Encoding", "identity")
// w.Header().Set("Content-Length", fmt.Sprintf("%d", byteCount))
// mu.Lock()
// defer mu.Unlock()
// inSize := r.ContentLength
// outSize := 0
// for {
// n, err := w.Write(bytes)
// if err != nil {
// panic(err)
// }
// outSize += n
// if outSize >= byteCount {
// break
// }
// }
// fmt.Printf("in %v out %v\n", inSize, outSize)
// })
// log.Fatal(http.ListenAndServe(":80", nil))
// }