效率测试小工具runstats学习及应用示例


runstats是tom写的一款很好的基准测试小工具。其安装及测试示例如下。

以下测试工具为SQL TOOLS。

  1. 1./*以sys登陆,给u1cp授权四个视图的权限*/  
  2. grant select on v_$latch to u1cp;  
  3. grant select on v_$mystat to u1cp;  
  4. grant select on V_$timer to u1cp;  
  5. grant select on v_$statname to u1cp;  
  6.   
  7. 2./*在u1cp下建立sys视图的同义词*/  
  8. CREATE SYNONYM v_$latch FOR sys.v_$latch;  
  9. CREATE SYNONYM v_$mystat FOR sys.v_$mystat;  
  10. CREATE SYNONYM V_$timer FOR sys.V_$timer;  
  11. CREATE SYNONYM v_$statname FOR sys.v_$statname;  
  12.   
  13.   
  14. 3./*以u1cp登陆创建自己的统计视图。v$为v_$的同义词,v_$才是实际的底层视图,之前授权的就是v_$*/  
  15. create or replace view stats  
  16. as  
  17. select 'STAT..' || a.name name, b.value  
  18. from v$statname a, v$mystat b  
  19. where a.statistic# = b.statistic#  
  20. union all  
  21. select 'LATCH.' || name, gets  
  22. from v$latch  
  23. union all  
  24. select 'STAT...Elapsed time', hsecs from v$timer;  
  25.   
  26.   
  27. 4./*创建run_stats临时表*/  
  28. create global temporary table run_stats  
  29. ( runid varchar2(15),  
  30. name varchar2(80),  
  31. value int  
  32. )on commit preserve rows;  
  33.   
  34.   
  35. 5./*创建runstat包*/  
  36. create or replace package runstats_pkg  
  37. as   
  38.  procedure rs_start;  
  39.  procedure rs_middle;  
  40.  procedure rs_stop( p_difference_threshold in number default 0); --控制打印量,默认输出全部   
  41. end;  
  42. /  
  43. create or replace package body runstats_pkg  
  44. as  
  45.   
  46.   
  47. g_start number;  
  48. g_run1 number;  
  49. g_run2 number;  
  50.   
  51.   
  52. procedure rs_start  
  53. is  
  54. begin  
  55.  delete from run_stats;  
  56.  insert into run_stats select 'before',stats.* from stats;  
  57.  g_start := dbms_utility.get_cpu_time;  
  58. end;  
  59. procedure rs_middle  
  60. is  
  61. begin  
  62.  g_run1 := (dbms_utility.get_cpu_time-g_start);  
  63.  insert into run_stats select 'after 1', stats.* from stats;  
  64.  g_start := dbms_utility.get_cpu_time;  
  65. end;  
  66. procedure rs_stop(p_difference_threshold in number default 0)  
  67. is  
  68. begin  
  69.  g_run2 := (dbms_utility.get_cpu_time-g_start);  
  70.  dbms_output.put_line('Run1 ran in '||g_run1||' cpu hsecs');  
  71.  dbms_output.put_line('Run2 ran in '||g_run2||' cpu hsecs');  
  72.  if (g_run2 <> 0)  
  73.  then  
  74.   dbms_output.put_line('Run 1 ran in '||round(g_run1/g_run2*100,2)||' % of the time');  
  75.  end if;  
  76.  dbms_output.put_line(chr(9));  
  77.    
  78.  insert into run_stats select 'after 2', stats.* from stats;  
  79.  dbms_output.put_line(rpad('Name',30)||lpad('Run1',12)||lpad('Run2',12)||lpad('Diff',12));  
  80.    
  81.  for x in  
  82.  (select rpad(a.name,30)||to_char(b.value-a.value,'999,999,999')||to_char(c.value-b.value,'999,999,999')||to_char((c.value-b.value)-(b.value-a.value),'999,999,999') data  
  83.   from run_stats a, run_stats b,run_stats c  
  84.   where a.name=b.name and b.name=c.name and a.runid='before' and b.runid='after 1' and c.runid='after 2'   
  85.   and abs((c.value-b.value)-(b.value-a.value))>p_difference_threshold   
  86.   order by abs((c.value-b.value)-(b.value-a.value))  
  87.  ) loop  
  88.   dbms_output.put_line(x.data);  
  89.  end loop;  
  90.  dbms_output.put_line(chr(9));  
  91.    
  92.  dbms_output.put_line  
  93.  ( 'Run1 latches total versus runs -- difference and pct' );  
  94.  dbms_output.put_line  
  95.  ( lpad( 'Run1', 12 ) || lpad( 'Run2', 12 ) ||  
  96.  lpad( 'Diff', 12 ) || lpad( 'Pct', 10 ) );  
  97.    
  98.  for x in  
  99.  ( select to_char( run1, '999,999,999' ) ||  
  100.  to_char( run2, '999,999,999' ) ||  
  101.  to_char( diff, '999,999,999' ) ||  
  102.  to_char( round( run1/run2*100,2 ), '99,999.99' ) || '%' data  
  103.  from ( select sum(b.value-a.value) run1, sum(c.value-b.value) run2,  
  104.  sum( (c.value-b.value)-(b.value-a.value)) diff  
  105.  from run_stats a, run_stats b, run_stats c  
  106.  where a.name = b.name  
  107.  and b.name = c.name  
  108.  and a.runid = 'before'  
  109.  and b.runid = 'after 1'  
  110.  and c.runid = 'after 2'  
  111.  and a.name like 'LATCH%'  
  112.  )  
  113.  ) loop  
  114.  dbms_output.put_line( x.data );  
  115.  end loop;  
  116. end;  
  117. end;   
  118.   
  119.   
  120.   
  121. --=================================测试==================================   
  122.   
  123. create table t1 (id number);  
  124. create table t2 (id number);  
  125.   
  126. exec runstats_pkg.rs_start;  
  127. Begin  
  128. for i in 1..10000 Loop  
  129. insert into t1 values(i);  
  130. end loop;  
  131. end;   
  132.   
  133.   
  134. exec runstats_pkg.rs_middle;  
  135. Begin  
  136. for i in 1..10000 Loop  
  137. Execute Immediate 'insert into t2 values('||i||')';  
  138. end loop;  
  139. end;--SQL拼接   
  140. exec runstats_pkg.rs_stop;  
  141.   
  142.   
  143. output:  
  144.   
  145. 18  PL/SQL block, executed in 0.172 sec.                                
  146.     Run1 ran in 2 cpu hsecs                                             
  147.     Run2 ran in 744 cpu hsecs                                           
  148.     Run 1 ran in .27 % of the time                                      
  149.                                                                         
  150.     Name                                  Run1        Run2        Diff  
  151.     LATCH.job_queue_processes para           0           1           1  
  152.     LATCH.ncodef allocation latch            0           1           1  
  153.     LATCH.kwqbsn:qsga                        1           0          -1  
  154.     LATCH.threshold alerts latch             1           0          -1  
  155.     LATCH.active checkpoint queue            5           4          -1  
  156.     LATCH.transaction branch alloc           0           1           1  
  157.     LATCH.sort extent pool                   0           1           1  
  158.     LATCH.resmgr:actses change gro           0           1           1  
  159.     LATCH.Shared B-Tree                      1           0          -1  
  160.     STAT..parse count (failures)             1           0          -1  
  161.     LATCH.session switching                  0           1           1  
  162.     LATCH.ksuosstats global area             0           1           1  
  163.     LATCH.event group latch                  0           1           1  
  164.     LATCH.FOB s.o list latch                 0           1           1  
  165.     STAT..rows fetched via callbac           0           1           1  
  166.     STAT..session cursor cache cou           0          -1          -1  
  167.     LATCH.ncodef allocation latch            0           1           1  
  168.     LATCH.kwqbsn:qsga                        0           1           1  
  169.     LATCH.threshold alerts latch             0           1           1  
  170.     STAT..messages sent                      1           0          -1  
  171.     LATCH.transaction branch alloc           0           1           1  
  172.     LATCH.sort extent pool                   0           1           1  
  173.     LATCH.resmgr:actses change gro           0           1           1  
  174.     LATCH.Shared B-Tree                      0           1           1  
  175.     LATCH.job_queue_processes para           0           1           1  
  176.     LATCH.session switching                  0           1           1  
  177.     LATCH.ksuosstats global area             0           1           1  
  178.     LATCH.event group latch                  0           1           1  
  179.     LATCH.FOB s.o list latch                 0           1           1  
  180.     LATCH.active checkpoint queue            4           5           1  
  181.     STAT..rows fetched via callbac           0           1           1  
  182.     STAT..session cursor cache cou           0          -1          -1  
  183.     STAT..parse count (failures)             0           1           1  
  184.     STAT..messages sent                      1           0          -1  
  185.     LATCH.session timer                      2           4           2  
  186.     LATCH.KMG MMAN ready and start           2           4           2  
  187.     LATCH.checkpoint queue latch            68          70           2  
  188.     LATCH.list of block allocation           1           3           2  
  189.     LATCH.transaction allocation             0           2           2  
  190.     LATCH.user lock                          0           2           2  
  191.     LATCH.post/wait queue                    4           2          -2  
  192.     LATCH.process allocation                 0           2           2  
  193.     LATCH.process group creation             0           2           2  
  194.     LATCH.parameter table allocati           0           2           2  
  195.     LATCH.channel handle pool latc           0           2           2  
  196.     LATCH.OS process: request allo           0           2           2  
  197.     STAT..cursor authentications             2           0          -2  
  198.     STAT..redo buffer allocation r           2           0          -2  
  199.     STAT..redo log space requests            2           0          -2  
  200.     LATCH.list of block allocation           1           3           2  
  201.     LATCH.transaction allocation             0           2           2  
  202.     LATCH.user lock                          0           2           2  
  203.     LATCH.post/wait queue                    4           2          -2  
  204.     LATCH.process allocation                 0           2           2  
  205.     LATCH.process group creation             0           2           2  
  206.     LATCH.parameter table allocati           0           2           2  
  207.     LATCH.channel handle pool latc           0           2           2  
  208.     LATCH.OS process: request allo           0           2           2  
  209.     STAT..redo buffer allocation r           2           0          -2  
  210.     STAT..redo log space requests            2           0          -2  
  211.     LATCH.JS slv state obj latch             0           3           3  
  212.     LATCH.mostly latch-free SCN              3           6           3  
  213.     LATCH.lgwr LWN SCN                       3           6           3  
  214.     LATCH.Consistent RBA                     3           6           3  
  215.     LATCH.JS slv state obj latch             0           3           3  
  216.     LATCH.parallel query alloc buf           4           0          -4  
  217.     LATCH.compile environment latc           5           9           4  
  218.     LATCH.session state list latch           0           4           4  
  219.     LATCH.qmn task queue latch               4           0          -4  
  220.     LATCH.PL/SQL warning settings           13           9          -4  
  221.     LATCH.cache buffers lru chain           18          22           4  
  222.     LATCH.dummy allocation                   0           4           4  
  223.     LATCH.resmgr:actses active lis           0           4           4  
  224.     LATCH.resmgr:free threads list           0           4           4  
  225.     STAT..buffer is pinned count             0           4           4  
  226.     LATCH.session state list latch           0           4           4  
  227.     LATCH.qmn task queue latch               0           4           4  
  228.     LATCH.dummy allocation                   0           4           4  
  229.     LATCH.resmgr:actses active lis           0           4           4  
  230.     LATCH.resmgr:free threads list           0           4           4  
  231.     LATCH.session timer                      1           5           4  
  232.     LATCH.parallel query alloc buf           0           4           4  
  233.     LATCH.compile environment latc           5           9           4  
  234.     LATCH.KMG MMAN ready and start           1           5           4  
  235.     LATCH.cache buffers lru chain           18          22           4  
  236.     STAT..heap block compress                6          10           4  
  237.     STAT..buffer is pinned count             0           4           4  
  238.     LATCH.object queue header oper         130         135           5  
  239.     LATCH.redo writing                      23          28           5  
  240.     LATCH.resmgr group change latc           0           5           5  
  241.     STAT..active txn count during           25          20          -5  
  242.     STAT..cleanout - number of ktu          25          20          -5  
  243.     STAT..calls to kcmgcs                   25          20          -5  
  244.     LATCH.resmgr group change latc           0           5           5  
  245.     LATCH.Consistent RBA                     2           7           5  
  246.     STAT..calls to kcmgcs                   20          25           5  
  247.     STAT..active txn count during           20          25           5  
  248.     STAT..cleanout - number of ktu          20          25           5  
  249.     STAT..SQL*Net roundtrips to/fr          12           6          -6  
  250.     STAT..heap block compress               11           5          -6  
  251.     STAT..SQL*Net roundtrips to/fr           6          12           6  
  252.     STAT..workarea executions - op          13          20           7  
  253.     LATCH.OS process allocation              3          10           7  
  254.     LATCH.mostly latch-free SCN              1           8           7  
  255.     LATCH.lgwr LWN SCN                       1           8           7  
  256.     LATCH.object queue header oper         129         136           7  
  257.     LATCH.session idle bit                  49          57           8  
  258.     STAT..workarea memory allocate          -1           7           8  
  259.     LATCH.PL/SQL warning settings            7          15           8  
  260.     STAT..redo entries                  10,108      10,116           8  
  261.     STAT..workarea memory allocate          -1           7           8  
  262.     LATCH.OS process                         0           9           9  
  263.     LATCH.OS process allocation              2          11           9  
  264.     LATCH.channel operations paren          55          64           9  
  265.     LATCH.OS process                         0           9           9  
  266.     STAT..user calls                        10          20          10  
  267.     STAT..change write time                  2          12          10  
  268.     STAT..redo entries                  10,117      10,107         -10  
  269.     STAT..user calls                        20          10         -10  
  270.     STAT..change write time                  2          12          10  
  271.     LATCH.active service list               18          30          12  
  272.     STAT..shared hash latch upgrad           4          16          12  
  273.     STAT..index scans kdiixs1                4          16          12  
  274.     STAT..redo log space wait time          12           0         -12  
  275.     STAT..shared hash latch upgrad           4          16          12  
  276.     STAT..redo log space wait time          12           0         -12  
  277.     STAT..index scans kdiixs1                4          16          12  
  278.     LATCH.library cache pin alloca           0          13          13  
  279.     LATCH.library cache pin alloca           0          13          13  
  280.     LATCH.redo allocation                   71          55         -16  
  281.     STAT..table fetch by rowid               0          19          19  
  282.     STAT..workarea executions - op           7          26          19  
  283.     STAT..consistent changes                40          21         -19  
  284.     LATCH.undo global data                  40          59          19  
  285.     STAT..table fetch by rowid               0          19          19  
  286.     STAT..consistent changes                21          40          19  
  287.     LATCH.library cache lock alloc           3          24          21  
  288.     LATCH.redo writing                      15          36          21  
  289.     LATCH.library cache lock alloc           3          24          21  
  290.     LATCH.client/application info            0          25          25  
  291.     LATCH.client/application info            0          25          25  
  292.     LATCH.messages                          60          86          26  
  293.     STAT..db block changes              20,258      20,230         -28  
  294.     STAT..db block changes              20,230      20,258          28  
  295.     LATCH.active service list               10          38          28  
  296.     LATCH.undo global data                  35          64          29  
  297.     STAT..cluster key scans                  4          37          33  
  298.     LATCH.In memory undo latch              10          43          33  
  299.     STAT..cluster key scans                  4          37          33  
  300.     LATCH.redo allocation                   82          44         -38  
  301.     STAT..index fetch by key                 5          44          39  
  302.     STAT..index fetch by key                 5          44          39  
  303.     STAT..sorts (memory)                     7          47          40  
  304.     STAT..cluster key scan block g           6          47          41  
  305.     STAT..cluster key scan block g           6          47          41  
  306.     STAT..sorts (memory)                     6          48          42  
  307.     STAT..session cursor cache hit          10          55          45  
  308.     LATCH.dml lock allocation               21          66          45  
  309.     LATCH.In memory undo latch               2          51          49  
  310.     STAT..execute count                 10,024      10,074          50  
  311.     LATCH.session idle bit                  27          79          52  
  312.     STAT..session cursor cache hit           6          59          53  
  313.     STAT..opened cursors cumulativ          27          83          56  
  314.     STAT..no work - consistent rea           8          67          59  
  315.     STAT..no work - consistent rea           8          67          59  
  316.     STAT..execute count                 10,018      10,080          62  
  317.     LATCH.channel operations paren          28          91          63  
  318.     LATCH.library cache load lock            8          74          66  
  319.     LATCH.library cache load lock            8          74          66  
  320.     STAT..opened cursors cumulativ          21          89          68  
  321.     LATCH.JS queue state obj latch          36         108          72  
  322.     STAT..buffer is not pinned cou           6          81          75  
  323.     STAT..buffer is not pinned cou           6          81          75  
  324.     LATCH.checkpoint queue latch            31         107          76  
  325.     LATCH.dml lock allocation                5          82          77  
  326.     LATCH.messages                          34         112          78  
  327.     STAT..consistent gets - examin          40         126          86  
  328.     STAT..consistent gets - examin          35         131          96  
  329.     STAT...Elapsed time                    819       1,087         268  
  330.     LATCH.SQL memory manager worka          75         355         280  
  331.     STAT..parse time elapsed                 5         636         631  
  332.     STAT..parse time cpu                     2         639         637  
  333.     STAT..parse time elapsed                 0         641         641  
  334.     STAT..parse time cpu                     0         641         641  
  335.     STAT..recursive cpu usage               40         717         677  
  336.     STAT..DB time                           65         745         680  
  337.     STAT..recursive cpu usage               35         722         687  
  338.     STAT..CPU used when call start          49         745         696  
  339.     STAT..DB time                           57         753         696  
  340.     STAT..CPU used by this session          47         751         704  
  341.     STAT..CPU used when call start          44         750         706  
  342.     STAT..CPU used by this session          42         756         714  
  343.     LATCH.simulator lru latch                7         727         720  
  344.     LATCH.simulator hash latch              10         730         720  
  345.     LATCH.simulator lru latch                5         729         724  
  346.     LATCH.simulator hash latch               8         732         724  
  347.     STAT...Elapsed time                    398       1,508       1,110  
  348.     STAT..bytes sent via SQL*Net t       2,120         882      -1,238  
  349.     STAT..bytes sent via SQL*Net t         882       2,120       1,238  
  350.     STAT..bytes received via SQL*N       2,807       1,426      -1,381  
  351.     STAT..bytes received via SQL*N       1,393       2,840       1,447  
  352.     LATCH.session allocation               236       1,979       1,743  
  353.     LATCH.session allocation               236       1,979       1,743  
  354.     STAT..undo change vector size      643,096     645,148       2,052  
  355.     STAT..undo change vector size      645,156     643,088      -2,068  
  356.     STAT..sorts (rows)                   4,685       2,421      -2,264  
  357.     STAT..sorts (rows)                   2,343       4,763       2,420  
  358.     STAT..redo size                  2,378,192   2,380,624       2,432  
  359.     STAT..redo size                  2,380,900   2,377,916      -2,984  
  360.     STAT..enqueue releases                  19      10,024      10,005  
  361.     STAT..enqueue requests                  20      10,025      10,005  
  362.     STAT..parse count (hard)                 4      10,010      10,006  
  363.     STAT..enqueue releases                  18      10,025      10,007  
  364.     STAT..enqueue requests                  19      10,026      10,007  
  365.     STAT..parse count (hard)                 3      10,011      10,008  
  366.     STAT..parse count (total)               26      10,043      10,017  
  367.     STAT..parse count (total)               19      10,050      10,031  
  368.     STAT..calls to get snapshot sc          32      10,084      10,052  
  369.     STAT..calls to get snapshot sc          31      10,085      10,054  
  370.     STAT..consistent gets                   85      10,233      10,148  
  371.     STAT..consistent gets from cac          85      10,233      10,148  
  372.     STAT..consistent gets from cac          74      10,244      10,170  
  373.     STAT..consistent gets                   74      10,244      10,170  
  374.     STAT..recursive calls               10,193      20,871      10,678  
  375.     STAT..recursive calls               10,191      20,873      10,682  
  376.     STAT..db block gets from cache      10,385      30,341      19,956  
  377.     STAT..db block gets                 10,385      30,341      19,956  
  378.     STAT..db block gets from cache      10,362      30,364      20,002  
  379.     STAT..db block gets                 10,362      30,364      20,002  
  380.     LATCH.enqueues                         197      20,222      20,025  
  381.     LATCH.enqueue hash chains              221      20,295      20,074  
  382.     LATCH.enqueues                         115      20,304      20,189  
  383.     LATCH.enqueue hash chains              122      20,394      20,272  
  384.     STAT..session logical reads         10,470      40,574      30,104  
  385.     STAT..session logical reads         10,436      40,608      30,172  
  386.     LATCH.kks stats                          6      37,782      37,776  
  387.     LATCH.kks stats                          6      37,782      37,776  
  388.     LATCH.library cache pin             20,337      70,613      50,276  
  389.     LATCH.library cache pin             20,271      70,679      50,408  
  390.     LATCH.library cache lock               239      60,453      60,214  
  391.     LATCH.library cache lock               221      60,471      60,250  
  392.     LATCH.cache buffers chains          51,373     113,607      62,234  
  393.     LATCH.cache buffers chains          51,251     113,729      62,478  
  394.     LATCH.row cache objects                151     120,611     120,460  
  395.     LATCH.row cache objects                133     120,629     120,496  
  396.     LATCH.library cache                 20,574     234,325     213,751  
  397.     LATCH.library cache                 20,473     234,426     213,953  
  398.     LATCH.shared pool                   10,155     229,543     219,388  
  399.     LATCH.shared pool                   10,111     229,587     219,476  
  400.     STAT..session pga memory                 0     262,144     262,144  
  401.     STAT..session pga memory                 0     262,144     262,144  
  402.                                                                         
  403.     Run1 latches total versus runs -- difference and pct                 
  404.             Run1        Run2        Diff       Pct                      
  405.          207,967   1,825,217   1,617,250     11.39%                     
  406.     Total execution time 0.219 sec.                                     
  407.    
  408.   
  409. ps:runstats_pkg.rs_stop不带参数会输出全部讯息,也可带参数(参数表示前后差异数超过的数目)  
  410.   
  411. eg. runstats_pkg.rs_stop(1000)表示前后差异数超过1000的讯息(不包含1000)。  

相关内容

    暂无相关文章