summaryrefslogtreecommitdiffstats
path: root/test/testswmr.pwsh.in
blob: c2cc97e89939d7cba57051ae25723861a8fc29a1 (plain)
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
# Copyright by The HDF Group.
# All rights reserved.
#
# This file is part of HDF5.  The full HDF5 copyright notice, including
# terms governing use, modification, and redistribution, is contained in
# the COPYING file, which can be found at the root of the source code
# distribution tree, or in https://www.hdfgroup.org/licenses.
# If you do not have access to either file, you may request a copy from
# help@hdfgroup.org.
#
# Tests for the swmr feature.

$srcdir  = '@srcdir@'
$bindir  = '@bindir@'
$testdir = '@testdir@'

###############################################################################
## test parameters
###############################################################################

$Nreaders     = 5          # number of readers to launch
$Nrdrs_spa    = 3          # number of sparse readers to launch
$Nrecords     = 200000     # number of records to write
$Nrecs_rem    = 40000      # number of times to shrink
$Nrecs_spa    = 20000      # number of records to write in the sparse test
$Nsecs_add    = 5          # number of seconds per read interval
$Nsecs_rem    = 3          # number of seconds per read interval
$Nsecs_addrem = 8          # number of seconds per read interval
$nerrors      = 0

###############################################################################
## definitions for message file to coordinate test runs
###############################################################################
$WRITER_MESSAGE = 'SWMR_WRITER_MESSAGE'    # The message file created by writer that the open is complete
                                           # This should be the same as the define in "$bindir/swmr_common.h"
$MESSAGE_TIMEOUT = 300                     # Message timeout length in secs
                                           # This should be the same as the define in "./h5test.h"

# Print a line-line message left justified in a field of 70 characters
# beginning with the word "Testing".
#
function Test-WithSpaces {
   $SPACES='                                                               '
   #Write-Output  "Testing $* $SPACES" | cut -c1-70 | tr -d '\012'
}

# To wait for the writer message file or till the maximum # of seconds is reached
# $Message is the message file to wait for
# This performs similar function as the routine h5_wait_message() in test/h5test.c
function Wait-Message {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory, ValueFromPipeline)]
        [string]$TestPath,                             # Get the path of the message file to wait for
        [Parameter(Mandatory, ValueFromPipeline)]
        [string]$Message                              # Get the name of the message file to wait for
    )

    PROCESS {
        $message = Join-Path -Path $TestPath -ChildPath $Message
        $t0 = Get-Date                                # Get current time
        $t1 = $t0
        $difft = New-Timespan -Start $t0 -End $t1     # Initialize the time difference
        $mexist = 0                                   # Indicate whether the message file is found
        while ($difft.TotalSeconds -lt $MESSAGE_TIMEOUT) {    # Loop till message times out
            $t1 = Get-Date                                    # Get current time in seconds
            $difft = New-Timespan -Start $t0 -End $t1         # Calculate the time difference
            #Write-Output "Check for $message : time=$difft"
            if ([System.IO.File]::Exists($message)) {         # If message file is found:
                $mexist = 1                                   #       indicate the message file is found
                Remove-Item $message                          #       remove the message file
                break                                         #       get out of the while loop
            }
        }
        if ($mexist -eq 0) {
            # Issue warning that the writer message file is not found, continue with launching the reader(s)
            Write-Warning -Message "$WRITER_MESSAGE is not found after waiting $MESSAGE_TIMEOUT seconds"
        }
        else {
            Write-Output "$WRITER_MESSAGE is found"
        }
    }
}

###############################################################################
## Main
##
## Modifications:
##   Vailin Choi; July 2013
##     Add waiting of message file before launching the reader(s).
##     Due to the implementation of file locking, coordination
##     is needed in file opening for the writer/reader tests
##     to proceed as expected.
##
###############################################################################
# The build (current) directory might be different than the source directory.
if (![System.IO.Directory]::Exists($srcdir)) {
   $srcdir = Get-Location
   Write-Output "Setting srcdir to default: $srcdir"
}

# If the bindir directory is not set just use current (.).
if (![System.IO.Directory]::Exists($bindir)) {
   $bindir = Get-Location
   Write-Output "Setting bindir to default: $bindir"
}

# If the testdir directory is not set just use current (.).
if (![System.IO.Directory]::Exists($testdir)) {
   $testdir = Get-Location
   Write-Output "Setting testdir to default: $testdir"
}

# Check to see if the VFD specified by the HDF5_DRIVER environment variable
# supports SWMR.
$testprog = Join-Path -Path $bindir -ChildPath swmr_check_compat_vfd.exe
$rp = Start-Process -FilePath $testprog -PassThru -Wait -NoNewWindow
if ($rp.ExitCode -ne 0) {
    Write-Output ""
    Write-Output "The VFD specified by the HDF5_DRIVER environment variable"
    Write-Output "does not support SWMR."
    Write-Output ""
    Write-Output "SWMR acceptance tests skipped"
    Write-Output ""
    exit 0
}

Set-Location -Path swmr_test
$testdir = Join-Path -Path $testdir -ChildPath swmr_test

# Loop over index types
$IndexTypeLoop = '-i ea', '-i b2'
foreach ($index_type in $IndexTypeLoop) {
    # Try with and without compression
    $CompressLoop = '', '-c 5'
    foreach ($compress in $CompressLoop) {
        Write-Output ""
        Write-Output "*******************************************************************************"
        Write-Output "** Loop testing parameters: $index_type $compress"
        Write-Output "*******************************************************************************"
        Write-Output ""
        Write-Output ""
        Write-Output "###############################################################################"
        Write-Output "## Generator test"
        Write-Output "###############################################################################"
        # Launch the Generator without SWMR_WRITE
        Write-Output "launch the swmr_generator"
        $rp = Start-Process -FilePath $bindir/swmr_generator.exe -NoNewWindow -ArgumentList "$compress $index_type" -PassThru -Wait
        if ($rp.ExitCode -ne 0) {
            Write-Warning "generator had error"
            $nerrors += 1
        }

        # Launch the Generator with SWMR_WRITE
        Write-Output "launch the swmr_generator with SWMR_WRITE"
        $rp = Start-Process -FilePath $bindir/swmr_generator.exe -NoNewWindow -ArgumentList "-s $compress $index_type" -PassThru -Wait
        if ($rp.ExitCode -ne 0) {
            Write-Warning "generator had error"
            $nerrors += 1
        }

        # Check for error and exit if one occurred
        #Write-Output "nerrors=$nerrors"
        if ($nerrors -ne 0) {
            Write-Warning "SWMR tests failed with $nerrors errors."
            exit 1
        }

        Write-Output ""
        Write-Output "###############################################################################"
        Write-Output "## Use H5Fstart_swmr_write() to enable SWMR writing mode"
        Write-Output "###############################################################################"

        # Remove any possible writer message file before launching writer
        if ([System.IO.File]::Exists($WRITER_MESSAGE)) {                # If message file is found
            Remove-Item $WRITER_MESSAGE
        }
        #
        # Launch the Writer
        Write-Output "launch the swmr_start_writer"
        $seed = "" # Put -r <random seed> command here
        $rp = Start-Process -FilePath $bindir/swmr_start_write.exe -NoNewWindow -PassThru -ArgumentList "$compress $index_type $Nrecords $seed" 2>&1 |tee swmr_writer.out
        $pid_writer = $rp.id
        #Write-Output "pid_writer=$pid_writer"

        # Wait for message from writer process before starting reader(s)
        Wait-Message $testdir $WRITER_MESSAGE

        #
        # Launch the Readers
        #declare -a seeds = (<seed1> <seed2> <seed3> ... )
        Write-Output "launch $Nreaders swmr_readers"
        $pid_readers = @()
        $n = 0
        while ($n -lt $Nreaders) {
            #seed = "-r ${seeds[$n]}"
            $seed = ""
            $rp = Start-Process -FilePath $bindir/swmr_reader.exe -NoNewWindow -PassThru -ArgumentList "$Nsecs_add $seed" 2>&1 | tee swmr_reader.out.$n
            $pid_readers +=  $rp.id
            $n += 1
        }
        #Write-Output "pid_readers=$pid_readers"

        # Collect exit code of the readers first because they usually finish
        # before the writer.
        foreach ($xpid in $pid_readers) {
            #Write-Output "checked reader $xpid"
            $result = Wait-Process -Id  $xpid
            if ($result.ExitCode -ne 0) {
                Write-Warning "reader had error"
                $nerrors += 1
            }
        }

        # Collect exit code of the writer
        #Write-Output "checked writer $pid_writer"
        $result = Wait-Process -Id  $pid_writer
        if ($result.ExitCode -ne 0) {
            Write-Warning "writer had error"
            $nerrors += 1
        }

        # Check for error and exit if one occurred
        #Write-Output "nerrors=$nerrors"
        if ($nerrors -ne 0) {
            Write-Warning "SWMR tests failed with $nerrors errors."
            Write-Warning "(Writer and reader output preserved)"
            exit 1
        }

        # Clean up output files
        Remove-Item swmr_writer.out
        Remove-Item swmr_reader.out.*

        Write-Output ""
        Write-Output "###############################################################################"
        Write-Output "## Writer test - test expanding the dataset"
        Write-Output "###############################################################################"

        # Launch the Generator
        Write-Output "launch the swmr_generator"
        $rp = Start-Process -FilePath $bindir/swmr_generator.exe -NoNewWindow -PassThru -Wait -ArgumentList "-s $compress $index_type"
        if ($rp.ExitCode -ne 0) {
            Write-Warning "generator had error"
            $nerrors += 1
        }

        # Remove any possible writer message file before launching writer
        Remove-Item $WRITER_MESSAGE
        #
        # Launch the Writer
        Write-Output "launch the swmr_writer"
        $seed = "" # Put -r <random seed> command here
        $rp = Start-Process -FilePath $bindir/swmr_writer.exe -NoNewWindow -PassThru -ArgumentList "-o $Nrecords $seed" 2>&1 |tee swmr_writer.out
        $pid_writer = $rp.id
        #Write-Output "pid_writer=$pid_writer"

        # Wait for message from writer process before starting reader(s)
        Wait-Message $testdir $WRITER_MESSAGE
        #
        # Launch the Readers
        #declare -a seeds = (<seed1> <seed2> <seed3> ... )
        Write-Output "launch $Nreaders swmr_readers"
        $pid_readers = @()
        $n = 0
        while ($n -lt $Nreaders) {
            #seed = "-r ${seeds[$n]}"
            $seed = ""
            $rp = Start-Process -FilePath $bindir/swmr_reader.exe -NoNewWindow -PassThru -ArgumentList "$Nsecs_add $seed" 2>&1 |tee swmr_reader.out.$n
            $pid_readers += $rp.id
            $n += 1
        }
        #Write-Output "pid_readers=$pid_readers"

        # Collect exit code of the readers first because they usually finish
        # before the writer.
        foreach ($xpid in $pid_readers) {
            #Write-Output "checked reader $xpid"
            $result = Wait-Process -Id  $xpid
            if ($result.ExitCode -ne 0) {
                Write-Warning "reader had error"
                $nerrors += 1
            }
        }

        # Collect exit code of the writer
        #Write-Output "checked writer $pid_writer"
        $result = Wait-Process -Id  $pid_writer
        if ($result.ExitCode -ne 0) {
            Write-Warning "writer had error"
            $nerrors += 1
        }

        # Check for error and exit if one occurred
        #Write-Output "nerrors=$nerrors"
        if ($nerrors -ne 0) {
            Write-Warning "SWMR tests failed with $nerrors errors."
            Write-Warning "(Writer and reader output preserved)"
            exit 1
        }

        # Clean up output files
        Remove-Item swmr_writer.out
        Remove-Item swmr_reader.out.*

        Write-Output ""
        Write-Output "###############################################################################"
        Write-Output "## Remove test - test shrinking the dataset"
        Write-Output "###############################################################################"

        # Remove any possible writer message file before launching writer
        Remove-Item $WRITER_MESSAGE
        # Launch the Remove Writer
        Write-Output "launch the swmr_remove_writer"
        $seed = "" # Put -r <random seed> command here
        $rp = Start-Process -FilePath $bindir/swmr_remove_writer.exe -NoNewWindow -PassThru -ArgumentList "-o $Nrecs_rem $seed" 2>&1 |tee swmr_writer.out
        $pid_writer = $rp.id
        #Write-Output "pid_writer=$pid_writer"

        # Wait for message from writer process before starting reader(s)
        Wait-Message $testdir $WRITER_MESSAGE
        #
        # Launch the Remove Readers
        #declare -a seeds = (<seed1> <seed2> <seed3> ... )
        $n = 0
        $pid_readers = @()
        Write-Output "launch $Nreaders swmr_remove_readers"
        while ($n -lt $Nreaders) {
            #seed = "-r ${seeds[$n]}"
            $seed = ""
            $rp = Start-Process -FilePath $bindir/swmr_remove_reader.exe -NoNewWindow -PassThru -ArgumentList "$Nsecs_rem $seed" 2>&1 |tee swmr_reader.out.$n
            $pid_readers += $rp.id
            $n += 1
        }
        #Write-Output "pid_readers=$pid_readers"

        # Collect exit code of the readers first because they usually finish
        # before the writer.
        foreach ($xpid in $pid_readers) {
            #Write-Output "checked reader $xpid"
            $result = Wait-Process -Id  $xpid
            if ($result.ExitCode -ne 0) {
                Write-Warning "reader had error"
                $nerrors += 1
            }
        }

        # Collect exit code of the writer
        #Write-Output "checked writer $pid_writer"
        $result = Wait-Process -Id  $pid_writer
        if ($result.ExitCode -ne 0) {
            Write-Warning "writer had error"
            $nerrors += 1
        }

        # Check for error and exit if one occurred
        #Write-Output "nerrors=$nerrors"
        if ($nerrors -ne 0) {
            Write-Warning "SWMR tests failed with $nerrors errors."
            Write-Warning "(Writer and reader output preserved)"
            exit 1
        }

        # Clean up output files
        Remove-Item swmr_writer.out
        Remove-Item swmr_reader.out.*

        Write-Output ""
        Write-Output "###############################################################################"
        Write-Output "## Add/remove test - randomly grow or shrink the dataset"
        Write-Output "###############################################################################"

        # Launch the Generator
        Write-Output "launch the swmr_generator"
        $rp = Start-Process -FilePath $bindir/swmr_generator.exe -NoNewWindow-PassThru -Wait -ArgumentList "$compress $index_type"
        if ($rp.ExitCode -ne 0) {
            Write-Warning "generator had error"
            $nerrors += 1
        }

        # Launch the Writer (not in parallel - just to rebuild the datasets)
        Write-Output "launch the swmr_writer"
        $seed = "" # Put -r <random seed> command here
        $rp = Start-Process -FilePath $bindir/swmr_writer.exe -NoNewWindow -PassThru -Wait -ArgumentList "$Nrecords $seed"
        if ($rp.ExitCode -ne 0) {
            Write-Warning "writer had error"
            $nerrors += 1
        }

        # Remove any possible writer message file before launching writer
        Remove-Item $WRITER_MESSAGE
        #
        # Launch the Add/Remove Writer
        Write-Output "launch the swmr_addrem_writer"
        $seed = "" # Put -r <random seed> command here
        $rp = Start-Process -FilePath $bindir/swmr_addrem_writer.exe -NoNewWindow -PassThru -ArgumentList "$Nrecords $seed" 2>&1 |tee swmr_writer.out
        $pid_writer = $rp.id
        #Write-Output "pid_writer=$pid_writer"

        # Wait for message from writer process before starting reader(s)
        Wait-Message $testdir $WRITER_MESSAGE
        #
        # Launch the Add/Remove Readers
        #declare -a seeds = (<seed1> <seed2> <seed3> ... )
        $n = 0
        $pid_readers = @()
        Write-Output "launch $Nreaders swmr_remove_readers"
        while ($n -lt $Nreaders) {
            #seed = "-r ${seeds[$n]}"
            $seed = ""
            $rp = Start-Process -FilePath $bindir/swmr_remove_reader.exe -NoNewWindow -PassThru -ArgumentList "$Nsecs_addrem $seed" 2>&1 |tee swmr_reader.out.$n
            $pid_readers += $rp.id
            $n += 1
        }
        #Write-Output "pid_readers=$pid_readers"

        # Collect exit code of the readers first because they usually finish
        # before the writer.
        foreach ($xpid in $pid_readers) {
            #Write-Output "checked reader $xpid"
            $result = Wait-Process -Id  $xpid
            if ($result.ExitCode -ne 0) {
                Write-Warning "reader had error"
                $nerrors += 1
            }
        }

        # Collect exit code of the writer
        #Write-Output "checked writer $pid_writer"
        $result = Wait-Process -Id  $pid_writer
        if ($result.ExitCode -ne 0) {
            Write-Warning "writer had error"
            $nerrors += 1
        }

        # Check for error and exit if one occurred
        #Write-Output "nerrors=$nerrors"
        if ($nerrors -ne 0) {
            Write-Warning "SWMR tests failed with $nerrors errors."
            Write-Warning "(Writer and reader output preserved)"
            exit 1
        }

        # Clean up output files
        Remove-Item swmr_writer.out
        Remove-Item swmr_reader.out.*

        Write-Output ""
        Write-Output "###############################################################################"
        Write-Output "## Sparse writer test - test writing to random locations in the dataset"
        Write-Output "###############################################################################"

        # Launch the Generator
        # NOTE: Random seed is shared between readers and writers and is
        #       created by the generator.
        Write-Output "launch the swmr_generator"
        $seed = "" # Put -r <random seed> command here
        $rp = Start-Process -FilePath $bindir/swmr_generator.exe -NoNewWindow -PassThru -Wait -ArgumentList "$compress $index_type $seed"
        if ($rp.ExitCode -ne 0) {
            Write-Warning "generator had error"
            $nerrors += 1
        }

        # Remove any possible writer message file before launching writer
        Remove-Item $WRITER_MESSAGE
        # Launch the Sparse writer
        Write-Output "launch the swmr_sparse_writer"
        $rp = Start-Process -FilePath $bindir/swmr_sparse_writer.exe -NoNewWindow -PassThru -ArgumentList "$Nrecs_spa" 2>&1 |tee swmr_writer.out
        $pid_writer = $rp.Id
        #Write-Output "pid_writer=$pid_writer"

        # Wait for message from writer process before starting reader(s)
        Wait-Message $testdir $WRITER_MESSAGE
        #
        # Launch the Sparse readers
        $n = 0
        $pid_readers = @()
        Write-Output "launch $Nrdrs_spa swmr_sparse_readers"
        while ($n -lt $Nrdrs_spa) {
            # The sparse reader spits out a LOT of data so it's set to 'quiet'
            $rp = Start-Process -FilePath $bindir/swmr_sparse_reader.exe -NoNewWindow -PassThru -ArgumentList "-q $Nrecs_spa" 2>&1 |tee swmr_reader.out.$n
            $pid_readers += $rp.id
            $n += 1
        }
        #Write-Output "pid_readers=$pid_readers"

        # Collect exit code of the writer
        #Write-Output "checked writer $pid_writer"
        $result = Wait-Process -Id  $pid_writer
        if ($result.ExitCode -ne 0) {
            Write-Warning "writer had error"
            $nerrors += 1
        }

        # Collect exit code of the readers
        foreach ($xpid in $pid_readers) {
            #Write-Output "checked reader $xpid"
            $result = Wait-Process -Id  $xpid
            if ($result.ExitCode -ne 0) {
                Write-Warning "reader had error"
                $nerrors += 1
            }
        }

        # Check for error and exit if one occurred
        #Write-Output "nerrors=$nerrors"
        if ($nerrors -ne 0) {
            Write-Warning "SWMR tests failed with $nerrors errors."
            Write-Warning "(Writer and reader output preserved)"
            exit 1
        }

        # Clean up output files
        Remove-Item swmr_writer.out
        Remove-Item swmr_reader.out.*
    }
}

###############################################################################
## Report and exit
###############################################################################
cd ..
#Write-Output "nerrors=$nerrors"
if ($nerrors -eq 0) {
    Write-Output "SWMR tests passed."
#    if test -z "$HDF5_NOCLEANUP"; then
#        # delete the test directory
#        Remove-Item swmr_test -Recurse
#    fi
    exit 0
}
else {
    Write-Warning "SWMR tests failed with $nerrors errors."
    exit 1
}