Skip to content

Commit

Permalink
Merge pull request #920 from percona/PMM-12468-plan-summary-and-coll-…
Browse files Browse the repository at this point in the history
…scan

PMM-12468 Plan summary, COLLSCAN.
  • Loading branch information
svetasmirnova authored Feb 6, 2025
2 parents d82723f + 5972093 commit 75cddb4
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 10 deletions.
8 changes: 5 additions & 3 deletions src/go/mongolib/fingerprinter/fingerprinter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ import (
"strings"
"testing"

"github.com/percona/percona-toolkit/src/go/lib/tutil"
"github.com/percona/percona-toolkit/src/go/mongolib/proto"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.mongodb.org/mongo-driver/bson"

"github.com/percona/percona-toolkit/src/go/lib/tutil"
"github.com/percona/percona-toolkit/src/go/mongolib/proto"
)

const (
Expand All @@ -33,7 +34,8 @@ func TestMain(m *testing.M) {
log.Printf("cannot get root path: %s", err.Error())
os.Exit(1)
}
os.Exit(m.Run())
code := m.Run()
os.Exit(code)
}

func TestSingleFingerprint(t *testing.T) {
Expand Down
10 changes: 6 additions & 4 deletions src/go/mongolib/profiler/profiler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ import (
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.mongodb.org/mongo-driver/bson/primitive"

tu "github.com/percona/percona-toolkit/src/go/internal/testutils"
"github.com/percona/percona-toolkit/src/go/lib/tutil"
"github.com/percona/percona-toolkit/src/go/mongolib/fingerprinter"
"github.com/percona/percona-toolkit/src/go/mongolib/stats"
"github.com/percona/percona-toolkit/src/go/pt-mongodb-query-digest/filter"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.mongodb.org/mongo-driver/bson/primitive"
)

const (
Expand All @@ -38,7 +39,8 @@ func TestMain(m *testing.M) {
log.Printf("cannot get root path: %s", err.Error())
os.Exit(1)
}
os.Exit(m.Run())
code := m.Run()
os.Exit(code)
}

func TestRegularIterator(t *testing.T) {
Expand Down
1 change: 1 addition & 0 deletions src/go/mongolib/proto/system.profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ type SystemProfile struct {
Ns string `bson:"ns"`
NumYield int `bson:"numYield"`
Op string `bson:"op"`
PlanSummary string `bson:"planSummary"`
Protocol string `bson:"protocol"`
Query bson.D `bson:"query"`
UpdateObj bson.D `bson:"updateobj"`
Expand Down
26 changes: 26 additions & 0 deletions src/go/mongolib/stats/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"crypto/md5"
"fmt"
"sort"
"strings"
"sync"
"time"

Expand All @@ -13,6 +14,11 @@ import (
"github.com/percona/percona-toolkit/src/go/mongolib/proto"
)

const (
planSummaryCollScan = "COLLSCAN"
planSummaryIXScan = "IXSCAN"
)

type StatsError struct {
error
}
Expand Down Expand Up @@ -86,13 +92,22 @@ func (s *Stats) Add(doc proto.SystemProfile) error {
Namespace: fp.Namespace,
TableScan: false,
Query: string(queryBson),
PlanSummary: doc.PlanSummary,
}
s.setQueryInfoAndCounters(key, qiac)
}
qiac.Count++
// docsExamined is renamed from nscannedObjects in 3.2.0.
// https://docs.mongodb.com/manual/reference/database-profiler/#system.profile.docsExamined
s.Lock()
qiac.PlanSummary = doc.PlanSummary
if qiac.PlanSummary == planSummaryCollScan {
qiac.CollScanCount++
qiac.CollScanSum += int64(doc.Millis)
}
if strings.HasPrefix(qiac.PlanSummary, planSummaryIXScan) {
qiac.PlanSummary = planSummaryIXScan
}
if doc.NscannedObjects > 0 {
qiac.NScanned = append(qiac.NScanned, float64(doc.NscannedObjects))
} else {
Expand Down Expand Up @@ -188,6 +203,10 @@ type QueryInfoAndCounters struct {
NScanned []float64
QueryTime []float64 // in milliseconds
ResponseLength []float64

PlanSummary string
CollScanCount int
CollScanSum int64 // in milliseconds
}

// times is an array of time.Time that implements the Sorter interface
Expand Down Expand Up @@ -238,6 +257,10 @@ type QueryStats struct {
ResponseLength Statistics
Returned Statistics
Scanned Statistics

PlanSummary string
CollScanCount int
CollScanSum int64 // in milliseconds
}

type Statistics struct {
Expand Down Expand Up @@ -267,6 +290,9 @@ func countersToStats(query QueryInfoAndCounters, uptime int64, tc totalCounters)
LastSeen: query.LastSeen,
Namespace: query.Namespace,
QPS: float64(query.Count) / float64(uptime),
PlanSummary: query.PlanSummary,
CollScanCount: query.CollScanCount,
CollScanSum: query.CollScanSum,
}
if tc.Scanned > 0 {
queryStats.Scanned.Pct = queryStats.Scanned.Total * 100 / tc.Scanned
Expand Down
5 changes: 3 additions & 2 deletions src/go/mongolib/stats/stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"time"

"github.com/golang/mock/gomock"

"github.com/percona/percona-toolkit/src/go/lib/tutil"
"github.com/percona/percona-toolkit/src/go/mongolib/fingerprinter"
"github.com/percona/percona-toolkit/src/go/mongolib/proto"
Expand All @@ -40,8 +41,8 @@ func TestMain(m *testing.M) {
log.Printf("cannot get root path: %s", err.Error())
os.Exit(1)
}
// TODO: Review with the new sandbox
// os.Exit(m.Run())
code := m.Run()
os.Exit(code)
}

func TestTimesLen(t *testing.T) {
Expand Down
3 changes: 2 additions & 1 deletion src/go/pt-pg-summary/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ var logger = logrus.New()

func TestMain(m *testing.M) {
logger.SetLevel(logrus.WarnLevel)
os.Exit(m.Run())
code := m.Run()
os.Exit(code)
}

func TestConnection(t *testing.T) {
Expand Down

0 comments on commit 75cddb4

Please sign in to comment.