Skip to content

Commit

Permalink
perf: add tracing tasks and regions
Browse files Browse the repository at this point in the history
  • Loading branch information
ivan1993spb committed Apr 9, 2024
1 parent 14d200f commit b9bdec5
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 3 deletions.
4 changes: 4 additions & 0 deletions internal/bot/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package bot
import (
"context"
"math/rand"
"runtime/trace"
"sync/atomic"
"time"

Expand Down Expand Up @@ -94,6 +95,9 @@ func (b *Bot) getMe() uint32 {
}

func (b *Bot) operate(ctx context.Context) (types.Direction, bool) {
ctx, task := trace.NewTask(ctx, "operate")
defer task.End()

if b.getState() != stateExplore {
return types.DirectionZero, false
}
Expand Down
11 changes: 8 additions & 3 deletions internal/core/bot_operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package core
import (
"context"
"errors"
"runtime/trace"
"sync"
"time"

Expand Down Expand Up @@ -304,9 +305,13 @@ func (bo *botOperator) runBot(ctx context.Context, conn connect.Connection) erro
break
}

if err := bo.parser.Parse(message); err != nil {
log.WithError(err).Error("parse fail")
}
trace.WithRegion(ctx, "parse", func() {
err := bo.parser.Parse(message)
if err != nil {
// If parsing fails, we just skip the message.
log.WithError(err).Error("parse fail")
}
})
}

return nil
Expand Down
11 changes: 11 additions & 0 deletions internal/core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package core

import (
"context"
"runtime/trace"
"sync"
"time"

Expand Down Expand Up @@ -132,6 +133,9 @@ func (c *Core) preloadState(ctx context.Context) {
}

func (c *Core) applyState(ctx context.Context, state map[int]int) map[int]int {
ctx, task := trace.NewTask(ctx, "applyState")
defer task.End()

c.mux.Lock()
defer c.mux.Unlock()

Expand Down Expand Up @@ -196,6 +200,9 @@ func (c *Core) unsafeGetState() map[int]int {
var ErrRequestedTooManyBots = errors.New("requested too many bots")

func (c *Core) SetState(ctx context.Context, state map[int]int) (map[int]int, error) {
ctx, task := trace.NewTask(ctx, "SetState")
defer task.End()

if stateBotsNumber(state) > c.botsLimit {
return nil, ErrRequestedTooManyBots
}
Expand Down Expand Up @@ -272,7 +279,11 @@ func (c *Core) SetOne(ctx context.Context, gameId, bots int) (map[int]int, error
}

func (c *Core) GetState(ctx context.Context) map[int]int {
_, task := trace.NewTask(ctx, "GetState")
defer task.End()

c.mux.Lock()
defer c.mux.Unlock()

return c.unsafeGetState()
}
5 changes: 5 additions & 0 deletions internal/http/handlers/get_state_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package handlers
import (
"context"
"net/http"
"runtime/trace"

"github.com/ivan1993spb/snake-bot/internal/models"
"github.com/ivan1993spb/snake-bot/internal/utils"
Expand All @@ -25,6 +26,10 @@ func NewGetStateHandler(app AppGetState) http.Handler {

func (h *GetStateHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()

ctx, task := trace.NewTask(ctx, "GetStateHandler")
defer task.End()

ctx = utils.WithModule(ctx, "get_state_handler")
log := utils.GetLogger(ctx)

Expand Down
5 changes: 5 additions & 0 deletions internal/http/handlers/set_state_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"mime"
"net/http"
"runtime/trace"
"strconv"
"time"

Expand Down Expand Up @@ -42,6 +43,10 @@ const setStateTimeout = 200 * time.Millisecond

func (h *SetStateHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()

ctx, task := trace.NewTask(ctx, "SetStateHandler")
defer task.End()

ctx = utils.WithModule(ctx, "set_state_handler")
log := utils.GetLogger(ctx)

Expand Down

0 comments on commit b9bdec5

Please sign in to comment.