summaryrefslogtreecommitdiffstats
path: root/api/post_benchmark_test.go
blob: 861c687fb26a38065fdaef2bde2384474e5e5b2e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// Copyright (c) 2015 Spinpunch, Inc. All Rights Reserved.
// See License.txt for license information.

package api

import (
	"github.com/mattermost/platform/utils"
	"testing"
)

const (
	NUM_POSTS = 100
)

func BenchmarkCreatePost(b *testing.B) {
	var (
		NUM_POSTS_RANGE = utils.Range{NUM_POSTS, NUM_POSTS}
	)
	_, _, channel := SetupBenchmark()

	testPoster := NewAutoPostCreator(Client, channel.Id)

	// Benchmark Start
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		testPoster.CreateTestPosts(NUM_POSTS_RANGE)
	}
}

func BenchmarkUpdatePost(b *testing.B) {
	var (
		NUM_POSTS_RANGE = utils.Range{NUM_POSTS, NUM_POSTS}
		UPDATE_POST_LEN = 100
	)
	_, _, channel := SetupBenchmark()

	testPoster := NewAutoPostCreator(Client, channel.Id)
	posts, valid := testPoster.CreateTestPosts(NUM_POSTS_RANGE)
	if valid == false {
		b.Fatal("Unable to create test posts")
	}

	for i := range posts {
		posts[i].Message = utils.RandString(UPDATE_POST_LEN, utils.ALPHANUMERIC)
	}

	// Benchmark Start
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		for i := range posts {
			if _, err := Client.UpdatePost(posts[i]); err != nil {
				b.Fatal(err)
			}
		}
	}
}

func BenchmarkGetPosts(b *testing.B) {
	var (
		NUM_POSTS_RANGE = utils.Range{NUM_POSTS, NUM_POSTS}
	)
	_, _, channel := SetupBenchmark()

	testPoster := NewAutoPostCreator(Client, channel.Id)
	testPoster.CreateTestPosts(NUM_POSTS_RANGE)

	// Benchmark Start
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		Client.Must(Client.GetPosts(channel.Id, 0, NUM_POSTS, ""))
	}
}

func BenchmarkSearchPosts(b *testing.B) {
	var (
		NUM_POSTS_RANGE = utils.Range{NUM_POSTS, NUM_POSTS}
	)
	_, _, channel := SetupBenchmark()

	testPoster := NewAutoPostCreator(Client, channel.Id)
	testPoster.CreateTestPosts(NUM_POSTS_RANGE)

	// Benchmark Start
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		Client.Must(Client.SearchPosts("nothere"))
		Client.Must(Client.SearchPosts("n"))
		Client.Must(Client.SearchPosts("#tag"))
	}
}

func BenchmarkEtagCache(b *testing.B) {
	var (
		NUM_POSTS_RANGE = utils.Range{NUM_POSTS, NUM_POSTS}
	)
	_, _, channel := SetupBenchmark()

	testPoster := NewAutoPostCreator(Client, channel.Id)
	testPoster.CreateTestPosts(NUM_POSTS_RANGE)

	etag := Client.Must(Client.GetPosts(channel.Id, 0, NUM_POSTS/2, "")).Etag

	// Benchmark Start
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		Client.Must(Client.GetPosts(channel.Id, 0, NUM_POSTS/2, etag))
	}
}

func BenchmarkDeletePosts(b *testing.B) {
	var (
		NUM_POSTS_RANGE = utils.Range{NUM_POSTS, NUM_POSTS}
	)
	_, _, channel := SetupBenchmark()

	testPoster := NewAutoPostCreator(Client, channel.Id)
	posts, valid := testPoster.CreateTestPosts(NUM_POSTS_RANGE)
	if valid == false {
		b.Fatal("Unable to create test posts")
	}

	// Benchmark Start
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		for i := range posts {
			Client.Must(Client.DeletePost(channel.Id, posts[i].Id))
		}
	}

}