Advertisement
Guest User

Untitled

a guest
May 28th, 2015
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 2.38 KB | None | 0 0
  1. use warnings;
  2. use strict;
  3.  
  4. use threads;
  5. use threads::shared;
  6.  
  7. use Time::HiRes qw(usleep);
  8. use Try::Tiny;
  9. use DBI;
  10.  
  11.  
  12.  
  13. my $succeded_count = 0;
  14. my $no_of_retries = 0;
  15. share($succeded_count);
  16. share($no_of_retries);
  17.  
  18. sub transaction($$$){
  19.     my ($sleep_milisec, $retry, $no_of_threads) = @_;
  20.    
  21.     my $dbh;
  22.     $no_of_retries++;
  23.     try
  24.     {
  25.         $dbh = DBI->connect("DBI:Pg:dbname=paralel_transaction_test;host=localhost", "transact_tester", "123", {RaiseError => 1, AutoCommit => 0}) or die $DBI::errstr;
  26.         my $sth = $dbh->prepare(q{
  27.             SELECT * FROM foo
  28.         }) or die $dbh->errstr;
  29.         $sth->execute() or die $dbh->errstr;
  30.  
  31.         usleep($sleep_milisec);
  32.  
  33.         $sth = $dbh->prepare(q{
  34.             UPDATE foo SET name = name || ? WHERE id = ?
  35.         }) or die $dbh->errstr;
  36.         $sth->execute(int(rand(9)) + 1, int(rand($no_of_threads))) or die $dbh->errstr;
  37.  
  38.         usleep($sleep_milisec);
  39.  
  40.         $sth = $dbh->prepare(q{
  41.             SELECT * FROM foo
  42.         }) or die $dbh->errstr;
  43.         $sth->execute() or die $dbh->errstr;
  44.         $sth->finish;
  45.         $dbh->commit() or die $dbh->errstr;
  46.  
  47.         $dbh->disconnect();
  48.         $succeded_count++;
  49.     }
  50.     catch
  51.     {
  52.        # $dbh->rollback();
  53.         print "FAILED\n";
  54.         $no_of_retries++;
  55.         if(defined $dbh)
  56.         {
  57.             $dbh->rollback();
  58.             $dbh->disconnect();
  59.         }
  60.         if($retry)
  61.         {
  62.             transaction($sleep_milisec, $retry, $no_of_threads);
  63.         }    
  64.     }
  65. }
  66.  
  67. if(!defined $ARGV[0] || !defined $ARGV[1] || !defined $ARGV[2])
  68. {
  69.     die "Incorrect Input.
  70.            Usage:
  71.            perl test.pl <option> <no_of_paralel_transactions> <wait_betwean_queries_milisec>
  72.  
  73.        Options:
  74.            -s get the number of succeeded calls without retrying
  75.            -r get the number of retries needed to finish all transactiosns\n";
  76. }
  77.  
  78. my $mode = $ARGV[0];
  79. my $no_of_threads = $ARGV[1];
  80. my $sleep_milisec = $ARGV[2];
  81.  
  82. my $retry = 0;
  83. if($mode eq "-r")
  84. {
  85.     $retry = 1;
  86. }
  87.  
  88. my @arr;
  89. for(my $i = 0; $i < $no_of_threads; $i++)
  90. {
  91.     push @arr, threads->create(\&transaction, $sleep_milisec, $retry, $no_of_threads);
  92. }
  93.  
  94. foreach my $thread (@arr)
  95. {
  96.     $thread->join();
  97. }
  98.  
  99. print "succeeded: $succeded_count/$no_of_threads \n";
  100.  
  101. if($retry)
  102. {
  103.     print "retries needed: $no_of_retries \n";
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement