测试环境:gearman-java-0.5.jar MyEclipse 8.5 (具体测试工程附后similar)
问题描述:在gearman-java-0.5.jar中,通过org.gearman.client.GearmanClient对象submit方法提交GearmanJob,随后通过GearmanJob对象get方法获取处理结果。在测试环境中,平均每次请求响应时间约为270+ms,响应时间过长。请问现在通过何种方法能够减小每次缩短响应时间?
============================================================
1.未删除"Thread.sleep(200);",采用非批处理
============================================================
/*测试核心代码*/
long start = System.currentTimeMillis();
for (int i = 0; i < 1000; i++ ) {
GearmanJob gearmanJob = GearmanJobImpl.createJob("finalfinger", fingerInput.toByteArray(), null);
gearmanFactory.getInstance().submit(gearmanJob);
GearmanJobResult res = gearmanJob.get();
}
long time = System.currentTimeMillis() -start;
System.out.println("TIME: "+time/1000);
/*原始测试结果*/
[2012-12-20 14:23:28,019] [main] INFO [AbstractApplicationContext.java] - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@94884d: startup date [Thu Dec 20 14:23:28 CST 2012]; root of context hierarchy
[2012-12-20 14:23:28,059] [main] INFO [XmlBeanDefinitionReader.java] - Loading XML bean definitions from class path resource [applicationContext.xml]
[2012-12-20 14:23:28,339] [main] INFO [PropertiesLoaderSupport.java] - Loading properties file from class path resource [jdbc.properties]
[2012-12-20 14:23:28,379] [main] INFO [DefaultListableBeanFactory.java] - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@a45536: defining beans [pictureFeatureServiceImpl,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalAsyncAnnotationProcessor,org.springframework.context.annotation.internalScheduledAnnotationProcessor,org.springframework.beans.factory.config.PropertyPlaceholderConfigurer#0,dataSource,sqlSessionFactory,resourceMapper,threadPool,writeTask,readTask,org.springframework.context.annotation.ConfigurationClassPostProcessor$ImportAwareBeanPostProcessor#0]; root of factory hierarchy
[2012-12-20 14:23:35,950] [main] INFO [ExecutorConfigurationSupport.java] - Initializing ExecutorService 'threadPool'
AVERAGE TIME: 277
extractFinger:277999
============================================================
2.未删除"Thread.sleep(200);",采用批处理
============================================================
/*测试核心代码*/
ArrayList<GearmanJob> jobs = new ArrayList<GearmanJob>();
long start = System.currentTimeMillis();
for (int i = 0; i < 1000; i++ ) {
GearmanJob gearmanJob = GearmanJobImpl.createJob("finalfinger", fingerInput.toByteArray(), null);
gearmanFactory.getInstance().submit(gearmanJob);
jobs.add(gearmanJob);
}
for (GearmanJob currentJob : jobs) {
GearmanJobResult res = currentJob.get();
}
long time = System.currentTimeMillis() - start;
System.out.println("AVERAGE TIME: " + time / 1000);
/*原始测试结果*/
[2012-12-20 14:19:42,119] [main] INFO [AbstractApplicationContext.java] - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@2a6f16: startup date [Thu Dec 20 14:19:42 CST 2012]; root of context hierarchy
[2012-12-20 14:19:42,179] [main] INFO [XmlBeanDefinitionReader.java] - Loading XML bean definitions from class path resource [applicationContext.xml]
[2012-12-20 14:19:42,468] [main] INFO [PropertiesLoaderSupport.java] - Loading properties file from class path resource [jdbc.properties]
[2012-12-20 14:19:42,515] [main] INFO [DefaultListableBeanFactory.java] - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@186f247: defining beans [pictureFeatureServiceImpl,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalAsyncAnnotationProcessor,org.springframework.context.annotation.internalScheduledAnnotationProcessor,org.springframework.beans.factory.config.PropertyPlaceholderConfigurer#0,dataSource,sqlSessionFactory,resourceMapper,threadPool,writeTask,readTask,org.springframework.context.annotation.ConfigurationClassPostProcessor$ImportAwareBeanPostProcessor#0]; root of factory hierarchy
[2012-12-20 14:19:48,575] [main] INFO [ExecutorConfigurationSupport.java] - Initializing ExecutorService 'threadPool'
AVERAGE TIME: 78
extractFinger:78862
============================================================
3.删除"Thread.sleep(200);",采用非批处理(重新编译后的jar另附)
============================================================
/*测试核心代码*/
long start = System.currentTimeMillis();
for (int i = 0; i < 1000; i++ ) {
GearmanJob gearmanJob = GearmanJobImpl.createJob("finalfinger", fingerInput.toByteArray(), null);
gearmanFactory.getInstance().submit(gearmanJob);
GearmanJobResult res = gearmanJob.get();
}
long time = System.currentTimeMillis() -start;
System.out.println("TIME: "+time/1000);
/*原始测试结果*/
[2012-12-20 18:55:19,759] [main] INFO [AbstractApplicationContext.java] - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@94884d: startup date [Thu Dec 20 18:55:19 CST 2012]; root of context hierarchy
[2012-12-20 18:55:19,806] [main] INFO [XmlBeanDefinitionReader.java] - Loading XML bean definitions from class path resource [applicationContext.xml]
[2012-12-20 18:55:20,102] [main] INFO [PropertiesLoaderSupport.java] - Loading properties file from class path resource [jdbc.properties]
[2012-12-20 18:55:20,149] [main] INFO [DefaultListableBeanFactory.java] - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@186f247: defining beans [pictureFeatureServiceImpl,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalAsyncAnnotationProcessor,org.springframework.context.annotation.internalScheduledAnnotationProcessor,org.springframework.beans.factory.config.PropertyPlaceholderConfigurer#0,dataSource,sqlSessionFactory,resourceMapper,threadPool,writeTask,readTask,org.springframework.context.annotation.ConfigurationClassPostProcessor$ImportAwareBeanPostProcessor#0]; root of factory hierarchy
[2012-12-20 18:55:26,157] [main] INFO [ExecutorConfigurationSupport.java] - Initializing ExecutorService 'threadPool'
AVERAGE TIME: 98
extractFinger:98287 98287/1=98287
============================================================
4.删除"Thread.sleep(200);",采用批处理(重新编译后的jar另附)
============================================================
/*测试核心代码*/
ArrayList<GearmanJob> jobs = new ArrayList<GearmanJob>();
long start = System.currentTimeMillis();
for (int i = 0; i < 1000; i++ ) {
GearmanJob gearmanJob = GearmanJobImpl.createJob("finalfinger", fingerInput.toByteArray(), null);
gearmanFactory.getInstance().submit(gearmanJob);
jobs.add(gearmanJob);
}
for (GearmanJob currentJob : jobs) {
GearmanJobResult res = currentJob.get();
}
long time = System.currentTimeMillis() - start;
System.out.println("AVERAGE TIME: " + time / 1000);
/*原始测试结果*/
[2012-12-20 19:04:56,729] [main] INFO [AbstractApplicationContext.java] - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1e3118a: startup date [Thu Dec 20 19:04:56 CST 2012]; root of context hierarchy
[2012-12-20 19:04:56,789] [main] INFO [XmlBeanDefinitionReader.java] - Loading XML bean definitions from class path resource [applicationContext.xml]
[2012-12-20 19:04:57,159] [main] INFO [PropertiesLoaderSupport.java] - Loading properties file from class path resource [jdbc.properties]
[2012-12-20 19:04:57,209] [main] INFO [DefaultListableBeanFactory.java] - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@a45536: defining beans [pictureFeatureServiceImpl,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalAsyncAnnotationProcessor,org.springframework.context.annotation.internalScheduledAnnotationProcessor,org.springframework.beans.factory.config.PropertyPlaceholderConfigurer#0,dataSource,sqlSessionFactory,resourceMapper,threadPool,writeTask,readTask,org.springframework.context.annotation.ConfigurationClassPostProcessor$ImportAwareBeanPostProcessor#0]; root of factory hierarchy
[2012-12-20 19:05:03,421] [main] INFO [ExecutorConfigurationSupport.java] - Initializing ExecutorService 'threadPool'
AVERAGE TIME: 56
extractFinger:56122 56122/1=56122
NOTE:
1.在submit和get之间存在隐性的初始化过程,如下逻辑,初始化过程会重复1000次;如果采用。
For (...;i=1000;){
Submit();
Get();
}
2.采用第二种方式——批处理,只会初始化一次,避免多次初始化过程。
3.采用第三种方式,节省了多次调用sleep时间。
4.与作者沟通后,他给出代码中加入sleep理由是:采用了一种黑客手段,防止恶意的请求攻击导致服务器负载过重而崩溃。