Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use strict;
- use warnings;
- use Socket;
- use Getopt::Long;
- use DBI ':sql_types';
- use Time::HiRes 'gettimeofday';
- use Try::Tiny;
- Getopt::Long::Configure(qw/bundling no_ignore_case/);
- my %opt;
- GetOptions(\%opt, qw/
- port|p=s host|h=s count|c=i fork|f=i debug|d=s
- /);
- $opt{host} ||= '127.0.0.1';
- my $pid = $$;
- my $cc = 1;
- my @childs;
- my $logname = sprintf("db_c%s_f%s_%s.log", $opt{count}, $opt{fork}, 'insert');
- _log(sprintf "fork [%s], count [%s]", $opt{fork}, $opt{count});
- for (my $f = 1; $f <= $opt{fork}; $f++) {
- my $parent = fork();
- if ($parent) {
- push(@childs, $parent);
- } elsif ($parent == 0) {
- my $dbh = db_connect();
- commands: for (my $i = 1; $i <= $opt{count}; $i++) {
- my $handle = sprintf "T%d%03d%03d%03d", $$, $cc, $i, $f;
- my $t0 = [gettimeofday];
- try {
- my $sql = insert_contact();
- my $sth = $dbh->prepare($sql);
- $sth->bind_param(':handle', $handle);
- $sth->execute;
- $sth->finish;
- $dbh->commit;
- } catch {
- $dbh = db_connect();
- _log("reconnecting $i, $f, $cc");
- redo commands;
- };
- my $elapsed = Time::HiRes::tv_interval ( $t0 );
- _log("handle:[$handle];timed[$elapsed]");
- # sleep 1;
- }
- $dbh->disconnect();
- exit(0);
- } ## end of fork if
- else { die "we have a furcking problem $!\n"; }
- }
- foreach (@childs) {
- waitpid($_, 0);
- }
- exit;
- sub db_connect {
- my $host = 'localhost';
- my $dbname = 'mh_regbox';
- my $port = '5432';
- my $username = 'mh';
- my $password = 'mh';
- my $dbh = DBI->connect("dbi:Pg:dbname=$dbname;host=$host;port=$port;", "$username", "$password",
- {AutoCommit => 0});
- return $dbh;
- }
- sub _log {
- my $str = shift;
- 1 while chomp($str);
- my $message = sprintf "%s [%08d] %s\n", precdate(1), $$, $str;
- # print STDERR $message;
- open LOG, ">>$logname";
- print LOG $message;
- close LOG;
- }
- sub precdate {
- my @hires = gettimeofday();
- return sub {
- sprintf "%04d%02d%02d.%02d%02d%02d",
- $_[5]+1900,$_[4]+1,@_[3,2,1,0]
- }->(localtime($hires[0]))
- . (@_ ? sprintf(".%06d",$hires[1]) : "");
- }
- sub insert_contact {
- qq/INSERT INTO bla (
- /;
- }
Add Comment
Please, Sign In to add comment