-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathGSUtil.c
971 lines (874 loc) · 20.2 KB
/
GSUtil.c
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
/*
* Copyright (c) 1999, 2018, Oracle and/or its affiliates. All rights reserved.
*
* Licensed under the Universal Permissive License v 1.0 as shown
* at http://oss.oracle.com/licenses/upl
*/
/****************************************************************************
*
* TimesTen Scaleout sample programs - GridSample
*
* GSUtil.c - various utility functions
*
****************************************************************************/
#include "GridSample.h"
#if defined(WIN32)
#include <stdint.h>
#define srandom(x) srand(x)
#define random() rand()
#endif /* WIN32 */
/*
* Array of transaction names indexed by transaction type.
*
* Used to convert between a transaction type and a transatcion name.
* See 'getTxnType()' and 'getTxnName()' below.
*/
static char *
txnTypeMap[] =
{
TXN_UPDGRIDINFO_NAME,
TXN_GETCOUNTS_NAME,
TXN_CLEARHISTORY_NAME,
TXN_AUTHORIZE_NAME,
TXN_QUERY_NAME,
TXN_CHARGE_NAME,
TXN_TOPUP_NAME,
TXN_PURGE_NAME
};
// number of entries in the txnTypeMap array
static int
numTxnTypes = (sizeof(txnTypeMap) / sizeof(char *));
/*
* The maximum allowed random number. Also used to indicate if the random
* number mechanism has been initialised (> 0).
*/
static long maxRand = 0L;
/*
* Signal receievd indicator. If != 0 then the value indicates the signal
* that was last received.
*/
static volatile int
sigReceived = 0;
/****************************************************************************
*
* Internal functions
*
****************************************************************************/
/*
* Signal handler function.
*
* This is the function invoked when a handled signal is received.
*
* INPUTS:
* sig - signal value
*
* OUTPUTS: None
*
* RETURNS: Nothing
*/
#if defined(WIN32)
static BOOL WINAPI
ConCtrlHandler(DWORD ctrlType)
{
switch(ctrlType) {
case CTRL_C_EVENT :
case CTRL_BREAK_EVENT :
case CTRL_CLOSE_EVENT :
case CTRL_LOGOFF_EVENT :
case CTRL_SHUTDOWN_EVENT :
sigReceived = SIGINT;
break;
default:
return FALSE; /* Let someone else handle this */
}
return TRUE; /* We've handled these */
}
#else /* ! WIN32 */
static void
sigHandler(
int sig
)
{
sigReceived = sig;
} // sigHandler
#endif /* ! WIN32 */
/****************************************************************************
*
* Public functions
*
****************************************************************************/
#if defined(WIN32)
/*
* The 'gettimeofday' function.
*/
int gettimeofday( struct timeval * tp, struct timezone * tzp )
{
static const uint64_t EPOCH = ((uint64_t)116444736000000000ULL);
SYSTEMTIME system_time;
FILETIME file_time;
uint64_t time;
GetSystemTime( &system_time );
SystemTimeToFileTime( &system_time, &file_time );
time = (uint64_t)file_time.dwLowDateTime;
time += ((uint64_t)file_time.dwHighDateTime << 32);
tp->tv_sec = (long)((time - EPOCH) / 10000000L);
tp->tv_usec = (long)(system_time.wMilliseconds * 1000);
return 0;
} /* gettimeofday */
/*
* The 'strsep' function.
*/
char *
strsep(
char **stringp,
const char *delim
)
{
char * str, * p, * d;
if ( (stringp == NULL) || (*stringp == NULL) ||
(delim == NULL) || (*delim == '\0') )
return NULL;
p = str = *stringp;
while ( *p )
{
if ( strchr(delim, (int)*p) != NULL )
break;
p++;
}
if ( *p )
{
*p++ = '\0';
*stringp = p;
}
else
*stringp = NULL;
return str;
} /* strsep */
#endif /* WIN32 */
/*
* Sleep for a specified number of milliseconds.
*
* INPUTS:
* ms - number of ms to sleep for
*
* OUTPUTS: None
*
* RETURNS: Nothing
*/
#if defined(WIN32)
void
sleepMs(
unsigned int ms
)
{
Sleep( ms );
} // sleepMs
#else /* ! WIN32 */
void
sleepMs(
unsigned int ms
)
{
struct timespec rqtm, rmtm;
rqtm.tv_sec = (time_t)(ms / 1000);
rqtm.tv_nsec = (long)(ms % 1000000);
while ( nanosleep( &rqtm, &rmtm ) )
rqtm = rmtm;
} // sleepMs
#endif /* ! WIN32 */
/*
* Have we received a signal?
*
* INPUTS: None
*
* OUTPUTS: None
*
* RETURNS:
* Number of signal last received or 0.
*/
int
signalReceived(
void
)
{
return sigReceived;
} // signalReceived
/*
* Install signal handlers for SIGHUP, SIGINT and SIGTERM.
*
* INPUTS: None
*
* OUTPUTS: None
*
* RETURNS:
* Success or error code.
*/
#if defined(WIN32)
int
installSignalHandlers(
void
)
{
int rc;
rc = SetConsoleCtrlHandler(ConCtrlHandler, TRUE);
/* Our rc and their's is inverted */
if ( rc )
return 0;
return 1;
} // installSignalHandlers
#else /* ! WIN32 */
int
installSignalHandlers(
void
)
{
struct sigaction sa;
sa.sa_handler = sigHandler;
sigemptyset(&sa.sa_mask);
sa.sa_flags = 0;
if ( sigaction( SIGHUP, &sa, NULL ) )
return ERR_SIGNAL;
if ( sigaction( SIGINT, &sa, NULL ) )
return ERR_SIGNAL;
if ( sigaction( SIGTERM, &sa, NULL ) )
return ERR_SIGNAL;
return SUCCESS;
} // installSignalHandlers
#endif /* ! WIN32 */
/*
* Global cleanup and termination function.
*
* INPUTS:
* ctxt - pointer to a pointer to a context_t
exitcode - process exit code to use for exit()
*
* OUTPUTS: None
*
* RETURNS: Nothing, does not return.
*/
void
cleanup(
context_t * * ctxt,
int exitcode
)
{
if ( (ctxt != NULL) && (*ctxt != NULL) )
freeContext( ctxt );
exit( exitcode );
} // cleanup
/*
* Check if a string might be a simple positive integer value.
*
* INPUTS:
* s - the string
*
* OUTPUTS: Nothing
*
* RETURNS:
* true or false
*/
boolean
isSimpleInt(
char const * s
)
{
char * p;
if ( (s == NULL) || (*s == '\0') || (strlen(s) > 9) )
return false;
while ( *s )
if ( (*s < '0') || (*s > '9') )
return false;
else
s++;
return true;
} // isSimpleInt
/*
* Check if a string might be a simple positive long long value.
*
* INPUTS:
* s - the string
*
* OUTPUTS: Nothing
*
* RETURNS:
* true or false
*/
boolean
isSimpleLong(
char const * s
)
{
char * p;
if ( (s == NULL) || (*s == '\0') || (strlen(s) > 19) )
return false;
while ( *s )
if ( (*s < '0') || (*s > '9') )
return false;
else
s++;
return true;
} // isSimpleLong
/*
* Free a 'context_t' and everything it contains.
*
* INPUTS:
* ctxt - a pointer to a pointer to a context_t
*
* OUTPUTS: Nothing
*
* RETURNS: Nothing
*/
void
freeContext(
context_t ** ctxt
)
{
apptxn_t * txn = NULL;
if ( (ctxt == NULL) || (*ctxt == NULL) )
return; // already freed
// set all specific txn pointers to NULL
(*ctxt)->ugitxn = NULL;
(*ctxt)->gctxn = NULL;
(*ctxt)->chtxn = NULL;
(*ctxt)->ptxn = NULL;
(*ctxt)->qtxn = NULL;
(*ctxt)->atxn = NULL;
(*ctxt)->ctxn = NULL;
(*ctxt)->ttxn = NULL;
// cleanup all transactions, free memory
if ( (*ctxt)->apptxns != NULL )
{
while ( (*ctxt)->apptxns->first != NULL )
{
txn = (*ctxt)->apptxns->first;
LIST_UNLINK( (*ctxt)->apptxns, txn );
if ( txn->cleanup != NULL )
txn->cleanup( txn->parent );
free( (void *)txn );
}
free( (void *)((*ctxt)->apptxns) );
}
// disconnect from the database amnd cleanup ODBC resources
if ( (*ctxt)->dbconn != NULL )
{
if ( (*ctxt)->dbconn->connected )
{
ODBCRollback( (*ctxt)->dbconn );
ODBCDisconnect( (*ctxt)->dbconn );
}
ODBCFreeConn( &((*ctxt)->dbconn) );
}
// free error list
if ( (*ctxt)->errors != NULL )
{
ODBCFreeErrors( &((*ctxt)->errors) );
free( (void *)(*ctxt)->errors);
(*ctxt)->errors = NULL;
}
// close log file
if ( (*ctxt)->logOut != NULL )
{
fclose( (*ctxt)->logOut );
(*ctxt)->logOut = NULL;
}
// free memory and mark as freed
free( (void *)(*ctxt) );
*ctxt = NULL;
} // freeContext
/*
* Allocate and initialise a 'context_t'
*
* INPUTS:
* nctxt - a pointer to a pointer to a context_t
*
* OUTPUTS:
* nctxt set to point to the allocated and initialized context_t
*
* RETURNS:
* Success or an error code.
*/
int
allocContext(
context_t ** nctxt
)
{
int ret = SUCCESS;
context_t * ctxt;
// check parameter
if ( nctxt == NULL )
return ERR_PARAM_INTERNAL;
if ( *nctxt != NULL )
return ERR_STATE_INTERNAL;
// Allocate context
ctxt = calloc( 1, sizeof(context_t) );
if ( ctxt == NULL )
return ERR_NOMEM;
// Allocate 'apptxns' list
ctxt->apptxns = (apptxnlist_t *)calloc( 1, sizeof(apptxnlist_t) );
if ( ctxt->apptxns == NULL )
{
free( (void *)ctxt );
return ERR_NOMEM;
}
// Initialize all non-zero, non-NULL fields
ctxt->progName = DFLT_PROGNAME;
ctxt->dbDSN = DFLT_DSN;
ctxt->dbUID = DFLT_UID;
ctxt->dbPWD = DFLT_PWD;
ctxt->doCleanup = true;
ctxt->vbLevel = VB_NORMAL;
ctxt->vbInterval = VB_DFLT_RPT_INTERVAL;
ctxt->pctAuthorize = DFLT_PCT_AUTHORIZE;
ctxt->pctQuery = DFLT_PCT_QUERY;
ctxt->pctCharge = DFLT_PCT_CHARGE;
ctxt->pctTopUp = DFLT_PCT_TOPUP;
ctxt->pctPurge = DFLT_PCT_PURGE;
ctxt->purgeAge = DFLT_PURGE_AGE;
ctxt->connName = DFLT_CONN_NAME;
ctxt->retryLimit = DFLT_RETRY_LIMIT;
ctxt->failoverLimit = DFLT_FAILOVER_LIMIT;
ret = ODBCAllocConn( &(ctxt->dbconn) );
if ( ret == ERR_ODBC_NORMAL )
ctxt->errors = ctxt->dbconn->errors;
if ( ret == SUCCESS )
*nctxt = ctxt;
else
{
if ( ctxt->dbconn != NULL )
ODBCFreeConn( &(ctxt->dbconn) );
if ( ctxt->apptxns != NULL )
free( (void *)ctxt->apptxns );
free( (void *)ctxt );
}
return ret;
} // allocContext
/*
* Get current clock time with millisecond precision.
*
* INPUTS: None
*
* OUTPUTS: None
*
* RETURNS:
* Current time since the epoch expressed as milliseconds.
*/
long
getTimeMS(
void
)
{
struct timeval tv;
long currtim;
if ( gettimeofday( &tv, NULL ) )
return -1L;
currtim = (long)tv.tv_sec * (long long)1000;
currtim += (long)tv.tv_usec / (long long)1000;
return currtim;
} // getTimeMS
/*
* Generate a timestamp string for messages.
*
* INPUTS: None
*
* OUTPUTS: None
*
* RETURNS:
* A pointer to a static buffer containing the timestamp string in the
* format YYYY-MM-DD hh:mm:ss.fff.
*/
char *
getTS(
void
)
{
static char tsstring[32];
struct timeval tv;
struct tm * tval;
time_t sec;
if ( gettimeofday( &tv, NULL ) )
return NULL;
sec = tv.tv_sec;
tval = localtime( &sec );
if ( tval == NULL )
return NULL;
sprintf( tsstring,
"%4.4d-%2.2d-%2.2d %2.2d:%2.2d:%2.2d.%3.3d",
tval->tm_year + 1900,
tval->tm_mon + 1,
tval->tm_mday,
tval->tm_hour,
tval->tm_min,
tval->tm_sec,
tv.tv_usec / 1000 );
return tsstring;
} // getTS
/*
* Display (and log) an error message. Errors are always displayed and
* also are always logged if logging or debug is enabled. If an ODBC error
* list is passed, display the list of errors.
*
* INPUTS:
* ctxt - a pointer to a valid context_t
* msg - pointer to a message to display. If NULL is passed,
* uses ctxt->msgBuff.
* odbcerrs - an ODBC error list or NULL
*
* OUTPUTS: None
*
* RETURNS: Nothing
*/
void
displayError(
context_t const * ctxt,
char const * msg,
odbcerrlist_t const * odbcerrs
)
{
char * ts;
if ( ctxt != NULL )
{
if ( msg == NULL )
msg = ctxt->msgBuff;
ts = getTS();
fprintf( stdout, "%s: %s\n", ts, msg );
if ( ctxt->logOut != NULL )
fprintf( ctxt->logOut, "%s: %s\n", ts, msg );
if ( odbcerrs != NULL )
{
odbcerr_t * err = odbcerrs->first;
while ( err != NULL )
{
fprintf( stdout, "%s: %s / %d / %s\n",
ts, err->sqlstate,
err->native_error, err->error_msg );
if ( ctxt->logOut != NULL )
fprintf( ctxt->logOut, "%s: %s / %d / %s\n",
ts, err->sqlstate,
err->native_error, err->error_msg );
err = err->next;
}
}
}
} // displayError
/*
* Log a message (if logging is enabled). Nothing is displayed.
*
* INPUTS:
* ctxt - a pointer to a valid context_t
* msg - pointer to a message to log. If NULL is passed,
* uses ctxt->msgBuff.
*
* OUTPUTS: None
*
* RETURNS: Nothing
*/
void
logMessage(
context_t const * ctxt,
char const * msg
)
{
char * ts;
if ( (ctxt != NULL) && (ctxt->logOut != NULL) )
{
if ( msg == NULL )
msg = ctxt->msgBuff;
ts = getTS();
fprintf( ctxt->logOut, "%s: %s\n", ts, msg );
}
} // logMessage
/*
* Display and/or log a message. Messages are only displayed if
* silent mode is not in effect. Messages are always logged if logging
* or debug is in effect.
*
* INPUTS:
* ctxt - a pointer to a valid context_t
* msg - pointer to a message to display/log. If NULL is passed,
* uses ctxt->msgBuff.
*
* OUTPUTS: None
*
* RETURNS: Nothing
*/
void
displayMessage(
context_t const * ctxt,
char const * msg
)
{
char * ts;
if ( ctxt != NULL )
{
if ( msg == NULL )
msg = ctxt->msgBuff;
ts = getTS();
if ( ctxt->vbLevel > VB_SILENT )
fprintf( stdout, "%s: %s\n", ts, msg );
if ( ctxt->logOut != NULL )
fprintf( ctxt->logOut, "%s: %s\n", ts, msg );
}
} // displayMessage
/*
* Log a message if debug mode is enabled. Nothing is displayed.
*
* INPUTS:
* ctxt - a pointer to a valid context_t
* msg - pointer to a message to log. If NULL is passed,
* uses ctxt->msgBuff.
*
* OUTPUTS: None
*
* RETURNS: Nothing
*/
#if defined(DEBUG)
void
debugMessage(
context_t const * ctxt,
char const * msg
)
{
char * ts;
if ( (ctxt != NULL) && ctxt->debugMode && (ctxt->logOut != NULL) )
{
if ( msg == NULL )
msg = ctxt->msgBuff;
ts = getTS();
fprintf( ctxt->logOut, "%s: %s\n", ts, msg );
}
} // debugMessage
#endif // DEBUG
/*
* Map a transaction name to a type.
*
* INPUTS:
* txnname - a transaction name
*
* OUTPUTS: None
*
* RETURNS:
* The txn type code matching the name or TXN_INVALID (0) if the name
* cannot be matched (case sensitive).
*/
int
getTxnType(
char const * txnname
)
{
int txntype = 0;
if ( txnname != NULL )
{
while ( txntype < numTxnTypes )
{
if ( strcmp( txnname, txnTypeMap[txntype] ) == 0 )
return txntype + 1;
txntype += 1;
}
}
return 0;
} // getTxnType
/*
* Map a transaction type to a name.
*
* INPUTS:
* txntype - a transaction code
*
* OUTPUTS: None
*
* RETURNS:
* The txn name matching the passed in code or NULL if the code is invalid.
*/
char *
getTxnName(
int txntype
)
{
txntype--;
if ( (txntype < 0) || (txntype >= numTxnTypes) )
return NULL;
return txnTypeMap[txntype];
} // getTxnName
/*
* Should we stop?
*
* Examines the signal state and the execution limit parameters and indicates
* if the time has come to cease execution.
*
* INPUTS:
* ctxt - a valid context_t
*
* OUTPUTS: None
*
* RETURNS:
* true or false.
*/
boolean
shouldTerminate(
context_t * ctxt
)
{
long now;
int sig;
sig = signalReceived(); // interrupt?
if ( sig ) // interrupted
{
if ( (ctxt->vbLevel != VB_SILENT) && (sig == SIGINT) )
printf("\010\010"); // erase '^C' from interrupt
displayMessage( ctxt, MSG_INTERRUPT );
return true;
}
if ( ! ctxt->limitExecution )
return false;
// execution limit is defined
if ( ctxt->runNumTxn > 0 )
{
// check if we have done the required number of txns
if ( ctxt->totalTxnExecuted >= ctxt->runNumTxn )
{
displayMessage( ctxt, MSG_TXN_LIMIT );
return true;
}
}
else
{
// check if we have run for the required time
if ( ((getTimeMS() - ctxt->startTime) / 1000) >= ctxt->runDuration )
{
displayMessage( ctxt, MSG_TIME_LIMIT );
return true;
}
}
return false;
} // shouldTerminate
/*
* Get a random long in the range 'min' to 'max'.
*
* INPUTS:
* min - minimum value ( > 0)
* max - maximum value (> min)
*
* OUTPUTS: None
*
* RETURNS:
* A random long in the range 'min' to 'max' inclusive or 0 if error.
*/
long
getRandomLong(
long min,
long max
)
{
long rval = 0;
if ( maxRand == 0L )
{
#if defined(RAND_MAX)
maxRand = RAND_MAX;
#else
maxRand = (long)exp2(31) - 1L;
#endif
srandom( (unsigned int)( (int)(getTimeMS() + getpid()) ) );
}
if ( (min >= max) || (max < 1) || (min < 0) || ((max-min) >= maxRand) )
rval = 0;
else
// rval = ( random() % ((max-min)+1) ) + min;
rval = ( random() % (max-min) ) + min;
return rval;
} // getRandomLong
/*
* Get a random double in the range 'min' to 'max'.
*
* INPUTS:
* min - minimum value ( > 0.0)
* max - maximum value (> min)
*
* OUTPUTS: None
*
* RETURNS:
* A random double in the range 'min' to 'max' inclusive or 0.0 if error.
*/
double
getRandomDouble(
double min,
double max
)
{
return (double)(getRandomLong( (long)min*100, (long)max*100 ) / 100.0);
} // getRandomDouble
/*
* Get a random (but valid) CustomerID.
*
* INPUTS:
* ctxt - pointer to a valid context_t
*
* OUTPUTS: None
*
* RETURNS:
* A random customer ID that is guaranteed to exist in the database.
*/
long
getRandomCustomer(
context_t * ctxt
)
{
return getRandomLong( ctxt->minCustID, ctxt->maxCustID );
} // getRandomCustomer
/*
* Get a random (but valid) AccountID.
*
* INPUTS:
* ctxt - pointer to a valid context_t
*
* OUTPUTS: None
*
* RETURNS:
* A random account ID that is guaranteed to exist in the database.
*/
long
getRandomAccount(
context_t * ctxt
)
{
return getRandomLong( ctxt->minAccountID, ctxt->maxAccountID );
} // getRandomCustomer
/*
* Randomly choose a transaction type based on the relative percentages
* for each workload transaction.
*
* INPUTS:
* ctxt - pointer to a valid context_t
*
* OUTPUTS: None
*
* RETURNS:
* A random txn type. The distribution of values follows the relative
* percentages specified for the workload transaction mix.
*/
int
chooseTxnType(
context_t * ctxt
)
{
int rnd;
rnd = (int)getRandomLong(0, 100);
if ( rnd < (ctxt->pctAuthorize + ctxt->pctQuery +
ctxt->pctCharge + ctxt->pctTopUp) )
{
if ( rnd < (ctxt->pctAuthorize + ctxt->pctQuery + ctxt->pctCharge) )
{
if ( rnd < (ctxt->pctAuthorize + ctxt->pctQuery) )
{
if ( rnd < ctxt->pctAuthorize )
return TXN_AUTHORIZE;
else
return TXN_QUERY;
}
else
return TXN_CHARGE;
}
else
return TXN_TOPUP;
}
else
return TXN_PURGE;
} // chooseTxnType