Skip to content

Commit

Permalink
backend: add memprofiling
Browse files Browse the repository at this point in the history
  • Loading branch information
bojand committed Jan 16, 2025
1 parent 331e010 commit d44ac8d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
20 changes: 19 additions & 1 deletion backend/pkg/proto/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ import (
"encoding/binary"
"errors"
"fmt"
"log"
"os"
"runtime"
"runtime/pprof"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -468,6 +472,8 @@ func (s *Service) tryCreateProtoRegistry() {
}

func (s *Service) createProtoRegistry(ctx context.Context) error {
runtime.GC() //nolint:revive // testing get up-to-date statistics

startTime := time.Now()

files := make(map[string]filesystem.File)
Expand Down Expand Up @@ -532,6 +538,16 @@ func (s *Service) createProtoRegistry(ctx context.Context) error {
zap.Int("registered_types", len(fileDescriptors)),
zap.Duration("operation_duration", totalDuration))

fend, err := os.Create("chesscom-protomemprofile-protocompile-optimized")
if err != nil {
log.Fatal("could not create memory profile: ", err) //nolint:gocritic,revive // testing
}
defer fend.Close() // error handling omitted for example
runtime.GC() //nolint:revive // testing get up-to-date statistics
if err := pprof.WriteHeapProfile(fend); err != nil {
log.Fatal("could not write memory profile: ", err) // //nolint:revive // testing
}

return nil
}

Expand Down Expand Up @@ -632,7 +648,9 @@ func (s *Service) protoFileToDescriptor(files map[string]filesystem.File) ([]lin
Accessor: protocompile.SourceAccessorFromMap(filesStr),
ImportPaths: []string{"."},
}),
Reporter: reporter.NewReporter(errorReporter, nil),
Reporter: reporter.NewReporter(errorReporter, nil),
SourceInfoMode: protocompile.SourceInfoNone,
RetainASTs: false,
}

compiledFiles, err := compiler.Compile(context.Background(), filePaths...)
Expand Down
6 changes: 5 additions & 1 deletion backend/pkg/schema/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,9 @@ func (s *Service) compileProtoSchemas(ctx context.Context, schema *SchemaVersion
Accessor: protocompile.SourceAccessorFromMap(schemasByPath),
ImportPaths: []string{"."},
}),
Reporter: reporter.NewReporter(errorReporter, nil),
Reporter: reporter.NewReporter(errorReporter, nil),
SourceInfoMode: protocompile.SourceInfoNone,
RetainASTs: false,
}

compiledFiles, err := compiler.Compile(ctx, schema.Subject)
Expand Down Expand Up @@ -502,6 +504,8 @@ func (s *Service) ValidateProtobufSchema(ctx context.Context, name string, sch S
Accessor: protocompile.SourceAccessorFromMap(schemasByPath),
ImportPaths: []string{"."},
}),
SourceInfoMode: protocompile.SourceInfoNone,
RetainASTs: false,
}

_, err = compiler.Compile(ctx, name)
Expand Down

0 comments on commit d44ac8d

Please sign in to comment.