add tree benchmark

This commit is contained in:
Umputun
2018-01-07 17:45:49 -06:00
parent f25230ae8c
commit c08a097dcb
4 changed files with 1142 additions and 3 deletions
+2
View File
@@ -10,3 +10,5 @@ debug.test
.idea/
/web/node_modules/
/web/public/
*.prof
*.test
File diff suppressed because it is too large Load Diff
+5 -3
View File
@@ -22,10 +22,11 @@ type Node struct {
func MakeTree(comments []store.Comment, sortType string) *Tree {
res := Tree{}
repComments := res.filter(comments, "")
for _, rc := range repComments {
topComments := res.filter(comments, "")
res.Nodes = make([]*Node, len(topComments))
for i, rc := range topComments {
node := Node{Comment: rc}
res.Nodes = append(res.Nodes, res.proc(comments, &node, rc.ID))
res.Nodes[i] = res.proc(comments, &node, rc.ID)
}
res.sortNodes(sortType)
@@ -49,6 +50,7 @@ func (t *Tree) proc(comments []store.Comment, node *Node, parentID string) *Node
// filter returns comments for parentID
func (t *Tree) filter(comments []store.Comment, parentID string) (f []store.Comment) {
for _, c := range comments {
if c.ParentID == parentID {
f = append(f, c)
+14
View File
@@ -3,6 +3,7 @@ package format
import (
"bytes"
"encoding/json"
"io/ioutil"
"testing"
"time"
@@ -39,6 +40,19 @@ func TestStore_MakeTree(t *testing.T) {
// t.Log(string(buf.Bytes()))
}
func BenchmarkTree(b *testing.B) {
comments := []store.Comment{}
data, err := ioutil.ReadFile("testfile.json")
assert.Nil(b, err)
err = json.Unmarshal(data, &comments)
assert.Nil(b, err)
for i := 0; i < b.N; i++ {
res := MakeTree(comments, "time")
assert.NotNil(b, res)
}
}
const expJSON = `{
"comments": [
{