高手的存在,就是让服务10亿人的时候,你感觉只是为你一个人服务......

ngrinder 混合场景脚本

目录
  1. 1. 依据vuser数量
  2. 2. 依据用户比例

nginder混合场景不如LR方便,多场景执行的比例可以用程序来控制。

依据vuser数量

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
import net.grinder.script.GTest
import net.grinder.scriptengine.groovy.junit.GrinderRunner
import net.grinder.scriptengine.groovy.junit.annotation.BeforeProcess
import net.grinder.scriptengine.groovy.junit.annotation.BeforeThread
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import static net.grinder.script.Grinder.grinder


@RunWith(GrinderRunner)
class MixSceneByUser {

int vuserNO //vuser唯一编号,从0开始
int totalVusers //用户总数
def action

GTest test1 = new GTest(1, "事务1")
GTest test2 = new GTest(2, "事务2")
GTest test3 = new GTest(3, "事务3")

@BeforeProcess
public static void beforeProcess() {
//进程初始化
grinder.logger.info("beforeProcess.")

}

@BeforeThread
public void beforeThread() {
test1.record(this, "action1")
test2.record(this, "action2")
test3.record(this, "action3")
grinder.logger.info("beforeThread.")
initNumbers(0) //计算唯一编号
if (vuserNO < 10) {
//0~9号用户个vuser执行
action = {this.action1()}
}
else if (vuserNO < 90 ) {
//10~89
action = {this.action2()}
} else {
//其它
action = {this.action3()}
}
}
@Test
public void test() {
action()
}

public void action1() {
grinder.logger.info("action1")

}

public void action2() {
grinder.logger.info("action2")
}

public void action3() {
grinder.logger.info("action3")

}

//根据运行时配置计算唯一编号,调试时只有1个用户,返回值0
void initNumbers(int iteration) {
int intAgents = 0
int intProcs = 0
int intThreads = 0
if (grinder.getProperties() != null) {
intAgents = Integer.parseInt(grinder.properties.get("grinder.agents").toString())
intProcs = Integer.parseInt(grinder.properties.get("grinder.processes").toString())
intThreads = Integer.parseInt(grinder.properties.get("grinder.threads").toString())
}
int agent_no = grinder.agentNumber
int proc_no = grinder.processNumber
int thread_no = grinder.threadNumber

totalVusers = intAgents * intProcs * intThreads
vuserNO = agent_no * intProcs * intThreads + proc_no * intThreads + thread_no + intAgents * intProcs * intThreads * iteration
}

}

依据用户比例

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
import net.grinder.script.GTest
import net.grinder.scriptengine.groovy.junit.GrinderRunner
import net.grinder.scriptengine.groovy.junit.annotation.BeforeProcess
import net.grinder.scriptengine.groovy.junit.annotation.BeforeThread
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import static net.grinder.script.Grinder.grinder


@RunWith(GrinderRunner)
class MixSceneByUser {

GTest test1 = new GTest(1, "事务1")


@BeforeProcess
public static void beforeProcess() {
//进程初始化
grinder.logger.info("beforeProcess.")
}

@BeforeThread
public void beforeThread() {
test1.record(this, "action1")
test1.record(this, "action2")
test1.record(this, "action3")
grinder.logger.info("beforeThread.")

}

@Before
public void before() {
grinder.logger.info("before.")
}

@Test
public void test1() {
grinder.logger.info("test1.")

//10% 用户
if(grinder.runNumber % 10 == 1){

action1()
}
action2()

}


public void action1(){}
public void action2(){}
}