`
id.alex
  • 浏览: 21934 次
社区版块
存档分类
最新评论

Hessian, CXF, Spring httpinvoke 对比

    博客分类:
  • Java
阅读更多
做了一个 Hessian, CXF, Spring httpinvoke 速度对比

时间消耗  cxf > spring httpinvoke > hessian

顺序调用1W次所耗时间

hessian2652-2922
spring httpinvoke4080-4949
cxf9732-10432


并发为10, 调用1W次所耗时间
hessian1625-1753
spring httpinvoke3165-3338
cxf5709-5863


当然, 都知道 cxf 和 hessian 实现以及应用场景不太一样, 但差这么多还是很意外的..
另外在并发测试时, spring httpinvoke cpu 消耗明显更高.

=============================================================
测试代码:

服务端

public class Param implements Serializable {
	private static final long serialVersionUID = 7414597783500374225L;
	private Integer i;
	private String s;
	private Long l;
	private List<Param> list = new ArrayList<Param>();
	private boolean b;
......
}

public class Result implements Serializable {
	private static final long serialVersionUID = 2729153186117404170L;
	private Integer i;
	private String s;
	private Long l;
	private List<Result> list;
	private boolean b;
......
}


@WebService
public interface TestService {
	Result method(Param p);
}


@Service
@WebService(endpointInterface = "org.alex.test.webservice.TestService")
public class TestServiceImpl implements TestService {

	@Override
	public Result method(Param p) {
		Result r = new Result();
		BeanUtils.copyProperties(p, r);
		return r;
	}

}



	<!-- cxf -->
	<import resource="classpath:META-INF/cxf/cxf.xml" />
	<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
	<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
	<jaxws:endpoint id="testCxfService" implementor="org.alex.test.webservice.TestServiceImpl" address="/cxf" />


	<bean id="testServiceImpl" class="org.alex.test.webservice.TestServiceImpl" />

	<!-- hessian -->
	<bean name="/hes" class="org.springframework.remoting.caucho.HessianServiceExporter">
		<property name="service" ref="testServiceImpl" />
		<property name="serviceInterface" value="org.alex.test.webservice.TestService" />
	</bean>

	<!-- spring http invoke -->
	<bean name="/spr" class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">
		<property name="service" ref="testServiceImpl" />
		<property name="serviceInterface" value="org.alex.test.webservice.TestService" />
	</bean>


客户端

public class Client {

	static String CXF = "cxfService";
	static String HESSIAN = "hesService";
	static String SPRING = "sprService";

	public static void main(String[] args) {
		ApplicationContext ac = new ClassPathXmlApplicationContext("client.xml");

		TestService service = (TestService) ac.getBean(SPRING);

		Param p = new Param();
		p.setI(100);
		p.setB(true);
		p.setL(1000L);
		p.setS("123456789123456789abcdefghijklmnopqrstuvwxyz!@#$%^&*()_\r\n\\\t");
		p.getList().add(new Param());
		p.getList().add(new Param());
		p.getList().add(new Param());
		service.method(p);

		long now = System.currentTimeMillis();
		for (int i = 0; i < 10000; i++) {
			service.method(p);
		}
		System.out.println(System.currentTimeMillis() - now);
	}
}


public class MultiThreadClient {

	static String CXF = "cxfService";
	static String HESSIAN = "hesService";
	static String SPRING = "sprService";

	public static void main(String[] args) {
		ApplicationContext ac = new ClassPathXmlApplicationContext("client.xml");

		TestService service = (TestService) ac.getBean(HESSIAN);

		Param p = new Param();
		p.setI(100);
		p.setB(true);
		p.setL(1000L);
		p.setS("123456789123456789abcdefghijklmnopqrstuvwxyz!@#$%^&*()_\r\n\\\t");
		p.getList().add(new Param());
		p.getList().add(new Param());
		p.getList().add(new Param());
		service.method(p);

		ExecutorService exe = Executors.newFixedThreadPool(10);
		Task task = new Task(service, p);

		long now = System.currentTimeMillis();
		for (int i = 0; i < 10000; i++) {
			exe.submit(task);
		}
		exe.shutdown();
		while (!exe.isTerminated()) {
		}
		System.out.println(System.currentTimeMillis() - now);

	}

	private static class Task implements Runnable {

		TestService service;
		Param p;

		public Task(TestService service, Param p) {
			this.service = service;
			this.p = p;
		}

		@Override
		public void run() {
			service.method(p);
		}
	}
}



	<jaxws:client id="cxfService" serviceClass="org.alex.test.webservice.TestService" address="http://localhost:8080/webservice/cxf/cxf?wsdl" />

	<bean id="hesService" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
		<property name="serviceUrl" value="http://localhost:8080/webservice/app/hes" />
		<property name="serviceInterface" value="org.alex.test.webservice.TestService" />
	</bean>

	<bean id="sprService" class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">
		<property name="serviceUrl" value="http://localhost:8080/webservice/app/spr" />
		<property name="serviceInterface" value="org.alex.test.webservice.TestService" />
	</bean>



分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics