Skip to content

Instantly share code, notes, and snippets.

@nsaje
Last active January 4, 2016 08:09
Show Gist options
  • Save nsaje/8593304 to your computer and use it in GitHub Desktop.
Save nsaje/8593304 to your computer and use it in GitHub Desktop.
Dependency-less HTTP-controlled CPU burner
package main
import (
"fmt"
"net/http"
"time"
)
var burnCpu = make(chan bool)
func cpuBurn() {
var burn bool = false
for {
select {
case burn = <-burnCpu:
default:
if burn {
_ = 5*5
} else {
time.Sleep(100 * time.Millisecond)
}
}
}
}
func startCpu(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Starting CPU burn\n")
burnCpu <- true
}
func stopCpu(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Stopping CPU burn\n")
burnCpu <- false
}
func main() {
go cpuBurn()
http.HandleFunc("/start_cpu", startCpu)
http.HandleFunc("/stop_cpu", stopCpu)
http.ListenAndServe(":8080", nil)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment