java gearman_Gearman-java,请求和响应要花费这么多时间?(二)

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 jobs = newArrayList();

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值