- import com.danga.MemCached.MemCachedClient;
- import com.danga.MemCached.SockIOPool;
- public class TestMemCache {
- static SockIOPool pool;
- static int threads; // 运行的测试线程数
- static int runs; // 每个线程运行的次数
- static int size; // 设置到memcache中的数据包大小,单位k
- static Integer myLock;// 锁定以下计数器
- static long putTimes = 0; // put总时间,单位微秒
- static long getTimes = 0; // get总时间,单位微秒
- /**
- * @param args
- */
- public static void main(String[] args) {
- String[] serverlist = { "192.168.0.56:11211", "192.168.0.56:11212" };
- // initialize the pool for memcache servers
- pool = SockIOPool.getInstance();
- pool.setServers(serverlist);
- pool.setInitConn(5);
- pool.setMinConn(5);
- pool.setMaxConn(50);
- pool.setMaintSleep(30);
- pool.setNagle(false);
- pool.initialize();
- if (args.length < 3) {
- System.out.println("用法:TestMemCache 启动线程数 每线程执行测试数量 测试数据大小(k)");
- System.exit(1);
- }
- threads = Integer.parseInt(args[0]);
- runs = Integer.parseInt(args[1]);
- size = 100 * Integer.parseInt(args[2]);
- myLock = new Integer(threads);
- for (int i = 0; i < threads; i++) {
- Thread thread = new WorkerThread();
- thread.start();
- }
- }
- private static class WorkerThread extends Thread {
- // 构造函数
- WorkerThread() {
- }
- public void run() {
- // get client instance
- MemCachedClient mc = new MemCachedClient();
- mc.setCompressEnable(false);
- mc.setCompressThreshold(0);
- // get object to store
- int[] obj = new int[size];
- for (int i = 0; i < size; i++) {
- obj[i] = i;
- }
- String[] keys = new String[runs];
- for (int i = 0; i < runs; i++) {
- keys[i] = "test_key" + i;
- }
- for (int i = 0; i < runs; i++) {
- mc.delete(keys[i]);
- }
- long startTime = System.currentTimeMillis();
- for (int i = 0; i < runs; i++) {
- mc.set(keys[i], obj);
- }
- long time = System.currentTimeMillis() - startTime;
- synchronized (myLock) {
- putTimes += time;
- }
- startTime = System.currentTimeMillis();
- for (int i = 0; i < runs; i++) {
- mc.get(keys[i]);
- }
- time = System.currentTimeMillis() - startTime;
- synchronized (myLock) {
- getTimes += time;
- myLock--;
- if (myLock.equals(0)) {
- System.out.println("测试完成! 启动线程数:" + threads
- + ", 每线程执行测试数量: " + runs + ", 测试数据大小(byte):" + size);
- System.out.println("put处理时间:" + putTimes
- + "微秒,处理put速度: 每秒 " + runs * threads * 1000 / putTimes
- + " 次");
- System.out.println("get处理时间:" + getTimes
- + "微秒,处理get速度: 每秒 " + runs * threads * 1000 / getTimes
- + " 次");
- pool.shutDown();
- }
- }
- }
- }
- }
博客摘录「 MemCached 压力测试」2023年12月27日
于 2023-12-27 22:20:11 首次发布