-
Notifications
You must be signed in to change notification settings - Fork 14
/
demo.go
139 lines (125 loc) · 4.31 KB
/
demo.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
// Copyright 2021 The cpuworker Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"crypto/rand"
"fmt"
"hash/crc32"
"log"
_ "net/http/pprof"
mathrand "math/rand"
"net/http"
"runtime"
"time"
"github.com/hnes/cpuworker"
)
var glCrc32bs = make([]byte, 1024*256)
func cpuIntensiveTask(amt int) uint32 {
//ts := time.Now()
var ck uint32
for range make([]struct{}, amt) {
ck = crc32.ChecksumIEEE(glCrc32bs)
}
//fmt.Println("log: crc32.ChecksumIEEE time cost (without checkpoint):", time.Now().Sub(ts))
return ck
}
func cpuIntensiveTaskWithCheckpoint(amt int, checkpointFp func()) uint32 {
//ts := time.Now()
var ck uint32
for range make([]struct{}, amt) {
ck = crc32.ChecksumIEEE(glCrc32bs)
checkpointFp()
}
//fmt.Println("log: crc32.ChecksumIEEE time cost (with checkpoint):", time.Now().Sub(ts))
return ck
}
func handleChecksumWithoutCpuWorker(w http.ResponseWriter, _ *http.Request) {
ts := time.Now()
ck := cpuIntensiveTask(10000 + mathrand.Intn(10000))
w.Write([]byte(fmt.Sprintln("crc32 (without cpuworker):", ck, "time cost:", time.Now().Sub(ts))))
}
func handleChecksumWithCpuWorkerAndHasCheckpoint(w http.ResponseWriter, _ *http.Request) {
ts := time.Now()
var ck uint32
cpuworker.Submit1(func(checkpointFp func()) {
ck = cpuIntensiveTaskWithCheckpoint(10000+mathrand.Intn(10000), checkpointFp)
}).Sync()
w.Write([]byte(fmt.Sprintln("crc32 (with cpuworker and checkpoint):", ck, "time cost:", time.Now().Sub(ts))))
}
func handleChecksumSmallTaskWithCpuWorker(w http.ResponseWriter, _ *http.Request) {
ts := time.Now()
var ck uint32
cpuworker.Submit(func() {
ck = cpuIntensiveTask(10)
}).Sync()
w.Write([]byte(fmt.Sprintln("crc32 (with cpuworker and small task):", ck, "time cost:", time.Now().Sub(ts))))
}
func handleDelay(w http.ResponseWriter, _ *http.Request) {
t0 := time.Now()
wCh := make(chan struct{})
go func() {
time.Sleep(time.Millisecond)
wCh <- struct{}{}
}()
<-wCh
w.Write([]byte(fmt.Sprintf("delayed 1ms, time cost %s :)\n", time.Now().Sub(t0))))
}
func handleDelayLoop(w http.ResponseWriter, _ *http.Request) {
t0 := time.Now()
for idx := range make([]byte, 10) {
t0 := time.Now()
wCh := make(chan struct{})
go func() {
time.Sleep(time.Millisecond)
wCh <- struct{}{}
}()
<-wCh
w.Write([]byte(fmt.Sprintf("delayed 1ms loop, idx:%d , time cost %s :)\n", idx, time.Now().Sub(t0))))
}
w.Write([]byte(fmt.Sprintf("delayed 1ms loop, final , total time cost %s :)\n", time.Now().Sub(t0))))
}
func handleDelayLoopWithCpuWorker(w http.ResponseWriter, _ *http.Request) {
cpuworker.Submit3(func(eventCall func(func())) {
t0 := time.Now()
for idx := range make([]byte, 10) {
t0 := time.Now()
wCh := make(chan struct{})
go func() {
time.Sleep(time.Millisecond)
wCh <- struct{}{}
}()
eventCall(func() {
<-wCh
w.Write([]byte(fmt.Sprintf("delayed 1ms loop with cpuworker, idx:%d , time cost %s :)\n", idx, time.Now().Sub(t0))))
})
}
eventCall(func() {
w.Write([]byte(fmt.Sprintf("delayed 1ms loop with cpuworker, final , total time cost %s :)\n", time.Now().Sub(t0))))
})
}, 0, true).Sync()
}
func main() {
rand.Read(glCrc32bs)
nCPU := runtime.GOMAXPROCS(0)
cpuP := cpuworker.GetGlobalWorkers().GetMaxP()
fmt.Println("GOMAXPROCS:", nCPU, "DefaultMaxTimeSlice:", cpuworker.DefaultMaxTimeSlice,
"cpuWorkerMaxP:", cpuP, "length of crc32 bs:", len(glCrc32bs))
http.HandleFunc("/checksumWithCpuWorker", handleChecksumWithCpuWorkerAndHasCheckpoint)
http.HandleFunc("/checksumSmallTaskWithCpuWorker", handleChecksumSmallTaskWithCpuWorker)
http.HandleFunc("/checksumWithoutCpuWorker", handleChecksumWithoutCpuWorker)
http.HandleFunc("/delay1ms", handleDelay)
http.HandleFunc("/delay1msLoop", handleDelayLoop)
http.HandleFunc("/delay1msLoopWithCpuWorker", handleDelayLoopWithCpuWorker)
log.Fatal(http.ListenAndServe(":8080", nil))
}