Last active
January 3, 2016 05:49
-
-
Save lanhuai/8418713 to your computer and use it in GitHub Desktop.
我写的测试同事开发的Zookeeper分布式锁性能的代码
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.lanhuai.zookeeper.distributelock.DistributeLock; | |
import com.lanhuai.zookeeper.distributelock.DistributeLockFactory; | |
import junit.framework.TestCase; | |
import org.apache.zookeeper.KeeperException; | |
import java.util.concurrent.ConcurrentHashMap; | |
import java.util.concurrent.ConcurrentMap; | |
import java.util.concurrent.CountDownLatch; | |
import java.util.concurrent.CyclicBarrier; | |
/** | |
* @author <a href="mailto:[email protected]">Ning Yubin</a> | |
* @version $Id$ | |
*/ | |
public class DistributedLockTest extends TestCase { | |
final int concurrentNum = 500; | |
static String hosts = "10.0.0.135:2181,10.0.0.135:2182,10.0.0.135:2183"; | |
static final DistributeLockFactory factory; | |
static { | |
long start = System.currentTimeMillis(); | |
factory = new DistributeLockFactory(hosts, 300); | |
System.out.println(String.format("init zookeeper in %s ms", System.currentTimeMillis() - start)); | |
} | |
public void testDistributedLockMoreTimes() { | |
for (int i = 0; i < 10; i++) { | |
testDistributedLockOneTime(); | |
} | |
} | |
public void testDistributedLockOneTime() { | |
long beginTime = System.currentTimeMillis(); | |
final CyclicBarrier startSignal = new CyclicBarrier(concurrentNum); | |
final CountDownLatch doneSignal = new CountDownLatch(concurrentNum); | |
final ConcurrentMap<String, Long> getLockTimeMap = new ConcurrentHashMap<String, Long>(concurrentNum); | |
final ConcurrentMap<String, Long> releaseLockTimeMap = new ConcurrentHashMap<String, Long>(concurrentNum); | |
for (int x = 0; x < concurrentNum; x++) { | |
final int count = x; | |
Thread thread = new Thread(new Runnable() { | |
public void run() { | |
try { | |
startSignal.await(); //CyclicBarrier 使 concurrentNum 个线程同时开始运行 | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
String lockName = String.format("lock_test_%s_%s", count, String.valueOf(System.currentTimeMillis())); | |
DistributeLock lock = factory.newLock(lockName); | |
long start = System.currentTimeMillis(); | |
try { | |
lock.lock(); | |
long getLockTime = System.currentTimeMillis() - start; | |
getLockTimeMap.putIfAbsent(lockName, getLockTime); | |
// System.out.println(String.format("Get lock[%s] in %s ms", lockName, getLockTime)); | |
Thread.sleep(100); //模拟业务逻辑消耗的时间 | |
} catch (KeeperException.SessionExpiredException e) { | |
e.printStackTrace(); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} catch (KeeperException e) { | |
e.printStackTrace(); | |
} | |
start = System.currentTimeMillis(); | |
try { | |
lock.unlock(); | |
long releaseLockTime = System.currentTimeMillis() - start; | |
releaseLockTimeMap.putIfAbsent(lockName, releaseLockTime); | |
// System.out.println(String.format("Release lock[%s] in %s ms", lockName, releaseLockTime)); | |
} catch (KeeperException.SessionExpiredException e) { | |
e.printStackTrace(); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} catch (KeeperException e) { | |
e.printStackTrace(); | |
} | |
doneSignal.countDown(); | |
} | |
}); | |
thread.start(); | |
} | |
long initThreadTime = System.currentTimeMillis() - beginTime; | |
System.out.println(String.format("init %s threads in %s ms", concurrentNum, initThreadTime)); | |
beginTime += initThreadTime; | |
try { | |
doneSignal.await(); // 等待 concurrentNum 个线程都执行完毕 | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
long timeElapsed = System.currentTimeMillis() - beginTime; | |
System.out.println(String.format("Time Elapsed : %s ms, TPS %s", timeElapsed, Math.round((double)concurrentNum / (double)timeElapsed * 1000))); | |
long totalGetLockTime = 0; | |
for (ConcurrentMap.Entry<String, Long> entry : getLockTimeMap.entrySet()) { | |
totalGetLockTime += entry.getValue(); | |
} | |
System.out.println(String.format("Avg Get Lock Time : %s ms", totalGetLockTime / concurrentNum)); | |
long totalReleaseLockTime = 0; | |
for (ConcurrentMap.Entry<String, Long> entry : releaseLockTimeMap.entrySet()) { | |
totalReleaseLockTime += entry.getValue(); | |
} | |
System.out.println(String.format("Avg Release Lock Time : %s ms", totalReleaseLockTime / concurrentNum)); | |
System.out.println("======================================="); | |
} | |
@Override | |
protected void tearDown() throws Exception { | |
super.tearDown(); | |
factory.closeZooKeeper(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment