Skip to content

Commit 5bfb7e0

Browse files
committed
Fix parent_tree desc sorting for performance test (fix #27)
1 parent 3c5e77e commit 5bfb7e0

File tree

3 files changed

+37
-8
lines changed

3 files changed

+37
-8
lines changed

tests/check_thread_get_posts.go

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,22 @@ type PPostSortTree []*PPost
6161
func (a PPostSortTree) Len() int { return len(a) }
6262
func (a PPostSortTree) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
6363
func (a PPostSortTree) Less(i, j int) bool {
64-
return a.ComparePath(&a[i].Path, &a[j].Path) < 0
64+
return comparePath(&a[i].Path, &a[j].Path) < 0
6565
}
66-
func (a PPostSortTree) ComparePath(p1 *[]int32, p2 *[]int32) int {
66+
67+
type PPostSortParentDesc []*PPost
68+
69+
func (a PPostSortParentDesc) Len() int { return len(a) }
70+
func (a PPostSortParentDesc) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
71+
func (a PPostSortParentDesc) Less(i, j int) bool {
72+
iTop := a[i].Path[0]
73+
jTop := a[j].Path[0]
74+
if iTop != jTop {
75+
return iTop > jTop
76+
}
77+
return comparePath(&a[i].Path, &a[j].Path) < 0
78+
}
79+
func comparePath(p1 *[]int32, p2 *[]int32) int {
6780
l1 := len(*p1)
6881
l2 := len(*p2)
6982
for i := 0; (i < l1) && (i < l2); i++ {
@@ -372,29 +385,32 @@ func PerfThreadGetPostsSuccess(p *Perf, f *Factory) {
372385

373386
slugOrId := GetSlugOrId(thread.Slug, int64(thread.ID))
374387
limit := GetRandomLimit()
375-
ff := true
376-
desc := &ff // GetRandomDesc()
388+
desc := GetRandomDesc()
377389
order := GetRandomSort()
378390

379391
// Sort
380392
limitType := func(post *PPost) int32 {
381393
return post.Index
382394
}
383395
var expected []*PPost
396+
reverse := (desc != nil) && (*desc == true)
384397
switch order {
385398
case SORT_FLAT:
386399
expected = p.data.GetThreadPostsFlat(thread)
387400
case SORT_TREE:
388401
expected = p.data.GetThreadPostsTree(thread)
389402
case SORT_PARENT:
390403
expected = p.data.GetThreadPostsTree(thread)
404+
if reverse {
405+
expected = p.data.GetThreadPostsParentDesc(thread)
406+
reverse = false
407+
}
391408
limitType = func(post *PPost) int32 {
392409
return post.Path[0]
393410
}
394411
default:
395412
panic("Unexpected sort type: " + order)
396413
}
397-
reverse := (desc != nil) && (*desc == true)
398414
var last_id *int64 = nil
399415
index := -1
400416
if reverse {

tests/filler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ func (self *Perf) Fill(threads int, timeout_sec int, config *PerfConfig) {
284284
log.Infof("Creating posts (%d threads)", threads)
285285
FillPosts(self, threads, timeout, config.PostCount, config.PostBatch)
286286

287-
log.Info("Normalize data")
287+
log.Info("Prepare for saving data")
288288
self.data.Normalize()
289289

290290
log.Info("Done")

tests/perf_data.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ type PerfData struct {
3333
usersByForum map[string][]*PUser
3434
postsByThreadFlat map[int32][]*PPost
3535
postsByThreadTree map[int32][]*PPost
36+
postsByThreadParentDesc map[int32][]*PPost
3637
userByNickname map[string]*PUser
3738
forumBySlug map[string]*PForum
3839
postById map[int64]*PPost
@@ -104,6 +105,7 @@ func NewPerfData(config *PerfConfig) *PerfData {
104105
threadsByForum: map[string][]*PThread{},
105106
usersByForum: map[string][]*PUser{},
106107
postsByThreadTree: map[int32][]*PPost{},
108+
postsByThreadParentDesc: map[int32][]*PPost{},
107109
postsByThreadFlat: map[int32][]*PPost{},
108110
userByNickname: map[string]*PUser{},
109111
forumBySlug: map[string]*PForum{},
@@ -205,6 +207,7 @@ func (self *PerfData) AddThread(thread *PThread) {
205207
self.threads = append(self.threads, thread)
206208
self.threadById[thread.ID] = thread
207209
self.postsByThreadTree[thread.ID] = []*PPost{}
210+
self.postsByThreadParentDesc[thread.ID] = []*PPost{}
208211
self.postsByThreadFlat[thread.ID] = []*PPost{}
209212
self.threadsByForum[thread.Forum.Slug] = append(self.threadsByForum[thread.Forum.Slug], thread)
210213
self.usersByForum[thread.Forum.Slug] = append(self.usersByForum[thread.Forum.Slug], thread.Author)
@@ -343,6 +346,13 @@ func (self *PerfData) GetThreadPostsTree(thread *PThread) []*PPost {
343346
return self.postsByThreadTree[thread.ID]
344347
}
345348

349+
func (self *PerfData) GetThreadPostsParentDesc(thread *PThread) []*PPost {
350+
self.mutex.RLock()
351+
defer self.mutex.RUnlock()
352+
353+
return self.postsByThreadParentDesc[thread.ID]
354+
}
355+
346356
func (self *PerfData) AddPost(post *PPost) {
347357
self.mutex.Lock()
348358
defer self.mutex.Unlock()
@@ -363,9 +373,9 @@ func (self *PerfData) AddPost(post *PPost) {
363373
post.Path = []int32{post.Index}
364374
}
365375

366-
tree := append(self.postsByThreadTree[post.Thread.ID], post)
367376
self.postsByThreadFlat[post.Thread.ID] = append(self.postsByThreadFlat[post.Thread.ID], post)
368-
self.postsByThreadTree[post.Thread.ID] = tree
377+
self.postsByThreadTree[post.Thread.ID] = append(self.postsByThreadTree[post.Thread.ID], post)
378+
self.postsByThreadParentDesc[post.Thread.ID] = append(self.postsByThreadParentDesc[post.Thread.ID], post)
369379

370380
post.Thread.Forum.Posts++
371381
post.Thread.Posts++
@@ -379,6 +389,9 @@ func (self *PerfData) Normalize() {
379389
for _, threads := range self.threadsByForum {
380390
sort.Sort(PThreadByCreated(threads))
381391
}
392+
for _, posts := range self.postsByThreadParentDesc {
393+
sort.Sort(PPostSortParentDesc(posts))
394+
}
382395
for key, users := range self.usersByForum {
383396
sort.Sort(PUserByNickname(users))
384397
size := 0

0 commit comments

Comments
 (0)