Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Resolved #241 - Slow spring context performance #242

Merged
merged 4 commits into from

3 participants

@vladimirkl

Resolved #241 - Slow spring context performance
Removed child spring context and implemented custom scope for glue code.
cucumber-glue.xml is internal now. All context configuration may be done in cucumber.xml

vladimirkl added some commits
@vladimirkl vladimirkl Resolved #241
Removed child spring context and implemented custom scope for glue code.
cucumber-glue.xml is internal now. All context configuration may be done in cucumber.xml
574afff
@vladimirkl vladimirkl Fixed bean definition overriding 43c0098
@vladimirkl vladimirkl Minor renaming e0c11e6
@vladimirkl vladimirkl Changed name for CustomScopeConfigurer bean c4f419e
@aslakhellesoy

Looks great. I'll merge this after @TeDDaN or @hutchy2570 have confirmed that this fixes their speed regression.

@hutchy2570

I can confirm this has fixed the speed issue we've been seeing (in fact it's faster than RC15). Good work!

@aslakhellesoy aslakhellesoy merged commit c4f419e into cucumber:master
@hutchy2570 hutchy2570 referenced this pull request from a commit in lgi-kitd/cucumber-jvm
@aslakhellesoy aslakhellesoy Attribution. Closes #241,#242. df6bec6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 2, 2012
  1. @vladimirkl

    Resolved #241

    vladimirkl authored
    Removed child spring context and implemented custom scope for glue code.
    cucumber-glue.xml is internal now. All context configuration may be done in cucumber.xml
  2. @vladimirkl
  3. @vladimirkl

    Minor renaming

    vladimirkl authored
  4. @vladimirkl
This page is out of date. Refresh to see the latest.
View
55 spring/src/main/java/cucumber/runtime/java/spring/GlueCodeContext.java
@@ -0,0 +1,55 @@
+package cucumber.runtime.java.spring;
+
+import java.util.HashMap;
+import java.util.Map;
+
+public class GlueCodeContext {
+ public static final GlueCodeContext INSTANCE = new GlueCodeContext();
+ private final Map<String, Object> objects = new HashMap<String, Object>();
+ private final Map<String, Runnable> callbacks = new HashMap<String, Runnable>();
+ private int counter;
+
+ private GlueCodeContext() {
+ }
+
+ public void start () {
+ cleanUp();
+ counter++;
+ }
+
+ public String getId() {
+ return "cucumber_glue_"+ counter;
+ }
+
+ public void stop() {
+ for (Runnable callback : callbacks.values()) {
+ callback.run();
+ }
+ cleanUp();
+ }
+
+ public Object get(String name){
+ return objects.get(name);
+ }
+
+ public void put(String name, Object object){
+ objects.put(name, object);
+ }
+
+ public Object remove (String name){
+ callbacks.remove(name);
+ return objects.remove(name);
+ }
+
+
+
+ private void cleanUp() {
+ objects.clear();
+ callbacks.clear();
+ }
+
+
+ public void registerDestructionCallback(String name, Runnable callback) {
+ callbacks.put(name, callback);
+ }
+}
View
41 spring/src/main/java/cucumber/runtime/java/spring/GlueCodeScope.java
@@ -0,0 +1,41 @@
+package cucumber.runtime.java.spring;
+
+import org.springframework.beans.factory.ObjectFactory;
+import org.springframework.beans.factory.config.Scope;
+
+public class GlueCodeScope implements Scope {
+ public static final String NAME = "cucumber-glue";
+
+ private final GlueCodeContext context = GlueCodeContext.INSTANCE;
+
+ @Override
+ public Object get(String name, ObjectFactory<?> objectFactory) {
+ Object obj = context.get(name);
+ if (obj == null) {
+ obj = objectFactory.getObject();
+ context.put(name, obj);
+ }
+
+ return obj;
+ }
+
+ @Override
+ public Object remove(String name) {
+ return context.remove(name);
+ }
+
+ @Override
+ public void registerDestructionCallback(String name, Runnable callback) {
+ context.registerDestructionCallback(name, callback);
+ }
+
+ @Override
+ public Object resolveContextualObject(String key) {
+ return null;
+ }
+
+ @Override
+ public String getConversationId() {
+ return context.getId();
+ }
+}
View
43 spring/src/main/java/cucumber/runtime/java/spring/SpringFactory.java
@@ -8,8 +8,8 @@
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
-import java.util.ArrayList;
import java.util.Collection;
+import java.util.HashSet;
/**
* Spring based implementation of ObjectFactory.
@@ -33,50 +33,47 @@
private static AbstractApplicationContext applicationContext;
- private ClassPathXmlApplicationContext stepContext;
- private final Collection<Class<?>> stepClasses = new ArrayList<Class<?>>();
+ private final Collection<Class<?>> stepClasses = new HashSet<Class<?>>();
public SpringFactory() {
}
static {
- applicationContext = new ClassPathXmlApplicationContext(new String[]{"cucumber.xml"});
+ applicationContext = new ClassPathXmlApplicationContext(
+ "cucumber/runtime/java/spring/cucumber-glue.xml",
+ "cucumber.xml");
applicationContext.registerShutdownHook();
}
@Override
- public void addClass(final Class<?> clazz) {
- stepClasses.add(clazz);
- }
+ public void addClass(final Class<?> stepClass) {
+ if (!stepClasses.contains(stepClass)) {
+ stepClasses.add(stepClass);
- @Override
- public void createInstances() {
- createNewStepContext();
- populateStepContext();
- }
+ BeanDefinitionRegistry registry = (BeanDefinitionRegistry) applicationContext.getAutowireCapableBeanFactory();
+ registry.registerBeanDefinition(stepClass.getName(),
+ BeanDefinitionBuilder.genericBeanDefinition(stepClass)
+ .setScope(GlueCodeScope.NAME)
+ .getBeanDefinition());
- private void createNewStepContext() {
- stepContext = new ClassPathXmlApplicationContext(new String[]{"classpath*:cucumber-glue.xml"},
- applicationContext);
+ }
}
- private void populateStepContext() {
- BeanDefinitionRegistry registry = (BeanDefinitionRegistry) stepContext.getAutowireCapableBeanFactory();
- for (Class<?> stepClass : stepClasses) {
- registry.registerBeanDefinition(stepClass.getName(),
- BeanDefinitionBuilder.genericBeanDefinition(stepClass).getBeanDefinition());
- }
+ @Override
+ public void createInstances() {
+ GlueCodeContext.INSTANCE.start();
}
@Override
public void disposeInstances() {
- stepContext.close();
+ GlueCodeContext.INSTANCE.stop();
}
+ @SuppressWarnings("unchecked")
@Override
public <T> T getInstance(final Class<T> type) {
try {
- return stepContext.getBean(type);
+ return applicationContext.getBean(type);
} catch (NoSuchBeanDefinitionException exception) {
throw new CucumberException(exception.getMessage(), exception);
}
View
11 spring/src/main/resources/cucumber-glue.xml → ...ber/runtime/java/spring/cucumber-glue.xml
@@ -3,5 +3,16 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
+ <bean id="glueCodeScope" class="cucumber.runtime.java.spring.GlueCodeScope">
+ </bean>
+
+ <bean id="glueCodeScopeConfigurer" class="org.springframework.beans.factory.config.CustomScopeConfigurer">
+ <property name="scopes">
+ <map>
+ <entry key="cucumber-glue" value-ref="glueCodeScope"/>
+ </map>
+ </property>
+ </bean>
<context:annotation-config/>
+
</beans>
View
9 spring/src/test/resources/applicationContext.xml
@@ -5,4 +5,13 @@
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean id="bellyBean" class="cucumber.runtime.java.spring.BellyBean"/>
+
+ <bean id="placeholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
+ <property name="properties">
+ <props>
+ <prop key="cukes.test.property">property value</prop>
+ </props>
+ </property>
+ </bean>
+
</beans>
View
13 spring/src/test/resources/cucumber-glue.xml
@@ -1,13 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
-
- <bean id="placeholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
- <property name="properties">
- <props>
- <prop key="cukes.test.property">property value</prop>
- </props>
- </property>
- </bean>
-</beans>
Something went wrong with that request. Please try again.