Skip to content

Commit 8796c22

Browse files
committed
improves some performance related to testng-team#772
1 parent 6768e00 commit 8796c22

File tree

3 files changed

+64
-30
lines changed

3 files changed

+64
-30
lines changed

src/main/java/org/testng/TestClass.java

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -120,61 +120,75 @@ public void addInstance(Object instance) {
120120
}
121121

122122
private void initMethods() {
123+
long timeToCreateMethods = System.currentTimeMillis();
123124
ITestNGMethod[] methods = m_testMethodFinder.getTestMethods(m_testClass, m_xmlTest);
124125
m_testMethods = createTestMethods(methods);
125-
126+
System.out.println("Time to create test methods: " + (System.currentTimeMillis() - timeToCreateMethods) + " ms.");
127+
long time = System.currentTimeMillis();
128+
// moving these out of the loops reduces creation time of the methods from 196 seconds to 2 seconds under extreme circumstances
129+
ITestNGMethod[] beforeSuiteMethods = m_testMethodFinder.getBeforeSuiteMethods(m_testClass);
130+
ITestNGMethod[] afterSuiteMethods = m_testMethodFinder.getAfterSuiteMethods(m_testClass);
131+
ITestNGMethod[] beforeTestConfMethods = m_testMethodFinder.getBeforeTestConfigurationMethods(m_testClass);
132+
ITestNGMethod[] afterTestConfMethods = m_testMethodFinder.getAfterTestConfigurationMethods(m_testClass);
133+
ITestNGMethod[] beforeClassMethods = m_testMethodFinder.getBeforeClassMethods(m_testClass);
134+
ITestNGMethod[] afterClassMethods = m_testMethodFinder.getAfterClassMethods(m_testClass);
135+
ITestNGMethod[] beforeGroupsMethods = m_testMethodFinder.getBeforeGroupsConfigurationMethods(m_testClass);
136+
ITestNGMethod[] afterGroupsMethods = m_testMethodFinder.getAfterGroupsConfigurationMethods(m_testClass);
137+
ITestNGMethod[] beforeTestMethods = m_testMethodFinder.getBeforeTestMethods(m_testClass);
138+
ITestNGMethod[] afterTestMethods = m_testMethodFinder.getAfterTestMethods(m_testClass);
126139
for (Object instance : m_iClass.getInstances(false)) {
127140
m_beforeSuiteMethods = ConfigurationMethod
128-
.createSuiteConfigurationMethods(m_testMethodFinder.getBeforeSuiteMethods(m_testClass),
141+
.createSuiteConfigurationMethods(beforeSuiteMethods,
129142
m_annotationFinder,
130143
true,
131144
instance);
132145
m_afterSuiteMethods = ConfigurationMethod
133-
.createSuiteConfigurationMethods(m_testMethodFinder.getAfterSuiteMethods(m_testClass),
146+
.createSuiteConfigurationMethods(afterSuiteMethods,
134147
m_annotationFinder,
135148
false,
136149
instance);
137150
m_beforeTestConfMethods = ConfigurationMethod
138-
.createTestConfigurationMethods(m_testMethodFinder.getBeforeTestConfigurationMethods(m_testClass),
151+
.createTestConfigurationMethods(beforeTestConfMethods,
139152
m_annotationFinder,
140153
true,
141154
instance);
142155
m_afterTestConfMethods = ConfigurationMethod
143-
.createTestConfigurationMethods(m_testMethodFinder.getAfterTestConfigurationMethods(m_testClass),
156+
.createTestConfigurationMethods(afterTestConfMethods,
144157
m_annotationFinder,
145158
false,
146159
instance);
147160
m_beforeClassMethods = ConfigurationMethod
148-
.createClassConfigurationMethods(m_testMethodFinder.getBeforeClassMethods(m_testClass),
161+
.createClassConfigurationMethods(beforeClassMethods,
149162
m_annotationFinder,
150163
true,
151164
instance);
152165
m_afterClassMethods = ConfigurationMethod
153-
.createClassConfigurationMethods(m_testMethodFinder.getAfterClassMethods(m_testClass),
166+
.createClassConfigurationMethods(afterClassMethods,
154167
m_annotationFinder,
155168
false,
156169
instance);
157170
m_beforeGroupsMethods = ConfigurationMethod
158-
.createBeforeConfigurationMethods(m_testMethodFinder.getBeforeGroupsConfigurationMethods(m_testClass),
171+
.createBeforeConfigurationMethods(beforeGroupsMethods,
159172
m_annotationFinder,
160173
true,
161174
instance);
162175
m_afterGroupsMethods = ConfigurationMethod
163-
.createAfterConfigurationMethods(m_testMethodFinder.getAfterGroupsConfigurationMethods(m_testClass),
176+
.createAfterConfigurationMethods(afterGroupsMethods,
164177
m_annotationFinder,
165178
false,
166179
instance);
167180
m_beforeTestMethods = ConfigurationMethod
168-
.createTestMethodConfigurationMethods(m_testMethodFinder.getBeforeTestMethods(m_testClass),
181+
.createTestMethodConfigurationMethods(beforeTestMethods,
169182
m_annotationFinder,
170183
true,
171184
instance);
172185
m_afterTestMethods = ConfigurationMethod
173-
.createTestMethodConfigurationMethods(m_testMethodFinder.getAfterTestMethods(m_testClass),
186+
.createTestMethodConfigurationMethods(afterTestMethods,
174187
m_annotationFinder,
175188
false,
176189
instance);
177190
}
191+
System.out.println("time taken to create everything else " + (System.currentTimeMillis() - time) + " ms.");
178192
}
179193

180194
/**

src/main/java/org/testng/internal/Graph.java

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public void topologicalSort() {
115115
for (Node<T> n : getNodes()) {
116116
if (! isIndependent(n.getObject())) {
117117
ppp("ADDING FOR SORT: " + n.getObject());
118-
nodes2.add(n.clone());
118+
nodes2.add(n);
119119
}
120120
else {
121121
ppp("SKIPPING INDEPENDENT NODE " + n);
@@ -287,15 +287,17 @@ public Map<T, T> getPredecessors() {
287287
public boolean removePredecessor(T o) {
288288
boolean result = false;
289289

290-
T pred = m_predecessors.get(o);
291-
if (null != pred) {
292-
result = null != m_predecessors.remove(o);
293-
if (result) {
294-
ppp(" REMOVED PRED " + o + " FROM NODE " + m_object);
295-
}
296-
else {
297-
ppp(" FAILED TO REMOVE PRED " + o + " FROM NODE " + m_object);
298-
}
290+
if (!m_predecessors.isEmpty()) {
291+
T pred = m_predecessors.get(o);
292+
if (null != pred) {
293+
result = null != m_predecessors.remove(o);
294+
if (result) {
295+
ppp(" REMOVED PRED " + o + " FROM NODE " + m_object);
296+
}
297+
else {
298+
ppp(" FAILED TO REMOVE PRED " + o + " FROM NODE " + m_object);
299+
}
300+
}
299301
}
300302

301303
return result;

src/main/java/org/testng/internal/annotations/JDK15AnnotationFinder.java

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
import org.testng.annotations.TestInstance;
4040
import org.testng.internal.collections.Pair;
4141

42+
import com.google.common.collect.Maps;
43+
4244
/**
4345
* This class implements IAnnotationFinder with JDK5 annotations
4446
*
@@ -97,16 +99,32 @@ private <A extends Annotation> A findAnnotationInSuperClasses(Class<?> cls, Clas
9799
return null;
98100
}
99101

100-
@Override
102+
private Map<Pair,IAnnotation> annotationCache = Maps.newConcurrentMap();
103+
private final static IAnnotation NULL_CACHE_ENTRY = new IAnnotation() {};
104+
@Override
101105
public <A extends IAnnotation> A findAnnotation(Method m, Class<A> annotationClass) {
102-
final Class<? extends Annotation> a = m_annotationMap.get(annotationClass);
103-
if (a == null) {
104-
throw new IllegalArgumentException("Java @Annotation class for '"
105-
+ annotationClass + "' not found.");
106-
}
107-
Annotation annotation = m.getAnnotation(a);
108-
return findAnnotation(m.getDeclaringClass(), annotation, annotationClass, null, null, m,
109-
new Pair<>(annotation, m));
106+
Pair<Method,Class<A>> cacheKey = new Pair<>(m,annotationClass);
107+
IAnnotation cachedValue = annotationCache.get(cacheKey);
108+
if (cachedValue == null) {
109+
final Class<? extends Annotation> a = m_annotationMap.get(annotationClass);
110+
if (a == null) {
111+
throw new IllegalArgumentException("Java @Annotation class for '"
112+
+ annotationClass + "' not found.");
113+
}
114+
Annotation annotation = m.getAnnotation(a);
115+
cachedValue = findAnnotation(m.getDeclaringClass(), annotation, annotationClass, null, null, m,
116+
new Pair<>(annotation, m));
117+
if (cachedValue == null) {
118+
cachedValue = NULL_CACHE_ENTRY;
119+
}
120+
annotationCache.put(cacheKey, cachedValue);
121+
}
122+
123+
if (cachedValue == NULL_CACHE_ENTRY) {
124+
return null;
125+
} else {
126+
return (A) cachedValue;
127+
}
110128
}
111129

112130
@Override

0 commit comments

Comments
 (0)