Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use warnings;
- use strict;
- use threads;
- use threads::shared;
- use Time::HiRes qw(usleep);
- use Try::Tiny;
- use DBI;
- my $succeded_count = 0;
- my $no_of_retries = 0;
- share($succeded_count);
- share($no_of_retries);
- sub transaction($$$){
- my ($sleep_milisec, $retry, $no_of_threads) = @_;
- my $dbh;
- $no_of_retries++;
- try
- {
- $dbh = DBI->connect("DBI:Pg:dbname=paralel_transaction_test;host=localhost", "transact_tester", "123", {RaiseError => 1, AutoCommit => 0}) or die $DBI::errstr;
- my $sth = $dbh->prepare(q{
- SELECT * FROM foo
- }) or die $dbh->errstr;
- $sth->execute() or die $dbh->errstr;
- usleep($sleep_milisec);
- $sth = $dbh->prepare(q{
- UPDATE foo SET name = name || ? WHERE id = ?
- }) or die $dbh->errstr;
- $sth->execute(int(rand(9)) + 1, int(rand($no_of_threads))) or die $dbh->errstr;
- usleep($sleep_milisec);
- $sth = $dbh->prepare(q{
- SELECT * FROM foo
- }) or die $dbh->errstr;
- $sth->execute() or die $dbh->errstr;
- $sth->finish;
- $dbh->commit() or die $dbh->errstr;
- $dbh->disconnect();
- $succeded_count++;
- }
- catch
- {
- # $dbh->rollback();
- print "FAILED\n";
- $no_of_retries++;
- if(defined $dbh)
- {
- $dbh->rollback();
- $dbh->disconnect();
- }
- if($retry)
- {
- transaction($sleep_milisec, $retry, $no_of_threads);
- }
- }
- }
- if(!defined $ARGV[0] || !defined $ARGV[1] || !defined $ARGV[2])
- {
- die "Incorrect Input.
- Usage:
- perl test.pl <option> <no_of_paralel_transactions> <wait_betwean_queries_milisec>
- Options:
- -s get the number of succeeded calls without retrying
- -r get the number of retries needed to finish all transactiosns\n";
- }
- my $mode = $ARGV[0];
- my $no_of_threads = $ARGV[1];
- my $sleep_milisec = $ARGV[2];
- my $retry = 0;
- if($mode eq "-r")
- {
- $retry = 1;
- }
- my @arr;
- for(my $i = 0; $i < $no_of_threads; $i++)
- {
- push @arr, threads->create(\&transaction, $sleep_milisec, $retry, $no_of_threads);
- }
- foreach my $thread (@arr)
- {
- $thread->join();
- }
- print "succeeded: $succeded_count/$no_of_threads \n";
- if($retry)
- {
- print "retries needed: $no_of_retries \n";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement