公司想从memcached client for java 换成spymemcached,责令我测试一下性能。
使用client for java 10线程,每线程运行10次,每次的数据为100K,put和get的速度能达到6250次/秒。但是换用了spymemcached后,用几乎同样的程序测试,却最多只能达到1000次/秒.小弟初学,想了很久也没想明白原因。麻烦各位高手帮忙看下,指点一二。
下面是代码import java.net.InetSocketAddress;   import net.spy.memcached.MemcachedClient;public class SpyMemCache {
//    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.1.77:11211" };        // 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.setAliveCheck(false);
//
//         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();
            try{
                MemcachedClient mc  = new MemcachedClient(new InetSocketAddress("127.0.0.1", 11211));//             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],500, 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 / time
                             + " 次");
                     System.out.println("get处理时间:" + getTimes
                             + "微秒,处理get速度: 每秒 " + runs * threads * 1000 / time
                             + " 次");                     mc.shutdown();
                 }
             }
            }
            catch (Exception e){}         }
     }}有劳了,多谢

解决方案 »

  1.   


    /**
     * Created by IntelliJ IDEA.
     * User: Edain
     * Date: 2009-3-6
     * Time: 10:03:57
     * To change this template use File | Settings | File Templates.
     */
    //import com.danga.MemCached.MemCachedClient;
    //import com.danga.MemCached.SockIOPool;
    import java.io.IOException;
    import java.net.InetSocketAddress;   import net.spy.memcached.MemcachedClient;public class SpyMemCache {
    //    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.1.77:11211" };        // 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.setAliveCheck(false);
    //
    //         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();
                try{
                    MemcachedClient mc  = new MemcachedClient(new InetSocketAddress("127.0.0.1", 11211));//             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],500, 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;
                long time1=System.currentTimeMillis() - startTime;            synchronized (myLock) {
                     getTimes += time1;
                     myLock--;                if (myLock.equals(0)) {
                         System.out.println("测试完成! 启动线程数:" + threads
                                 + ", 每线程执行测试数量: " + runs + ", 测试数据大小(byte):" + size);                     System.out.println("put处理时间:" + putTimes
                                 + "微秒,处理put速度: 每秒 " + runs * threads * 1000 / time
                                 + " 次");
                         System.out.println("get处理时间:" + getTimes
                                 + "微秒,处理get速度: 每秒 " + runs * threads * 1000 / time1
                                 + " 次");                     mc.shutdown();
                     }
                 }
                }
                catch (Exception e){}         }
         }}
      

  2.   

    多谢回复
    2楼的代码确实是我贴错了。下面是memcached client for java的测试代码。不过代码不是关键问题,关键是算法。看了很多遍了,觉得这思路应该是对的,不过得出的结果总是不对。经过这两天的测试,发现在不同线程数和执行次数的情况下,spymemcached同client for java相比在写的速度上确实有较大提高。虽然达不到网上说的15000TPS,但相对提高的幅度是很大的。但是读的效率上则是还不及client for java。
    百思不得其解啊下面是memcached client for java的测试代码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 = { "127.0.0.1:11211" };        // 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.setAliveCheck(false);         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 = 250 * 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++) {
    //                 if(!mc.set(keys[i], obj)){
    //                     System.out.println("1111111111111");
    //                 }
                    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;
                long time1=System.currentTimeMillis() - startTime;            synchronized (myLock) {
                     getTimes += time1;
                     myLock--;                if (myLock.equals(0)) {
                         System.out.println("测试完成! 启动线程数:" + threads
                                 + ", 每线程执行测试数量: " + runs + ", 测试数据大小(k):" + size/250);                     System.out.println("put处理时间:" + putTimes
                                 + "毫秒,处理put速度: 每秒 " + runs * threads * 1000 / time
                                 + " 次");
                         System.out.println("get处理时间:" + getTimes
                                 + "毫秒,处理get速度: 每秒 " + runs * threads * 1000 / time1
                                 + " 次");
                         mc.flushAll();
                         pool.shutDown();
                     }
                 }
             }
         }}