作者解释:Eric Lambert
引用自:http://www.mentby.com/Group/gearman/why-one-requestresponse-cost-too-much-time.html
Hi Bruce:
I think I am able to reproduce your issue. Using release 0.04 of gearman-java i tried to replicate your setup. I have a worker which simple generates a results with value HelloWorld (see below)
public GearmanJobResult executeFunction() {
byte [] helloWorldBytes = "Hello World".getBytes();
GearmanJobResult gjr = newGearmanJobResultImpl(this.jobHandle, true, helloWorldBytes, new byte[0],new byte[0], 0, 0);
return gjr;
}
Then, I created a worker which submitted a HelloWorld job and thenretrieved the result. It did this 1000 times and kept track of how longit took to submit the job and retrieve the result (see snippet below)
for (int i = 0; i < 1000; i ) {
GearmanJob job =GearmanJobImpl.createJob(function, data, uniqueId);
long start = System.currentTimeMillis();
client.submit(job);
res = job.get();
long time = System.currentTimeMillis() -start;
System.out.println("TIME: " time); =0
}
and sure enough, when I run it, the average time is around 200milliseconds.
Is this similar to what you are doing? If so, there are a couple of issues here. 1) Some idiot (and I can say that because that idiot was me:-) ) put a sleep in the job.get() implementation. When I remove the sleep from the get method and rerun the test, the average drops from 200milliseconds to 1 millisecond (which is not as fast as some of the other implementations, but is significantly better then 200 milliseconds). I am not really sure why I put the sleep into the get call, perhaps there was a good reason, or perhaps not (its been a while :-) ).
The second issue is this pattern of submit() followed immediately by get job.get() was not really how I would expect the client to be used.Instead, if you are going to be submitting a large number of jobs for which you need results, I would first submit all (or some large chunk )of jobs and then after the jobs have been submitted iterate through the jobs and retrieve their results. (some thing like the following)
public void runBatch() {
String function =HelloWorldFunction.class.getCanonicalName();
String uniqueId = null;
byte[] data = new byte [0];
GearmanJobResult res = null;
try {
ArrayList<GearmanJob> jobs = newArrayList<GearmanJob>();
long start = System.currentTimeMillis();
for (int i = 0; i < 1000; i ) {
GearmanJob job =GearmanJobImpl.createJob(function, data, uniqueId);
client.submit(job);
jobs.add(job);
}
for (GearmanJob currentJob : jobs) {
res = currentJob.get();
}
long time = System.currentTimeMillis() - start;
System.out.println("AVERAGE TIME: " time / 1000);
} catch (Exception e) {
e.printStackTrace();
}
=09
}
I have found that when I use this "batch" approach, the average is about1 millisecond even with the sleep still in the job.get() method.
So for my part, if I can't think of a good reason for why I have thesleep in the get method(), then I will remove it (who knows it may evenbe removed by the time you get up). And for your part, if you are indeedperforming submit followed by get as opposed to batching your submits,you may want to consider batching your submits.
Also, I want to note that I have not been giving the gearman-javaproject the love and attention it deserves and I don't really seeingthat change in the near future. So you may want to consider usinggearman-java-service if you can. Isaiah (the maintainer ofgearman-java-service) is really responsive and his approach and API iscleaner in my opinion :-).
Eric
相关推荐
java-gearman-service-0.6.6.zip 包,gearman分为3部分,client - server - worker,创建 java 版本的client和worker部分。 其实在gearman中,client和worker的编写不复杂,但是不同厂商提供的API是不大相同的,本...
java实现gearman的job实现的jar包,包括gearman server,client和work客户端API
gearman-java-0.03,这是一个有关gearman-java-0.03相关的java源代码
gearman的java库有两个,一个是gearman service ,一个是gearman java,相比来说service版本更好用一些,并且网上的教程一般是用的这个版本。因此我打好了gearman service的包提供给需要的开发者使用。
java实现gearman的job实现的jar包,包括gearman server,client和work客户端API
gearman java 0.6.6版本 java-gearman-service-0.6.6.jar
Gearman 的 MySQL 插件
实测可用
实测可用
实测可用
实测可用
gearman-1.0.2.tgz gearman-1.0.2.tgz
Laravel开发-laravel-gearman-rpc Laravel/Lumen Gearman RPC。基于https://github.com/mhlavc/gearman
gearman-0.8.0.tgz
资源分类:Python库 所属语言:Python 资源全名:gearman-1.3.1.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
gearman-0.5.0.tgz(在使用)
php的gearman客户端插件 net_gearman-master.zip
1)软件资源列表:《gearmand-1.1.12.tar.gz》,《gearman-1.1.2.tgz》。 2)安装操作系统环境:CentOS-6.5,(其它linux操作系统请自行尝试)。 3)本软件安装步骤详解:...
Gearman java API和一个小Demo
Gearman-Go 是 Gearman-API 的 Go 语言封装版本,示例代码:Worker:// Limit number of concurrent jobs execution. // Use worker.Unlimited (0) if you want no limitation. w := worker.New...