Zookeeper读写性能测试
作者:网络转载 发布时间:[ 2014/2/20 14:33:32 ] 推荐标签:性能测试 测试技术
关于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;
}
}
|
本文内容不用于商业目的,如涉及知识产权问题,请权利人联系SPASVO小编(021-61079698-8054),我们将立即处理,马上删除。
相关推荐
更新发布
功能测试和接口测试的区别
2023/3/23 14:23:39如何写好测试用例文档
2023/3/22 16:17:39常用的选择回归测试的方式有哪些?
2022/6/14 16:14:27测试流程中需要重点把关几个过程?
2021/10/18 15:37:44性能测试的七种方法
2021/9/17 15:19:29全链路压测优化思路
2021/9/14 15:42:25性能测试流程浅谈
2021/5/28 17:25:47常见的APP性能测试指标
2021/5/8 17:01:11热门文章
常见的移动App Bug??崩溃的测试用例设计如何用Jmeter做压力测试QC使用说明APP压力测试入门教程移动app测试中的主要问题jenkins+testng+ant+webdriver持续集成测试使用JMeter进行HTTP负载测试Selenium 2.0 WebDriver 使用指南