Skip to content

Instantly share code, notes, and snippets.

@rodkranz
Created July 24, 2017 17:50
Show Gist options
  • Save rodkranz/75a4d92d018ca21a746ed1884734a614 to your computer and use it in GitHub Desktop.
Save rodkranz/75a4d92d018ca21a746ed1884734a614 to your computer and use it in GitHub Desktop.
Test Usage of CPU Raw
// Copyright 2017 rodkranz. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package main
import (
"fmt"
"runtime"
"time"
)
var t int = 60 // seconds
var i int = 0
func init() {
// set the cpu number here (1, 2, 3...)
// runtime.NumCPU get the max cpu number.
runtime.GOMAXPROCS(runtime.NumCPU())
}
func main() {
go func() {
fmt.Print("Exiting in... ")
for i := t; i > 0; i-- {
fmt.Printf("%d... ", i)
time.Sleep(1 * time.Second)
}
}()
for i := 0; i < 10000; i++ {
go Inc()
}
time.Sleep(time.Duration(t) * time.Second)
fmt.Printf("\nFinished \\o/ (%dx)!\n", i)
}
func Inc() {
for {
i++
time.Sleep(1 * time.Nanosecond) // if you remove this line your cpu will be 100%
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment