diff --git a/README.md b/README.md index 282ca56..521bda3 100644 --- a/README.md +++ b/README.md @@ -6,9 +6,10 @@ Available endpoints: | Endpoint | Functionality | Example | |----------|-------------|---------| -| /cpu?x={n} | Stress the CPU by a factor of "x" | `curl 127.1:8080/cpu?x=42` | -| /envs?env={var} | Returns an environment variable | `curl 127.1:8080/envs?env=DB_NAME` | +| /cpu?x={n} | Stresses the CPU by a factor of "x" | `curl 127.1:8080/cpu?x=42` | +| /mem?add={mb} | Increases used memory in megabytes | `curl 127.1:8080/mem?add=100` | | /tcp?addr={addr} | Tests a TCP connection | `curl 127.1:8080/tcp?addr=google.com:443` | +| /envs?env={var} | Returns an environment variable | `curl 127.1:8080/envs?env=DB_NAME` | | /json?size={n} | Returns a JSON batch | `curl 127.1:8080/json?size=10000` | | /exit | Exits the application | `curl 127.1:8080/exit` | | /log?={m} | Writes to standard out | `curl 127.1:8080/log?m=Hello` | diff --git a/main.go b/main.go index 4b7310b..56f1c9d 100644 --- a/main.go +++ b/main.go @@ -27,6 +27,7 @@ func main() { http.HandleFunc("/envs", env) http.HandleFunc("/tcp", tcp) http.HandleFunc("/cpu", cpu) + http.HandleFunc("/mem", mem) http.HandleFunc("/exit", exit) http.HandleFunc("/log", logFunc) @@ -79,6 +80,31 @@ func jsonFunc(w http.ResponseWriter, r *http.Request) { fmt.Fprintln(w, string(data)) } +var mb []byte +var s [][]byte + +func mem(w http.ResponseWriter, r *http.Request) { + addStr := r.URL.Query().Get("add") + add, err := strconv.Atoi(addStr) + if err != nil { + w.WriteHeader(http.StatusBadRequest) + w.Write([]byte("Invalid integer received: " + addStr)) + return + } + if len(mb) == 0 { + tmp := make([]byte, 1048576) + for i := 0; i < len(tmp); i++ { + tmp[i] = 10 + } + mb = tmp + } + for i := 0; i < add; i++ { + dst := make([]byte, len(mb)) + copy(dst, mb) + s = append(s, dst) + } +} + func env(w http.ResponseWriter, r *http.Request) { env := r.URL.Query().Get("env") value := os.Getenv(env)