Skip to content

Commit

Permalink
Merge pull request #15 from epomatti/mem
Browse files Browse the repository at this point in the history
Added the mem endpoint
  • Loading branch information
epomatti authored Jul 30, 2023
2 parents b88a779 + 8153df2 commit e2f1c5f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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` |
Expand Down
26 changes: 26 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit e2f1c5f

Please sign in to comment.