From 4faf1a8832a102453995903ea5b6a596845030eb Mon Sep 17 00:00:00 2001 From: Simon Let Date: Tue, 10 Dec 2019 18:53:28 +0100 Subject: [PATCH] add debug option, estimate performance of /recall --- cmd/daemon/main.go | 4 ++++ cmd/daemon/recall.go | 10 ++++++++-- cmd/daemon/run-server.go | 4 +++- conf/config.toml | 1 + pkg/cfg/cfg.go | 1 + pkg/sesshist/sesshist.go | 11 +++++++---- 6 files changed, 24 insertions(+), 7 deletions(-) diff --git a/cmd/daemon/main.go b/cmd/daemon/main.go index 8bc48f9..5591399 100644 --- a/cmd/daemon/main.go +++ b/cmd/daemon/main.go @@ -48,6 +48,10 @@ func main() { log.Println("Error reading config", err) return } + if config.Debug { + log.SetFlags(log.LstdFlags | log.Lmicroseconds) + } + res, err := isDaemonRunning(config.Port) if err != nil { log.Println("Error while checking if the daemon is runnnig", err) diff --git a/cmd/daemon/recall.go b/cmd/daemon/recall.go index 7c5efeb..0f0ad83 100644 --- a/cmd/daemon/recall.go +++ b/cmd/daemon/recall.go @@ -16,6 +16,8 @@ type recallHandler struct { } func (h *recallHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { + log.Println("/recall START") + log.Println("/recall reading body ...") jsn, err := ioutil.ReadAll(r.Body) if err != nil { log.Println("Error reading the body", err) @@ -23,19 +25,22 @@ func (h *recallHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { } rec := records.Record{} + log.Println("/recall unmarshaling record ...") err = json.Unmarshal(jsn, &rec) if err != nil { log.Println("Decoding error:", err) log.Println("Payload:", jsn) return } + log.Println("/recall recalling ...") cmd, err := h.sesshistDispatch.Recall(rec.SessionID, rec.RecallHistno, rec.RecallPrefix) if err != nil { log.Println("/recall - sess id:", rec.SessionID, " - histno:", rec.RecallHistno, " -> ERROR") log.Println("Recall error:", err) return } - resp := collect.SingleResponse{cmd} + resp := collect.SingleResponse{CmdLine: cmd} + log.Println("/recall marshaling response ...") jsn, err = json.Marshal(&resp) if err != nil { log.Println("Encoding error:", err) @@ -43,6 +48,7 @@ func (h *recallHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { return } log.Println(string(jsn)) + log.Println("/recall writing response ...") w.Write(jsn) - log.Println("/recall - sess id:", rec.SessionID, " - histno:", rec.RecallHistno, " -> ", cmd) + log.Println("/recall END - sess id:", rec.SessionID, " - histno:", rec.RecallHistno, " -> ", cmd) } diff --git a/cmd/daemon/run-server.go b/cmd/daemon/run-server.go index 34a530e..9d83e79 100644 --- a/cmd/daemon/run-server.go +++ b/cmd/daemon/run-server.go @@ -39,7 +39,9 @@ func runServer(config cfg.Config, historyPath string) { histfileBox := histfile.New(histfileRecords, historyPath, 10000, histfileSessionsToDrop, histfileSignals, shutdown) // sesshist New - sesshistDispatch := sesshist.NewDispatch(sesshistSessionsToInit, sesshistSessionsToDrop, sesshistRecords, histfileBox, config.SesshistInitHistorySize) + sesshistDispatch := sesshist.NewDispatch(sesshistSessionsToInit, sesshistSessionsToDrop, + sesshistRecords, histfileBox, + config.SesshistInitHistorySize) // sesswatch sesswatchSessionsToWatch := make(chan records.Record) diff --git a/conf/config.toml b/conf/config.toml index 1970f34..931767c 100644 --- a/conf/config.toml +++ b/conf/config.toml @@ -1,3 +1,4 @@ port = 2627 sesswatchPeriodSeconds = 120 sesshistInitHistorySize = 1000 +debug = true diff --git a/pkg/cfg/cfg.go b/pkg/cfg/cfg.go index 16726bb..1ed3cb7 100644 --- a/pkg/cfg/cfg.go +++ b/pkg/cfg/cfg.go @@ -5,4 +5,5 @@ type Config struct { Port int SesswatchPeriodSeconds uint SesshistInitHistorySize int + Debug bool } diff --git a/pkg/sesshist/sesshist.go b/pkg/sesshist/sesshist.go index 50b79ec..0079b40 100644 --- a/pkg/sesshist/sesshist.go +++ b/pkg/sesshist/sesshist.go @@ -133,7 +133,9 @@ func (s *Dispatch) addRecentRecord(sessionID string, record records.Record) erro // Recall command from recent session history func (s *Dispatch) Recall(sessionID string, histno int, prefix string) (string, error) { + log.Println("sesshist - recall: RLocking main lock ...") s.mutex.RLock() + log.Println("sesshist - recall: Getting session history struct ...") session, found := s.sessions[sessionID] s.mutex.RUnlock() @@ -141,13 +143,14 @@ func (s *Dispatch) Recall(sessionID string, histno int, prefix string) (string, // go s.initSession(sessionID) return "", errors.New("sesshist ERROR: No session history for SessionID " + sessionID + " - should we create one?") } + log.Println("sesshist - recall: Locking session lock ...") + session.mutex.Lock() + defer session.mutex.Unlock() if prefix == "" { - session.mutex.Lock() - defer session.mutex.Unlock() + log.Println("sesshist - recall: Getting records by histno ...") return session.getRecordByHistno(histno) } - session.mutex.Lock() - defer session.mutex.Unlock() + log.Println("sesshist - recall: Searching for records by prefix ...") return session.searchRecordByPrefix(prefix, histno) }