Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Access to temporary tables improvements.
Moved temp tables cleanup into ut_utils (authid definer), so that we can use truncate rather then delete.
Refactored ut_coverage and ut_coverage_helper.
Changed the RunAll script to use packaged version of mystats.
  • Loading branch information
jgebal committed Jul 21, 2017
commit 9c19355b071ca3a9c2971311180e36340c95d35c
11 changes: 2 additions & 9 deletions source/api/ut_runner.pkb
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,6 @@ create or replace package body ut_runner is
return ut_utils.gc_version;
end;

procedure cleanup_temp_tables is
pragma autonomous_transaction;
begin
delete from ut_cursor_data where 1 = 1;
commit;
end;

procedure run(
a_paths ut_varchar2_list, a_reporters ut_reporters, a_color_console boolean := false,
a_coverage_schemes ut_varchar2_list := null, a_source_file_mappings ut_file_mappings := null, a_test_file_mappings ut_file_mappings := null,
Expand Down Expand Up @@ -78,11 +71,11 @@ create or replace package body ut_runner is
);
l_items_to_run.do_execute(l_listener);

cleanup_temp_tables;
ut_utils.cleanup_temp_tables;
ut_output_buffer.close(l_listener.reporters);
exception
when others then
cleanup_temp_tables;
ut_utils.cleanup_temp_tables;
ut_output_buffer.close(l_listener.reporters);
dbms_output.put_line(dbms_utility.format_error_backtrace);
dbms_output.put_line(dbms_utility.format_error_stack);
Expand Down
56 changes: 34 additions & 22 deletions source/core/coverage/ut_coverage.pkb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ create or replace package body ut_coverage is
-- The subquery is optimized by:
-- - COALESCE function -> it will execute only for TRIGGERS
-- - scalar subquery cache -> it will only execute once for one trigger source code.
function populate_sources_tmp_table(a_coverage_options ut_coverage_options) return varchar2 is
function get_populate_sources_tmp_sql(a_coverage_options ut_coverage_options) return varchar2 is
l_result varchar2(32767);
l_full_name varchar2(100);
begin
Expand Down Expand Up @@ -98,17 +98,46 @@ create or replace package body ut_coverage is
return l_result;
end;

function is_tmp_table_populated return boolean is
l_result integer;
begin
select 1 into l_result from ut_coverage_sources_tmp where rownum = 1;
return (l_result = 1);
exception
when no_data_found then
return false;
end;

procedure populate_tmp_table(a_coverage_options ut_coverage_options, a_skipped_objects ut_object_names) is
pragma autonomous_transaction;
l_schema_names ut_varchar2_rows;
begin
delete from ut_coverage_sources_tmp;
l_schema_names := coalesce(a_coverage_options.schema_names, ut_varchar2_rows(sys_context('USERENV','CURRENT_SCHEMA')));
if a_coverage_options.file_mappings is not empty then
execute immediate get_populate_sources_tmp_sql(a_coverage_options) using a_coverage_options.file_mappings, a_skipped_objects, a_coverage_options.include_objects;
else
execute immediate get_populate_sources_tmp_sql(a_coverage_options) using l_schema_names, a_skipped_objects, a_coverage_options.include_objects;
end if;
commit;
end;


/**
* Public functions
*/
procedure coverage_start is
begin
ut_coverage_helper.coverage_start('utPLSQL Code coverage run '||ut_utils.to_string(systimestamp));
if not ut_coverage_helper.is_started() then
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we schec the state here and perform the same check in tha package itself.
Maybe we can remove the chack from here and leave it only in ut_coverage_helper?

ut_coverage_helper.coverage_start('utPLSQL Code coverage run '||ut_utils.to_string(systimestamp));
end if;
end;

procedure coverage_start_develop is
begin
ut_coverage_helper.coverage_start_develop();
if not ut_coverage_helper.is_started() then
ut_coverage_helper.coverage_start_develop();
end if;
end;

procedure coverage_pause is
Expand All @@ -132,41 +161,24 @@ create or replace package body ut_coverage is
end;

function get_coverage_data(a_coverage_options ut_coverage_options) return t_coverage is

pragma autonomous_transaction;

type t_coverage_row is record(
name varchar2(500),
line_number integer,
total_occur number(38,0)
);
type tt_coverage_rows is table of t_coverage_row;
l_line_calls ut_coverage_helper.unit_line_calls;
l_result t_coverage;
l_new_unit t_unit_coverage;
l_skipped_objects ut_object_names := ut_object_names();

type t_source_lines is table of binary_integer;
l_source_lines t_source_lines;
line_no binary_integer;
l_schema_names ut_varchar2_rows;
l_query varchar2(32767);
begin
l_schema_names := coalesce(a_coverage_options.schema_names, ut_varchar2_rows(sys_context('USERENV','CURRENT_SCHEMA')));

if not ut_coverage_helper.is_develop_mode() then
--skip all the utplsql framework objects and all the unit test packages that could potentially be reported by coverage.
l_skipped_objects := ut_utils.get_utplsql_objects_list() multiset union all coalesce(a_coverage_options.exclude_objects, ut_object_names());
end if;

--prepare global temp table with sources
delete from ut_coverage_sources_tmp;
if a_coverage_options.file_mappings is not null and a_coverage_options.file_mappings.count > 0 then
execute immediate populate_sources_tmp_table(a_coverage_options) using a_coverage_options.file_mappings, l_skipped_objects, a_coverage_options.include_objects;
else
execute immediate populate_sources_tmp_table(a_coverage_options) using l_schema_names, l_skipped_objects, a_coverage_options.include_objects;
if ut_coverage_helper.is_develop_mode() or not is_tmp_table_populated() then
populate_tmp_table(a_coverage_options, l_skipped_objects);
end if;
commit;

for src_object in (
select o.owner, o.name, o.full_name, max(o.line) lines_count,
Expand Down
30 changes: 20 additions & 10 deletions source/core/coverage/ut_coverage_helper.pkb
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,41 @@ create or replace package body ut_coverage_helper is
limitations under the License.
*/

g_coverage_id integer;
g_develop_mode boolean;
g_coverage_id integer;
g_develop_mode boolean := false;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how about making it not null?

g_is_started boolean := false;

function is_develop_mode return boolean is
function is_develop_mode return boolean is
begin
return g_develop_mode;
end;

function is_started return boolean is
begin
return g_is_started;
end;

procedure coverage_start_internal(a_run_comment varchar2) is
begin
dbms_profiler.start_profiler(run_comment => a_run_comment, run_number => g_coverage_id);
g_is_started := true;
coverage_pause();
end;

procedure coverage_start(a_run_comment varchar2) is
begin
if g_develop_mode is null then
if not g_is_started then
g_develop_mode := false;
coverage_start_internal(a_run_comment);
end if;
end;

procedure coverage_start_develop is
begin
g_develop_mode := true;
coverage_start_internal('utPLSQL Code coverage run in development MODE '||ut_utils.to_string(systimestamp));
if not g_is_started then
g_develop_mode := true;
coverage_start_internal('utPLSQL Code coverage run in development MODE '||ut_utils.to_string(systimestamp));
end if;
end;

procedure coverage_pause is
Expand All @@ -59,17 +68,18 @@ create or replace package body ut_coverage_helper is
end;

procedure coverage_stop is
l_return_code binary_integer;
begin
if not g_develop_mode then
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it correct to check for develop here? We are stoping coverage regardless of the mode, aren't we?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, not really.
We need to override coverage_stop in out unit tests (for now), so that when we test the framework, our "develop-mode" coverage will not stop, if we test execution for one of coverage reporters.

We will be able to remove the develop mode, once we move to testing ut3 with another ut3 installation.

It needs to stay like this for now, otherwise we will not get a true picture of coverage for out own code.

l_return_code := dbms_profiler.stop_profiler();
g_is_started := false;
dbms_profiler.stop_profiler();
end if;
end;

procedure coverage_stop_develop is
l_return_code binary_integer;
begin
l_return_code := dbms_profiler.stop_profiler();
g_develop_mode := false;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should call coverage_stop from here not to duplicate code

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can't, as described in previous comment.
The start_develop/stop_develop override and block start/stop of coverage gathering.

g_is_started := false;
dbms_profiler.stop_profiler();
end;

function get_raw_coverage_data(a_object_owner varchar2, a_object_name varchar2) return unit_line_calls is
Expand Down
4 changes: 3 additions & 1 deletion source/core/coverage/ut_coverage_helper.pks
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ create or replace package ut_coverage_helper authid definer is
*/

--table of line calls indexed by line number
-- table is sparse!!!
--!!! this table is sparse!!!
type unit_line_calls is table of number(38,0) index by binary_integer;

function is_develop_mode return boolean;

function is_started return boolean;

procedure coverage_start(a_run_comment varchar2);

/*
Expand Down
8 changes: 8 additions & 0 deletions source/core/ut_utils.pkb
Original file line number Diff line number Diff line change
Expand Up @@ -341,5 +341,13 @@ create or replace package body ut_utils is
return l_result;
end;

procedure cleanup_temp_tables is
pragma autonomous_transaction;
begin
execute immediate 'truncate table ut_cursor_data';
execute immediate 'truncate table ut_coverage_sources_tmp$';
commit;
end;

end ut_utils;
/
4 changes: 3 additions & 1 deletion source/core/ut_utils.pks
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ create or replace package ut_utils authid definer is
ex_invalid_rep_event_name exception;
gc_invalid_rep_event_name constant pls_integer := -20211;
pragma exception_init(ex_invalid_rep_event_name, -20211);

-- Any of tests failed
ex_some_tests_failed exception;
gc_some_tests_failed constant pls_integer := -20213;
Expand Down Expand Up @@ -219,5 +219,7 @@ create or replace package ut_utils authid definer is

function convert_collection(a_collection ut_varchar2_list) return ut_varchar2_rows;

procedure cleanup_temp_tables;

end ut_utils;
/
2 changes: 1 addition & 1 deletion tests/RunAll.cmd
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
echo off
set UT3_OWNER=ut3
set UT3_OWNER_PASSWORD=ut3
set UT3_OWNER_PASSWORD=XNtxj8eEgA6X6b6f
set ORACLE_SID=XE
if not [%1] == [] (set UT3_OWNER=%1)
if not [%2] == [] (set UT3_OWNER_PASSWORD=%2)
Expand Down
7 changes: 5 additions & 2 deletions tests/RunAll.sql
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ set longchunksize 1000000
set serveroutput on size unlimited format truncated
@@lib/RunVars.sql

@@lib/mystats/mystats start
@@lib/mystats/mystats_pkg.sql
exec mystats_pkg.ms_start;

spool RunAll.log

Expand Down Expand Up @@ -557,7 +558,9 @@ spool coverage.html
exec ut_output_buffer.lines_to_dbms_output(:html_reporter_id);
spool off

@@lib/mystats/mystats stop t=1000
spool stats.log
exec mystats_pkg.ms_stop(1000);
spool off

--can be used by CI to check for tests status
exit :failures_count
30 changes: 15 additions & 15 deletions tests/lib/mystats/mystats_pkg.sql
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ create or replace package mystats_pkg authid current_user as
||
|| Author: Adrian Billington
|| www.oracle-developer.net
|| (c) oracle-developer.net
|| (c) oracle-developer.net
||
|| Description: PL/SQL-only version of Jonathan Lewis's SNAP_MY_STATS package.
|| This package is used to output the resource usage as recorded
|| This package is used to output the resource usage as recorded
|| in V$MYSTAT and V$LATCH.
||
|| Key Differences
Expand Down Expand Up @@ -50,7 +50,7 @@ create or replace package mystats_pkg authid current_user as
|| exec mystats_pkg.ms_stop;
||
|| 2. Output statistics with delta values >= 1,000
|| -------------------------------------------------------------
|| -------------------------------------------------------------
|| exec mystats_pkg.ms_start;
|| --<do some work>--
|| exec mystats_pkg.ms_stop(1000);
Expand All @@ -61,21 +61,21 @@ create or replace package mystats_pkg authid current_user as
|| --<do some work>--
|| exec mystats_pkg.ms_stop(mystats_pkg.statname_ntt('redo size', 'user commits'));
||
|| 4. Output statistics for those containing the word 'memory'
|| 4. Output statistics for those containing the word 'memory'
|| -----------------------------------------------------------
|| exec mystats_pkg.ms_start;
|| --<do some work>--
|| exec mystats_pkg.ms_stop('memory');
||
|| Notes: 1. Serveroutput must be on (and set higher than default);
||
||
|| 2. See http://www.jlcomp.demon.co.uk/snapshot.html for original
|| version.
||
|| 3. A free-standing, SQL*Plus-script version of MyStats is also
|| 3. A free-standing, SQL*Plus-script version of MyStats is also
|| available. The script version works without creating any
|| database objects.
||
||
|| Disclaimer: http://www.oracle-developer.net/disclaimer.php
||
|| ----------------------------------------------------------------------------
Expand Down Expand Up @@ -183,7 +183,7 @@ create or replace package body mystats_pkg as
union all
select 'LATCH'
, name
, gets
, gets
from v$latch
union all
select 'TIME'
Expand Down Expand Up @@ -221,7 +221,7 @@ create or replace package body mystats_pkg as

/*
|| Downside of using associative arrays is that we have to sort
|| the output. So here's a couple of types and a variable to enable us
|| the output. So here's a couple of types and a variable to enable us
|| to do that...
*/
type aat_mystats_output is table of st_output
Expand Down Expand Up @@ -315,13 +315,13 @@ create or replace package body mystats_pkg as
v_value := ga_mystats(c_run2)(v_name).value - ga_mystats(c_run1)(v_name).value;

/*
|| If it's greater than the threshold or a statistic we are interested in,
|| If it's greater than the threshold or a statistic we are interested in,
|| then output it. The downside of using purely associative arrays is that
|| we don't have any easy way of sorting. So we have to do it ourselves...
*/
if (p_threshold is not null and abs(v_value) >= p_threshold)
or (p_statnames is not empty and v_name member of p_statnames)
or (p_statname_like is not null and v_name like '%'||p_statname_like||'%')
or (p_statname_like is not null and v_name like '%'||p_statname_like||'%')
then
/*
|| Fix for bug 1713403. If redo goes over 2Gb then it is reported as a negative
Expand All @@ -334,7 +334,7 @@ create or replace package body mystats_pkg as
sort(v_type, v_name, v_value);
end if;
end if;

/*
|| Next statname please...
*/
Expand Down Expand Up @@ -402,7 +402,7 @@ create or replace package body mystats_pkg as
then ms_report(p_statnames => p_statnames);
when p_statname_like is not null
then ms_report(p_statname_like => p_statname_like);
else ms_report;
else ms_report;
end case;
ms_reset;
else
Expand Down Expand Up @@ -438,5 +438,5 @@ create or replace package body mystats_pkg as
end mystats_pkg;
/

create or replace public synonym mystats_pkg for mystats_pkg;
grant execute on mystats_pkg to public;
-- create or replace public synonym mystats_pkg for mystats_pkg;
-- grant execute on mystats_pkg to public;