`
zhouwei064
  • 浏览: 27259 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类

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

阅读更多

作者解释: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

    java-gearman-service-0.6.6.zip 包,gearman分为3部分,client - server - worker,创建 java 版本的client和worker部分。 其实在gearman中,client和worker的编写不复杂,但是不同厂商提供的API是不大相同的,本...

    java-gearman-service(gearman-java-service)

    java实现gearman的job实现的jar包,包括gearman server,client和work客户端API

    gearman-java.zip_BadMagicException_Gearman java

    `org`目录可能包含了`gearman-java`库的源代码包结构,其中的类文件对应于Gearman-Java的各个组件和接口。 为了深入理解并解决"BadMagicException"问题,你需要检查以下几个方面: 1. **类文件的完整性**:确认所有...

    java-gearman-service jar

    gearman的java库有两个,一个是gearman service ,一个是gearman java,相比来说service版本更好用一些,并且网上的教程一般是用的这个版本。因此我打好了gearman service的包提供给需要的开发者使用。

    gearman-mysql-udf-0.6.tar.gz

    - **类型**:UDFs 分为整数、字符串、时间和二进制四种类型。 - **作用**:通过 UDF,MySQL 可以调用外部程序或库,比如 Gearman 插件,来执行特定任务。 3. **gearman-mysql-udf 插件**: - **功能**:该插件...

    java-gearman-service-0.6.6.jar

    java实现gearman的job实现的jar包,包括gearman server,client和work客户端API

    java-gearman-service-0.6.6.jar gearman java调度工具包

    在项目中使用这个压缩包,你需要包含`java-gearman-service依赖包`中的所有文件,确保运行时库能够正常加载和运行。这可能包括JAR文件和其他必要的资源文件。确保你的构建系统(如Maven或Gradle)正确地配置了依赖...

    gearman-1.0.2.tgz

    这种模式提高了系统的并发性和响应速度。 - **负载均衡**:通过将任务分发到多个工作器,Gearman 实现了负载均衡,有助于避免单点瓶颈,提高系统整体处理能力。 - **持久化**:虽然 Gearman 不直接支持任务的持久...

    gearman-java-0.6.jar

    实测可用

    gearman-java-0.5.jar

    实测可用

    gearman-java-0.5-sources.jar

    实测可用

    gearman-java-0.6-sources.jar

    实测可用

    gearman下载gearman下载

    `java-gearman-service-0.2.jar` 文件可能是一个特定的Java服务实现,可能包含用于启动和管理Gearman服务的类。这可能是将Gearman集成到Java应用程序中的一个方便工具,使得服务器端的部署和管理更加简单。 `...

    gearman-0.5.0.tgz(在使用)

    这种模式使得应用程序能够将耗时的操作异步化,提高系统的整体性能和响应速度。 在您提到的“gearman-0.5.0.tgz”文件中,我们可以推断这包含了 Gearman 0.5.0 版本的源代码和相关构建文件。`.tgz` 是一种常见的...

    net_gearman-master.zip

    《PHP Gearman客户端插件——深入理解net_gearman-master》 在PHP的世界里,Gearman是一个强大的分布式任务处理框架,它允许开发者将任务分发到多台服务器上执行,从而实现负载均衡和异步处理。本文将深入探讨...

    Gearman java APIs和一个小Demo

    Gearman 是一个分布式任务队列系统,它允许应用程序在需要时分发工作负载,并在有可用资源时异步处理这些...在实际项目中, Gearman 可以作为解耦组件,使得各个服务之间能独立扩展,提升整体系统的稳定性和响应速度。

    Laravel开发-laravel-gearman-rpc

    通过集成 Laravel 和 Gearman,你可以创建一个强大的异步任务处理系统,提高应用程序的响应速度和并发处理能力。理解 Gearman 的工作原理和如何在 Laravel 中使用它,对于构建高可用性和可扩展性的 Web 应用至关重要...

    gearman-0.8.0.tgz

    齿轮人提供了一系列的命令行工具,如`gearmand`(服务器)和`gearman-client`(客户端),这些工具可以帮助开发者测试和调试任务处理流程。 7. **监控与管理**: 对于生产环境,监控齿轮人的运行状态非常重要。...

    Python库 | gearman-1.3.1.tar.gz

    资源分类:Python库 所属语言:Python 资源全名:gearman-1.3.1.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059

    gearman-1.1.2

    1)软件资源列表:《gearmand-1.1.12.tar.gz》,《gearman-1.1.2.tgz》。 2)安装操作系统环境:CentOS-6.5,(其它linux操作系统请自行尝试)。 3)本软件安装步骤详解:...

Global site tag (gtag.js) - Google Analytics