1. 程式人生 > >SpringBoot Redis 訊息 效能測試簡例

SpringBoot Redis 訊息 效能測試簡例

參考: http://spring.io/guides/gs/messaging-redis/

一、運用SpringBoot2.0 ,先看Pom檔案引用

    需要引入springboot及、redis依賴

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
            <version>2.0.3.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.0.3.RELEASE</version>
        </dependency>
    

二、訊息接收端 Receiver

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.concurrent.CountDownLatch;
import org.springframework.beans.factory.annotation.Autowired;

public class Receiver {
    private static final Logger LOGGER = LoggerFactory.getLogger( Receiver.class );
    private CountDownLatch latch;
    @Autowired
    public Receiver( CountDownLatch latch ) {
        this.latch = latch;
    }
    public void receiveMessage( String message ) {
        LOGGER.info( "Received <" + message + ">" );
        latch.countDown();
    }
}

三、啟動類

這裡也可以寫一個Config類來註冊一些Redis配置

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;

import java.util.concurrent.CountDownLatch;

@SpringBootApplication
public class SpringbootRedisApplication {

	private static final Logger LOGGER = LoggerFactory.getLogger(SpringbootRedisApplication.class);

	@Bean
	MessageListenerAdapter listenerAdapter(Receiver receiver) {
		return new MessageListenerAdapter(receiver, "receiveMessage");
	}

	@Bean
	RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory,MessageListenerAdapter listenerAdapter) {
		RedisMessageListenerContainer container = new RedisMessageListenerContainer();
		container.setConnectionFactory(connectionFactory);
		container.addMessageListener(listenerAdapter, new PatternTopic("chat"));
		return container;
	}

	@Bean
	Receiver receiver(CountDownLatch latch) {
		return new Receiver(latch);
	}

	@Bean
	CountDownLatch latch() {
        //測試傳送100次訊息,所以這裡鎖為100
		return new CountDownLatch(100);
	}

	@Bean
	StringRedisTemplate template(RedisConnectionFactory connectionFactory) {
		return new StringRedisTemplate(connectionFactory);
	}

	public static void main(String[] args) throws InterruptedException {
		ApplicationContext ctx =  SpringApplication.run(SpringbootRedisApplication.class, args);
		StringRedisTemplate template = ctx.getBean(StringRedisTemplate.class);
		CountDownLatch latch = ctx.getBean(CountDownLatch.class);
		LOGGER.info("Sending message...");
		long startTime=System.currentTimeMillis();
		for(int i=0;i<100;i++){
	            template.convertAndSend( "chat","Hello Redis:"+i );
		}
		latch.await();
		long endTime=System.currentTimeMillis();
		int time = ( int ) (endTime-startTime);
		LOGGER.info( "程式執行時間:"+time+"ms" );
		System.exit(0);
	}

}

執行結果如下圖所示:傳送100次訊息大約用了0.5s,效果不太好,應該還能優化