/
endpoint.go
73 lines (64 loc) · 1.59 KB
/
endpoint.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
package main
import (
"crypto/sha256"
"flag"
"fmt"
client "github.com/jackdoe/back-to-back/client"
. "github.com/jackdoe/back-to-back/spec"
"log"
"math/rand"
"net/http"
"runtime"
// "time"
)
func handleRequest() int {
// assume time takes 50 + rand(200)
//time.Sleep(50 * time.Millisecond)
k := 0
for i := 0; i < rand.Intn(100); i++ {
sha256.Sum256(make([]byte, 100000))
k++
}
return k
// time.Sleep(time.Duration(rand.Intn(50)) * time.Millisecond)
}
func handleRequestFast() int {
sha256.Sum256(make([]byte, 100000))
return 1
}
func main() {
var pbroker = flag.String("broker", "127.0.0.1:9001", "broker")
var pbindhttp = flag.String("bindhttp", ":12312", "http bind")
var pmaxproc = flag.Int("gomaxproc", 2, "gomaxproc")
flag.Parse()
runtime.GOMAXPROCS(*pmaxproc)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
n := handleRequest()
w.Write([]byte(fmt.Sprintf("%d", n)))
})
http.HandleFunc("/fast", func(w http.ResponseWriter, r *http.Request) {
n := handleRequestFast()
w.Write([]byte(fmt.Sprintf("%d", n)))
})
dispatch := map[string]func(*Message) *Message{}
dispatch["sim"] = func(*Message) *Message {
n := handleRequest()
return &Message{
Topic: "sim",
Data: []byte(fmt.Sprintf("%d", n)),
}
}
dispatch["sim-fast"] = func(*Message) *Message {
n := handleRequestFast()
return &Message{
Topic: "sim-fast",
Data: []byte(fmt.Sprintf("%d", n)),
}
}
addrs := []string{}
for i := 0; i < *pmaxproc; i++ {
addrs = append(addrs, *pbroker)
}
client.NewConsumer(addrs, dispatch)
log.Fatal(http.ListenAndServe(*pbindhttp, nil))
}