1胚股、Linux系統(tǒng)下安裝jmeter:
1.1、官網(wǎng)下載jmeter安裝包
https://jmeter.apache.org/download_jmeter.cgi#binaries
1.2棍矛、安裝jmeter
1.2.1匹表、安裝java 8環(huán)境
1.2.2、創(chuàng)建jmeter安裝目錄伴奥,解壓jmeter
mkidr /root/jmeter
unzip apache-jmeter-5.5.zip -d /root/jmeter
1.2.3丰辣、對jmeter程序進(jìn)程授權(quán)
cd /root/jmeter/apache-jmeter-5.5/bin
chmod 777 jmeter
1.2.4撒强、修改環(huán)境變量
vim ~/.bashrc
export JMETER_BIN=/root/jmeter/apache-jmeter-5.5/bin
export PATH=$JMETER_BIN:$PATH
source ~/.bashrc
1.2.5、運(yùn)行jmeter程序后無法調(diào)取圖形化界面
1.2.6笙什、檢查xorg-x11的安裝環(huán)境
[root@tpcc01 ~/jmeter/apache-jmeter-5.5/bin]# yum whatprovides */xhost
上次元數(shù)據(jù)過期檢查:0:55:17 前飘哨,執(zhí)行于 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芽隆、設(shè)置DISPLAY
[root@tpcc01 ~]# export DISPLAY=192.168.100.101:0
備注:此處的ip為本地物理機(jī)的網(wǎng)卡ip,而不是我的虛擬機(jī)ip淘正。
1.2.9摆马、重新運(yùn)行jmeter
2、配置jmeter:
添加線程組:
添加jdbc連接:
在jmeter的lib文件夾中添加VB的jdbc驅(qū)動
創(chuàng)建jdbc request
創(chuàng)建監(jiān)聽器
添加Transactions per Second
3鸿吆、案例測試
3.1 初始化數(shù)據(jù):
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 '狀態(tài)(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;
--生成手機(jī)號
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千萬條數(shù)據(jù)
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:用戶注冊
創(chuàng)建索引:
create index idx_userselect_idx1 on sys_user(username,password,cardid);
數(shù)據(jù)插入,在jmeter中設(shè)置
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:用戶查詢
初始化數(shù)據(jù)
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 '狀態(tài)(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萬行數(shù)據(jù)并創(chuàng)建索引
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查詢:
注意:導(dǎo)出的文件應(yīng)放到j(luò)meter能讀取到的位置。
[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
把導(dǎo)出的csv文件思犁,放入jmeter中讀取
添加<CSV 數(shù)據(jù)文件設(shè)置>
字段組合查詢代虾,在jmeter中語法設(shè)置
select username,password,cardid
from sys_user
where username =? and password =? and cardid =?;
3.1.3 測試3:視圖查詢
初始化數(shù)據(jù)
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;
備注:循環(huán)insert,插夠25萬條數(shù)據(jù)
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);
創(chuàng)建視圖
create view t_view as
select * from COLUMNS_hj
union all
select * from COLUMNS_hj2;
導(dǎo)出csv文件:
vsql -r -d testdb
copy (select table_name from t_view) to '/tmp/t_view1.csv' csv header;
配置jmeter
3.1.4 測試4:多表統(tǒng)計(jì)查詢
初始化數(shù)據(jù)
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);
導(dǎo)出csv文件:
vsql -r -d testdb
copy (select distinct table_name from columns_hj) to '/tmp/hash.csv' delimiter ',' csv header;
配置jmeter
多表統(tǒng)計(jì)查詢jmeter語法設(shè)置:
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:前綴模糊查詢
初始化數(shù)據(jù)激蹲,創(chuàng)建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);
導(dǎo)出csv文件:
vsql -r -d testdb
copy (select distinct table_name from columns_hj) to '/tmp/hash5.csv' delimiter ',' csv header;
配置jmeter
前綴模糊查詢jmeter語法設(shè)置:
select * from columns_hj where table_name like ? limit 2;
4棉磨、壓測場景下,使用非GUI模式運(yùn)行jmeter:
備注:為什么要用命令行運(yùn)行Jmeter学辱?
GUI模式比較適用于測試創(chuàng)建和測試調(diào)試乘瓤;負(fù)載測試時环形,最好是使用NON GUI模式:
1、NON GUI模式能大大縮減所需要的系統(tǒng)資源衙傀;
2抬吟、GUI模式,會會消耗大量的內(nèi)存資源统抬,容易使壓力機(jī)本身達(dá)到瓶頸火本,從而影響測試結(jié)果
(例如:查看結(jié)果樹,會依次把每條請求的結(jié)果寫入內(nèi)存聪建,降低性能)钙畔;
3、GUI模式妆偏,不能支持大型的負(fù)載測試與性能測試刃鳄,并發(fā)超大時盅弛,JMeter會崩潰钱骂;
4、使用使用NON GUI模式執(zhí)行可以進(jìn)行自動化腳本編寫挪鹏,并持續(xù)集成到Jinkens上自動運(yùn)行见秽。
4.1、運(yùn)行命令之前讨盒,先在圖形化界面下生成測試文件解取,即user join.jmx文件
4.2、命令行方式啟動jmeter進(jìn)行業(yè)務(wù)壓測返顺,并生成測試報告:
[root@tpcc01 ~]# jmeter -n -t /root/jmeter/apache-jmeter-5.5/bin/'user join.jmx' -l a.csv -e -o myDashboard
參數(shù)解釋:
-n 非GUI模式 -> 在非GUI模式下運(yùn)行JMeter
-t 測試文件<參數(shù)> ->要運(yùn)行的jmeter測試(.jmx)文件
(若命令行模式在bin/文件夾下禀苦,則可以使用相對路徑,否則使用絕對路徑)
-l 日志文件<參數(shù)> ->生成的日志文件遂鹊,指定結(jié)果文件路徑 (同上)
-j 指定日志文件路徑 (同上)
-o 指定報表文件路徑(同上)
-R 指定遠(yuǎn)程服務(wù)器列表
-g 制定csv測試結(jié)果文件路徑(同上)
-e 測試完成生產(chǎn)報表
-r 設(shè)置分布式執(zhí)行
-H 設(shè)置JMeter設(shè)置代理服務(wù)器IP或域名
-P 設(shè)置JMeter代理服務(wù)器端口號