Skip to content

Commit

Permalink
Merge pull request #17 from radu-matei/feat/add-timing
Browse files Browse the repository at this point in the history
  • Loading branch information
radu-matei authored May 18, 2023
2 parents 8855951 + 277d1d0 commit ec962a1
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions explorer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"os"
"path"
"strings"
"time"

spin "github.com/fermyon/spin/sdk/go/http"
kv "github.com/fermyon/spin/sdk/go/key_value"
Expand Down Expand Up @@ -77,8 +78,6 @@ func serve(w http.ResponseWriter, r *http.Request) {
}

spinRoute = getBasePath(r.Header)
log.Println("spin route: ", spinRoute)

router := spin.NewRouter()

// Access to the list, get, create, and delete KV pairs endpoints is behind basic auth,
Expand Down Expand Up @@ -114,7 +113,9 @@ func ListKeysHandler(w http.ResponseWriter, _ *http.Request, p spin.Params) {
}
defer kv.Close(store)

start := time.Now()
keys, err := kv.GetKeys(store)
log.Printf("LIST operation took: %s", time.Since(start))
if err != nil {
log.Printf("ERROR: cannot list keys: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
Expand All @@ -138,7 +139,9 @@ func GetKeyHandler(w http.ResponseWriter, _ *http.Request, p spin.Params) {
}
defer kv.Close(store)

start := time.Now()
value, err := kv.Get(store, key)
log.Printf("GET operation took %s", time.Since(start))
if err != nil {
log.Printf("ERROR: cannot get key: %v", err)
w.WriteHeader(http.StatusNotFound)
Expand All @@ -162,7 +165,9 @@ func DeleteKeyHandler(w http.ResponseWriter, _ *http.Request, p spin.Params) {
}
defer kv.Close(store)

start := time.Now()
err = kv.Delete(store, key)
log.Printf("DELETE operation took %s", time.Since(start))
if err != nil {
log.Printf("ERROR: cannot delete key: %v", err)
w.WriteHeader(http.StatusNotFound)
Expand All @@ -187,7 +192,9 @@ func AddKeyHandler(w http.ResponseWriter, r *http.Request, p spin.Params) {
}
defer kv.Close(store)

start := time.Now()
err = kv.Set(store, input.Key, []byte(input.Value))
log.Printf("SET operation took %s", time.Since(start))
if err != nil {
log.Printf("ERROR: cannot add key: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
Expand Down

0 comments on commit ec962a1

Please sign in to comment.