如何在启动时改善Guice的性能 [英] How to improve Guice performance at startup

查看:56
本文介绍了如何在启动时改善Guice的性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我知道我的计算不是客观的,依此类推,但是无论如何,我讨厌在执行单元测试时要花很多时间:

Ok, I know my computations are not objective and so on, but anyway, I hate to wait so much time when performing my unit-tests:

我的guice swing应用程序大约需要7秒钟来初始化.这是一个简单的IRC客户端.那时,没有连接打开,我什至还没有调用任何java.io或java.net类.我试图缩小确切的错误范围,并得到Guice使用5.8秒(平均)的时间,以便使用我正在使用的2个模块(一个普通模块和一个使用FactoryModuleBuilder内置的模块)创建注射器安装在原始模块中.

My guice swing application takes about 7 seconds to initialize. It's a simple IRC client. At that moment, no connection are open, I even haven't called any java.io or java.net classes yet. I've tried to narrow down what exactly is wrong and I get that 5.8 seconds (average) are used by Guice in order to create the injector with the 2 modules I'm using (one normal module and one built with FactoryModuleBuilder, installed within the original module).

当我删除所有模块(因此基本上只完全调用Guice.createInjector()时),仍然需要3.5秒.

When I remove all modules (so basically calling only and exactly Guice.createInjector()), it still takes 3.5 seconds.

我使用的Guice版本是3.0 rc2.我的计算机肯定不是最新的,但仍不超过3年.

The version of Guice I use is the 3.0 rc2. My computer is certainly not the latest, but it is still not older than 3 years.

那么,如果可能的话,我该如何改善Guice的性能?

So how can I improve Guice's performance, if possible?

作为参考,这是我使用的主要方法,导致3.5秒.随后的通话需要0.01秒

For reference, here's the main method I'm using, causing the 3.5 seconds. Subsequent calls take 0.01 second

public static void main(String[] args) {

    long t = System.currentTimeMillis();
    Injector injector = Guice.createInjector();
    long t1 = System.currentTimeMillis();
    System.out.println(((t1 - t)) / 1000.0);
}

结果

3.578

推荐答案

  • 您无需在单元测试中使用Guice.如果您确实需要使用它,则它们可能并不是真正的单元测试(用于测试隔离中的内容),或者您没有正确使用Guice,或者两者均未使用.
  • 除非您的代码中有什么引起奇怪的提示,否则Guise绝对不应该花任何时间启动.在我的计算机(具有3.0 rc2)上,创建具有Guice.createInjector()并且没有模块的Injector大约需要100毫秒.
    • You shouldn't need to use Guice in unit tests. If you do need to use it, they probably aren't really unit tests (which test things in isolation) or you aren't using Guice correctly or both.
    • Guice definitely shouldn't be taking any number of seconds to start up unless there's something in your code causing something weird. On my machine (with 3.0 rc2) it takes a little over 100ms to create an Injector with Guice.createInjector() and no modules.
    • 这篇关于如何在启动时改善Guice的性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆