CountDownLatchアナログマルチユーザ同時アクセス

1952 ワード

需要:プロジェクト導入のkafkaが同時にどれだけの同時要求を処理できるかをテストする
コード:まずユーザースレッドクラスを作成し、runメソッドでテスト内容を記述します.ここではssmプロジェクトなのでstructsのactionクラスにtestメソッドを直接書きます.
package com.besto.authbilling.po;

import java.util.concurrent.CountDownLatch;

import org.apache.log4j.Logger;


import cn.com.besto.util.HttpClientUtil;



public class UserDemo implements Runnable{
	private String url;
	private static Logger logger = Logger.getLogger(UserDemo.class); 
	private static CountDownLatch begin;
	
	public UserDemo(String url, CountDownLatch begin) {
		this.url = url;
		UserDemo.begin = begin;
	}

	
	public String getUrl() {
		return url;
	}


	public void setUrl(String url) {
		this.url = url;
	}


	public CountDownLatch getBegin() {
		return begin;
	}


	public void setBegin(CountDownLatch begin) {
		UserDemo.begin = begin;
	}


	@Override
	public void run() {
		try {
			logger.debug("  "+Thread.currentThread().getName()+"     ");
			begin.await();//      
			logger.debug("  "+Thread.currentThread().getName()+"    ");
			String ret=HttpClientUtil.getHttpClientInfo(this.getUrl());//          
			logger.debug("  "+Thread.currentThread().getName()+"    ");
			logger.debug("  "+Thread.currentThread().getName()+"     "+ret);
		} catch (InterruptedException e) {
			logger.debug("    :"+e.getMessage());
		}
	}
	
}

public String test() throws Exception{
		//...       
		CountDownLatch begin = new CountDownLatch(1);
		for(int i=0;i<600;i++){
			String str=AESUtil.encrypt(params1+r.nextLong()+params2);
			logger.debug("      >>>>>>>"+str);
			UserDemo user=new UserDemo(str1+"&userid="+list.get(i).toString()+str2+"&params="+str,begin);//     
			Thread test=new Thread(user, "user"+list.get(i).toString());//    
			test.start();
		}
		begin.countDown();//      
		return null;
	}