Last active
January 4, 2016 08:09
-
-
Save nsaje/8593304 to your computer and use it in GitHub Desktop.
Dependency-less HTTP-controlled CPU burner
This file contains hidden or 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" | |
"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