Skip to content

Instantly share code, notes, and snippets.

@agreatfool
Created March 26, 2019 06:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save agreatfool/24a00ddbdb04c68d6fd2792ba8808ce6 to your computer and use it in GitHub Desktop.
Save agreatfool/24a00ddbdb04c68d6fd2792ba8808ce6 to your computer and use it in GitHub Desktop.
cpu.go
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