| 63 | } |
| 64 | |
| 65 | func rootHandler(w http.ResponseWriter, req *http.Request) { |
| 66 | workload := req.Header.Get("workload") |
| 67 | function := req.Header.Get("function") |
| 68 | requestedCpu := req.Header.Get("requested_cpu") |
| 69 | multiplier := req.Header.Get("multiplier") |
| 70 | |
| 71 | switch workload { |
| 72 | case "empty": |
| 73 | responseBytes, _ := json.Marshal(FunctionResponse{ |
| 74 | Status: "OK - EMPTY", |
| 75 | Function: function, |
| 76 | MachineName: machineName, |
| 77 | ExecutionTime: 0, |
| 78 | }) |
| 79 | |
| 80 | _, _ = w.Write(responseBytes) |
| 81 | w.WriteHeader(http.StatusOK) |
| 82 | case "trace": |
| 83 | tlm, err := strconv.Atoi(requestedCpu) |
| 84 | if err != nil { |
| 85 | w.WriteHeader(http.StatusBadRequest) |
| 86 | return |
| 87 | } |
| 88 | |
| 89 | mpl, err := strconv.Atoi(multiplier) |
| 90 | if err != nil { |
| 91 | w.WriteHeader(http.StatusBadRequest) |
| 92 | return |
| 93 | } |
| 94 | |
| 95 | start := time.Now() |
| 96 | timeLeftMilliseconds := uint32(tlm) |
| 97 | |
| 98 | timeConsumedMilliseconds := uint32(time.Since(start).Milliseconds()) |
| 99 | if timeConsumedMilliseconds < timeLeftMilliseconds { |
| 100 | timeLeftMilliseconds -= timeConsumedMilliseconds |
| 101 | if timeLeftMilliseconds > 0 { |
| 102 | busySpin(uint32(mpl), timeLeftMilliseconds) |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | responseBytes, _ := json.Marshal(FunctionResponse{ |
| 107 | Status: "OK", |
| 108 | Function: function, |
| 109 | MachineName: machineName, |
| 110 | ExecutionTime: time.Since(start).Microseconds(), |
| 111 | }) |
| 112 | |
| 113 | _, _ = w.Write(responseBytes) |
| 114 | w.WriteHeader(http.StatusOK) |
| 115 | default: |
| 116 | w.WriteHeader(http.StatusBadRequest) |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | type FunctionResponse struct { |
| 121 | Status string `json:"Status"` |