Line 0
Link Here
|
|
|
1 |
/* |
2 |
* Licensed to the Apache Software Foundation (ASF) under one or more |
3 |
* contributor license agreements. See the NOTICE file distributed with |
4 |
* this work for additional information regarding copyright ownership. |
5 |
* The ASF licenses this file to You under the Apache License, Version 2.0 |
6 |
* (the "License"); you may not use this file except in compliance with |
7 |
* the License. You may obtain a copy of the License at |
8 |
* |
9 |
* http://www.apache.org/licenses/LICENSE-2.0 |
10 |
* |
11 |
* Unless required by applicable law or agreed to in writing, software |
12 |
* distributed under the License is distributed on an "AS IS" BASIS, |
13 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
14 |
* See the License for the specific language governing permissions and |
15 |
* limitations under the License. |
16 |
* |
17 |
*/ |
18 |
|
19 |
package org.apache.jmeter.gui.action; |
20 |
|
21 |
import java.awt.Toolkit; |
22 |
import java.awt.event.ActionEvent; |
23 |
import java.util.HashSet; |
24 |
import java.util.Set; |
25 |
|
26 |
import org.apache.jmeter.control.Controller; |
27 |
import org.apache.jmeter.exceptions.IllegalUserActionException; |
28 |
import org.apache.jmeter.gui.GuiPackage; |
29 |
import org.apache.jmeter.gui.action.thinktime.ThinkTimeCreator; |
30 |
import org.apache.jmeter.gui.tree.JMeterTreeNode; |
31 |
import org.apache.jmeter.samplers.Sampler; |
32 |
import org.apache.jmeter.threads.ThreadGroup; |
33 |
import org.apache.jmeter.util.JMeterUtils; |
34 |
import org.apache.jorphan.logging.LoggingManager; |
35 |
import org.apache.log.Logger; |
36 |
|
37 |
/** |
38 |
* Add ThinkTime (TestAction + UniformRandomTimer) |
39 |
* @since 3.2 |
40 |
*/ |
41 |
public class AddThinkTimeBetweenEachStep extends AbstractAction { |
42 |
private static final Logger log = LoggingManager.getLoggerForClass(); |
43 |
|
44 |
private static final Set<String> commands = new HashSet<>(); |
45 |
|
46 |
private static final String DEFAULT_IMPLEMENTATION = |
47 |
JMeterUtils.getPropDefault("think_time_creator.impl", |
48 |
"org.apache.jmeter.thinktime.DefaultThinkTimeCreator"); |
49 |
static { |
50 |
commands.add(ActionNames.ADD_THINK_TIME_BETWEEN_EACH_STEP); |
51 |
} |
52 |
|
53 |
/** |
54 |
* |
55 |
*/ |
56 |
public AddThinkTimeBetweenEachStep() { |
57 |
super(); |
58 |
} |
59 |
|
60 |
@Override |
61 |
public void doAction(ActionEvent e) { |
62 |
GuiPackage guiPackage = GuiPackage.getInstance(); |
63 |
JMeterTreeNode currentNode = guiPackage.getTreeListener().getCurrentNode(); |
64 |
if (! |
65 |
(currentNode.getUserObject() instanceof Controller || |
66 |
currentNode.getUserObject() instanceof ThreadGroup) |
67 |
) { |
68 |
Toolkit.getDefaultToolkit().beep(); |
69 |
return; |
70 |
} |
71 |
try { |
72 |
addThinkTimeToChildren(guiPackage, currentNode); |
73 |
} catch (Exception err) { |
74 |
Toolkit.getDefaultToolkit().beep(); |
75 |
log.error("Failed to add think times", err); |
76 |
JMeterUtils.reportErrorToUser("Failed to add think times", err); |
77 |
} |
78 |
} |
79 |
|
80 |
/** |
81 |
* Add Think Time to children of parentNode |
82 |
* @param guiPackage {@link GuiPackage} |
83 |
* @param parentNode Parent node of elements on which we add think times |
84 |
* @throws IllegalUserActionException |
85 |
*/ |
86 |
private void addThinkTimeToChildren(GuiPackage guiPackage, |
87 |
JMeterTreeNode parentNode) throws IllegalUserActionException { |
88 |
guiPackage.updateCurrentNode(); |
89 |
boolean insertThinkTime = false; |
90 |
try { |
91 |
int index = 0; |
92 |
while(true) { |
93 |
if(index == parentNode.getChildCount()) { |
94 |
index++; |
95 |
break; |
96 |
} |
97 |
JMeterTreeNode childNode = (JMeterTreeNode) parentNode.getChildAt(index); |
98 |
Object userObject = childNode.getUserObject(); |
99 |
if(userObject instanceof Sampler || |
100 |
userObject instanceof Controller) { |
101 |
insertThinkTime = true; |
102 |
} |
103 |
if(insertThinkTime) { |
104 |
JMeterTreeNode[] nodes = createThinkTime(guiPackage, parentNode); |
105 |
if(nodes.length != 2) { |
106 |
throw new IllegalArgumentException("Invalid Think Time, expected 2 nodes, got:"+nodes.length); |
107 |
} |
108 |
index++; |
109 |
addNodesToTreeHierachically(guiPackage, parentNode, nodes, index); |
110 |
insertThinkTime = false; |
111 |
} |
112 |
index++; |
113 |
} |
114 |
} catch(Exception ex) { |
115 |
throw new IllegalUserActionException("Cannot add think times", ex); |
116 |
} |
117 |
} |
118 |
|
119 |
/** |
120 |
* add nodes to JMeter Tree |
121 |
* @param guiPackage {@link GuiPackage} |
122 |
* @param parentNode {@link JMeterTreeNode} |
123 |
* @param childNodes Child nodes |
124 |
* @param index insertion index |
125 |
*/ |
126 |
private void addNodesToTreeHierachically(GuiPackage guiPackage, |
127 |
JMeterTreeNode parentNode, |
128 |
JMeterTreeNode[] childNodes, |
129 |
int index) { |
130 |
guiPackage.getTreeModel().insertNodeInto(childNodes[0], parentNode, index); |
131 |
guiPackage.getTreeModel().insertNodeInto(childNodes[1], childNodes[0], 0); |
132 |
} |
133 |
|
134 |
/** |
135 |
* |
136 |
* @param guiPackage {@link GuiPackage} |
137 |
* @param parentNode {@link JMeterTreeNode} |
138 |
* @return array of {@link JMeterTreeNode} |
139 |
* @throws IllegalUserActionException |
140 |
*/ |
141 |
private JMeterTreeNode[] createThinkTime(GuiPackage guiPackage, JMeterTreeNode parentNode) |
142 |
throws Exception { |
143 |
Class<?> clazz = Class.forName(DEFAULT_IMPLEMENTATION); |
144 |
ThinkTimeCreator thinkTimeCreator = (ThinkTimeCreator) clazz.newInstance(); |
145 |
return thinkTimeCreator.createThinkTime(guiPackage, parentNode); |
146 |
} |
147 |
|
148 |
@Override |
149 |
public Set<String> getActionNames() { |
150 |
return commands; |
151 |
} |
152 |
} |