-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathJMeterPlugin.gradle
More file actions
173 lines (145 loc) · 4.64 KB
/
JMeterPlugin.gradle
File metadata and controls
173 lines (145 loc) · 4.64 KB
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import java.awt.*
apply plugin: JMeterPlugin
jmeterConfig {
pathJMeter = "${System.properties['user.home']}/apache-jmeter-5.0/bin/jmeter.sh"
pathJmx = project.projectDir.toString() + '/src/test/jmeter/local.jmx'
pathJtl = project.buildDir.toString() + '/jmeter/local.jtl'
pathReport = project.buildDir.toString() + '/jmeter/report'
}
class JMeterPluginExtension {
String pathJMeter = ""
String pathJmx = ""
String pathJtl = ""
String pathReport = ""
}
@SuppressWarnings("unused")
class JMeterPlugin implements Plugin<Project> {
static final String TASK_GROUP = 'JMeter'
@Override
void apply(Project project) {
JMeterPluginExtension extension = project.getExtensions().create("jmeterConfig", JMeterPluginExtension.class)
project.getTasks().create("jmEdit", TaskEdit.class) {
group = TASK_GROUP
description = 'Open test plan'
project.afterEvaluate {
pathJMeter = "${extension.pathJMeter}"
pathJmx = "${extension.pathJmx}"
}
}
def taskRun = project.getTasks().create("jmRun", TaskRun.class) {
group = TASK_GROUP
description = 'Run test'
project.afterEvaluate {
pathJMeter = "${extension.pathJMeter}"
pathJmx = "${extension.pathJmx}"
pathJtl = "${extension.pathJtl}"
pathReport = "${extension.pathReport}"
}
}
project.getTasks().create("jmShowReport", TaskShowReport.class) {
group = TASK_GROUP
description = 'Open page with report'
project.afterEvaluate {
File file = new File("${extension.pathReport}", "/index.html")
if (!file.exists()) {
dependsOn taskRun
}
pathReport = "${extension.pathReport}"
}
}
}
}
class TaskRun extends DefaultTask {
String pathJMeter
String pathJmx
String pathJtl
String pathReport
void check() {
File file = new File(pathJMeter)
if (!file.exists()) {
throw new InvalidUserDataException("JMeter file not exists")
}
file = new File(pathJmx)
if (!file.exists()) {
throw new InvalidUserDataException("File with test plan not exists")
}
}
void prepared() {
File file = new File(pathJtl)
if (file.exists()) {
file.delete()
}
File directory = new File(pathReport)
if (directory.exists()) {
deleteDirectory(directory)
} else {
if (directory.mkdirs()) {
println "Create " + directory.toString()
} else {
throw new InvalidUserDataException("Cannot create " + directory.toString())
}
}
}
boolean deleteDirectory(File directoryToBeDeleted) {
File[] allContents = directoryToBeDeleted.listFiles()
if (allContents != null) {
for (File file : allContents) {
deleteDirectory(file)
}
}
return directoryToBeDeleted.delete()
}
@SuppressWarnings("unused")
@TaskAction
void start() {
try {
check()
prepared()
project.exec {
ignoreExitValue = false
commandLine "${pathJMeter}", '-n', '-t', "${pathJmx}", "-l", "${pathJtl}", '-e', '-o', "${pathReport}"
}
} catch (Exception ex) {
throw new GradleException(ex.getLocalizedMessage())
}
}
}
class TaskEdit extends DefaultTask {
String pathJMeter
String pathJmx
void check() {
File file = new File(pathJMeter)
if (!file.exists()) {
throw new InvalidUserDataException("JMeter file not exists")
}
file = new File(pathJmx)
if (!file.exists()) {
throw new InvalidUserDataException("File with test plan not exists")
}
}
@SuppressWarnings("unused")
@TaskAction
void start() {
try {
check()
ProcessBuilder pb = new ProcessBuilder("${pathJMeter}", '-t', "${pathJmx}")
pb.start()
}
catch (Exception ex) {
throw new GradleException(ex.getLocalizedMessage())
}
}
}
class TaskShowReport extends DefaultTask {
String pathReport
@SuppressWarnings("unused")
@TaskAction
void start() {
try {
Desktop.desktop.browse "file:///${pathReport}/index.html".toURI()
}
catch (Exception ex) {
throw new GradleException(ex.getLocalizedMessage())
}
}
}