Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MAKE-509: Database method to find all perf test results #20

Merged
merged 4 commits into from
Nov 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
62 changes: 57 additions & 5 deletions model/perf.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/mongodb/grip"
"github.com/mongodb/grip/message"
"github.com/pkg/errors"
"gopkg.in/mgo.v2/bson"
)

const perfResultCollection = "perf_results"
Expand Down Expand Up @@ -198,8 +199,10 @@ type PerformanceResults struct {
}

type PerfFindOptions struct {
Interval util.TimeRange
Info PerformanceResultInfo
Interval util.TimeRange
Info PerformanceResultInfo
MaxDepth int
GraphLookup bool
}

func (r *PerformanceResults) Setup(e sink.Environment) { r.env = e }
Expand All @@ -223,7 +226,11 @@ func (r *PerformanceResults) Find(options PerfFindOptions) error {
r.populated = false
err = session.DB(conf.DatabaseName).C(perfResultCollection).Find(search).All(&r.Results)
if options.Info.Parent != "" && len(r.Results) > 0 { // i.e. the parent fits the search criteria
err = r.findAllChildren(options.Info.Parent)
if options.GraphLookup {
err = r.findAllChildrenGraphLookup(options.Info.Parent, options.MaxDepth)
} else {
err = r.findAllChildren(options.Info.Parent, options.MaxDepth)
}
}
if err != nil && !db.ResultsNotFound(err) {
return errors.WithStack(err)
Expand Down Expand Up @@ -276,7 +283,11 @@ func (r *PerformanceResults) createFindQuery(options PerfFindOptions) map[string
}

// All children of parent are recursively added to r.Results
func (r *PerformanceResults) findAllChildren(parent string) error {
func (r *PerformanceResults) findAllChildren(parent string, depth int) error {
if depth < 0 {
return nil
}

search := db.Document{bsonutil.GetDottedKeyName("info", "parent"): parent}
conf, session, err := sink.GetSessionWithConfig(r.env)
if err != nil {
Expand All @@ -288,11 +299,52 @@ func (r *PerformanceResults) findAllChildren(parent string) error {
r.Results = append(r.Results, temp...)
for _, result := range temp {
// look into that parent
err = r.findAllChildren(result.ID)
err = r.findAllChildren(result.ID, depth-1)
}
return err
}

// All children of parent are recursively added to r.Results using $graphLookup
func (r *PerformanceResults) findAllChildrenGraphLookup(parent string, maxDepth int) error {
conf, session, err := sink.GetSessionWithConfig(r.env)
if err != nil {
return errors.WithStack(err)
}
defer session.Close()

match := bson.M{"$match": bson.M{"_id": parent}}
graphLookup := bson.M{
"$graphLookup": bson.M{
"from": perfResultCollection,
"startWith": "$" + "_id",
"connectFromField": "_id",
"connectToField": bsonutil.GetDottedKeyName("info", "parent"),
"maxDepth": maxDepth,
"as": "children",
},
}
project := bson.M{"$project": bson.M{"_id": 0, "children": 1}}
pipeline := []bson.M{
match,
graphLookup,
project,
}
pipe := session.DB(conf.DatabaseName).C(perfResultCollection).Pipe(pipeline)
iter := pipe.Iter()
defer iter.Close()

doc := struct {
Children []PerformanceResult `bson:"children,omitempty"`
}{}
for iter.Next(&doc) {
r.Results = append(r.Results, doc.Children...)
}
if err = iter.Err(); err != nil {
return errors.Wrap(err, "problem getting children")
}
return nil
}

////////////////////////////////////////////////////////////////////////
//
// Performance Data Roll up Processing
Expand Down
46 changes: 46 additions & 0 deletions model/perf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ func (s *perfResultSuite) TestFindResultsByTimeInterval() {
start := getTimeForTestingByDate(15)
options := PerfFindOptions{
Interval: util.GetTimeRange(start, time.Hour*48),
MaxDepth: 5,
}
s.NoError(s.r.Find(options))
s.Require().Len(s.r.Results, 1)
Expand All @@ -95,6 +96,7 @@ func (s *perfResultSuite) TestFindResultsWithOptionsInfo() {
start := getTimeForTestingByDate(15)
options := PerfFindOptions{
Interval: util.GetTimeRange(start, time.Hour*72),
MaxDepth: 5,
}
options.Info.Version = "1"
s.NoError(s.r.Find(options))
Expand Down Expand Up @@ -152,9 +154,11 @@ func (s *perfResultSuite) TestSearchResultsWithParent() {
nodeD.CreatedAt = getTimeForTestingByDate(17)
s.NoError(nodeD.Save())

// Without $graphLookup
start := getTimeForTestingByDate(15)
options := PerfFindOptions{
Interval: util.GetTimeRange(start, time.Hour*72),
MaxDepth: 5,
}
options.Info.Parent = nodeA.ID
s.NoError(s.r.Find(options))
Expand All @@ -163,6 +167,48 @@ func (s *perfResultSuite) TestSearchResultsWithParent() {
s.Equal(s.r.Results[1].Info.Parent, nodeA.ID)
s.Equal(s.r.Results[2].Info.Parent, nodeA.ID)
s.Equal(s.r.Results[3].ID, nodeD.ID)

// Test max depth without $graphLookup
start = getTimeForTestingByDate(15)
options = PerfFindOptions{
Interval: util.GetTimeRange(start, time.Hour*72),
MaxDepth: 0,
}
options.Info.Parent = nodeA.ID
s.NoError(s.r.Find(options))
s.Require().Len(s.r.Results, 3)
s.Equal(s.r.Results[0].ID, nodeA.ID)
s.Equal(s.r.Results[1].Info.Parent, nodeA.ID)
s.Equal(s.r.Results[2].Info.Parent, nodeA.ID)

// With $graphLookup
start = getTimeForTestingByDate(15)
options = PerfFindOptions{
Interval: util.GetTimeRange(start, time.Hour*72),
MaxDepth: 5,
GraphLookup: true,
}
options.Info.Parent = nodeA.ID
s.NoError(s.r.Find(options))
s.Require().Len(s.r.Results, 4)
s.Equal(s.r.Results[0].ID, nodeA.ID)
s.Equal(s.r.Results[1].ID, nodeD.ID)
s.Equal(s.r.Results[2].Info.Parent, nodeA.ID)
s.Equal(s.r.Results[3].Info.Parent, nodeA.ID)

// Test max depth with $graphLookup
start = getTimeForTestingByDate(15)
options = PerfFindOptions{
Interval: util.GetTimeRange(start, time.Hour*72),
MaxDepth: 0,
GraphLookup: true,
}
options.Info.Parent = nodeA.ID
s.NoError(s.r.Find(options))
s.Require().Len(s.r.Results, 3)
s.Equal(s.r.Results[0].ID, nodeA.ID)
s.Equal(s.r.Results[1].Info.Parent, nodeA.ID)
s.Equal(s.r.Results[2].Info.Parent, nodeA.ID)
}

func (s *perfResultSuite) TearDownTest() {
Expand Down