????????Zookeeper?????й??????????????????????????????? ????С???????????Zookeeper?????д????????μ????????? ???????????java???????????????嵥??

 

package com.kiven.test;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import org.apache.log4j.PropertyConfigurator;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.Watcher.Event.KeeperState;
import org.apache.zookeeper.ZooDefs.Ids;
import org.apache.zookeeper.ZooKeeper;
public class Test{
/**
* server?б?? ???????
*/
protected static String hosts = "172.16.217.148:2181";
/**
* ?????????? ????
*/
private final int SESSION_TIMEOUT = 5000;
private CountDownLatch connectedSignal = new CountDownLatch(1);
protected static ZooKeeper zk;
private static String nodePath = "/Test/test1";
//static String data = "a very long string about data to set to zookeeper";
static int threads = 10;    //?????
static int runs = 1000;     //????????
static int start = 0;       //none
static int size = 1024*4;   //д????????С????λ?????
static byte[] testdata;     //????????
public static void main(String[] args) throws Exception {
PropertyConfigurator.configure("log4j.properties");
//????д??????????Сsize???
testdata = new byte[size];
for(int i=0;i<size;i++){
testdata[i] = 'A';
}
Test test = new Test();
//????
test.connect();
WorkerStat[] statArray = new WorkerStat[threads];
Thread[] threadArray = new Thread[threads];
WorkerStat mainStat = new WorkerStat();
mainStat.runs = runs * threads;
long begin = System.currentTimeMillis();
for (int i = 0; i < threads; i++) {
statArray[i] = new WorkerStat();
statArray[i].start = start + i * runs;
statArray[i].runs = runs;
threadArray[i] = new SetterThread(statArray[i]);
threadArray[i].start();
}
for (int i = 0; i < threads; i++) {
threadArray[i].join();
}
mainStat.setterTime = System.currentTimeMillis() - begin;
begin = System.currentTimeMillis();
for (int i = 0; i < threads; i++) {
threadArray[i] = new GetterThread(statArray[i]);
threadArray[i].start();
}
for (int i = 0; i < threads; i++) {
threadArray[i].join();
}
mainStat.getterTime = System.currentTimeMillis() - begin;
WorkerStat totalStat = new WorkerStat();
System.out.println("Test over!!");
System.out.println("Thread("+threads+") runs set time(ms) get time(ms)");
for (int i = 0; i < threads; i++) {
totalStat.runs = totalStat.runs + statArray[i].runs;
totalStat.setterTime = totalStat.setterTime + statArray[i].setterTime;
totalStat.getterTime = totalStat.getterTime + statArray[i].getterTime;
}
System.out.println("Total " + totalStat.runs + " "+ totalStat.setterTime + " " + totalStat.getterTime);
System.out.println("Avg " + runs + " " + totalStat.setterTime/ threads + " " + totalStat.getterTime / threads);
System.out.println("TPS " + 1000 * totalStat.runs/ totalStat.setterTime + " " + 1000 * totalStat.runs/ totalStat.getterTime);
System.out.println(" Main " + mainStat.runs + " "+ mainStat.setterTime + " " + mainStat.getterTime);
System.out.println("TPS " + 1000 * mainStat.runs/ mainStat.setterTime + " " + 1000 * mainStat.runs/ mainStat.getterTime);
}
private static class WorkerStat {
public int start?? runs;
public long setterTime?? getterTime;
public WorkerStat() {
start = runs = 0;
setterTime = getterTime = 0;
}
}
private static class SetterThread extends Thread {
private WorkerStat stat;
SetterThread(WorkerStat stat) {
this.stat = stat;
}
public void run() {
long begin = System.currentTimeMillis();
for (int i = stat.start; i < stat.start + stat.runs; i++) {
//д????????
try {
zk.setData(nodePath?? testdata?? -1);
} catch (Exception e) {
e.printStackTrace();
}
}
long end = System.currentTimeMillis();
stat.setterTime = end - begin;
}
}
private static class GetterThread extends Thread {
private WorkerStat stat;
GetterThread(WorkerStat stat) {
this.stat = stat;
}
public void run() {
long begin = System.currentTimeMillis();
for (int i = stat.start; i < stat.start + stat.runs; i++) {
//??????????
try {
zk.getData(nodePath?? false?? null);
} catch (Exception e) {
e.printStackTrace();
}
}
long end = System.currentTimeMillis();
stat.getterTime = end - begin;
}
}