性能测试工具操作数据库(十一)-Jmeter与Hbase

简介: 由于在网上找不到Jmeter连接Hbase的源文件或是插件,所以本文只是通过Jmeter的BeanShell来调用和调试Hbase的远程连接操作,具体性能测试时,需要怎么应用(比如通过Java Request等方式),等具体开展测试时再进行灵活扩展和调整。
版权声明:本文为博主原创文章,未经博主允许不得转载。欢迎访问我的博客 https://blog.csdn.net/smooth00/article/details/74279856

由于在网上找不到Jmeter连接Hbase的源文件或是插件,所以本文只是通过Jmeter的BeanShell来调用和调试Hbase的远程连接操作,具体性能测试时,需要怎么应用(比如通过Java Request等方式),等具体开展测试时再进行灵活扩展和调整。

关键的是要引用正确的Hbase jar包(还要保证版本的兼容,Hbase1.0开始就要求JDK1.7及以上,而且Jmeter引用的Hbase Jar包最好是与服务端的Hbase一致,否则也会出现兼容性问题)。以下的测试代码都是在Jmeter3.1+JDK1.8环境下进行的。

1、下载Hbase Jar包,可以到官网下载整个包,获取其中的lib jar包:http://archive.apache.org/dist/hbase/,也可以下载我为本次测试所整理好的jar包(性能测试工具所引用的hbase依赖包

2、在Jmeter的测试计划里把所有Jar包添加到classpath中(我不习惯把Jar包直接扔到Jmeter目录的lib\ext下,不方便区分管理)


3、将hadoop-common-2.2.0-bin-master文件目录放到Jmeter的bin目录下,主要是Windows下调用Hadoop组件,如果没有winutils.exe会报错(可以到网上下载这个包,上面提供的性能测试工具所引用的hbase依赖包里也有)。

4、在Jmeter中建立线程组,和建一个BeanShell Sampler,直接将以下代码复制上去进行调试(注意要配好zookeeper连接地址和端口)

import java.io.IOException;               
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.MasterNotRunningException;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
import java.io.File;

public class HbaseTest
{ 
     String tableName="student";
     Configuration configuration=null;	
     public void SetConfiguration() {    
   		configuration = HBaseConfiguration.create();       
   		//config.set("hbase.zookeeper.quorum", "hellotest");//单机            
   		configuration.set("hbase.zookeeper.quorum", "agent01.org.cn,agent02.org.cn,master.org.cn");
   		//包括三个节点的zookeeper地址(也可以是Host IP),第一个是主节点
   		configuration.set("hbase.zookeeper.property.clientPort", "2181");// zookeeper端口  
   		configuration.set("hbase.regionserver.port", "16020");
   		configuration.set("zookeeper.znode.parent", "/hbase-unsecure");
   		configuration.set("hbase.rootdir", "hdfs://master.org.cn:8020/apps/hbase/data");
   		//connection = ConnectionFactory.createConnection(config); 
     }

     /**
  * create a new Table
  * @param configuration Configuration
  * @param tableName String,the new Table's name
  * */
 public static void createTable(Configuration configuration,String tableName){
  HBaseAdmin admin;
  	try {
   		admin = new HBaseAdmin(configuration);
   		if(admin.tableExists(tableName)){
    		admin.disableTable(tableName);
    		admin.deleteTable(tableName);
    		System.out.println(tableName+"is exist ,delete ......");
    		log.info(tableName+"is exist ,delete ......");
   		}
 
   		HTableDescriptor tableDescriptor=new HTableDescriptor(TableName.valueOf(tableName));
   		tableDescriptor.addFamily(new HColumnDescriptor("info"));
   		tableDescriptor.addFamily(new HColumnDescriptor("address"));
   		admin.createTable(tableDescriptor);
   		System.out.println("end create table");
   		log.info("end create table");
  	} catch (MasterNotRunningException e) {
   		// TODO Auto-generated catch block
   		e.printStackTrace();
  	} catch (ZooKeeperConnectionException e) {
   		// TODO Auto-generated catch block
   		e.printStackTrace();
  	} catch (IOException e) {
   		// TODO Auto-generated catch block
   		e.printStackTrace();
  	}
  
 }
 
 /**
  * Delete the existing table
  * @param configuration Configuration
  * @param tableName String,Table's name
  * */
 public static void dropTable(Configuration configuration,String tableName){
  HBaseAdmin admin;
  	try {
   		admin = new HBaseAdmin(configuration);
   		if(admin.tableExists(tableName)){
    		admin.disableTable(tableName);
    		admin.deleteTable(tableName);
    		System.out.println(tableName+"delete success!");
    		log.info(tableName+"delete success!");
   		}else{
    		System.out.println(tableName+"Table does not exist!");
    		log.info(tableName+"Table does not exist!");
   		}
  	} catch (MasterNotRunningException e) {
   		// TODO Auto-generated catch block
   		e.printStackTrace();
  	} catch (ZooKeeperConnectionException e) {
   		// TODO Auto-generated catch block
   		e.printStackTrace();
  	} catch (IOException e) {
   		// TODO Auto-generated catch block
   		e.printStackTrace();
  	}
 }
 
 /**
  * insert a data
  * @param configuration Configuration
  * @param tableName String,Table's name
  * */
 public static void addData(Configuration configuration,String tableName){
  HBaseAdmin admin;
  	try {
   		admin = new HBaseAdmin(configuration);
   		if(admin.tableExists(tableName)){
    		HTable table=new HTable(configuration, tableName);
    		Put put=new Put(Bytes.toBytes("zhangsan"));
    		put.add(Bytes.toBytes("info"), Bytes.toBytes("age"), Bytes.toBytes("28"));
    		table.put(put);
    		System.out.println("add success!");
    		log.info("add success!");
   		}else{
    		System.out.println(tableName+"Table does not exist!");
    		log.info(tableName+"Table does not exist!");
   		}
  	} catch (MasterNotRunningException e) {
   		// TODO Auto-generated catch block
   		e.printStackTrace();
  	} catch (ZooKeeperConnectionException e) {
   		// TODO Auto-generated catch block
   		e.printStackTrace();
  	} catch (IOException e) {
   		// TODO Auto-generated catch block
   		e.printStackTrace();
  	}
 }
 
 /**
  * Delete a data
  * @param configuration Configuration
  * @param tableName String,Table's name
  * */
 public static void deleteDate(Configuration configuration,String tableName){
  HBaseAdmin admin;
  	try {
   		admin=new HBaseAdmin(configuration);
   		if(admin.tableExists(tableName)){
    		HTable table=new HTable(configuration, tableName);
    		Delete delete=new Delete(Bytes.toBytes("zhangsan"));
    		table.delete(delete);
    		System.out.println("delete success!");
    		log.info("delete success!");
   		}else{
    		System.out.println("Table does not exist!");
   		}
  	} catch (MasterNotRunningException e) {
   		// TODO Auto-generated catch block
   		e.printStackTrace();
  	} catch (ZooKeeperConnectionException e) {
   		// TODO Auto-generated catch block
   		e.printStackTrace();
  	} catch (IOException e) {
   		// TODO Auto-generated catch block
   		e.printStackTrace();
  	}
 	}
 

 /**
  * get a data
  * @param configuration Configuration
  * @param tableName String,Table's name
  * */
 public static void getData(Configuration configuration,String tableName){
  HTable table;
  	try {
   		table = new HTable(configuration, tableName);
   		Get get=new Get(Bytes.toBytes("zhangsan"));
   		Result result=table.get(get);
 
   		for(Cell cell:result.rawCells()){
    		System.out.println("RowName:"+new String(CellUtil.cloneRow(cell))+" ");
    		System.out.println("Timetamp:"+cell.getTimestamp()+" ");
    		System.out.println("column Family:"+new String(CellUtil.cloneFamily(cell))+" ");
    		System.out.println("row Name:"+new String(CellUtil.cloneQualifier(cell))+" ");
    		System.out.println("value:"+new String(CellUtil.cloneValue(cell))+" ");
    
    		log.info("RowName:"+new String(CellUtil.cloneRow(cell))+" ");
    		log.info("Timetamp:"+cell.getTimestamp()+" ");
    		log.info("column Family:"+new String(CellUtil.cloneFamily(cell))+" ");
    		log.info("row Name:"+new String(CellUtil.cloneQualifier(cell))+" ");
    		log.info("value:"+new String(CellUtil.cloneValue(cell))+" ");
   		}
  	} catch (IOException e) {
   		// TODO Auto-generated catch block
   		e.printStackTrace();
  	}
 }
 
 /**
  * insert all data
  * @param configuration Configuration
  * @param tableName String,Table's name
  * */
 public static void getAllData(Configuration configuration,String tableName){
  HTable table;
  	try {
   		table=new HTable(configuration, tableName);
   		Scan scan=new Scan();
   		ResultScanner results=table.getScanner(scan);
   		for (Result result = results.next(); result != null; result = results.next()) {
   		// print out the row we found and the columns we were looking for
    		for(Cell cell:result.rawCells()){   
     			System.out.println("RowName:"+new String(CellUtil.cloneRow(cell))+" ");
     			System.out.println("Timetamp:"+cell.getTimestamp()+" ");
     			System.out.println("column Family:"+new String(CellUtil.cloneFamily(cell))+" ");
     			System.out.println("row Name:"+new String(CellUtil.cloneQualifier(cell))+" ");
     			System.out.println("value:"+new String(CellUtil.cloneValue(cell))+" ");

     			log.info("RowName:"+new String(CellUtil.cloneRow(cell))+" ");
     			log.info("Timetamp:"+cell.getTimestamp()+" ");
     			log.info("column Family:"+new String(CellUtil.cloneFamily(cell))+" ");
     			log.info("row Name:"+new String(CellUtil.cloneQualifier(cell))+" ");
     			log.info("value:"+new String(CellUtil.cloneValue(cell))+" ");
    		}
   		}
  	} catch (IOException e) {
   		// TODO Auto-generated catch block
   		e.printStackTrace();
  	}  
 }
 public boolean runActions(){
	 	File directory = new File(".");
	 	System.setProperty("hadoop.home.dir", directory.getCanonicalPath()+"\\hadoop-common-2.2.0-bin-master");
  		 SetConfiguration();
	      createTable(configuration, tableName);
	      addData(configuration, tableName);
	      getData(configuration, tableName);
	      getAllData(configuration, tableName);
	      deleteDate(configuration, tableName);
	      dropTable(configuration, tableName);
	      return true;
	}
}

boolean runFlag=new HbaseTest().runActions();
log.info("runFlag:"+runFlag);
以上代码中,加了log.info,用来调试代码以验证代码是否执行成功

5、在线程组中加上查看结果树、Summary Report、Debug Sampler,以全运行时查看成功情况。通过点击Jmeter右上方的三角!号(如果代码报错也能看能看到提示),可以查看代码运行日志(这是调试Java代码比较方便的工具),运行结果如下:

6、可以将以上的测试代码保存成java(或者可以打成jar包再用),直接通过jmerter进行引用和测试

7、相对来讲,Jmeter不适合调试Java代码,因为报一段长长的错误日志后,可能报错的最后一句话才能看出错误所在,所以以上代码也可以等在Java开发工具中调测通过后,再移植过来,然后再进一步调试和修改一些兼容性的错误。

目录
相关文章
|
11月前
|
数据可视化 BI API
无缝对接云数据库:自定义报表生成工具在混合云环境下的部署指南
自定义报表生成工具通过拖拽设计、多数据源整合及自动化输出,帮助业务人员零代码创建个性化报表,解决传统工具灵活性不足、技术门槛高的问题。文章对比其与传统报表差异,列举行业应用场景(如财务、零售),并给出选型建议与主流工具(如FineReport、Power BI、板栗看板)的优劣势分析。
371 0
|
9月前
|
机器学习/深度学习 人工智能 测试技术
EdgeMark:嵌入式人工智能工具的自动化与基准测试系统——论文阅读
EdgeMark是一个面向嵌入式AI的自动化部署与基准测试系统,支持TensorFlow Lite Micro、Edge Impulse等主流工具,通过模块化架构实现模型生成、优化、转换与部署全流程自动化,并提供跨平台性能对比,助力开发者在资源受限设备上高效选择与部署AI模型。
737 9
EdgeMark:嵌入式人工智能工具的自动化与基准测试系统——论文阅读
|
9月前
|
Java 测试技术 API
自动化测试工具集成及实践
自动化测试用例的覆盖度及关键点最佳实践、自动化测试工具、集成方法、自动化脚本编写等(兼容多语言(Java、Python、Go、C++、C#等)、多框架(Spring、React、Vue等))
707 6
|
10月前
|
前端开发 Java jenkins
Jmeter压力测试工具全面教程和使用技巧。
JMeter是一个能够模拟高并发请求以检查应用程序各方面性能的工具,包括但不限于前端页面、后端服务及数据库系统。熟练使用JMeter不仅能够帮助发现性能瓶颈,还能在软件开发早期就预测系统在面对真实用户压力时的表现,确保软件质量和用户体验。在上述介绍的基础上,建议读者结合官方文档和社区最佳实践,持续深入学习和应用。
1977 10
|
9月前
|
人工智能 数据库 iOS开发
DBeaver Ultimate Edtion 25.2 发布 - 通用数据库工具
DBeaver Ultimate Edtion 25.2 Multilingual (macOS, Linux, Windows) - 通用数据库工具
796 0
|
12月前
|
Java 测试技术 容器
Jmeter工具使用:HTTP接口性能测试实战
希望这篇文章能够帮助你初步理解如何使用JMeter进行HTTP接口性能测试,有兴趣的话,你可以研究更多关于JMeter的内容。记住,只有理解并掌握了这些工具,你才能充分利用它们发挥其应有的价值。+
1561 23
|
11月前
|
敏捷开发 运维 数据可视化
DevOps看板工具中的协作功能:如何打破开发、测试与运维之间的沟通壁垒
在DevOps实践中,看板工具通过可视化任务管理和自动化流程,提升开发与运维团队的协作效率。它支持敏捷开发、持续交付,助力团队高效应对需求变化,实现跨职能协作与流程优化。
|
10月前
|
监控 Java 数据挖掘
利用Jmeter工具进行HTTP接口的性能测试操作
基础上述步骤反复迭代调整直至满足预期目标达成满意水平结束本轮压力评估周期进入常态监控阶段持续关注系统运转状态及时发现处理新出现问题保障服务稳定高效运作
1267 0
|
12月前
|
数据可视化 测试技术 Go
Go 语言测试与调试:`go test` 工具用法
`go test` 是 Go 语言内置的测试工具,支持单元测试、基准测试、示例测试等功能。本文详解其常用参数、调试技巧及性能测试命令,并提供实际项目中的应用示例与最佳实践。
|
11月前
|
SQL 存储 数据库
SQL Server Management Studio (SSMS) 21 - 微软数据库管理工具
SQL Server Management Studio (SSMS) 21 - 微软数据库管理工具
1434 0