-
Notifications
You must be signed in to change notification settings - Fork 151
/
Copy pathmain.go
197 lines (176 loc) · 3.8 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package main
import (
"crypto/sha256"
"fmt"
"log"
"math/big"
http2 "net/http"
"os"
"runtime"
"strconv"
"time"
"github.com/go-kratos/kratos/v2"
"github.com/go-kratos/kratos/v2/transport/http"
"github.com/gin-gonic/gin"
"github.com/gorilla/mux"
"github.com/labstack/echo/v4"
)
var (
port = 8080
sleepTime = 0
cpuBound bool
target = 15
sleepTimeDuration time.Duration
message = []byte("hello world")
samplingPoint = 20 // seconds
)
func main() {
args := os.Args
argsLen := len(args)
webFramework := "kratos"
if argsLen > 1 {
webFramework = args[1]
}
if argsLen > 2 { //nolint:gomnd
sleepTime, _ = strconv.Atoi(args[2])
if sleepTime == -1 {
cpuBound = true
sleepTime = 0
}
}
if argsLen > 3 { //nolint:gomnd
port, _ = strconv.Atoi(args[3])
}
if argsLen > 4 { //nolint:gomnd
samplingPoint, _ = strconv.Atoi(args[4])
}
sleepTimeDuration = time.Duration(sleepTime) * time.Millisecond
samplingPointDuration := time.Duration(samplingPoint) * time.Second
go func() {
time.Sleep(samplingPointDuration)
var mem runtime.MemStats
runtime.ReadMemStats(&mem)
var u uint64 = 1024 * 1024
fmt.Printf("TotalAlloc: %d\n", mem.TotalAlloc/u)
fmt.Printf("Alloc: %d\n", mem.Alloc/u)
fmt.Printf("HeapAlloc: %d\n", mem.HeapAlloc/u)
fmt.Printf("HeapSys: %d\n", mem.HeapSys/u)
}()
switch webFramework {
case "kratos":
KratosServer()
case "gin":
GinServer()
case "echo":
EchoServer()
case "mux":
MuxServer()
default:
fmt.Println("--------------------------------------------------------------------")
fmt.Println("------------- Unknown framework given!!! Check libs.sh -------------")
fmt.Println("------------- Unknown framework given!!! Check libs.sh -------------")
fmt.Println("------------- Unknown framework given!!! Check libs.sh -------------")
fmt.Println("--------------------------------------------------------------------")
}
}
func KratosServer() {
httpSrv := http.NewServer(
http.Address(":" + strconv.Itoa(port)),
)
httpSrv.HandleFunc("/", kratosHandle)
app := kratos.New(
kratos.Name("benchmark"),
kratos.Server(
httpSrv,
),
)
if err := app.Run(); err != nil {
log.Fatal(err)
}
}
func kratosHandle(w http2.ResponseWriter, q *http2.Request) {
if cpuBound {
pow(target)
} else {
if sleepTime > 0 {
time.Sleep(sleepTimeDuration)
} else {
runtime.Gosched()
}
}
_, _ = w.Write(message)
}
func GinServer() {
gin.SetMode(gin.ReleaseMode)
mux := gin.New()
mux.GET("/", ginHandler)
_ = mux.Run(":" + strconv.Itoa(port))
}
func ginHandler(c *gin.Context) {
if cpuBound {
pow(target)
} else {
if sleepTime > 0 {
time.Sleep(sleepTimeDuration)
} else {
runtime.Gosched()
}
}
_, _ = c.Writer.Write(message)
}
func EchoServer() {
e := echo.New()
e.GET("/", echoHandler)
_ = e.Start(":" + strconv.Itoa(port))
}
func echoHandler(c echo.Context) error {
if cpuBound {
pow(target)
} else {
if sleepTime > 0 {
time.Sleep(sleepTimeDuration)
} else {
runtime.Gosched()
}
}
_, _ = c.Response().Write(message)
return nil
}
func MuxServer() {
r := mux.NewRouter()
r.HandleFunc("/", muxHandle)
http2.Handle("/", r)
_ = http2.ListenAndServe(":"+strconv.Itoa(port), r)
}
func muxHandle(w http2.ResponseWriter, q *http2.Request) {
if cpuBound {
pow(target)
} else {
if sleepTime > 0 {
time.Sleep(sleepTimeDuration)
} else {
runtime.Gosched()
}
}
_, _ = w.Write(message)
}
func pow(targetBits int) {
target := big.NewInt(1)
target.Lsh(target, uint(256-targetBits))
var hashInt big.Int
var hash [32]byte
nonce := 0
for {
data := "hello world " + strconv.Itoa(nonce)
hash = sha256.Sum256([]byte(data))
hashInt.SetBytes(hash[:])
if hashInt.Cmp(target) == -1 {
break
} else {
nonce++
}
if nonce%100 == 0 {
runtime.Gosched()
}
}
}