Oracle replay工具用法

一、 简介

Oracle 11g推出的神器,可以抓取生产环境真实负载(sql语句),在目标环境重演,进行对比。在核心生产环境迁移等重大变更前,尽量确定变更带来的影响。

Oracle replay工具用法_第1张图片

核心步骤包括:准备工作、捕获负载、预处理负载、重演负载、对比分析,每步又包含多个子步骤,下面通过测试案例来看。

二、 准备工作

  • 源与目标库版本要一致,测试中使用19.3版本
  • 源与目标库启用AWR,并设置好采集间隔和保留时间
  • 目标库要有源库中数据,通过dump、rman、dg等同步均可
-- 本例中我们只建一个表
-- 源与目标库均执行
create table mytab (a varchar2(2000));
  • 目标库参数等要根据日后实际需求配置好
  • 查看文档 Doc ID 560977.1,根据源和目标库版本打文档要求补丁

三、捕获源库负载

  • 建立负载过滤器
--建立过滤器filter_test,用于过滤掉user为system的规则
BEGIN
DBMS_WORKLOAD_CAPTURE.ADD_FILTER (
fname => 'filter_test',
fattribute => 'USER',
fvalue => 'SYSTEM');
END;
/
  • 建立负载文件存储目录
create directory test_dir as '/data/bak';
  • 开始捕获负载
BEGIN
DBMS_WORKLOAD_CAPTURE.START_CAPTURE (name => 'peak_120s',
dir => 'TEST_DIR',
duration => 120);
END;
/

注意事项

  • 目录不能有其他文件,否则会报错ORA-15505
  • dir => 'DIR_NAME' 目录名要大写,否则会报错ORA-20222
  • duration => 120,指定120秒后自动结束。如果未指定,或者想提前结束,需要使用FINISH_CAPTURE存储过程来停止。
  • 另开一个窗口,执行负载脚本
declare
  l_stmt varchar2(2000);
begin
  for ctr in 1..100000 loop
     l_stmt := 'insert into mytab values (''''||
     lpad(round(dbms_random.value(1,9999999999999999999)),20,0)||'''')';
     dbms_output.put_line(l_stmt);
     execute immediate l_stmt;
     commit;
  end loop;
end;
/
  • 停止捕获负载

等待指定时间后自动强制,或使用FINISH_CAPTURE存储过程手动停止

exec dbms_workload_capture.FINISH_CAPTURE();
  • 查看捕获的负载AWR数据
[oracle@source-db ~]$ cd /data/bak/
[oracle@source-db bak]$ ll -h
total 0
drwxr-xr-x 2 oracle oinstall 92 Jul 21 15:13 cap
drwxr-xr-x 3 oracle oinstall 19 Jul 21 15:12 capfiles
[oracle@source-db bak]$ 
[oracle@source-db bak]$ cd cap
[oracle@source-db cap]$ ls
wcr_cr.html  wcr_cr.text  wcr_fcapture.wmd  wcr_scapture.wmd
[oracle@source-db cap]$ 
[oracle@source-db cap]$ cd ../capfiles/
[oracle@source-db capfiles]$ ls
inst1
[oracle@source-db capfiles]$ cd inst1/
[oracle@source-db inst1]$ ls
aa  ab  ac  ad  ae  af  ag  ah  ai  aj
[oracle@source-db inst1]$ cd aa/
[oracle@source-db aa]$ ls
wcr_ggk6th0000000.rec  wcr_ggk8ch0000001.rec

至此源库捕获工作完成,剩下的在目标库执行

四、目标库补丁安装

适用于较低版本的目标库,可参考 Mandatory Patches for Database Testing Functionality for Current and Earlier Releases

例如 11.2.0.3版本需要安装17411249补丁,19.3版本则无需安装

安装过程与普通补丁无异,参考readme.txt即可

五、 预处理负载

  • 目标库创建目录,上传负载文件
mkdir /data/bak
chown ora.dba -R /data/bak/
#上传文件后
[oracle@dest-db bak]$ ls
cap  capfiles 
  • 预处理负载

将捕获文件转换为重演文件,此步骤耗时耗资源,请在目标库进行,不要在生产库上进行。注意要让SYSAUX有足够的空间剩余,不然会报错ORA-1658。

BEGIN
DBMS_WORKLOAD_REPLAY.PROCESS_CAPTURE (capture_dir => 'TEST_DIR');
END;
/

预处理后会生成一个pp19.3.0.0.0文件夹,里面是待重演文件

[oracle@dest-db bak]$ ls
cap  capfiles  pp19.3.0.0.0
[oracle@dest-db bak]$ cd pp19.3.0.0.0/
[oracle@dest-db pp19.3.0.0.0]$ ls
capfiles           wcr_conn_data.extb  wcr_login.extb   wcr_references.extb   wcr_seq_data.extb
wcr_calibrate.xml  wcr_data.extb       wcr_login.pp     wcr_schema_info.extb  wcr_sqltext.extb
wcr_commits.extb   wcr_dep_graph.extb  wcr_process.wmd  wcr_scn_order.extb    wcr_xa.extb

六、 重演负载

wrc(Workload Replay Client)用于模拟将原生产库上的客户端的负载请求,模拟到测试库上。

1. wrc校准

wrc mode=calibrate replaydir=/home/oracle/bak

[oracle@dest-db bak]$ wrc mode=calibrate replaydir=/home/oracle/bak

Workload Replay Client: Release 19.3.0.0.0 - Production on Wed Jul 21 16:51:04 2021

Copyright (c) 1982, 2019, Oracle and/or its affiliates.  All rights reserved.


Report for Workload in: /home/oracle/bak
-----------------------

Recommendation:
Consider using at least 1 clients divided among 1 CPU(s)
You will need at least 3 MB of memory per client process.
If your machine(s) cannot match that number, consider using more clients.

Workload Characteristics:
- max concurrency: 1 sessions
- total number of sessions: 2

Assumptions:
- 1 client process per 100 concurrent sessions
- 4 client processes per CPU
- 256 KB of memory cache per concurrent session
- think time scale = 100
- connect time scale = 100
- synchronization = TRUE

2. 初始化重演

BEGIN
DBMS_WORKLOAD_REPLAY.INITIALIZE_REPLAY (replay_name => 'peak_120s',
replay_dir => 'TEST_DIR');
END;
/

Oracle replay工具用法_第2张图片

3. 连接映射

-- 映射连接
select conn_id,capture_conn,replay_conn from dba_workload_connection_map where replay_id=1;

-- 修改为测试库连接
-- 改HOST, PORT, SERVICE_NAME
select 'exec dbms_workload_replay.remap_connection('||conn_id||', ''(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.80.99)(PORT = 1521)) (CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME= PDBREPLAY)))'');' from dba_workload_connection_map where replay_id=1;

执行上面拼接出来的语句

4. 设置负载重演的选项

-- synchronization 是否完全按捕获时间线replay;connect_time_scale,think_time_scale 将时间设置为源库的50%,加大压测压力

BEGIN
dbms_workload_replay.PREPARE_REPLAY(synchronization => false,connect_time_scale => 50,think_time_scale => 50);
END;
/

Oracle replay工具用法_第3张图片

4. wrc重演

wrc system mode=replay replaydir=/home/oracle/bak

5. 负载重演

另开一个窗口

BEGIN
DBMS_WORKLOAD_REPLAY.START_REPLAY ();
END;
/

Oracle replay工具用法_第4张图片

6. 结束负载重演

等待负载执行完或者手动结束

BEGIN
DBMS_WORKLOAD_REPLAY.CANCEL_REPLAY ();
END;
/

结束后回到wrc的窗口,会看到如下输出

[oracle@dest-db bak]$ wrc system mode=replay replaydir=/home/oracle/bak 

Workload Replay Client: Release 19.3.0.0.0 - Production on Wed Jul 21 17:23:33 2021

Copyright (c) 1982, 2019, Oracle and/or its affiliates.  All rights reserved.


Password: 
Wait for the replay to start (17:23:40)
Replay client 1 started (17:23:59) <--------
Replay client 1 finished (17:27:10) <--------

7. 检查是否确实重演

select name,status from dba_workload_replays;

NAME           STATUS
-------------  ----------------------------
peak_120s      COMPLETED

也可以通过检查数据验证

-- 源库
SQL> select count(*) from mytab;

  COUNT(*)
----------
    100000

-- 目标库
SQL> select count(*) from mytab;

  COUNT(*)
----------
    100000

8. 查看replay报告

set pagesize 0 long 30000000 longchunksize 1000
select dbms_workload_replay.report(1,'TEXT') from dual;

-- 输出结果
DB Replay Report for test_replay_1
---------------------------------------------------------------------------
| DB Name  | DB Id     | Release    | RAC | Replay Name   | Replay Status |
---------------------------------------------------------------------------
| JCREPLAY | 147275354 | 11.1.0.5.0 | NO  | test_replay_1 | COMPLETED     |
---------------------------------------------------------------------------

Replay Information
--------------------------------------------------------------------------------------------------
|   Information    | Replay                               | Capture                 |
--------------------------------------------------------------------------------------------------
| Name             | test_replay_1                        | test_capture_2                 |
--------------------------------------------------------------------------------------------------
| Status           | COMPLETED                            | COMPLETED                 |
--------------------------------------------------------------------------------------------------
| Database Name    | JCREPLAY                             | V11B5                 |
--------------------------------------------------------------------------------------------------
| Database Version | 11.1.0.5.0                           | 11.1.0.5.0                 |
--------------------------------------------------------------------------------------------------
| Start Time       | 25-JUL-07 16:19:27                   | 25-JUL-07 14:09:49                 |
--------------------------------------------------------------------------------------------------
| End Time         | 25-JUL-07 16:20:31                   | 25-JUL-07 14:12:11                 |
--------------------------------------------------------------------------------------------------
| Duration         | 1 minute 4 seconds                   | 2 minutes 22 seconds                 |
--------------------------------------------------------------------------------------------------
| Directory Object | MY_WORKLOAD_DIR                      | MY_WORKLOAD_DIR                 |
--------------------------------------------------------------------------------------------------
| Directory Path   | /home/%usernm%/my_workload_directory | /home/%usernm%/my_workload_directory |
--------------------------------------------------------------------------------------------------

Replay Options
---------------------------------------------------------
|       Option Name       | Value                       |
---------------------------------------------------------
| Synchronization         | TRUE                        |
---------------------------------------------------------
| Connect Time            | 100%                        |
---------------------------------------------------------
| Think Time              | 100%                        |
---------------------------------------------------------
| Think Time Auto Correct | TRUE                        |
---------------------------------------------------------
| Number of WRC Clients   | 1 (1 Completed, 0 Running ) |
---------------------------------------------------------

Replay Statistics
------------------------------------------------
|        Statistic        | Replay   | Capture |
------------------------------------------------
| DB Time                 | 932061   | 1006294 |
------------------------------------------------
| Average Active Sessions | .01      | .01     |
------------------------------------------------
| User calls              | 46       | 50      |
------------------------------------------------
| Network Time            | 2724     | .       |
------------------------------------------------
| Think Time              | 17159449 | .       |
------------------------------------------------
| Elapsed Time Difference | 148907   | .       |
------------------------------------------------
| New Errors              | 0        | .       |
------------------------------------------------
| Mutated Errors          | 0        | .       |
------------------------------------------------
---------------------------------------------------------------------------------------------------
Workload Profile Top Events (+) Show (-) Hide
--------------------------------------------------
| No data exists for this section of the report. |
--------------------------------------------------
Top Service/Module/Action (+) Show (-) Hide
--------------------------------------------------
| No data exists for this section of the report. |
--------------------------------------------------
Top SQL with Top Events (+) Show (-) Hide
--------------------------------------------------
| No data exists for this section of the report. |
--------------------------------------------------
Top Sessions with Top Events (+) Show (-) Hide
--------------------------------------------------
| No data exists for this section of the report. |
--------------------------------------------------
Replay Divergence Session Failures By Application (+) Show (-) Hide
--------------------------------------------------
| No data exists for this section of the report. |
--------------------------------------------------
Error Divergence By Application (+) Show (-) Hide
--------------------------------------------------
| No data exists for this section of the report. |
--------------------------------------------------
By SQL (+) Show (-) Hide
--------------------------------------------------
| No data exists for this section of the report. |
--------------------------------------------------
By Session (+) Show (-) Hide
--------------------------------------------------
| No data exists for this section of the report. |
--------------------------------------------------
DML Data Divergence By Application (+) Show (-) Hide
--------------------------------------------------
| No data exists for this section of the report. |
--------------------------------------------------
By SQL (+) Show (-) Hide
--------------------------------------------------
| No data exists for this section of the report. |
--------------------------------------------------
SELECT Data Divergence By Application (+) Show (-) Hide
--------------------------------------------------
| No data exists for this section of the report. |
--------------------------------------------------

End of Report.

最后,对比分析监控及AWR数据,这就属于性能优化方面的内容了,这里不再多说。

参考 

11g新特性-重演(replay) – OracleBlog

Oracle 19c迁移前,做好RAT回放测试有多重要? - 云+社区 - 腾讯云

【RAT】Oracle Real Application Testing(真用应用测试)介绍_ITPUB博客

Master Note for Real Application Testing Option (Doc ID 1464274.1)

Database Testing: Best Practices (Doc ID 1535885.1)

Mandatory Patches for Database Testing Functionality for Current and Earlier Releases (Doc ID 560977.1)

Using Workload Capture and Replay (Doc ID 445116.1)

How to Setup and Run a Database Testing Replay in an Oracle Multitenant Environment (Real Application Testing - RAT) (Doc ID 1937920.1)

你可能感兴趣的:(Oracle,性能,oracle,数据库)