-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
63 lines (54 loc) · 1.43 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package main
import (
"fmt"
"html"
"log"
"math/big"
"net/http"
"os"
"strconv"
"time"
"github.com/gorilla/mux"
)
// our main function
func main() {
router := mux.NewRouter()
router.HandleFunc("/hello", Hello).Methods("GET")
router.HandleFunc("/loadtest/iterations/{iterations}", Loadtest).Methods("GET")
port := os.Getenv("PORT")
if port == "" {
port = "8000"
}
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", port), router))
}
/*
Hello handles request to hello endpoint and formats a http response message
*/
func Hello(w http.ResponseWriter, r *http.Request) {
log.Println("In Hello endpoint handler function")
fmt.Fprintf(w, "Hello, %q", html.EscapeString(r.URL.Path))
}
/*
Loadtest generates CPU load by multiplying an integer in a loop
*/
func Loadtest(w http.ResponseWriter, r *http.Request) {
log.Println("In loadtest handler....")
vars := mux.Vars(r)
iterationsStr := vars["iterations"]
iterations, err := strconv.ParseInt(iterationsStr, 10, 64)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
//TODO: Attempt doing primitive / basic integer multiplication!!!
factValue := new(big.Int)
factValue.SetInt64(1)
start := time.Now()
for i := int64(0); i < iterations; i++ {
factValue.Mul(factValue, big.NewInt(i))
}
end := time.Now()
elapsed := end.Sub(start)
log.Println("Loadtest took ", elapsed, " to perform ", iterations)
fmt.Fprintln(w, "Loadtest took ", elapsed, " to perform ", iterations)
}