原文:http://fusesource.com/docs/broker/5.4/tuning/GenTuning-Protocols.html#GenTuning-Protocols-Compress
标签:ActiveMQ 性能优化
Overview:协议优化可以在不同的协议层,如下所示
TCP transport(TCP传输):一般而言,通过增加TCP层的Buffer大小是可以改善TCP层的性能。
- Socket buffer size -默认的TCP Socket buffer的大小是64KB。这个设计只是适合最初的TCP协议,这个大小对于当初的猫(modern)网络而言是最合适的。下面的经验可以用于估算出合适的TCP socket buffer的大小。
Buffer size = BandWidth(带宽) X Round-Trip-Time(往返时延)
Round-Trip-Time指的是从TCP发送一个包到接收到这个包的确认的时长(Ping的时间),典型的做法是加倍Socket buffer的大小到128K。
如例:
tcp://hostA:61617?socketBufferSize=131072
更多请参照Wikipedia的文章 Network Improvement.
- I/O buffer size—I/O buffer是用来缓存TCP和它上层协议(就像OpenWire)之间的数据的。默认的I/O buffer的大小是8K,你可以加倍这个大小去获得更好的性能。如例:
tcp://hostA:61617?ioBufferSize=16384
OpenWire protocol:OpenWire协议开放了几个选项进行来调整性能,如表格1.1所示
Table 1.1. OpenWire Parameters Affecting Performance
Parameter | Default | Description |
---|---|---|
cacheEnabled | true | Specifies whether to cache commonly repeated values, in order to optimize marshaling. |
cacheSize | 1024 | The number of values to cache. Increase this value to improve performance of marshaling. |
tcpNoDelayEnabled | false | When true , disable the Nagles algorithm. The Nagles algorithm was devised to avoid sending tiny TCP packets containing only one or two bytes of data; for example, when TCP is used with the Telnet protocol. If you disable the Nagles algorithm, packets can be sent more promptly, but there is a risk that the number of very small packets will increase. |
tightEncodingEnabled | true | When true , implement a more compact encoding of basic data types. This results in smaller messages and better network performance, but comes at a cost of more calculation and demands made on CPU time. A trade off is therefore required: you need to determine whether the network or the CPU is the main factor that limits performance. |
设置任何其中一项都需要在URI中增加wireFormat
前缀,例如加倍OperWire的缓存(cache),你可以在URI中指定缓存的大小,就像如下所示
tcp://hostA:61617?wireFormat.cacheSize=2048
Enabling compression:如果应用发送大的消息,并且你知道网络非常慢的时候,是应该考虑在网络上传输上进行压缩。当允许压缩的时候,所有在JMS的消息体(不包括消息头)都会在传输之前被压缩。这样会生成小的消息和更好的网络性能。但是,不好的是,增加了CPU的压力。
进行压缩,需要设置ActiveMQConnectionFactory
的useCompression
选项。举例,如果在Client进行JMS Connection初始化的时候设置压缩,需要插入如下的代码。
// Java
...
// Create the connection.
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(user, password, url);
connectionFactory.setUseCompression(true);
Connection connection = connectionFactory.createConnection();
connection.start();
// Create the connection.
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(user, password, url);
connectionFactory.setUseCompression(true);
Connection connection = connectionFactory.createConnection();
connection.start();
另外,你还可以在URI中设置jms.useCompression
来进行压缩,如下所示
tcp://hostA:61617?jms.useCompression=true