channel_benchmark_test.go 7.09 KB
Newer Older
1
// Copyright (c) 2015 Mattermost, Inc. All Rights Reserved.
=Corey Hulen's avatar
=Corey Hulen committed
2 3 4 5 6 7
// See License.txt for license information.

package api

import (
	"github.com/mattermost/platform/model"
=Corey Hulen's avatar
=Corey Hulen committed
8
	"github.com/mattermost/platform/store"
=Corey Hulen's avatar
=Corey Hulen committed
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
	"github.com/mattermost/platform/utils"
	"testing"
)

const (
	NUM_CHANNELS = 140
)

func BenchmarkCreateChannel(b *testing.B) {
	var (
		NUM_CHANNELS_RANGE = utils.Range{NUM_CHANNELS, NUM_CHANNELS}
	)
	team, _, _ := SetupBenchmark()

	channelCreator := NewAutoChannelCreator(Client, team.Id)

	// Benchmark Start
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		channelCreator.CreateTestChannels(NUM_CHANNELS_RANGE)
	}
}

func BenchmarkCreateDirectChannel(b *testing.B) {
	var (
		NUM_CHANNELS_RANGE = utils.Range{NUM_CHANNELS, NUM_CHANNELS}
	)
	team, _, _ := SetupBenchmark()

	userCreator := NewAutoUserCreator(Client, team.Id)
	users, err := userCreator.CreateTestUsers(NUM_CHANNELS_RANGE)
	if err == false {
		b.Fatal("Could not create users")
	}

	data := make([]map[string]string, len(users))

	for i := range data {
		newmap := map[string]string{
			"user_id": users[i].Id,
		}
		data[i] = newmap
	}

	// Benchmark Start
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		for j := 0; j < NUM_CHANNELS; j++ {
			Client.CreateDirectChannel(data[j])
		}
	}
}

func BenchmarkUpdateChannel(b *testing.B) {
	var (
64 65
		NUM_CHANNELS_RANGE = utils.Range{NUM_CHANNELS, NUM_CHANNELS}
		CHANNEL_HEADER_LEN = 50
=Corey Hulen's avatar
=Corey Hulen committed
66 67 68 69 70 71 72 73 74 75
	)
	team, _, _ := SetupBenchmark()

	channelCreator := NewAutoChannelCreator(Client, team.Id)
	channels, valid := channelCreator.CreateTestChannels(NUM_CHANNELS_RANGE)
	if valid == false {
		b.Fatal("Unable to create test channels")
	}

	for i := range channels {
76
		channels[i].Header = utils.RandString(CHANNEL_HEADER_LEN, utils.ALPHANUMERIC)
=Corey Hulen's avatar
=Corey Hulen committed
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 131 132 133 134 135 136 137 138 139 140
	}

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

func BenchmarkGetChannels(b *testing.B) {
	var (
		NUM_CHANNELS_RANGE = utils.Range{NUM_CHANNELS, NUM_CHANNELS}
	)
	team, _, _ := SetupBenchmark()

	channelCreator := NewAutoChannelCreator(Client, team.Id)
	_, valid := channelCreator.CreateTestChannels(NUM_CHANNELS_RANGE)
	if valid == false {
		b.Fatal("Unable to create test channels")
	}

	// Benchmark Start
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		Client.Must(Client.GetChannels(""))
	}
}

func BenchmarkGetMoreChannels(b *testing.B) {
	var (
		NUM_CHANNELS_RANGE = utils.Range{NUM_CHANNELS, NUM_CHANNELS}
	)
	team, _, _ := SetupBenchmark()

	channelCreator := NewAutoChannelCreator(Client, team.Id)
	_, valid := channelCreator.CreateTestChannels(NUM_CHANNELS_RANGE)
	if valid == false {
		b.Fatal("Unable to create test channels")
	}

	// Benchmark Start
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		Client.Must(Client.GetMoreChannels(""))
	}
}

func BenchmarkJoinChannel(b *testing.B) {
	var (
		NUM_CHANNELS_RANGE = utils.Range{NUM_CHANNELS, NUM_CHANNELS}
	)
	team, _, _ := SetupBenchmark()

	channelCreator := NewAutoChannelCreator(Client, team.Id)
	channels, valid := channelCreator.CreateTestChannels(NUM_CHANNELS_RANGE)
	if valid == false {
		b.Fatal("Unable to create test channels")
	}

	// Secondary test user to join channels created by primary test user
141
	user := &model.User{TeamId: team.Id, Email: model.NewId() + "random@test.com", Nickname: "That Guy", Password: "pwd"}
=Corey Hulen's avatar
=Corey Hulen committed
142
	user = Client.Must(Client.CreateUser(user, "")).Data.(*model.User)
143
	store.Must(Srv.Store.User().VerifyEmail(user.Id))
144
	Client.LoginByEmail(team.Name, user.Email, "pwd")
=Corey Hulen's avatar
=Corey Hulen committed
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191

	// Benchmark Start
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		for j := range channels {
			Client.Must(Client.JoinChannel(channels[j].Id))
		}
	}
}

func BenchmarkDeleteChannel(b *testing.B) {
	var (
		NUM_CHANNELS_RANGE = utils.Range{NUM_CHANNELS, NUM_CHANNELS}
	)
	team, _, _ := SetupBenchmark()

	channelCreator := NewAutoChannelCreator(Client, team.Id)
	channels, valid := channelCreator.CreateTestChannels(NUM_CHANNELS_RANGE)
	if valid == false {
		b.Fatal("Unable to create test channels")
	}

	// Benchmark Start
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		for j := range channels {
			Client.Must(Client.DeleteChannel(channels[j].Id))
		}
	}
}

func BenchmarkGetChannelExtraInfo(b *testing.B) {
	var (
		NUM_CHANNELS_RANGE = utils.Range{NUM_CHANNELS, NUM_CHANNELS}
	)
	team, _, _ := SetupBenchmark()

	channelCreator := NewAutoChannelCreator(Client, team.Id)
	channels, valid := channelCreator.CreateTestChannels(NUM_CHANNELS_RANGE)
	if valid == false {
		b.Fatal("Unable to create test channels")
	}

	// Benchmark Start
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		for j := range channels {
192
			Client.Must(Client.GetChannelExtraInfo(channels[j].Id, -1, ""))
=Corey Hulen's avatar
=Corey Hulen committed
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
		}
	}
}

func BenchmarkAddChannelMember(b *testing.B) {
	var (
		NUM_USERS       = 100
		NUM_USERS_RANGE = utils.Range{NUM_USERS, NUM_USERS}
	)
	team, _, _ := SetupBenchmark()

	channel := &model.Channel{DisplayName: "Test Channel", Name: "a" + model.NewId() + "a", Type: model.CHANNEL_OPEN, TeamId: team.Id}
	channel = Client.Must(Client.CreateChannel(channel)).Data.(*model.Channel)

	userCreator := NewAutoUserCreator(Client, team.Id)
	users, valid := userCreator.CreateTestUsers(NUM_USERS_RANGE)
	if valid == false {
		b.Fatal("Unable to create test users")
	}

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

// Is this benchmark failing? Raise your file ulimit! 2048 worked for me.
func BenchmarkRemoveChannelMember(b *testing.B) {
	var (
		NUM_USERS       = 140
		NUM_USERS_RANGE = utils.Range{NUM_USERS, NUM_USERS}
	)
	team, _, _ := SetupBenchmark()

	channel := &model.Channel{DisplayName: "Test Channel", Name: "a" + model.NewId() + "a", Type: model.CHANNEL_OPEN, TeamId: team.Id}
	channel = Client.Must(Client.CreateChannel(channel)).Data.(*model.Channel)

	userCreator := NewAutoUserCreator(Client, team.Id)
	users, valid := userCreator.CreateTestUsers(NUM_USERS_RANGE)
	if valid == false {
		b.Fatal("Unable to create test users")
	}

	for i := range users {
		if _, err := Client.AddChannelMember(channel.Id, users[i].Id); err != nil {
			b.Fatal(err)
		}
	}

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

258
func BenchmarkUpdateNotifyProps(b *testing.B) {
=Corey Hulen's avatar
=Corey Hulen committed
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
	var (
		NUM_CHANNELS_RANGE = utils.Range{NUM_CHANNELS, NUM_CHANNELS}
	)
	team, user, _ := SetupBenchmark()

	channelCreator := NewAutoChannelCreator(Client, team.Id)
	channels, valid := channelCreator.CreateTestChannels(NUM_CHANNELS_RANGE)
	if valid == false {
		b.Fatal("Unable to create test channels")
	}

	data := make([]map[string]string, len(channels))

	for i := range data {
		newmap := map[string]string{
274 275 276 277
			"channel_id":  channels[i].Id,
			"user_id":     user.Id,
			"desktop":     model.CHANNEL_NOTIFY_MENTION,
			"mark_unread": model.CHANNEL_MARK_UNREAD_MENTION,
=Corey Hulen's avatar
=Corey Hulen committed
278 279 280 281 282 283 284 285
		}
		data[i] = newmap
	}

	// Benchmark Start
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		for j := range channels {
286
			Client.Must(Client.UpdateNotifyProps(data[j]))
=Corey Hulen's avatar
=Corey Hulen committed
287 288 289
		}
	}
}