/
Injector.scala
158 lines (129 loc) · 5.51 KB
/
Injector.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
155
156
157
158
/**
* Copyright 2011-2017 GatlingCorp (http://gatling.io)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.gatling.core.controller.inject
import scala.collection.breakOut
import scala.concurrent.duration._
import io.gatling.commons.util.{ LongCounter, PushbackIterator }
import io.gatling.commons.util.Collections._
import io.gatling.commons.util.Clock._
import io.gatling.commons.util.ClockSingleton._
import io.gatling.core.controller.ControllerCommand
import io.gatling.core.scenario.Scenario
import io.gatling.core.session.Session
import io.gatling.core.stats.StatsEngine
import io.gatling.core.stats.writer.UserMessage
import akka.actor.{ Cancellable, Props, ActorSystem, ActorRef }
sealed trait InjectorCommand
object InjectorCommand {
case object Start extends InjectorCommand
case object Tick extends InjectorCommand
}
private[inject] case class UserStream(scenario: Scenario, stream: PushbackIterator[FiniteDuration]) {
def withStream(batchWindow: FiniteDuration, injectTime: Long, startTime: Long)(f: (Scenario, FiniteDuration) => Unit): Injection = {
if (stream.hasNext) {
val batchTimeOffset = (injectTime - startTime).millis
val nextBatchTimeOffset = batchTimeOffset + batchWindow
var continue = true
var streamNonEmpty = true
var count = 0L
while (streamNonEmpty && continue) {
val startingTime = stream.next()
streamNonEmpty = stream.hasNext
val delay = startingTime - batchTimeOffset
continue = startingTime < nextBatchTimeOffset
if (continue) {
count += 1
// TODO instead of scheduling each user separately, we could group them by rounded-up delay (Akka defaults to 10ms)
f(scenario, delay)
} else {
streamNonEmpty = true
stream.pushback(startingTime)
}
}
Injection(count, streamNonEmpty)
} else {
Injection.Empty
}
}
}
private[inject] object Injection {
val Empty = Injection(0, continue = false)
}
private[inject] case class Injection(count: Long, continue: Boolean) {
def +(other: Injection): Injection =
Injection(count + other.count, continue && other.continue)
}
object Injector {
val InjectorActorName = "gatling-injector"
val TickPeriod = 1 second
val InitialBatchWindow = TickPeriod * 2
def apply(system: ActorSystem, controller: ActorRef, statsEngine: StatsEngine, scenarios: List[Scenario]): ActorRef = {
val userStreams: Map[String, UserStream] = scenarios.map(scenario => scenario.name -> UserStream(scenario, new PushbackIterator(scenario.injectionProfile.allUsers)))(breakOut)
system.actorOf(Props(new Injector(controller, statsEngine, userStreams)), InjectorActorName)
}
}
private[inject] class Injector(controller: ActorRef, statsEngine: StatsEngine, defaultStreams: Map[String, UserStream]) extends InjectorFSM {
import Injector._
import InjectorState._
import InjectorData._
import InjectorCommand._
val userIdGen = new LongCounter
private def inject(streams: Map[String, UserStream], batchWindow: FiniteDuration, startMillis: Long, count: Long, timer: Cancellable): State = {
val injection = injectStreams(streams, batchWindow, startMillis)
val newCount = injection.count + count
if (injection.continue) {
goto(Started) using StartedData(startMillis, newCount, timer)
} else {
controller ! ControllerCommand.InjectionStopped(newCount)
timer.cancel()
// FIXME do we really need to stop? Or go to a noop state?
stop()
}
}
private def injectStreams(streams: Map[String, UserStream], batchWindow: FiniteDuration, startTime: Long): Injection = {
val injections = streams.values.map(_.withStream(batchWindow, nowMillis, startTime)(injectUser))
val totalCount = injections.sumBy(_.count)
val totalContinue = injections.exists(_.continue)
logger.debug(s"Injecting $totalCount users, continue=$totalContinue")
Injection(totalCount, totalContinue)
}
private def startUser(scenario: Scenario, userId: Long): Unit = {
val rawSession = Session(scenario = scenario.name, userId = userId, onExit = scenario.onExit)
val session = scenario.onStart(rawSession)
scenario.entry ! session
logger.debug(s"Start user #${session.userId}")
val userStart = UserMessage(session, io.gatling.core.stats.message.Start, session.startDate)
statsEngine.logUser(userStart)
}
private def injectUser(scenario: Scenario, delay: FiniteDuration): Unit = {
val userId = userIdGen.incrementAndGet()
if (delay <= ZeroMs) {
startUser(scenario, userId)
} else {
system.scheduler.scheduleOnce(delay)(startUser(scenario, userId))
}
}
startWith(WaitingToStart, NoData)
when(WaitingToStart) {
case Event(Start, NoData) =>
val timer = system.scheduler.schedule(InitialBatchWindow, TickPeriod, self, Tick)
inject(defaultStreams, InitialBatchWindow, nowMillis, 0, timer)
}
when(Started) {
case Event(Tick, StartedData(startMillis, count, timer)) =>
inject(defaultStreams, TickPeriod, startMillis, count, timer)
}
}