forked from tinode/chat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtinode.scala
154 lines (141 loc) · 4.53 KB
/
tinode.scala
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package tinode
import java.util.Base64
import java.util.concurrent.ConcurrentHashMap
import scala.collection.JavaConverters._
import scala.collection._
import scala.concurrent.duration._
import io.gatling.core.Predef._
import io.gatling.http.Predef._
class TinodeBase extends Simulation {
val httpProtocol = http
.baseUrl("http://localhost:6060")
.wsBaseUrl("ws://localhost:6060")
// Auth tokens to share between sessions.
val tokenCache : concurrent.Map[String, String] = new ConcurrentHashMap() asScala
// Total number of messages to publish to a topic.
val publishCount = Integer.getInteger("publish_count", 10).toInt
// Maximum interval between publishing messages to a topic.
val publishInterval = Integer.getInteger("publish_interval", 100).toInt
// Total number of sessions.
val numSessions = Integer.getInteger("num_sessions", 10000)
// Ramp up period (0 to numSessions) in seconds.
val rampPeriod = java.lang.Long.getLong("ramp", 300L)
val hello = exitBlockOnFail {
exec {
ws("hi").sendText(
"""{"hi":{"id":"afabb3","ver":"0.22.8","ua":"Gatling-Loadtest/1.0; gatling/1.7.0"}}"""
)
.await(15 seconds)(
ws.checkTextMessage("hi")
.matching(jsonPath("$.ctrl").find.exists)
)
}
}
val loginBasic = exitBlockOnFail {
exec { session =>
val uname = session("username").as[String]
val password = session("password").as[String]
val secret = new String(java.util.Base64.getEncoder.encode((uname + ":" + password).getBytes()))
session.set("secret", secret)
}
.exec {
ws("login").sendText(
"""{"login":{"id":"${id}-login","scheme":"basic","secret":"${secret}"}}"""
)
.await(15 seconds)(
ws.checkTextMessage("login-ctrl")
.matching(jsonPath("$.ctrl").find.exists)
.check(jsonPath("$.ctrl.params.token").saveAs("token"))
)
}
.exec { session =>
val uname = session("username").as[String]
val token = session("token").as[String]
tokenCache.put(uname, token)
session
}
}
val loginToken = exitBlockOnFail {
exec { session =>
val uname = session("username").as[String]
var token = session("token").asOption[String]
if (token == None) {
token = tokenCache.get(uname)
}
session.set("token", token.getOrElse(""))
}
.exec {
ws("login-token").sendText(
"""{"login":{"id":"${id}-login2","scheme":"token","secret":"${token}"}}"""
)
.await(15 seconds)(
ws.checkTextMessage("login-ctrl")
.matching(jsonPath("$.ctrl").find.exists)
)
}
}
val subMe = exitBlockOnFail {
exec {
ws("sub-me").sendText(
"""{"sub":{"id":"${id}-sub-me","topic":"me","get":{"what":"desc"}}}"""
)
.await(15 seconds)(
ws.checkTextMessage("sub-me-desc")
.matching(jsonPath("$.ctrl").find.exists)
.check(jsonPath("$.ctrl.code").ofType[Int].in(200 to 299))
)
}
}
val subTopic = exitBlockOnFail {
exec {
ws("sub-topic").sendText(
"""{"sub":{"id":"${id}-sub-${sub}","topic":"${sub}","get":{"what":"desc sub data del"}}}"""
)
.await(15 seconds)(
ws.checkTextMessage("sub-topic-ctrl")
.matching(jsonPath("$.ctrl").find.exists)
.check(jsonPath("$.ctrl.code").ofType[Int].in(200 to 299))
)
}
}
val publish = exitBlockOnFail {
exec {
repeat(publishCount, "i") {
exec {
ws("pub-topic").sendText(
"""{"pub":{"id":"${id}-pub-${sub}-${i}","topic":"${sub}","content":"This is a Gatling test ${i}"}}"""
)
.await(15 seconds)(
ws.checkTextMessage("pub-topic-ctrl")
.matching(jsonPath("$.ctrl").find.exists)
.check(jsonPath("$.ctrl.code").ofType[Int].in(200 to 299))
)
}
.pause(0, publishInterval)
}
}
}
val getSubs = exitBlockOnFail {
exec {
ws("get-subs").sendText(
"""{"get":{"id":"${id}-get-subs","topic":"me","what":"sub"}}"""
)
.await(15 seconds)(
ws.checkTextMessage("save-subs")
.matching(jsonPath("$.meta.sub").find.exists)
.check(jsonPath("$.meta.sub[*].topic").findAll.saveAs("subs"))
)
}
}
val leaveTopic = exitBlockOnFail {
exec {
ws("leave-topic").sendText(
"""{"leave":{"id":"${id}-leave-${sub}","topic":"${sub}"}}"""
)
.await(15 seconds)(
ws.checkTextMessage("sub-topic-ctrl")
.matching(jsonPath("$.ctrl").find.exists)
)
}
}
}