您的位置:首页 > 数据库 > Oracle

Oracle PL/SQL编程之如何实现程序来统计另一个程序的性能(接上一篇 优化后)

2013-03-12 15:15 791 查看
不多说,直接上代码

/*******创建表ct用于保存第一次运行程序采集到的信息和优化后采集到的信息*******/

--drop table ct;
--create table ct(name varchar2(50),firstvalue number(8));
/*******创建表isFirst用于标识要测试的程序是第一次运行还是优化后运行*******/

--drop table isFirst;
--create table isFirst(first number(1));
--insert into isFirst values(0);
/************
设计思路:
oracle中创建以上两张表,当第一次运行被测试程序是,本程序采集被测试程序运行信息并存储到表ct中firstvalue列中,
同时修改isFirst表中first的值为1,表示被测试程序已经运行第一次。同时输出采集到的信息。当被测试程序优化后再次
运行,本程序采集被测试程序的运行信息插入到表ct中secondvalue中,同时修改isFirst表中first的值为0,将表中firstvalue
和secondvalue字段比较得出差值输出给用户即可。同时删除表ct中的行,节省空间。
************/

declare
b_isfirst number(1);
i_sid binary_integer :=0;
begin
select sid into i_sid from v$mystat where rownum = 1;
select first into b_isfirst from isFirst; --判断是否为第一次运行程序

declare
begin
if b_isfirst = 0 then
insert into ct select stat_name, value from V$SESS_TIME_MODEL where sid=i_sid and stat_name in('DB time','DB CPU');
insert into ct
select name,value
from v$sesstat a,v$statname b
where a.STATISTIC#=b.STATISTIC# and
sid=i_sid and
name in ('session logical reads',
'physical read IO requests',
'physical write IO requests');
commit;
dbms_output.put_line('**************采集******************');
for cur1 in (select name,firstvalue from ct) loop
dbms_output.put_line(cur1.name||':'||cur1.firstvalue);
end loop;
dbms_output.put_line('**************采集完毕**************');
update isFirst set first=1;
commit;
else
dbms_output.put_line('**************比较******************');
for cur1 in (select stat_name, value from V$SESS_TIME_MODEL where sid=i_sid and stat_name in('DB time','DB CPU')) loop
for cur2 in (select * from ct where name=cur1.stat_name ) loop
dbms_output.put_line(cur1.stat_name||' increase:'||(cur1.value-cur2.firstvalue));
end loop;
end
4000
loop;

for cur1 in (select name,value from v$sesstat a,v$statname b
where a.STATISTIC#=b.STATISTIC# and sid=i_sid and name in
('session logical reads','physical read IO requests',
'physical write IO requests') ) loop
for cur2 in (select * from ct where name=cur1.name ) loop
dbms_output.put_line(cur1.name||' increase:'||(cur1.value-cur2.firstvalue));
end loop;
end loop;
dbms_output.put_line('**************比较完毕**************');
update isFirst set first=0;
commit;
delete from ct;
commit;
end if;
end;
end;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息