File Coverage

blib/lib/DBIPR.pm
Criterion Covered Total %
statement 16 18 88.8
branch n/a
condition n/a
subroutine 6 6 100.0
pod n/a
total 22 24 91.6


line stmt bran cond sub pod time code
1             package DBIPR;
2            
3 4     4   26389 use warnings;
  4         8  
  4         127  
4 4     4   25 use strict;
  4         5  
  4         121  
5 4     4   21 use Carp;
  4         11  
  4         347  
6 4     4   22 use Exporter 'import';
  4         9  
  4         220  
7             our @EXPORT = qw(raw_insert cursor_insert array_insert bulk_insert trunc session);
8 4     4   41202 use DBI;
  4         137876  
  4         333  
9 4     4   9645 use DBD::Oracle qw(:ora_session_modes);
  0            
  0            
10            
11             my $db;
12             croak "not able to login local default SID as scott/tiger!" unless
13             $db=DBI->connect(q(dbi:Oracle:), q(scott), q(tiger), {PrintError => 0});
14            
15             =head1 NAME
16            
17             DBIPR - DBI PRessure test for different methods of oracle insert
18            
19             =head1 VERSION
20            
21             Version 0.01
22            
23             =cut
24            
25             our $VERSION = '0.01';
26            
27             =head1 SYNOPSIS
28            
29             Insert 1000 employees into emp table of scott user, in raw sql, cursor
30             based insert, client side array based insert and server side array based
31             insert. Working as pressure testing application for DML tunning.
32             With DBI::Profile and oracle tkprof tools support, advanced measure is
33             leave to experiment.
34            
35             use DBIPR; # import all functions list below
36            
37             raw_insert; trunc; # normal sql string concat method
38             cursor_insert; trunc; # cursor based version
39             array_insert; trunc; # perl array version
40             bulk_insert; trunc; # PL/SQL array version
41             session; # list all client sessions, for sql_trace & tkprof
42            
43             Perl One-Liner command line works as:
44            
45             $ perl -MDBIPR -e "for (1..100) {trunc; sleep 3; cursor_insert; sleep 3;}"
46            
47             =head1 FUNCTIONS
48            
49             =head2 raw_insert
50            
51             use raw sql 'insert into ... values ...' syntax and for loop
52            
53             =cut
54            
55             sub raw_insert {
56             $db->do(qq(insert into emp(empno, ename) values ($_,'clerk$_'))) for 1..1000;
57             }
58            
59             =head2 cursor_insert
60            
61             use prepared statment with ? and bind_param inside for loop
62            
63             =cut
64            
65             sub cursor_insert {
66             my $stmth=$db->prepare(qq(insert into emp(empno, ename) values (?,?)));
67             $stmth->execute($_,"clerk$_") for 1..1000;
68             }
69            
70             =head2 array_insert
71            
72             use bind_param_array and execute_array to work in one shot
73            
74             =cut
75            
76             sub array_insert {
77             my @empnos=(1..1000);
78             my @enames=();
79             my @rowstats=();
80             $enames[$_-1]="clerk$_" for 1..1000;
81             my $stmth=$db->prepare(qq(insert into emp(empno, ename) values (?,?)));
82             $stmth->bind_param_array(1, \@empnos);
83             $stmth->bind_param_array(2, \@enames);
84             $stmth->execute_array({ArrayTupleStatus=>\@rowstats});
85             }
86            
87             =head2 bulk_insert
88            
89             use hash array (table of index) & forall insert to populate in pl/sql
90            
91             =cut
92            
93             sub bulk_insert {
94             $db->do(q(
95             DECLARE
96             TYPE NumTab IS TABLE OF NUMBER(4) INDEX BY BINARY_INTEGER;
97             TYPE NameTab IS TABLE OF CHAR(10) INDEX BY BINARY_INTEGER;
98             pnums NumTab;
99             pnames NameTab;
100             BEGIN
101             FOR j IN 1..1000 LOOP -- load index-by tables
102             pnums(j) := j;
103             pnames(j) := 'clerk' || TO_CHAR(j);
104             END LOOP;
105             FORALL i IN 1..1000 -- use FORALL statement
106             INSERT INTO emp (empno, ename) VALUES (pnums(i), pnames(i));
107             END;
108             ));
109             }
110            
111             =head2 trunc
112            
113             use sql 'delete emp where deptno is null' to delete the test data
114            
115             =cut
116            
117             sub trunc {
118             $db->do(qq(delete emp where deptno is null));
119             }
120            
121             =head2 session
122            
123             use v$process joined with v$session to report the sid, serial# of
124             client programs. need to switch to sys account with account in dba group
125            
126             =cut
127            
128             sub session {
129             my $sysdb=DBI->connect(q(dbi:Oracle:), q(), q(),
130             {ora_session_mode => ORA_SYSDBA});
131             my $report=$sysdb->selectall_arrayref(q(
132             select b.username, b.program, a.spid, b.sid,
133             b.serial# sno
134             from v$process a join v$session b
135             on (a.addr=b.paddr and b.username is not null)
136             ), {Slice=>{}});
137             print qq(USER\tPROGRAM\tSID\tSERIAL\tSPID\n);
138             print qq($_->{USERNAME}\t$_->{PROGRAM}\t$_->{SID}\t$_->{SNO}\t$_->{SPID}\n)
139             for (@$report);
140             $sysdb->disconnect;
141             }
142            
143             =head1 AUTHOR
144            
145             Joe Jiang, C<< >>
146            
147             =head1 BUGS
148            
149             Please report any bugs or feature requests to
150             C, or through the web interface at
151             L.
152             I will be notified, and then you'll automatically be notified of progress on
153             your bug as I make changes.
154            
155             =head1 SUPPORT
156            
157             You can find documentation for this module with the perldoc command.
158            
159             perldoc DBIPR
160            
161             You can also look for information at:
162            
163             =over 4
164            
165             =item * AnnoCPAN: Annotated CPAN documentation
166            
167             L
168            
169             =item * CPAN Ratings
170            
171             L
172            
173             =item * RT: CPAN's request tracker
174            
175             L
176            
177             =item * Search CPAN
178            
179             L
180            
181             =back
182            
183             =head1 ACKNOWLEDGEMENTS
184            
185             =head1 COPYRIGHT & LICENSE
186            
187             Copyright 2007 Joe Jiang, all rights reserved.
188            
189             This program is free software; you can redistribute it and/or modify it
190             under the same terms as Perl itself.
191            
192             =cut
193            
194             1; # End of DBIPR