gpt4 book ai didi

Netty 4 性能下降

转载 作者:行者123 更新时间:2023-12-04 12:41:25 27 4
gpt4 key购买 nike

当我从 Netty 3 升级到 Netty 4 时,性能下降了大约 45%。

我在做性能测试时比较了 Netty 3 和 Netty 4 的线程转储。似乎 Netty 4 服务器在写操作上使用了更多时间。但是,如果我使用基于Netty 4的客户端和基于Netty 3的服务器,性能下降只有5%左右,所以我猜测原因在服务器端,但我找不到原因。

有人可以给我建议吗?

代码可以在这个网址看到:
https://code.google.com/p/nfs-rpc/source/browse/#svn%2Ftrunk%2Fnfs-rpc-netty4

最佳答案

Netty4 引入了一个新的线程模型,也许你应该调整你的代码以获得更好的性能。
以下是来自 Netty Wiki 的一些观点,而且netty4还有更多变化。

There is no well-defined thread model in 3.x although there was an attempt to fix its inconsistency in 3.5. 4.0 defines a strict thread model that helps a user write a ChannelHandler without worrying too much about thread safety.

Netty will never call a ChannelHandler's methods concurrently, unless the ChannelHandler is annotated with @Sharable. This is regardless of the type of handler methods - inbound, outbound, or life cycle event handler methods.

A user does not need to synchronize either inbound or outbound event handler methods anymore.

4.0 disallows adding a ChannelHandler more than once unless it's annotated with @Sharable.

There is always happens-before relationship between each ChannelHandler method invocations made by Netty.

A user does not need to define a volatile field to keep the state of a handler.A user can specify an EventExecutor when he or she adds a handler to a ChannelPipeline.

If specified, the handler methods of the ChannelHandler are always invoked by the specified EventExecutor.

If unspecified, the handler methods are always invoked by the EventLoop that its associated Channel is registered to.

EventExecutor and EventLoop assigned to a handler or a channel are always single-threaded.

The handler methods will always be invoked by the same thread.

If multithreaded EventExecutor or EventLoop is specified, one of the threads will be chosen first and then the chosen thread will be used until deregistration.

If two handlers in the same pipeline are assigned with different EventExecutors, they are invoked simultaneously. A user has to pay attention to thread safety if more than one handler access shared data even if the shared data is accessed only by the handlers in the same pipeline.The ChannelFutureListeners added to ChannelFuture are always invoked by the EventLoop thread assigned to the future's associated Channel.

关于Netty 4 性能下降,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19763911/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com