~drizzle-trunk/drizzle/development

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
#!/usr/bin/perl
# ======================================================================
#                     MySQL server stress test system 
# ======================================================================
#
##########################################################################
#
#                       SCENARIOS AND REQUIREMENTS
#
#   The system should perform stress testing of MySQL server with 
# following requirements and basic scenarios:
# 
# Basic requirements:
# 
# Design of stress script should allow one:
# 
#   - To stress test the mysqltest binary test engine.
#   - To stress test the regular test suite and any additional test suites
#     (such as mysql-test-extra-5.0).
#   - To specify files with lists of tests both for initialization of
#     stress db and for further testing itself.
#   - To define the number of threads to be concurrently used in testing.
#   - To define limitations for the test run. such as the number of tests or
#     loops for execution or duration of testing, delay between test
#     executions, and so forth.
#   - To get a readable log file that can be used for identification of
#     errors that occur during testing.
# 
# Basic scenarios:
# 
#     * It should be possible to run stress script in standalone mode
#       which will allow to create various scenarios of stress workloads:
# 
#       simple ones:
#
#         box #1:
#           - one instance of script with list of tests #1
# 
#       and more advanced ones:
# 
#         box #1:
#           - one instance of script with list of tests #1
#           - another instance of script with list of tests #2
#         box #2:
#           - one instance of script with list of tests #3
#           - another instance of script with list of tests #4
#             that will recreate whole database to back it to clean
#             state
# 
#       One kind of such complex scenarios maybe continued testing
#       when we want to run stress tests from many boxes with various
#       lists of tests that will last very long time. And in such case
#       we need some wrapper for MySQL server that will restart it in
#       case of crashes.
# 
#     * It should be possible to run stress script in ad-hoc mode from
#       shell or perl versions of mysql-test-run. This allows developers
#       to reproduce and debug errors that was found in continued stress 
#       testing
#
########################################################################

use Config;

if (!defined($Config{useithreads}))
{
  die <<EOF;
It is unable to run threaded version of stress test on this system 
due to disabled ithreads. Please check that installed perl binary 
was built with support of ithreads. 
EOF
}

use threads;
use threads::shared;

use IO::Socket;
use Sys::Hostname;
use File::Copy;
use File::Spec;
use File::Find;
use File::Basename;
use File::Path;
use Cwd;

use Data::Dumper;
use Getopt::Long;

my $stress_suite_version="1.0";

$|=1;

$opt_server_host="";
$opt_server_logs_dir="";
$opt_help="";
$opt_server_port="";
$opt_server_socket="";
$opt_server_user="";
$opt_server_password="";
$opt_server_database="";
$opt_cleanup="";
$opt_verbose="";
$opt_log_error_details="";


$opt_suite="main";
$opt_stress_suite_basedir="";
$opt_stress_basedir="";
$opt_stress_datadir="";
$opt_test_suffix="";

$opt_stress_mode="random";

$opt_loop_count=0;
$opt_test_count=0;
$opt_test_duration=0;
$opt_abort_on_error=0;
$opt_sleep_time = 0;
$opt_threads=1;
$pid_file="mysql_stress_test.pid";
$opt_mysqltest= ($^O =~ /mswin32/i) ? "mysqltest.exe" : "mysqltest";
$opt_check_tests_file="";
@mysqltest_args=("--silent", "-v");

# Client ip address
$client_ip=inet_ntoa((gethostbyname(hostname()))[4]);
$client_ip=~ s/\.//g;

%tests_files=(client => {mtime => 0, data => []},
              initdb => {mtime => 0, data => []});

# Error codes and sub-strings with corresponding severity 
#
# S1 - Critical errors - cause immediately abort of testing. These errors
#                        could be caused by server crash or impossibility
#                        of test execution
#
# S2 - Serious errors - these errors are bugs for sure as it knowns that 
#                       they shouldn't appear during stress testing  
#
# S3 - Non-seriuos errros - these errors could be caused by fact that 
#                           we execute simultaneously statements that
#                           affect tests executed by other threads
                            
%error_strings = ( 'Failed in mysql_real_connect()' => S1,
                   'not found (Errcode: 2)' => S1 );
  
%error_codes = ( 1012 => S2, 1015 => S2, 1021 => S2,
                 1027 => S2, 1037 => S2, 1038 => S2,
                 1039 => S2, 1040 => S2, 1046 => S2, 
                 1180 => S2, 1181 => S2, 1203 => S2,
                 1205 => S2, 1206 => S2, 1207 => S2, 
                 1223 => S2, 2013 => S1);

share(%test_counters);
%test_counters=( loop_count => 0, test_count=>0);

share($exiting);
$exiting=0;

share($test_counters_lock);
$test_counters_lock=0;
share($log_file_lock);
$log_file_lock=0;

$SIG{INT}= \&sig_INT_handler;
$SIG{TERM}= \&sig_TERM_handler;


GetOptions("server-host=s", "server-logs-dir=s", "server-port=s",
           "server-socket=s", "server-user=s", "server-password=s",
           "server-database=s",
           "stress-suite-basedir=s", "suite=s", "stress-init-file:s", 
           "stress-tests-file:s", "stress-basedir=s", "stress-mode=s",
           "stress-datadir=s",
           "threads=s", "sleep-time=s", "loop-count=i", "test-count=i",
           "test-duration=i", "test-suffix=s", "check-tests-file", 
           "verbose", "log-error-details", "cleanup", "mysqltest=s", 
           "abort-on-error", "help") || usage();

usage() if ($opt_help);

#$opt_abort_on_error=1;

$test_dirname=get_timestamp();
$test_dirname.="-$opt_test_suffix" if ($opt_test_suffix ne '');

print <<EOF;
#############################################################
                  CONFIGURATION STAGE
#############################################################
EOF

if ($opt_stress_basedir eq '' || $opt_stress_suite_basedir eq '' ||
    $opt_server_logs_dir eq '')
{
  die <<EOF;

Options --stress-basedir, --stress-suite-basedir and --server-logs-dir are 
required. Please use these options to specify proper basedir for 
client, test suite and location of server logs.

stress-basedir: '$opt_stress_basedir'
stress-suite-basedir: '$opt_stress_suite_basedir'
server-logs-dir: '$opt_server_logs_dir'

EOF
}

#Workaround for case when we got relative but not absolute path 
$opt_stress_basedir=File::Spec->rel2abs($opt_stress_basedir);
$opt_stress_suite_basedir=File::Spec->rel2abs($opt_stress_suite_basedir);
$opt_server_logs_dir=File::Spec->rel2abs($opt_server_logs_dir);

if ($opt_stress_datadir ne '')
{
  $opt_stress_datadir=File::Spec->rel2abs($opt_stress_datadir);
}

if (! -d "$opt_stress_basedir")
{
  die <<EOF;
  
Directory '$opt_stress_basedir' does not exist.
Use --stress-basedir option to specify proper basedir for client

EOF
}

if (!-d $opt_stress_suite_basedir)
{
  die <<EOF;

Directory '$opt_stress_suite_basedir' does not exist.
Use --stress-suite-basedir option to specify proper basedir for test suite

EOF
}

$test_dataset_dir=$opt_stress_suite_basedir;
if ($opt_stress_datadir ne '')
{
  if (-d $opt_stress_datadir)
  {
    $test_dataset_dir=$opt_stress_datadir;

  }
  else
  {
    die <<EOF;
Directory '$opt_stress_datadir' not exists. Please specify proper one 
with --stress-datadir option.
EOF
  }  
}

if ($^O =~ /mswin32/i)
{
  $test_dataset_dir=~ s/\\/\\\\/g;
}
else
{
  $test_dataset_dir.="/";
}



if (!-d $opt_server_logs_dir)
{
  die <<EOF;

Directory server-logs-dir '$opt_server_logs_dir' does not exist.
Use --server-logs-dir option to specify proper directory for storing 
logs 

EOF
}
else
{
  #Create sub-directory for test session logs
  mkpath(File::Spec->catdir($opt_server_logs_dir, $test_dirname), 0, 0755);
  #Define filename of global session log file
  $stress_log_file=File::Spec->catfile($opt_server_logs_dir, $test_dirname,
                                       "mysql-stress-test.log");
}

if ($opt_suite ne '' && $opt_suite ne 'main' && $opt_suite ne 'default')
{
  $test_suite_dir=File::Spec->catdir($opt_stress_suite_basedir, "suite", $opt_suite);
}
else
{
  $test_suite_dir= $opt_stress_suite_basedir;
}

if (!-d $test_suite_dir)
{
  die <<EOF

Directory '$test_suite_dir' does not exist.
Use --suite options to specify proper dir for test suite

EOF
}

$test_suite_t_path=File::Spec->catdir($test_suite_dir,'t');
$test_suite_r_path=File::Spec->catdir($test_suite_dir,'r');

foreach my $suite_dir ($test_suite_t_path, $test_suite_r_path)
{
  if (!-d $suite_dir)
  {
    die <<EOF;

Directory '$suite_dir' does not exist.
Please ensure that you specified proper source location for 
test/result files with --stress-suite-basedir option and name 
of test suite with --suite option

EOF
  }
}

$test_t_path=File::Spec->catdir($opt_stress_basedir,'t');
$test_r_path=File::Spec->catdir($opt_stress_basedir,'r');

foreach $test_dir ($test_t_path, $test_r_path)
{
  if (-d $test_dir)
  {
    if ($opt_cleanup)
    {
      #Delete existing 't', 'r', 'r/*' subfolders in $stress_basedir
      rmtree("$test_dir", 0, 0);
      print "Cleanup $test_dir\n";
    }
    else
    {
      die <<EOF;
Directory '$test_dir' already exist. 
Please ensure that you specified proper location of working dir
for current test run with --stress-basedir option or in case of staled
directories use --cleanup option to remove ones
EOF
    }
  }
  #Create empty 't', 'r' subfolders that will be filled later
  mkpath("$test_dir", 0, 0777);
}

if (!defined($opt_stress_tests_file) && !defined($opt_stress_init_file))
{
  die <<EOF;
You should run stress script either with --stress-tests-file or with 
--stress-init-file otions. See help for details.
EOF
}

if (defined($opt_stress_tests_file))
{ 
  if ($opt_stress_tests_file eq '')
  {
    #Default location of file with set of tests for current test run
    $tests_files{client}->{filename}= File::Spec->catfile($opt_stress_suite_basedir,
                                      "testslist_client.txt");
  }
  else
  {
    $tests_files{client}->{filename}= $opt_stress_tests_file;
  }

  if (!-f $tests_files{client}->{filename})
  {
    die <<EOF;

File '$tests_files{client}->{filename}' with list of tests not exists. 
Please ensure that this file exists, readable or specify another one with 
--stress-tests-file option.

EOF
  }
}

if (defined($opt_stress_init_file))
{
  if ($opt_stress_init_file eq '')
  {
    #Default location of file with set of tests for current test run
    $tests_files{initdb}->{filename}= File::Spec->catfile($opt_stress_suite_basedir,
                                      "testslist_initdb.txt");
  }
  else
  {
    $tests_files{initdb}->{filename}= $opt_stress_init_file;
  }

  if (!-f $tests_files{initdb}->{filename})
  {
    die <<EOF;

File '$tests_files{initdb}->{filename}' with list of tests for initialization of database
for stress test not exists. 
Please ensure that this file exists, readable or specify another one with 
--stress-init-file option.

EOF
  }
}

if ($opt_stress_mode !~ /^(random|seq)$/)
{
  die <<EOF
Was specified wrong --stress-mode. Correct values 'random' and 'seq'.
EOF
}

if (open(TEST, "$opt_mysqltest -V |"))
{
  $mysqltest_version=join("",<TEST>);
  close(TEST);
  print "FOUND MYSQLTEST BINARY: ", $mysqltest_version,"\n";
}
else
{
  die <<EOF;
ERROR: mysqltest binary $opt_mysqltest not found $!.
You must either specify file location explicitly using --mysqltest
option, or make sure path to mysqltest binary is listed 
in your PATH environment variable.
EOF
}

#        
#Adding mysql server specific command line options for mysqltest binary
#
$opt_server_host= $opt_server_host ? $opt_server_host : "localhost";
$opt_server_port= $opt_server_port ? $opt_server_port : "4427";
$opt_server_user= $opt_server_user ? $opt_server_user : "root";
$opt_server_socket= $opt_server_socket ? $opt_server_socket : "/tmp/mysql.sock";
$opt_server_database= $opt_server_database ? $opt_server_database : "test";

unshift @mysqltest_args, "--host=$opt_server_host";
unshift @mysqltest_args, "--port=$opt_server_port";
unshift @mysqltest_args, "--user=$opt_server_user";
unshift @mysqltest_args, "--password=$opt_server_password";
unshift @mysqltest_args, "--socket=$opt_server_socket";
unshift @mysqltest_args, "--database=$opt_server_database";

#Export variables that could be used in tests
$ENV{DRIZZLE_TEST_DIR}=$test_dataset_dir;
$ENV{MASTER_MYPORT}=$opt_server_port;
$ENV{MASTER_MYSOCK}=$opt_server_socket;

print <<EOF;
TEST-SUITE-BASEDIR: $opt_stress_suite_basedir
SUITE:              $opt_suite
TEST-BASE-DIR:      $opt_stress_basedir
TEST-DATADIR:       $test_dataset_dir
SERVER-LOGS-DIR:    $opt_server_logs_dir

THREADS:            $opt_threads
TEST-MODE:          $opt_stress_mode

EOF

#-------------------------------------------------------------------------------
#At this stage we've already checked all needed pathes/files 
#and ready to start the test
#-------------------------------------------------------------------------------

if (defined($opt_stress_tests_file) || defined($opt_stress_init_file))
{
  print <<EOF;
#############################################################
                  PREPARATION STAGE
#############################################################
EOF

  #Copy Test files from network share to 't' folder
  print "\nCopying Test files from $test_suite_t_path to $test_t_path folder...";
  find({wanted=>\&copy_test_files, bydepth=>1}, "$test_suite_t_path");
  print "Done\n";

  #$test_r_path/r0 dir reserved for initdb
  $count_start= defined($opt_stress_init_file) ? 0 : 1;

  our $r_folder='';
  print "\nCreating 'r' folder and copying Protocol files to each 'r#' sub-folder...";
  for($count=$count_start; $count <= $opt_threads; $count++)
  {
    $r_folder = File::Spec->catdir($test_r_path, "r".$count);
    mkpath("$r_folder", 0, 0777); 
     
    find(\&copy_result_files,"$test_suite_r_path");
  }  
  print "Done\n\n";
}

if (defined($opt_stress_init_file))
{
  print <<EOF;
#############################################################
                  INITIALIZATION STAGE
#############################################################
EOF

  #Set limits for stress db initialization 
  %limits=(loop_count => 1, test_count => undef);

  #Read list of tests from $opt_stress_init_file
  read_tests_names($tests_files{initdb});
  test_loop($client_ip, 0, 'seq', $tests_files{initdb});  
  #print Dumper($tests_files{initdb}),"\n";
  print <<EOF;

Done initialization of stress database by tests from 
$tests_files{initdb}->{filename} file.

EOF
}

if (defined($opt_stress_tests_file))
{
  print <<EOF;
#############################################################
                  STRESS TEST RUNNING STAGE
#############################################################
EOF

  $exiting=0;
  #Read list of tests from $opt_stress_tests_file 
  read_tests_names($tests_files{client});

  #Reset current counter and set limits
  %test_counters=( loop_count => 0, test_count=>0);
  %limits=(loop_count => $opt_loop_count, test_count => $opt_test_count);

  if (($opt_loop_count && $opt_threads > $opt_loop_count) || 
      ($opt_test_count && $opt_threads > $opt_test_count))
  {
    warn <<EOF;

WARNING: Possible inaccuracies in number of executed loops or 
         tests because number of threads bigger than number of 
         loops or tests:
         
         Threads will be started: $opt_threads
         Loops will be executed:  $opt_loop_count
         Tests will be executed:  $opt_test_count    

EOF
  }

  #Create threads (number depending on the variable )
  for ($id=1; $id<=$opt_threads && !$exiting; $id++)
  {
    $thrd[$id] = threads->create("test_loop", $client_ip, $id,
                                 $opt_stress_mode, $tests_files{client});

    print "main: Thread ID $id TID ",$thrd[$id]->tid," started\n";
    select(undef, undef, undef, 0.5);
  }

  if ($opt_test_duration)
  {
    sleep($opt_test_duration);
    kill INT, $$;                             #Interrupt child threads
  }

  #Let other threads to process INT signal
  sleep(1);

  for ($id=1; $id<=$opt_threads;$id++)
  {
    if (defined($thrd[$id]))
    {
      $thrd[$id]->join();
    }
  }
  print "EXIT\n";
}

sub test_init
{
  my ($env)=@_;
  
  $env->{session_id}=$env->{ip}."_".$env->{thread_id};
  $env->{r_folder}='r'.$env->{thread_id}; 
  $env->{screen_logs}=File::Spec->catdir($opt_server_logs_dir, $test_dirname, 
                                         "screen_logs", $env->{session_id});
  $env->{reject_logs}=File::Spec->catdir($opt_server_logs_dir, $test_dirname,
                                         "reject_logs", $env->{session_id});
  
  mkpath($env->{screen_logs}, 0, 0755) unless (-d $env->{screen_logs});
  mkpath($env->{reject_logs}, 0, 0755) unless (-d $env->{reject_logs});

  $env->{session_log}= File::Spec->catfile($env->{screen_logs}, $env->{session_id}.".log");     
}

sub test_execute
{
  my $env= shift;
  my $test_name= shift;

  my $g_start= "";
  my $g_end= "";
  my $mysqltest_cmd= "";
  my @mysqltest_test_args=();
  my @stderr=();

  #Get time stamp
  $g_start = get_timestamp();
  $env->{errors}={};
  @{$env->{test_status}}=();

  my $test_file= $test_name.".test";
  my $result_file= $test_name.".result";
  my $reject_file = $test_name.'.reject';
  my $output_file = $env->{session_id}.'_'.$test_name.'_'.$g_start."_".$env->{test_count}.'.txt';

  my $test_filename = File::Spec->catfile($test_t_path, $test_file);
  my $result_filename = File::Spec->catdir($test_r_path, $env->{r_folder}, $result_file);
  my $reject_filename = File::Spec->catdir($test_r_path, $env->{r_folder}, $reject_file);
  my $output_filename = File::Spec->catfile($env->{screen_logs}, $output_file);     


  push @mysqltest_test_args, "--basedir=$opt_stress_suite_basedir/",
                             "--tmpdir=$opt_stress_basedir",
                             "-x $test_filename",
                             "-R $result_filename",
                             "2>$output_filename";
                        
  $cmd= "$opt_mysqltest --no-defaults ".join(" ", @mysqltest_args)." ".
                                        join(" ", @mysqltest_test_args);

  system($cmd);

  $exit_value  = $? >> 8;
  $signal_num  = $? & 127;
  $dumped_core = $? & 128;

  my $tid= threads->self->tid;

  if (-s $output_filename > 0)
  { 
    #Read stderr for further analysis
    open (STDERR_LOG, $output_filename) or 
                             warn "Can't open file $output_filename";
    @stderr=<STDERR_LOG>;
    close(STDERR_LOG);
 
    if ($opt_verbose)
    {
      $session_debug_file="$opt_stress_basedir/error$tid.txt";
      
      stress_log($session_debug_file, 
                "Something wrong happened during execution of this command line:");
      stress_log($session_debug_file, "MYSQLTEST CMD - $cmd");    
      stress_log($session_debug_file, "STDERR:".join("",@stderr));      

      stress_log($session_debug_file, "EXIT STATUS:\n1. EXIT: $exit_value \n".
                                      "2. SIGNAL: $signal_num\n".
                                      "3. CORE: $dumped_core\n");
    }
  }

  #If something wrong trying to analyse stderr 
  if ($exit_value || $signal_num)
  {
    if (@stderr)
    {
      foreach my $line (@stderr)
      {
        #FIXME: we should handle case when for one sub-string/code 
        #       we have several different error messages        
        #       Now for both codes/substrings we assume that
        #       first found message will represent error

        #Check line for error codes
        if (($err_msg, $err_code)= $line=~/failed: ((\d+):.+?$)/)
        {
          if (!exists($error_codes{$err_code}))
          {
            $severity="S3";
            $err_code=0;
          }
          else
          {
            $severity=$error_codes{$err_code};
          }

          if (!exists($env->{errors}->{$severity}->{$err_code}))
          {
            $env->{errors}->{$severity}->{$err_code}=[0, $err_msg];
          }
          $env->{errors}->{$severity}->{$err_code}->[0]++;
          $env->{errors}->{$severity}->{total}++;          
        }

        #Check line for error patterns
        foreach $err_string (keys %error_strings)
        {
          $pattern= quotemeta $err_string;
          if ($line =~ /$pattern/i)
          {
            my $severity= $error_strings{$err_string};
            if (!exists($env->{errors}->{$severity}->{$err_string}))
            {
              $env->{errors}->{$severity}->{$err_string}=[0, $line];
            }
            $env->{errors}->{$severity}->{$err_string}->[0]++;
            $env->{errors}->{$severity}->{total}++;          
          }
        }
      }
    }
    else
    {
      $env->{errors}->{S3}->{'Unknown error'}=
                              [1,"Unknown error. Nothing was output to STDERR"];
      $env->{errors}->{S3}->{total}=1;
    }
  }

  #
  #FIXME: Here we can perform further analysis of recognized 
  #       error codes 
  #

  foreach my $severity (sort {$a cmp $b} keys %{$env->{errors}})
  {
    my $total=$env->{errors}->{$severity}->{total};
    if ($total)
    {
      push @{$env->{test_status}}, "Severity $severity: $total";
      $env->{errors}->{total}=+$total;
    }
  }

  #FIXME: Should we take into account $exit_value here?
  #       Now we assume that all stringified errors(i.e. errors without 
  #       error codes) which are not exist in %error_string structure 
  #       are OK
  if (!$env->{errors}->{total})
  {
    push @{$env->{test_status}},"No Errors. Test Passed OK";
  }

  log_session_errors($env, $test_file);

  if (!$exiting && ($signal_num == 2 || $signal_num == 15 || 
      ($opt_abort_on_error && $env->{errors}->{S1} > 0)))
  {
    #mysqltest was interrupted with INT or TERM signals or test was 
    #ran with --abort-on-error option and we got errors with severity S1
    #so we assume that we should cancel testing and exit
    $exiting=1;
    print STDERR<<EOF;
WARNING:
   mysqltest was interrupted with INT or TERM signals or test was 
   ran with --abort-on-error option and we got errors with severity S1
   (test cann't connect to the server or server crashed) so we assume that 
   we should cancel testing and exit. Please check log file for this thread 
   in $stress_log_file or 
   inspect below output of the last test case executed with mysqltest to 
   find out cause of error.
   
   Output of mysqltest:
   @stderr
   
EOF
  }

  if (-e $reject_filename)
  {  
    move_to_logs($env->{reject_logs}, $reject_filename, $reject_file);
  }    
  
  if (-e $output_filename)
  {  
    move_to_logs($env->{screen_logs}, $output_filename, $output_file);
  }    

}

sub test_loop
{     
  my %client_env=();
  my $test_name="";

  # KEY for session identification: IP-THREAD_ID
  $client_env{ip} = shift;
  $client_env{thread_id} = shift;

  $client_env{mode} = shift;
  $client_env{tests_file}=shift; 

  $client_env{test_seq_idx}=0;

  #Initialize session variables
  test_init(\%client_env);

LOOP:

  while(!$exiting)
  {
    if ($opt_check_tests_file)
    {
      #Check if tests_file was modified and reread it in this case
      read_tests_names($client_env{tests_file}, 0);
    }

    {
      lock($test_counters_lock);

      if (($limits{loop_count} && $limits{loop_count} <= $test_counters{loop_count}*1) ||
          ($limits{test_count} && $limits{test_count} <= $test_counters{test_count}*1) )
      {
        $exiting=1;
        next LOOP;
      }
    }

    #Get random file name 
    if (($test_name = get_test(\%client_env)) ne '')
    {
      {
        lock($test_counters_lock);

        #Save current counters values 
        $client_env{loop_count}=$test_counters{loop_count};
        $client_env{test_count}=$test_counters{test_count};
      }
      #Run test and analyze results
      test_execute(\%client_env, $test_name);

      print "test_loop[".$limits{loop_count}.":".
             $limits{test_count}." ".
             $client_env{loop_count}.":".
             $client_env{test_count}."]:".
             " TID ".$client_env{thread_id}.
             " test: '$test_name' ".
             " Errors: ".join(" ",@{$client_env{test_status}}),"\n";
      print "\n";
    }
  
    sleep($opt_sleep_time) if($opt_sleep_time);

  }
}

sub move_to_logs ($$$)
{
  my $path_to_logs = shift;
  my $src_file = shift;
  my $random_filename = shift;

  my $dst_file = File::Spec->catfile($path_to_logs, $random_filename);
  
  move ($src_file, $dst_file) or warn<<EOF;
ERROR: move_to_logs: File $src_file cannot be moved to $dst_file: $!
EOF
}

sub copy_test_files ()
{
  if (/\.test$/)
  { 
    $src_file = $File::Find::name;
    #print "## $File::Find::topdir - $File::Find::dir - $src_file\n";

    if ($File::Find::topdir eq $File::Find::dir && $src_file !~ /SCCS/)
    {
      $test_filename = basename($src_file);
      $dst_file = File::Spec->catfile($test_t_path, $test_filename);

      copy($src_file, $dst_file) or die "ERROR: copy_test_files: File cannot be copied. $!";
    }
  }
}

sub copy_result_files ()
{
  if (/\.result$/)
  { 
    $src_file = $File::Find::name;

    if ($File::Find::topdir eq $File::Find::dir && $src_file !~ /SCCS/)
    {
      $result_filename = basename($src_file) ;
      $dst_file = File::Spec->catfile($r_folder, $result_filename);

      copy($src_file, $dst_file) or die "ERROR: copy_result_files: File cannot be copied. $!";
    }
  }
}

sub get_timestamp
{
  my ($sec,$min,$hour,$mday,$mon,$year,$wday,$ydat,$isdst) = localtime();

  return sprintf("%04d%02d%02d%02d%02d%02d", $year+1900, $mon+1, $mday, $hour, $min, $sec);
}

sub read_tests_names
{
  my $tests_file = shift;
  my $force_load = shift;

  if ($force_load || ( (stat($tests_file->{filename}))[9] != $tests_file->{mtime}) )
  {
    open (TEST, $tests_file->{filename}) || die ("Could not open file <".
                                                  $tests_file->{filename}."> $!");
    @{$tests_file->{data}}= grep {!/^[#\r\n]|^$/} map { s/[\r\n]//g; $_ } <TEST>;

    close (TEST); 
    $tests_file->{mtime}=(stat(_))[9];
  }
}

sub get_random_test
{
  my $envt=shift;
  my $tests= $envt->{tests_file}->{data};

  my $random = int(rand(@{$tests}));
  my $test = $tests->[$random];

  return $test;
}

sub get_next_test
{
  my $envt=shift;
  my $test;

  if (@{$envt->{tests_file}->{data}})
  {
    $test=${$envt->{tests_file}->{data}}[$envt->{test_seq_idx}];
    $envt->{test_seq_idx}++;
  }
  
  #If we reach bound of array, reset seq index and increment loop counter
  if ($envt->{test_seq_idx} == scalar(@{$envt->{tests_file}->{data}}))
  {
    $envt->{test_seq_idx}=0;
    {
      lock($test_counters_lock);
      $test_counters{loop_count}++; 
    }
  }

  return $test;  
}

sub get_test
{
   my $envt=shift;

   {
     lock($test_counters_lock);
     $test_counters{test_count}++;
   }
   
   if ($envt->{mode} eq 'seq')
   {
     return get_next_test($envt);
   }
   elsif ($envt->{mode} eq 'random')
   {
     return get_random_test($envt);
   }
}

sub stress_log
{
  my ($log_file, $line)=@_;
 
  {
    open(SLOG,">>$log_file") or warn "Error during opening log file $log_file";
    print SLOG $line,"\n";
    close(SLOG);
  }
}

sub log_session_errors
{
  my ($env, $test_name) = @_;
  my $line='';

  {
    lock ($log_file_lock);

    #header in the begining of log file
    if (!-e $stress_log_file)
    {
      stress_log($stress_log_file, 
                   "TestID TID      Suite         TestFileName Found Errors");
      stress_log($stress_log_file, 
                   "=======================================================");    
    }

    $line=sprintf('%6d %3d %10s %20s %s', $env->{test_count}, threads->self->tid, 
                                          $opt_suite, $test_name, 
                                          join(",", @{$env->{test_status}}));
                                      
    stress_log($stress_log_file, $line);
    #stress_log_with_lock($stress_log_file, "\n");

    if ($opt_log_error_details)
    {
      foreach $severity (sort {$a cmp $b} keys %{$env->{errors}})
      {
        stress_log($stress_log_file, "");
        foreach $error (keys %{$env->{errors}->{$severity}})
        {
          if ($error ne 'total')
          {
            stress_log($stress_log_file, "$severity: Count:".
                      $env->{errors}->{$severity}->{$error}->[0].
                      " Error:". $env->{errors}->{$severity}->{$error}->[1]);
          }
        }
      }
    }
  }
}

sub sig_INT_handler
{
  $SIG{INT}= \&sig_INT_handler;
  $exiting=1;
  print STDERR "$$: Got INT signal-------------------------------------------\n";

}

sub sig_TERM_handler
{
  $SIG{TERM}= \&sig_TERM_handler;
  $exiting=1;
  print STDERR "$$: Got TERM signal\n";
}

sub usage
{
  print <<EOF;

The MySQL Stress suite Ver $stress_suite_version

mysql-stress-test.pl --stress-basedir=<dir> --stress-suite-basedir=<dir> --server-logs-dir=<dir>

--server-host
--server-port
--server-socket
--server-user
--server-password
--server-logs-dir
  Directory where all clients session logs will be stored. Usually 
  this is shared directory associated with server that used 
  in testing

  Required option.

--stress-suite-basedir=<dir>
  Directory that has r/ t/ subfolders with test/result files
  which will be used for testing. Also by default we are looking 
  in this directory for 'stress-tests.txt' file which contains 
  list of tests.  It is possible to specify other location of this 
  file with --stress-tests-file option.

  Required option.

--stress-basedir=<dir>
  Working directory for this test run. This directory will be used 
  as temporary location for results tracking during testing
  
  Required option.

--stress-datadir=<dir>
  Location of data files used which will be used in testing.
  By default we search for these files in <dir>/data where dir 
  is value of --stress-suite-basedir option.

--stress-init-file[=/path/to/file with tests for initialization of stress db]
  Using of this option allows to perform initialization of database
  by execution of test files. List of tests will be taken either from 
  specified file or if it omited from default file 'stress-init.txt'
  located in <--stress-suite-basedir/--suite> dir
    
--stress-tests-file[=/path/to/file with tests] 
  Using of this option allows to run stress test itself. Tests for testing 
  will be taken either from specified file or if it omited from default 
  file 'stress-tests.txt' located in <--stress-suite-basedir/--suite> dir

--stress-mode= [random|seq]
  There are two possible modes which affect order of selecting tests
  from the list:
    - in random mode tests will be selected in random order
    - in seq mode each thread will execute tests in the loop one by one as 
      they specified in the list file. 
      
--sleep-time=<time in seconds>
  Delay between test execution. Could be usefull in continued testsing 
  when one of instance of stress script perform periodical cleanup or
  recreating of some database objects

--threads=#number of threads
  Define number of threads

--check-tests-file
  Check file with list of tests. If file was modified it will force to
  reread list of tests. Could be usefull in continued testing for
  adding/removing tests without script interruption 

--mysqltest=/path/to/mysqltest binary

--verbose

--cleanup
  Force to clean up working directory (specified with --stress-basedir)

--log-error-details
  Enable errors details in the global error log file. (Default: off)

--test-count=<number of executed tests before we have to exit>
--loop-count=<number of executed loops in sequential mode before we have to exit>
--test-duration=<number of seconds that stress test should run>

Example of tool usage:

perl mysql-stress-test.pl \
--stress-suite-basedir=/opt/qa/mysql-test-extra-5.0/mysql-test \
--stress-basedir=/opt/qa/test \
--server-logs-dir=/opt/qa/logs \
--test-count=20  \
--stress-tests-file=innodb-tests.txt \
--stress-init-file=innodb-init.txt \
--threads=5 \
--suite=funcs_1  \
--mysqltest=/opt/mysql/mysql-5.0/client/mysqltest \
--server-user=root \
--server-database=test \
--cleanup \

EOF
exit(0);
}