Gearman框架的压力测试

 

1.模拟简单的压力测试环境:

Client10s提交一个job

Worker端处理一个job需要30s的时间

Client端一直提交job,当大量job堆积时对Gearman进行压力测试

2.Gearman-Java压力测试demo

client端代码:

 

import java.util.Date;

import org.gearman.Gearman;
import org.gearman.GearmanClient;
import org.gearman.GearmanServer;

public class ReverseClient {

	public static void main(String[] args) throws InterruptedException {

		// 创建一个Gearman实例
		Gearman gearman = Gearman.createGearman();

		// 创建一个Gearman client
		GearmanClient client = gearman.createGearmanClient();

		/*
		 * 创建一个jobserver
		 * 
		 * Parameter 1: job server的IP地址 Parameter 2: job server监听的端口
		 * 
		 * job server收到client的job,并将其分发给注册worker              *
		 */
		GearmanServer server = gearman.createGearmanServer(
				ReverseWorker.ECHO_HOST, ReverseWorker.ECHO_PORT);

		// 告诉客户端,提交工作时它可以连接到该服务器

		client.addServer(server);

		/*
		 * 向job server提交工作
		 * 
		 * Parameter 1: gearman function名字 Parameter 2: 传送给job server和worker的数据
		 * 
		 * GearmanJobReturn返回job发热结果
		 */

		// 压力测试,客户端10s提交一个job
		int count = 1;
		while (true) {
			
			System.out.println(new Date() + ":第" + count + "个任务正在提交...");
			client.submitJob(
					ReverseWorker.ECHO_FUNCTION_NAME,
					("第" + count + "个任务").getBytes());
			
			count ++ ;

			try {
				Thread.sleep(10000);
			} catch (Exception e) {
				e.printStackTrace();
			}
			
		}
		// 关闭
		// gearman.shutdown();
	}

}

 

 

 

worker端代码:

 

package test;

import java.util.Date;

import org.gearman.Gearman;
import org.gearman.GearmanFunction;
import org.gearman.GearmanFunctionCallback;
import org.gearman.GearmanServer;
import org.gearman.GearmanWorker;


public class ReverseWorker implements GearmanFunction {

	// function name
	public static final String ECHO_FUNCTION_NAME = "reverse";

	// job server地址
	public static final String ECHO_HOST = "192.168.125.131";

	// job server监听的端口
	public static final int ECHO_PORT = 4730;

	public static void main(String[] args) {

		// 创建一个Gearman实例
		Gearman gearman = Gearman.createGearman();

		/*
		 * 创建一个jobserver
		 * 
		 * Parameter 1: job server的IP地址 Parameter 2: job server监听的端口
		 * 
		 * job server收到client的job,并将其分发给注册worker              *
		 */
		GearmanServer server = gearman.createGearmanServer(
				ReverseWorker.ECHO_HOST, ReverseWorker.ECHO_PORT);

		// 创建一个Gearman的worker
		GearmanWorker worker = gearman.createGearmanWorker();

			// 告诉工人如何执行工作(主要实现了GearmanFunction接口)
			worker.addFunction(ReverseWorker.ECHO_FUNCTION_NAME,
					new ReverseWorker());

			// worker连接服务器
			worker.addServer(server);

	}

	@Override
	public byte[] work(String function, byte[] data,
			GearmanFunctionCallback callback) throws Exception {

		// work方法实现了GearmanFunction接口中的work方法,worker端30s处理一个方法
		if (data != null) {
			String str = new String(data);
			try {
				Thread.sleep(30000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			System.out.println(new Date() + ":" +str + "处理完成");
			return (str + "处理完成").getBytes();
		} else {
			return "未接收到data".getBytes();
		}

	}
}

 

 

 

你可能感兴趣的:(gearman)