-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathy_transaction.c
1109 lines (887 loc) · 38.8 KB
/
y_transaction.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
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
/*
* Ylib Loadrunner function library.
* Copyright (C) 2005-2014 Floris Kraak <[email protected]> | <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/*
* Documentation generated from this source code can be found here: http://randakar.github.io/y-lib/
* Main git repitory can be found at https://github.com/randakar/y-lib
*/
#ifndef _Y_TRANSACTION_C_
//! \cond include_protection
#define _Y_TRANSACTION_C_
//! \endcond
/*!
\file y_transaction.c
\brief ylib transaction library.
Allows you to extend your script with the following features:
+ Automatic transaction naming and numbering.
+ Transaction triggers: Code to be run when a transaction starts or stops.
+ Custom transaction implementations - Adding your own logging or time measurements to transactions.
Usage
-----
Include this file (or y_lib.c) in your script, call y_start_transaction() and y_end_transaction() everywhere you normally call the loadrunner variants.
Use search-and-replace to convert existing scripts. Add calls to y_start_transaction_block() and y_end_transaction_block() to set a transaction prefix.
Transaction naming
------------------
Y-lib transaction names take the form: '{transaction_prefix}_{transaction_nr}_{step_name}'.
Sub transaction names take the form: '{transaction_prefix}_{transaction_nr}_{sub_transaction_nr}_{step_name}'.
Calling y_start_transaction_block() starts a block of transactions that all use the name of the block as the transaction prefix.
Optionally the vuser group name can be added to the transaction names as well. This allows differentiation between different types of users that are all running the same script otherwise.
Triggers
--------
A trigger is a function that will execute every time an ylib transaction starts or stops. You can define seperate triggers for transaction start, stop, and the sub transaction start and stop.
The return value of the trigger is used to determine whether the transaction should pass or fail, in the case of end transaction triggers.
Custom transaction implementations
----------------------------------
Ylib can be configured to run your own custom transaction start/stop implementation instead of the usual loadrunner functions whenever a transaction starts or stops.
This can be used for a variety of things; For example, calculating the 99th percentile responstime for a specific group of transactions can be done by starting a seperate transaction on top of the normal one for the transactions in question. A different example would be having all transactions log a timestamp with the transaction name whenever a transaction starts or stops.
Testcase
--------
\code
y_setup_logging(); // Initialisation. May be omitted. (testcase?)
y_start_sub_transaction("alpha"); // starts trans '01 alpha' and subtrans '01_01 alpha'.
y_end_sub_transaction("", LR_AUTO); // ends both.
y_start_action_block("one"); // starts action block "one".
y_start_sub_transaction("beta"); // starts trans 'one_01 beta' and subtrans 'one_01_01 beta'.
y_end_sub_transaction("", LR_AUTO); // ends both.
y_end_action_block(); // ends action block "one".
y_set_add_group_to_transaction(1); // start adding the groupname - 'None', when running inside vugen.
y_start_sub_transaction("gamma"); // starts trans 'None_02 gamma' and subtrans 'None_02_01 gamma'.
y_end_sub_transaction("", LR_AUTO); // ends both.
y_start_action_block("two"); // starts action block "two".
y_start_sub_transaction("delta"); // starts trans 'None_two_01 delta' and subtrans 'None_two_01_01 delta'.
y_end_sub_transaction("", LR_AUTO); // ends both.
y_end_action_block(); // ends action block "two".
lr_abort(); // end testcase ;-)
return;
\endcode
*/
//! \cond function_removal
#define y_setup_step_waterfall 0_y_setup_step_waterfall_no_longer_exists_please_use_flow_lists
#define y_waterfall_random_weighted_continue 0_y_waterfall_random_weighted_continue_please_use_flow_lists
//! \endcond
// Needed to compile this - the definition of LAST is missing if it's not included.
#include "web_api.h"
// More C definitions
#include "vugen.h"
// Other Ylib functions
#include "y_logging.c"
#include "y_string.c"
#include "y_loadrunner_utils.c"
// Variables //
// Never access these variables directly - names may change.
// Use the get() and set() functions instead, if available. (otherwise, add them?)
//! INTERNAL: Whether to add the name of the vuser group to the transaction names. 1 = on, 0 = off.
int _y_add_group_to_trans = 0; //
//! INTERNAL: Whether to create a graph detailing wasted time. Debugging option.
int _y_wasted_time_graph = 0;
//! INTERNAL: Transaction counting for transaction blocks: \see y_start_transaction_block() and y_start_transaction()
int _y_transaction_nr = 1;
//! INTERNAL: Transaction counting for sub transactions: \see y_start_sub_transaction()
int _y_sub_transaction_nr = 1;
//! Transaction counting support for sessions. \see y_session_timer_start() and y_session_timer_end()
int y_session_transaction_count = -1;
//! Transaction status tracking
#define Y_TRANS_STATUS_NONE 0
//! Transaction status tracking
#define Y_TRANS_STATUS_STARTED 1
//! Transaction status tracking
#define Y_TRANS_STATUS_AUTO_STARTED 2
//! INTERNAL: Transaction status tracking
int _y_trans_status = Y_TRANS_STATUS_NONE;
//! Transaction trigger support typedef
typedef int (y_trigger_func)();
//! \see y_set_transaction_start_trigger()
y_trigger_func* _y_trigger_start_trans = NULL;
//! \see y_set_transaction_end_trigger()
y_trigger_func* _y_trigger_end_trans = NULL;
//! \see y_set_sub_transaction_start_trigger()
y_trigger_func* _y_trigger_start_sub_trans = NULL;
//! \see y_set_sub_transaction_end_trigger()
y_trigger_func* _y_trigger_end_sub_trans = NULL;
//! \see y_set_transaction_start_implementation()
typedef int (y_trans_start_impl_func)(char* trans_name);
//! \see y_set_transaction_end_implementation()
typedef int (y_trans_end_impl_func)(char* trans_name, int status);
//! Start transaction implementation pointer. Default lr_start_transaction(). \see y_set_transaction_start_implementation()
y_trans_start_impl_func* _y_trans_start_impl = &lr_start_transaction;
//! End transaction implementation pointer. Default lr_end_transaction(). \see y_set_transaction_end_implementation()
y_trans_end_impl_func* _y_trans_end_impl = &lr_end_transaction;
// Functions
/*! \brief Workaround for a bug in LR 11.
If you have a script that does not contain any regular loadrunner transactions but leans exclusively on ylib instead a very interesting error occurs when running.
Adding calls to lr_start_transaction() and lr_end_transaction() that are never actually used is enough to stop that from occurring.
Alternatively, the "y_start_trans_impl_func" and "y_end_trans_impl_func" pointers could be initialized to NULL. But that would stop y_start/y_end_transaction() from
actually recording transactions.. so we're not going to do that. :)
*/
void __y_do_not_call_this_is_a_workaround_that_only_exists_to_prevent_a_null_dereference_error_in_vugen_when_running()
{
lr_start_transaction("y_workaround_transaction_to_prevent_null_dereference");
lr_end_transaction( "y_workaround_transaction_to_prevent_null_dereference", LR_AUTO);
}
// Getters / Setters //
/*! Get the most recent transaction name as set by y_start_transaction().
\return The current full transaction name as set by y_start_transaction()
\note Returned pointer points to memory allocated by lr_eval_string().
\see y_start_transaction()
*/
char *y_get_current_transaction_name()
{
return lr_eval_string("{y_current_transaction}");
}
/*! Store the name of the current transaction.
\param [in] trans_name The full name of the transaction.
\see y_start_transaction()
*/
void y_set_current_transaction_name(char *trans_name)
{
lr_save_string(lr_eval_string(trans_name), "y_current_transaction");
}
/*! Get the most recent subtransaction name as set by y_start_sub_transaction().
\return The current full subtransaction name as set by y_start_sub_transaction()
\note Returned pointer points to memory allocated by lr_eval_string().
\see y_start_sub_transaction()
*/
char *y_get_current_sub_transaction_name()
{
return lr_eval_string("{y_current_sub_transaction}");
}
/*! Store the name of the current subtransaction.
\param [in] trans_name The full name of the subtransaction.
\see y_start_sub_transaction()
*/
void y_set_current_sub_transaction_name(char *trans_name)
{
lr_save_string(lr_eval_string(trans_name), "y_current_sub_transaction");
}
/*! Toggle using the vuser group name in ylib transaction names
Useful if you wish to set up two virtual user groups executing the exact same script with some kind of subtle different that makes it necessary to differentiate between the transactions from each group.
Default is off.
\param [in] add_group_to_trans Value determining if the group name is added to transaction name. Set to 1 it will be added, if set to 0 it will not be.
\see y_start_transaction()
*/
void y_set_add_group_to_transaction(int add_group_to_trans)
{
_y_add_group_to_trans = add_group_to_trans;
}
//! \cond function_removal
// Complain loudly at compile time if somebody tries to use the old versions of these calls
#define y_set_action_prefix 0_y_set_action_prefix_no_longer_exists_please_use_action_prefix
#define y_get_action_prefix 0_y_get_action_prefix_no_longer_exists_please_use_y_get_transaction_prefix
//! \endcond
/*! Set the common transaction prefix for all subsequent transactions.
\param [in] transaction_prefix The transaction prefix to store.
Used automatically by y_start_transaction_block() and friends.
\see y_start_transaction_block()
*/
void y_set_transaction_prefix(char *transaction_prefix)
{
// Bother. Let's do this the eminently simple and predictable way, then:
lr_save_string(transaction_prefix, "y_transaction_prefix");
}
/*! Get the currently used transaction prefix.
In some cases you may wish to make decisions based on what kind of clickflow is currently executing. The transaction prefix may provide exactly what you need to determine that, if you use different transaction blocks for different clickflows.
\return the current transaction_prefix.
\note The returned pointer points to memory allocated by lr_eval_string()
\see y_start_transaction_block(), y_set_transaction_prefix()
*/
char *y_get_transaction_prefix()
{
// Bother. Let's do this the eminently simple and predictable way, then:
if(y_is_empty_parameter("y_transaction_prefix"))
{
y_set_transaction_prefix("");
return "";
}
else
return lr_eval_string("{y_transaction_prefix}");
}
/*! Get the transaction number used for the next transaction started by y_start_transaction()
Useful for complicated flows where a little bit of handcrafted helper code is needed to keep the count correct.
\returns The transaction number used for the next transaction started by y_start_transaction()
\see y_start_transaction(), y_post_increment_transaction_nr()
*/
int y_get_next_transaction_nr()
{
return _y_transaction_nr;
}
/*! Increment the transaction number used by the transaction naming code.
\deprecated - y_increment_transaction_nr() does the same thing.
\returns The transaction number used for the next transaction started by y_start_transaction()
\see y_start_transaction(), y_get_next_transaction_nr()
*/
int y_post_increment_transaction_nr()
{
return _y_transaction_nr++;
}
/*! Increment the transaction number used by the transaction naming code.
Used internally.
\returns The transaction number used for the next transaction started by y_start_transaction()
\see y_start_transaction(), y_get_next_transaction_nr()
*/
int y_increment_transaction_nr()
{
return _y_transaction_nr++;
}
void y_set_next_transaction_nr(int trans_nr)
{
_y_transaction_nr = trans_nr;
}
int y_get_next_sub_transaction_nr()
{
return _y_sub_transaction_nr;
}
int y_post_increment_sub_transaction_nr()
{
return _y_sub_transaction_nr++;
}
int y_increment_sub_transaction_nr()
{
return _y_sub_transaction_nr++;
}
void y_set_next_sub_transaction_nr(int trans_nr)
{
_y_sub_transaction_nr = trans_nr;
}
// Complain loudly with a compiler error if people still use the old variants of the above.
// We renamed these on purpose, the semantics of these functions are subtly different from the old ones so existing scripts need to change.
// The most important change is that the internal transaction number now represents the *next* transaction number, not the previous one.
#define y_set_transaction_nr 0y_set_transaction_nr_no_longer_exists_please_use_y_set_next_transaction_nr
#define y_get_transaction_nr 0y_get_transaction_nr_no_longer_exists_please_use_y_get_next_transaction_nr
#define y_get_and_increment_transaction_nr 0y_get_and_increment_transaction_nr_no_longer_exists_please_use_y_increment_transaction_nr
#define y_get_sub_transaction_nr 0y_get_sub_transaction_nr_no_longer_exists_please_use_y_get_next_sub_transaction_nr
#define y_get_and_increment_sub_transaction_nr 0y_get_and_increment_sub_transaction_nr_no_longer_exists_please_use_y_increment_sub_transaction_nr
#define y_set_sub_transaction_nr 0y_set_sub_transaction_nr_no_longer_exists_please_use_y_set_next_sub_transaction_nr
int y_get_transaction_running()
{
return _y_trans_status;
}
/******
//
//Users can now set trigger functions for the start and end of y_ transactions.
//These triggers will run just before the transaction measurements start, and
//just before the transaction measurements end.
//
//Usage:
//Define a new function, as follows:
//
//int register_save_params_trigger()
//{
// web_reg_save_param("something", ...);
// web_reg_save_param("same thing in a slightly different layout", ...);
//
// return LR_PASS; // this is ignored for transaction start triggers
//}
//
//Then, at the point where this trigger should start firing call:
//
//y_set_transaction_start_trigger( ®ister_save_params_trigger );
//
//From then on out every call to y_start_transaction() and
//y_start_sub_transaction() will fire the trigger.
//A call to y_run_transaction_start_trigger() will fire the trigger too.
//
//To stop the trigger from firing call 'y_set_transaction_start_trigger(NULL);'
//
//Transaction end triggers are mirror images of transaction start triggers,
//with the notable exception that the return value is not ignored, but
//used to set the end status of the transaction before it ends.
//
*****/
void y_set_transaction_start_trigger( y_trigger_func *trigger_function )
{
_y_trigger_start_trans = trigger_function;
}
void y_set_transaction_end_trigger( y_trigger_func *trigger_function )
{
_y_trigger_end_trans = trigger_function;
}
void y_set_sub_transaction_start_trigger( y_trigger_func *trigger_function )
{
_y_trigger_start_sub_trans = trigger_function;
}
void y_set_sub_transaction_end_trigger( y_trigger_func *trigger_function )
{
_y_trigger_end_sub_trans = trigger_function;
}
// Transaction implementation change support
// For those people who for some reason feel compelled to implement lr_start_transaction() and lr_end_transaction themselves :p
// Definitions can be found further up. For reference:
// y_trans_start_impl_func* _y_trans_start_impl = &lr_start_transaction;
// y_trans_end_impl_func* _y_trans_end_impl = &lr_end_transaction;
//
void y_set_transaction_start_implementation( y_trans_start_impl_func* trans_start_func )
{
_y_trans_start_impl = trans_start_func;
}
void y_set_transaction_end_implementation( y_trans_end_impl_func* trans_end_func )
{
_y_trans_end_impl = trans_end_func;
}
y_trans_start_impl_func* y_get_transaction_start_implementation()
{
return _y_trans_start_impl;
}
y_trans_end_impl_func* y_get_transaction_end_implementation()
{
return _y_trans_end_impl;
}
// End getters/setters
int y_run_transaction_start_trigger()
{
if( _y_trigger_start_trans == NULL )
{
return 0;
}
else return _y_trigger_start_trans();
}
int y_run_transaction_end_trigger()
{
if( _y_trigger_end_trans == NULL )
{
return 0;
}
else return _y_trigger_end_trans();
}
int y_run_sub_transaction_start_trigger()
{
if( _y_trigger_start_sub_trans == NULL )
{
return 0;
}
else return _y_trigger_start_sub_trans();
}
int y_run_sub_transaction_end_trigger()
{
if( _y_trigger_end_sub_trans == NULL )
{
return 0;
}
else return _y_trigger_end_sub_trans();
}
//
// Helper function to save the transaction end status before y_end_(sub_)transaction() closes them.
//
void y_save_transaction_end_status(char* transaction_name, const char* saveparam, int status)
{
int actual_status = lr_get_transaction_status(transaction_name);
if( actual_status == LR_PASS )
{
// LR thinks everything is fine. If our code doesn't that becomes the new end status.
lr_set_transaction_status(status);
}
else // in case of a LR reported fail status of some kind.
{
// The loadrunner reported status takes precendence as those errors are usually quite fundamental.
status = actual_status;
}
if( actual_status == -16863 )
{
// Fix me: Lookup the corresponding LR constant.
lr_log_message("Warning: Possible attempt to close a transaction that has not been opened!");
}
lr_save_int(status, saveparam);
}
/////////// END HELPERS //////////
int y_session_transaction_count_increment()
{
return y_session_transaction_count++;
}
void y_session_transaction_count_report(char* session_name)
{
lr_log_message( lr_eval_string("Transaction count for %s: %d"), session_name, y_session_transaction_count);
lr_user_data_point( session_name, y_session_transaction_count);
}
void y_session_transaction_count_reset()
{
y_session_transaction_count = 0;
}
//
// Transaction blocks. Prefix all transactions in a series with the same text.
//
void y_start_transaction_block(char *transaction_prefix)
{
lr_log_message("Starting transaction block %s", transaction_prefix);
y_set_transaction_prefix(transaction_prefix);
y_set_next_transaction_nr(1);
// Start a transaction to measure total time spend in this block
//
//snprintf(_block_transaction, strlen(transaction_prefix)+7, "%s_TOTAL", transaction_prefix);
//lr_start_transaction(_block_transaction);
}
void y_end_transaction_block()
{
lr_log_message("Ending transaction block %s", y_get_transaction_prefix());
y_set_transaction_prefix("");
}
void y_pause_transaction_block()
{
lr_log_message("Pausing transaction block %s", y_get_transaction_prefix());
if( y_is_empty_parameter("y_transaction_prefix") )
{
lr_error_message("Attempt to pause transaction block when none has been started!");
return;
}
lr_save_int( y_get_next_transaction_nr(), lr_eval_string("y_paused_transaction_block_{y_transaction_prefix}_trans_nr") );
y_end_transaction_block();
}
void y_resume_transaction_block(char *transaction_prefix)
{
char *storage_param;
lr_log_message("Resuming transaction block %s", transaction_prefix);
lr_save_string(transaction_prefix, "y_resumed_transaction_block");
storage_param = lr_eval_string("y_paused_transaction_block_{y_resumed_transaction_block}_trans_nr");
if( y_is_empty_parameter(storage_param) )
{
lr_error_message("Attempt to resume transaction block %s but no such block has been paused.", transaction_prefix);
return;
}
y_start_transaction_block(transaction_prefix);
y_set_next_transaction_nr(atoi(y_get_parameter(storage_param)));
}
// DEPRECATED
void y_start_action_block(char *transaction_prefix)
{
y_start_transaction_block(transaction_prefix);
}
// DEPRECATED
void y_end_action_block()
{
y_end_transaction_block();
}
//! \cond function_removal
// Complain loudly at compile time if somebody tries to use the old versions of this call
#define y_calculate_actual_action_prefix 0_y_calculate_actual_action_prefix_no_longer_exists_please_use_y_calculate_actual_transaction_prefix
//! \endcond
/*! \brief Transaction name factory helper.
Add together the transaction prefix, vuser group name (if applicable), and transaction number, seperated by "_", and return the result in a newly allocated piece of memory on the heap.
Automatically called by y_start_transaction() and friends as needed.
\param [in] transaction_prefix The current transaction prefix as given to y_start_transaction_block()
\returns A newly allocated piece of memory on the heap containing the complete prefix, ready for concatenation with the actual transaction name.
\warning the return value of this function needs to be freed using free().
\see y_start_transaction(), y_create_transaction_name()
*/
char *y_calculate_actual_transaction_prefix(const char *transaction_prefix)
{
const char seperator[] = "_";
const int seperator_len = sizeof seperator - 1; // strlen(seperator);
int group_len = 0;
int prefix_len = strlen(transaction_prefix);
char *buffer;
size_t buffer_size;
// y_virtual_user_group is set only if y_setup() is called.
// See y_loadrunner_utils.c
y_setup();
if( _y_add_group_to_trans )
{
group_len = strlen(y_virtual_user_group);
}
// add room for the seperators
if(prefix_len > 0)
{
prefix_len += seperator_len;
}
if(group_len > 0)
{
group_len += seperator_len;
}
// allocate memory -- note this needs to be free()'ed afterwards!
buffer_size = group_len + prefix_len + 1;
buffer = y_mem_alloc(buffer_size);
buffer[0] = '\0';
// start concatenating things together
{
int len = 0;
if(group_len > 0)
{
len = snprintf(buffer, buffer_size, "%s%s", y_virtual_user_group, seperator);
}
if(prefix_len > 0)
{
snprintf(buffer + len, buffer_size-len,"%s%s", transaction_prefix, seperator);
}
}
return buffer;
}
//
// Generates the transaction name prefixed with a user defined action prefix and a transaction number.
// The result is saved in the "y_current_transaction" loadrunner parameter for use by some macro's.
//
// To use this scripts need to call y_start_action_block() - once at the start of each action.
//
//
// Dirty trick that no longer needs to be used:
//#define lr_start_transaction(transaction_name) y_start_new_transaction_name(transaction_name, _y_transaction_prefix, _trans_nr++); \
// lr_start_transaction(lr_eval_string("{y_current_transaction}"))
//#define lr_end_transaction(transaction_name, status) lr_end_transaction(lr_eval_string("{y_current_transaction}"), status)
//
void y_create_new_transaction_name(const char *transaction_name, const char *transaction_prefix, int transaction_nr)
{
const int trans_nr_len = 2; // eg. '01'
char *actual_prefix = y_calculate_actual_transaction_prefix(transaction_prefix);
int prefix_len = strlen(actual_prefix);
int trans_name_size = prefix_len + trans_nr_len +1 + strlen(transaction_name) +1;
char *actual_trans_name = y_mem_alloc( trans_name_size );
if( transaction_nr >= 100 )
{
lr_error_message("Transaction count too high (100+). Are you using y_start_action_block()?");
lr_exit(LR_EXIT_VUSER, LR_FAIL);
}
snprintf(actual_trans_name, trans_name_size, "%s%02d_%s", actual_prefix, transaction_nr, transaction_name);
free(actual_prefix);
y_set_current_transaction_name(actual_trans_name);
free(actual_trans_name);
}
void y_create_next_transaction_name( const char* transaction_name)
{
y_create_new_transaction_name(transaction_name, y_get_transaction_prefix(), y_increment_transaction_nr());
}
//
// Todo: Find a way to make this share more code with y_create_new_transaction_name()
//
void y_create_new_sub_transaction_name(const char *transaction_name, const char *transaction_prefix,
const int transaction_nr, const int sub_transaction_nr)
{
const int trans_nr_len = 2; // eg. '01'
char *actual_prefix = y_calculate_actual_transaction_prefix(transaction_prefix);
int prefix_len = strlen(actual_prefix);
int trans_name_size = prefix_len + (2 * (trans_nr_len +1)) + strlen(transaction_name) +1;
char *actual_trans_name = y_mem_alloc( trans_name_size );
if( transaction_nr >= 100 )
{
lr_error_message("Transaction count too high (100+). Are you using y_start_action_block()?");
lr_exit(LR_EXIT_VUSER, LR_FAIL);
}
snprintf(actual_trans_name, trans_name_size, "%s%02d_%02d_%s", actual_prefix, transaction_nr, sub_transaction_nr, transaction_name);
free(actual_prefix);
y_set_current_sub_transaction_name(actual_trans_name);
free(actual_trans_name);
}
void y_create_next_sub_transaction_name(const char* transaction_name)
{
y_create_new_sub_transaction_name(transaction_name,
y_get_transaction_prefix(),
y_get_next_transaction_nr()-1,
y_increment_sub_transaction_nr());
}
//
// y_start_transaction() / y_end_transaction()
// These are drop-in replacements for the loadrunner functions
// lr_start_transaction() and lr_end_transaction().
//
// These variants will add a bit more logging to facilitate post-test analysis
// with external tools, and add a common prefix based on the action block (see above)
// as well as consistent numbering.
//
int y_start_transaction(char *transaction_name)
{
// This saves it's result in the 'y_current_transaction' parameter.
y_create_next_transaction_name(transaction_name);
// Reset the sub transaction numbering.
y_set_next_sub_transaction_nr(1);
// Fire the start trigger. For complicated web_reg_find() / web_reg_save_param()
// statement collections that we want run right before starting every
// transaction in a group of transactions.
// Placed after the generation of the transaction name since that might be
// a nice thing to have available.for trigger authors.
y_run_transaction_start_trigger();
// Stops sub transactions from automagically
// creating outer transactions for themselves.
_y_trans_status = Y_TRANS_STATUS_STARTED;
//return lr_start_transaction(lr_eval_string("{y_current_transaction}"));
return _y_trans_start_impl(lr_eval_string("{y_current_transaction}"));
}
int y_start_transaction_with_number(char *transaction_name, int transaction_number)
{
y_set_next_transaction_nr(transaction_number);
return y_start_transaction(transaction_name);
}
// Note: This completely ignores the 'transaction_name' argument
// to retain compatibility with lr_end_transaction().
int y_end_transaction(char *transaction_name, int status)
{
char *trans_name = lr_eval_string("{y_current_transaction}");
// Fire the transaction end trigger. For processing the results of
// complicated web_reg_find() / web_reg_save_param() statement
// collections that repeat for a group of transactions.
// This fires before the actual end of the transaction so that it can
// still influence the transaction end status.
int trigger_result = y_run_transaction_end_trigger();
if( status == LR_PASS && trigger_result != LR_PASS )
{
lr_error_message("Transaction end trigger did not return LR_PASS");
status = trigger_result;
}
// Save the end status of this transaction. It won't be available after ending it.
y_save_transaction_end_status(trans_name, "y_last_transaction_status", status);
// Debugging: report wasted time.
// People who implement their own triggers will have to do this themselves.
if( _y_wasted_time_graph && _y_trans_end_impl == lr_end_transaction )
lr_user_data_point( lr_eval_string("wasted_{y_current_transaction}"), lr_get_transaction_wasted_time(trans_name));
// End the transaction
status = _y_trans_end_impl(trans_name, status);
// Tell our subtransaction support that there is no outer transaction
// so if a sub-transaction is created it may have to fake this.
_y_trans_status = Y_TRANS_STATUS_NONE;
if( y_session_transaction_count >= 0 ) // Values smaller than 0 means that transaction counting is disabled.
y_session_transaction_count_increment();
return status;
}
//
// y_start_sub_transaction() / y_end_sub_transaction()
// Like y_start_transaction() / y_end_transaction().
//
// Can be called outside of a running top level transaction,
// in which case it will create one as needed.
//
// This is not meant an excuse to be lazy about adding top level transactions
// but as a fallback for situations where the sub transactions can not neccesarily
// be predicted. (Think 'sudden popups from GUI apps' and similar cases.)
//
int y_start_sub_transaction(char *transaction_name)
{
// if there is no outer transaction yet, fake one
if( _y_trans_status == Y_TRANS_STATUS_NONE )
{
y_start_transaction(transaction_name);
_y_trans_status = Y_TRANS_STATUS_AUTO_STARTED;
}
y_create_next_sub_transaction_name(transaction_name);
// Fire the transaction start trigger.
y_run_sub_transaction_start_trigger();
// Actual sub transaction start
return lr_start_sub_transaction(
lr_eval_string("{y_current_sub_transaction}"),
lr_eval_string("{y_current_transaction}"));
}
int y_start_sub_transaction_with_number(char *transaction_name, int transaction_number)
{
y_set_next_sub_transaction_nr(transaction_number);
return y_start_sub_transaction(transaction_name);
}
int y_end_sub_transaction(char *transaction_name, int status)
{
char *trans_name = lr_eval_string("{y_current_sub_transaction}");
// Fire the transaction end trigger.
int trigger_result = y_run_sub_transaction_end_trigger();
if( status == LR_PASS && trigger_result != LR_PASS )
{
status = trigger_result;
}
// Save the end status of this transaction. It won't be available after ending it.
y_save_transaction_end_status(trans_name, "y_last_sub_transaction_status", status);
// Debugging: report wasted time.
if( _y_wasted_time_graph )
lr_user_data_point( lr_eval_string("wasted_{y_current_sub_transaction}"), lr_get_transaction_wasted_time(trans_name));
// End the transaction
status = lr_end_sub_transaction(trans_name, status);
// if we faked an outer transaction, fake closing it.
//
// Note: It might be an idea to move this to y_start_(sub_)transaction() instead, for
// better grouping. That may not be without it's problems though.
if( _y_trans_status == Y_TRANS_STATUS_AUTO_STARTED)
{
y_end_transaction(transaction_name, status);
}
return status;
}
int y_get_last_transaction_status()
{
char* last_trans_status_string = y_get_parameter_or_null("y_last_transaction_status");
if( last_trans_status_string == NULL )
{
return LR_AUTO; // No earlier transaction, the parameter doesn't even exist.
}
else
{
return atoi(last_trans_status_string);
}
}
//! Session timer variable for session timer support.
merc_timer_handle_t y_session_timer = NULL;
void y_session_timer_start(char* session_name)
{
lr_save_string(session_name, "y_session_name");
y_session_transaction_count_reset();
y_session_timer = lr_start_timer();
}
// Meet sessie duur en forceer een pause tot het einde van de sessie, indien nodig.
#define Y_NO_PAUSE 0
#define Y_FORCE_PAUSE 1
void y_session_timer_end(int required_session_duration, int force_pause)
{
double measured_duration;
if( y_session_timer == NULL )
{
lr_error_message("Error: y_session_timer_end() called without matching call to y_session_timer_end()!");
lr_set_transaction( "__y_sesssion_timer_end_call_without_y_session_timer_end_call", 0, LR_FAIL);
return;
}
else
{
double measured_duration = lr_end_timer(y_session_timer);
// Calculate how much time remains until the session should end.
int remaining_time = required_session_duration - measured_duration;
// Reset the timer insofar lr_end_timer() hasn't done that already.
y_session_timer = NULL;
lr_user_data_point( lr_eval_string("y_session_duration_{y_session_name}"), measured_duration);
if ( remaining_time > 0 )
{
if( force_pause == Y_FORCE_PAUSE )
{
lr_force_think_time(remaining_time);
}
}
else
{
lr_error_message( lr_eval_string("WARNING!: Measured duration of session {y_session_name} (%f) exceeded specified maximum of %d seconds!"), measured_duration, required_session_duration);
lr_set_transaction( lr_eval_string("_{y_session_name}_session_duration_overrun"), measured_duration, LR_FAIL);
}
}
y_session_transaction_count_report(lr_eval_string("y_transaction_count_{y_session_name}"));
}
//
// Shorthand for
// "y_start_transaction(transaction); web_link(linkname); y_end_transaction(transaction)"
//
// Note: In order for the logging to report correct line numbers it is advised to use the macro
// version TRANS_WEB_LINK() found below.
//
// @author: Floris Kraak
//
void y_trans_web_link(char *transaction, char *linkname)
{
char *link = lr_eval_string(linkname);
char *tmp, *trans;
size_t size;
if( !(strlen(link) > 0) )
{
lr_error_message("Zero-length link name - correlation error?");
lr_exit(LR_EXIT_ITERATION_AND_CONTINUE, LR_AUTO);
return;
}
size = strlen(link) + strlen("Text=") +1;
tmp = y_mem_alloc(size);
snprintf(tmp, size, "Text=%s", link);
trans = lr_eval_string(transaction);
y_start_transaction(trans);
web_link(link, tmp, LAST);
y_end_transaction(trans, LR_AUTO);
free(tmp);
}
//
// Shorthand for
// "y_start_transaction(transaction); web_link(linkname); y_end_transaction(transaction)"
// Macro version. Use this to preserve line numbers in the virtual user log.
// For the regular version see y_trans_web_link() above.
//