Created
March 26, 2019 06:43
-
-
Save agreatfool/24a00ddbdb04c68d6fd2792ba8808ce6 to your computer and use it in GitHub Desktop.
cpu.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"math/rand" | |
"net/http" | |
_ "net/http/pprof" | |
"runtime" | |
"time" | |
) | |
func consume(n int) int { | |
if n == 0 { | |
return 0 | |
} | |
if n == 1 { | |
return 1 | |
} | |
return consume(n - 1) + consume(n - 2) | |
} | |
func main() { | |
runtime.GOMAXPROCS(8) | |
concurrent() | |
err := http.ListenAndServe("127.0.0.1:8080", nil) | |
if err != nil { | |
fmt.Println("Error:", err) | |
} | |
} | |
func concurrent() { | |
for i := 0; i < 50; i++ { | |
interval := int((1 + rand.Float64()) * float64(20)) // interval with some offset | |
ticker := time.NewTicker(time.Duration(interval) * time.Millisecond) | |
go func() { | |
for range ticker.C { | |
consume(rand.Intn(33)) | |
} | |
}() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment