1、Linux系统下安装jmeter:
1.1、官网下载jmeter安装包
https://jmeter.apache.org/download_jmeter.cgi#binaries
1.2、安装jmeter
1.2.1、安装java 8环境
1.2.2、创建jmeter安装目录,解压jmeter
mkidr /root/jmeter
unzip apache-jmeter-5.5.zip -d /root/jmeter
1.2.3、对jmeter程序进程授权
cd /root/jmeter/apache-jmeter-5.5/bin
chmod 777 jmeter
1.2.4、修改环境变量
vim ~/.bashrc
export JMETER_BIN=/root/jmeter/apache-jmeter-5.5/bin
export PATH=$JMETER_BIN:$PATH
source ~/.bashrc
1.2.5、运行jmeter程序后无法调取图形化界面
1.2.6、检查xorg-x11的安装环境
[root@tpcc01 ~/jmeter/apache-jmeter-5.5/bin]# yum whatprovides */xhost
上次元数据过期检查:0:55:17 前,执行于 2022年10月22日 星期六 07时19分29秒。
bash-completion-1:2.10-1.ky10.noarch : Completion for bash command
仓库 :@System
匹配来源:
文件名 :/usr/share/bash-completion/completions/xhost
bash-completion-1:2.10-1.ky10.noarch : Completion for bash command
仓库 :local
匹配来源:
文件名 :/usr/share/bash-completion/completions/xhost
xorg-x11-server-utils-7.7-29.ky10.x86_64 : X.Org X11 X server utilities
仓库 :@System
匹配来源:
文件名 :/usr/bin/xhost
xorg-x11-server-utils-7.7-29.ky10.x86_64 : X.Org X11 X server utilities
仓库 :local
匹配来源:
文件名 :/usr/bin/xhost
1.2.7、安装xorg-x11
yum install -y xorg-x11-server-utils-7.7-29.ky10.x86_64
1.2.8、设置DISPLAY
[root@tpcc01 ~]# export DISPLAY=192.168.100.101:0
备注:此处的ip为本地物理机的网卡ip,而不是我的虚拟机ip。
1.2.9、重新运行jmeter
2、配置jmeter:
添加线程组:
添加jdbc连接:
在jmeter的lib文件夹中添加VB的jdbc驱动
创建jdbc request
创建监听器
添加Transactions per Second
3、案例测试
3.1 初始化数据:
3.1.1 建表:
vsql -r
create user testuser identified by 'Admin@123';
grant all privileges to testuser;
create database testdb owner testuser;
vsql -r -d testdb -Utestuser -WAdmin@123
CREATE TABLE "sys_user_1" (
"id" integer NOT NULL,
"username" varchar(100) NOT NULL,
"password" varchar(100) NOT NULL,
"nickname" varchar(100) NOT NULL,
"cardid" varchar(100) NOT NULL,
"telephone" varchar(100),
"email" varchar(100),
"status" smallint DEFAULT 0 NOT NULL,
"create_user" varchar(100),
"create_time" timestamp(0) without time zone DEFAULT "pg_systimestamp"(),
"update_user" varchar(100),
"update_time" timestamp(0) without time zone
);
COMMENT ON COLUMN "sys_user_1"."username" IS '账号';
COMMENT ON COLUMN "sys_user_1"."password" IS '密码';
COMMENT ON COLUMN "sys_user_1"."nickname" IS '昵称';
COMMENT ON COLUMN "sys_user_1"."telephone" IS '电话号码';
COMMENT ON COLUMN "sys_user_1"."email" IS '邮箱';
COMMENT ON COLUMN "sys_user_1"."status" IS '状态(0:锁定,1:解锁)';
CREATE SEQUENCE "sys_user_id_seq_1"
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
ALTER SEQUENCE "sys_user_id_seq_1" OWNED BY "sys_user_1"."id";
ALTER TABLE "sys_user_1" ALTER COLUMN "id" SET DEFAULT "nextval"('"sys_user_id_seq_1"'::"regclass");
--生成身份证
create or replace function gen_id(
a date,
b date
)
returns text as $$
select lpad((random()*99)::int::text, 2, '0') ||
lpad((random()*99)::int::text, 2, '0') ||
lpad((random()*99)::int::text, 2, '0') ||
to_char(a + (random()*(b-a)), 'yyyymmdd') ||
lpad((random()*99), 2, '0') ||
random()::int ||
(case when random()*10 >9 then 'X' else (random()*9)::int::text end ) ;
$$ language sql strict;
--生成手机号
CREATE or replace FUNCTION generatePhone() RETURN varchar
as
head VARCHAR(100) DEFAULT '156,136,176,151,139,138,132,125,122,187,188';
content CHAR(10) DEFAULT '0123456789';
phone text;
i int DEFAULT 1;
len int DEFAULT LENGTH(content);
BEGIN
phone:= CONCAT(phone, split_part(head ,',', replace((random()*10)%12,0,1))||substring(random()*10000000000,0,9));
RETURN phone;
END;
/
模拟初始化1千万条数据
insert into sys_user_1(username,password,nickname,cardid,telephone,email,status,create_user,update_user)
select 'user'||t,md5(random()::text),'nikename'||t,
gen_id('1900-01-01', '2021-10-16'),generatePhone(),ceil(random()*1000000000)||'@qq.com',
round(random()),session_user,null
from generate_series(1,10000000) t;
3.1.2 测试1:用户注册
创建索引:
create index idx_userselect_idx1 on sys_user(username,password,cardid);
数据插入,在jmeter中设置
insert into sys_user(username,password,nickname,cardid,telephone,email,status,create_user,update_user)
values (${__Random(1,100000000)},${__Random(1,100000000)},${__Random(1,100000000)},
${__Random(1,100000000)},${__Random(1,100000000)},${__Random(1,100000000)},
${__Random(0,1)},${__Random(1,100000000)},${__Random(1,100000000)});
jmeter配置:
3.1.3 测试2:用户查询
初始化数据
vsql -r -d testdb -Utestuser -WAdmin@123
CREATE TABLE "sys_user" (
"id" integer NOT NULL,
"username" varchar(100) NOT NULL,
"password" varchar(100) NOT NULL,
"nickname" varchar(100) NOT NULL,
"cardid" varchar(100) NOT NULL,
"telephone" varchar(100),
"email" varchar(100),
"status" smallint DEFAULT 0 NOT NULL,
"create_user" varchar(100),
"create_time" timestamp(0) without time zone DEFAULT "pg_systimestamp"(),
"update_user" varchar(100),
"update_time" timestamp(0) without time zone
);
COMMENT ON COLUMN "sys_user"."username" IS '账号';
COMMENT ON COLUMN "sys_user"."password" IS '密码';
COMMENT ON COLUMN "sys_user"."nickname" IS '昵称';
COMMENT ON COLUMN "sys_user"."telephone" IS '电话号码';
COMMENT ON COLUMN "sys_user"."email" IS '邮箱';
COMMENT ON COLUMN "sys_user"."status" IS '状态(0:锁定,1:解锁)';
CREATE SEQUENCE "sys_user_id_seq"
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
ALTER SEQUENCE "sys_user_id_seq" OWNED BY "sys_user"."id";
ALTER TABLE "sys_user" ALTER COLUMN "id" SET DEFAULT "nextval"('"sys_user_id_seq"'::"regclass");
插入3000万行数据并创建索引
insert into sys_user(username,password,nickname,cardid,telephone,email,status,create_user,update_user)
select 'user'||t,md5(random()::text),'nikename'||t,
gen_id('1900-01-01', '2021-10-16'),generatePhone(),ceil(random()*1000000000)||'@qq.com',
round(random()),session_user,null
from generate_series(1,30000) t;
create index idx_userselect_idx1 on sys_user(username,password,cardid);
生成csv文件,放入jmeter查询:
注意:导出的文件应放到jmeter能读取到的位置。
[vastbase@tpcc01 /tmp]$ vsql -r
vastbase= \c testdb
testdb= \dt
testdb= copy (select username,password,cardid from sys_user tablesample system(5)) to '/tmp/sys_user1.csv' delimiter ',' csv header;
COPY 5772
把导出的csv文件,放入jmeter中读取
添加<CSV 数据文件设置>
字段组合查询,在jmeter中语法设置
select username,password,cardid
from sys_user
where username =? and password =? and cardid =?;
3.1.3 测试3:视图查询
初始化数据
vsql -r -d testdb -Utestuser -WAdmin@123
CREATE TABLE COLUMNS_hj as select * from information_schema.COLUMNS;
INSERT INTO COLUMNS_hj SELECT * FROM COLUMNS_hj;
select count(*) from columns_hj;
备注:循环insert,插够25万条数据
CREATE TABLE COLUMNS_hj2 as select * from information_schema.COLUMNS;
select count(*) from columns_hj2;
create index idx_columns_hj on COLUMNS_hj(table_name);
create index idx_columns_hj2 on COLUMNS_hj2(table_name);
创建视图
create view t_view as
select * from COLUMNS_hj
union all
select * from COLUMNS_hj2;
导出csv文件:
vsql -r -d testdb
copy (select table_name from t_view) to '/tmp/t_view1.csv' csv header;
配置jmeter
3.1.4 测试4:多表统计查询
初始化数据
vsql -r -d testdb -Utestuser -WAdmin@123
create index col_hj_idx1 on columns_hj(table_name,column_name);
create index col_hj2_idx1 on columns_hj2(table_name,column_name);
导出csv文件:
vsql -r -d testdb
copy (select distinct table_name from columns_hj) to '/tmp/hash.csv' delimiter ',' csv header;
配置jmeter
多表统计查询jmeter语法设置:
select count(c1.table_name),sum(c1.ordinal_position)
from columns_hj c1, columns_hj2 c2
where c1.table_name = c2.table_name
and c1.column_name = c2.column_name
and c1.table_name = ?
group by c1.table_name,c1.column_name
order by c1.table_name,c1.column_name;
3.1.5 测试5:前缀模糊查询
初始化数据,创建gin索引
vsql -r -d testdb -Utestuser -WAdmin@123
create extension pg_zhtrgm;
create index idx_columns_hjgin1 on column_hj using gin(table_name gin_zhtrgm_ops);
导出csv文件:
vsql -r -d testdb
copy (select distinct table_name from columns_hj) to '/tmp/hash5.csv' delimiter ',' csv header;
配置jmeter
前缀模糊查询jmeter语法设置:
select * from columns_hj where table_name like ? limit 2;
4、压测场景下,使用非GUI模式运行jmeter:
备注:为什么要用命令行运行Jmeter?
GUI模式比较适用于测试创建和测试调试;负载测试时,最好是使用NON GUI模式:
1、NON GUI模式能大大缩减所需要的系统资源;
2、GUI模式,会会消耗大量的内存资源,容易使压力机本身达到瓶颈,从而影响测试结果
(例如:查看结果树,会依次把每条请求的结果写入内存,降低性能);
3、GUI模式,不能支持大型的负载测试与性能测试,并发超大时,JMeter会崩溃;
4、使用使用NON GUI模式执行可以进行自动化脚本编写,并持续集成到Jinkens上自动运行。
4.1、运行命令之前,先在图形化界面下生成测试文件,即user join.jmx文件
4.2、命令行方式启动jmeter进行业务压测,并生成测试报告:
[root@tpcc01 ~]# jmeter -n -t /root/jmeter/apache-jmeter-5.5/bin/'user join.jmx' -l a.csv -e -o myDashboard
参数解释:
-n 非GUI模式 -> 在非GUI模式下运行JMeter
-t 测试文件<参数> ->要运行的jmeter测试(.jmx)文件
(若命令行模式在bin/文件夹下,则可以使用相对路径,否则使用绝对路径)
-l 日志文件<参数> ->生成的日志文件,指定结果文件路径 (同上)
-j 指定日志文件路径 (同上)
-o 指定报表文件路径(同上)
-R 指定远程服务器列表
-g 制定csv测试结果文件路径(同上)
-e 测试完成生产报表
-r 设置分布式执行
-H 设置JMeter设置代理服务器IP或域名
-P 设置JMeter代理服务器端口号