-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathindex.php
1232 lines (1044 loc) · 50.5 KB
/
index.php
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
<?php
/**
* Load Testing Setup
*
* @copyright © 2007 The Open University
* @author [email protected]
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
* @package loadtesting
*/
/*
* This class takes one of my defined xml_elements and adds it as a child element
* to a simple xml structure given.
* It will also add any children of the object passed in.
*/
class simple_xml_constructor{
var $xml_element_pointer;
function __construct(&$xml_element_pointer) {
$this->xml_element_pointer = $xml_element_pointer;
}
public function add_child($element) {
// If there isn't a tagname throw exception
if(empty($element->tagname)) {
throw new Exception('Error: No tagname provided');
}
$current_child_element_pointer = $this->xml_element_pointer->addChild($element->tagname, $element->value);
$this->do_attributes($current_child_element_pointer, $element);
$this->do_children($current_child_element_pointer, $element);
$this->do_siblings($current_child_element_pointer, $element);
}
private function add_sibling($current_child_element_pointer, $element) {
$parent_element = array_pop($current_child_element_pointer->xpath('..'));
$current_element = $parent_element->addChild($element->tagname, $element->value);
$this->do_attributes($current_element, $element);
$this->do_children($current_element, $element);
$this->do_siblings($current_element, $element);
}
private function do_children($current_child_element_pointer, $element) {
if(!empty($element->children)) {
foreach($element->children as $data) {
$child = new simple_xml_constructor($current_child_element_pointer);
$child->add_child($data);
}
}
}
private function do_siblings($current_child_element_pointer, $element) {
if(!empty($element->siblings)) {
foreach($element->siblings as $data) {
$sibling = new simple_xml_constructor($current_child_element_pointer);
$sibling->add_sibling($current_child_element_pointer, $data);
}
}
}
private function do_attributes($current_child_element_pointer, $element) {
if(!empty($element->attributes)) {
foreach($element->attributes as $k => $v) {
$current_child_element_pointer->addAttribute($k, $v);
}
}
}
}
/*
* This class defines an xml element as a php object
*/
class xml_element {
public $tagname; // Tagname
public $attributes; // Array of Key => Values pairs
public $value; // Value for tag <tag>VALUE</tag>
public $children; // Array of xml_elements
public $siblings; // This allows siblings to be added. In jMeter some of
// the tags have connected siblings.
function __construct($tagname, $attributes=false, $value=false, $children=false, $siblings=false) {
$this->tagname = $tagname;
$this->attributes = $attributes;
$this->value = $value;
$this->children = $children;
$this->siblings = $siblings;
}
public function add_child($element) {
$this->children[] = $element;
}
public function add_sibling($element) {
$this->siblings[] = $element;
}
public function copy_to_this($data) {
$this->tagname = $data->tagname;
$this->attributes = $data->attributes;
$this->children = $data->children;
$this->siblings = $data->siblings;
}
}
class main_element extends xml_element {
function __construct($tagname, $guiclass=false, $testclass=false, $testname=false, $name=false, $enabled='true', $elementtype=false) {
$this->tagname = $tagname;
if(!empty($name)) {
$this->attributes['name'] = $name;
} else {
$this->attributes['name'] = 'NotSet';
}
if(!empty($elementtype)) {
$this->attributes['elementType'] = $elementtype;
}
if(!empty($guiclass)) {
$this->attributes['guiclass'] = $guiclass;
}
if(!empty($testclass)) {
$this->attributes['testclass'] = $testclass;
}
if(!empty($testname)) {
$this->attributes['testname'] = $testname;
}
if(!empty($enabled)) {
$this->attributes['enabled'] = $enabled;
}
}
}
class csv_dataset extends main_element {
function __construct($filename, $variablenames, $delimiter=',', $fileencoding=false, $recycle = 'true', $quoteddata = 'false', $stopthread = 'false', $sharemode='All threads') {
$csvstr = 'CSVDataSet';
parent::__construct($csvstr, "TestBeanGUI", $csvstr, "CSV Data Set Config");
$this->add_child(new stringprop('delimiter', $delimiter));
$this->add_child(new stringprop('fileEncoding', $fileencoding));
$this->add_child(new stringprop('filename', $filename));
$this->add_child(new boolprop('recycle', $recycle));
$this->add_child(new stringprop('variableNames', $variablenames));
$this->add_child(new boolprop('quotedData', $quoteddata));
$this->add_child(new boolprop('stopThread', $stopthread));
$this->add_child(new stringprop('shareMode', $sharemode));
$this->add_sibling(new hashtree());
}
}
class header_manager extends main_element {
function __construct($name, $key_value_pairs) {
parent::__construct('HeaderManager', 'HeaderPanel', 'HeaderManager', $name);
$this->add_child(new user_def_vars($key_value_pairs, 'Header', false, false, 'HeaderManager.headers', 'Header'));
}
}
class http_request_defaults extends main_element {
function __construct($properties=false) {
parent::__construct('ConfigTestElement', 'HttpDefaultsGui', 'ConfigTestElement', 'HTTP Request Defaults');
$httpstr = 'HTTPsampler';
$this->add_child(new user_def_vars_eleprop("{$httpstr}.Arguments", 'Arguments', 'HTTPArgumentsPanel', 'Arguments', 'User Defined Variables'));
$this->httpsampler_defaults($properties);
// Now add in an empty hashtree
$this->add_sibling(new hashtree());
}
function httpsampler_defaults($properties=false) {
global $CFG;
$defaults = new Object();
$defaults->domain = preg_replace('_https?://_', '', $CFG->wwwroot);
// This is for dev only, won't do anything on live!
// Find the first /
if(($pos = strpos($defaults->domain, '/')) !== false) {
$defaults->domain = substr($defaults->domain, 0, $pos);
}
$defaults->port = false;
$defaults->connect_timeout = false;
$defaults->response_timeout = false;
$defaults->protocol = false;
$defaults->contentEncoding = false;
$defaults->path = false;
if ($defaults->domain != $CFG->wwwroot) {
$defaults->protocol = preg_replace('_^(https?)://.*$_', '$1', $CFG->wwwroot);
}
if(empty($properties)) {
$properties = clone($defaults);
} else {
foreach($defaults as $key => $value) {
if(!isset($properties->$key)) {
$properties->$key = $value;
}
}
}
$htstr = 'HTTPSampler';
$this->add_child(new stringprop("{$htstr}.domain", $properties->domain));
$this->add_child(new stringprop("{$htstr}.port", $properties->port));
$this->add_child(new stringprop("{$htstr}.connect_timeout", $properties->connect_timeout));
$this->add_child(new stringprop("{$htstr}.response_timeout", $properties->response_timeout));
$this->add_child(new stringprop("{$htstr}.protocol", $properties->protocol));
$this->add_child(new stringprop("{$htstr}.contentEncoding", $properties->contentEncoding));
$this->add_child(new stringprop("{$htstr}.path", $properties->path));
}
}
/*
* DEFAULTS AS GET
*/
class httpsampler extends http_request_defaults {
function __construct($name, $path, $arguments=array(), $properties=false, $hashtree_children=false) {
$element = new main_element('HTTPSampler', 'HttpTestSampleGui', 'HTTPSampler', $name);
$element->add_child(new user_def_vars_eleprop('HTTPsampler.Arguments', 'Arguments', 'HTTPArgumentsPanel', 'Arguments', false, $arguments));
// Copy the element to this
$this->copy_to_this($element);
if(empty($properties)) {
$properties = new Object();
}
// Override the domain which is set in the master class to be empty.
if(!isset($properties->domain)) {
$properties->domain = false;
}
// Add the moodle path to the properties
if(!empty($path)) {
$path = MOODLE_PATH."/$path";
$properties->path = $path;
}
$this->httpsampler_defaults($properties);
$this->httpsampler_extra($properties);
// Now add in an empty hashtree
$hashtree = new hashtree();
if(!empty($hashtree_children)) {
foreach($hashtree_children as $child) {
$hashtree->add_child($child);
}
}
// Add the hastree
$this->add_sibling($hashtree);
}
function httpsampler_extra($properties=false) {
$defaults = new Object();
$defaults->method = 'GET';
$defaults->name = 'SomeName';
$defaults->follow_redirects = 'true';
$defaults->use_keepalive = 'false';
$defaults->auto_redirects = 'true';
$defaults->do_multipart_post = 'false';
$defaults->file_name = false;
$defaults->file_field = false;
$defaults->mimetype = false;
$defaults->monitor = 'false';
$defaults->embedded_url_re = false;
$defaults->image_parser = IMAGE_PARSER ? 'true' : 'false';
if(empty($properties)) {
$properties = clone($defaults);
} else {
foreach($defaults as $key => $value) {
if(!isset($properties->$key)) {
$properties->$key = $value;
}
}
}
$htstr = 'HTTPSampler';
$this->add_child(new stringprop("{$htstr}.method", strtoupper($properties->method)));
$this->add_child(new boolprop("{$htstr}.follow_redirects", $properties->follow_redirects));
$this->add_child(new boolprop("{$htstr}.auto_redirects", $properties->use_keepalive));
$this->add_child(new boolprop("{$htstr}.use_keepalive", $properties->auto_redirects));
$this->add_child(new boolprop("{$htstr}.DO_MULTIPART_POST", $properties->do_multipart_post));
$this->add_child(new stringprop("{$htstr}.FILE_NAME", $properties->file_name));
$this->add_child(new stringprop("{$htstr}.FILE_FIELD", $properties->file_field));
$this->add_child(new stringprop("{$htstr}.mimetype", $properties->mimetype));
$this->add_child(new boolprop("{$htstr}.monitor", $properties->monitor));
$this->add_child(new stringprop("{$htstr}.embedded_url_re", $properties->embedded_url_re));
$this->add_child(new stringprop("{$htstr}.name", 'SomeName'));
$this->add_child(new boolprop("{$htstr}.image_parser", $properties->image_parser));
}
}
class testplan extends main_element {
function __construct($name='Test Plan', $user_def_vars=array(), $consecutive_threads='false', $func_test_mode='false', $dir_or_jar_to_classpath=false, $comments=false) {
$tpstr = 'TestPlan';
parent::__construct($tpstr, "{$tpstr}Gui", $tpstr, $name);
$this->add_child(new stringprop("{$tpstr}.comments", $comments));
$this->add_child(new boolprop("{$tpstr}.functional_mode", $func_test_mode));
$this->add_child(new boolprop("{$tpstr}.serialize_threadgroups", $consecutive_threads));
$this->add_child(new user_def_vars_eleprop("{$tpstr}.user_defined_variables", 'Arguments', 'ArgumentsPanel', 'Arguments', 'User Defined Variables'));
$this->add_child(new stringprop("{$tpstr}.user_define_classpath"));
}
}
class stringprop extends xml_element {
function __construct($name, $value=false) {
parent::__construct('stringProp', array('name'=>$name), $value);
}
}
class boolprop extends xml_element {
function __construct($name, $value='false') {
parent::__construct('boolProp', array('name'=>$name), $value);
}
}
class longprop extends xml_element {
function __construct($name, $value=false) {
if(!empty($value) && intval($value) !== $value) {
throw new Exception('The value to long prop wasn\'t an int?!');
}
parent::__construct('longProp', array('name'=>$name), $value);
}
}
class eleprop extends main_element {
function __construct($name, $elementtype, $guiclass=false, $testclass=false, $testname=false, $enabled=false) {
parent::__construct('elementProp', $guiclass, $testclass, $testname, $name, $enabled, $elementtype);
}
}
class key_value_pair extends eleprop {
function __construct($name, $value, $type, $extra=false, $metadata='=', $string_bit='Argument', $encode='false', $encode_equals = 'true') {
$argstr = 'Argument';
parent::__construct($name, $type);
if(!empty($extra)) {
$this->add_child(new boolprop("HTTP{$argstr}.always_encode", $encode));
}
$this->add_child(new stringprop("{$string_bit}.value", $value));
if(!empty($metadata)) {
$this->add_child(new stringprop("{$argstr}.metadata", $metadata));
}
if(!empty($extra)) {
$this->add_child(new boolprop("HTTP{$argstr}.use_equals", $encode_equals));
}
$this->add_child(new stringprop("{$string_bit}.name", $name));
}
}
class user_def_vars extends xml_element {
function __construct($key_value_pairs=array(), $type='HTTPArgument', $extra=true, $metadata='=', $colprop_name='Arguments.arguments', $kv_name_bit='Argument') {
$col_prop = new colprop($colprop_name);
if(!empty($key_value_pairs)) {
foreach($key_value_pairs as $key => $value) {
$col_prop->add_child(new key_value_pair($key, $value, $type, $extra, $metadata, $kv_name_bit));
}
}
$this->copy_to_this($col_prop);
}
}
class user_def_vars_eleprop extends user_def_vars {
function __construct($name, $elementtype, $guiclass, $testclass, $testname, $key_value_pairs=array(), $enabled='true') {
parent::__construct($key_value_pairs);
$element = new eleprop($name, $elementtype, $guiclass, $testclass, $testname, $enabled);
$element->add_child(clone($this));
$this->copy_to_this($element);
}
}
class colprop extends xml_element {
function __construct($name='Arguments.arguments') {
$this->tagname = 'collectionProp';
$this->attributes = array('name'=>$name);
}
}
class threadgroup extends main_element {
function __construct($name, $loops=1, $loop_forever='false', $threads=1, $ramptime=1, $scheduler='false', $starttime=false, $endtime=false, $duration=false, $delay=false, $on_sample_error='continue') {
$tgstr = 'ThreadGroup';
$lcstr = 'LoopController';
parent::__construct($tgstr, "{$tgstr}Gui", $tgstr, $name);
$eleprop = new eleprop("{$tgstr}.main_controller", $lcstr, 'LoopControlPanel', $lcstr, 'Loop Controller', 'true');
$eleprop->add_child(new boolprop("{$lcstr}.continue_forever", $loop_forever));
$eleprop->add_child(new stringprop("{$lcstr}.loops", $loops));
$this->add_child($eleprop);
$this->add_child(new stringprop("{$tgstr}.num_threads", $threads));
$this->add_child(new stringprop("{$tgstr}.ramp_time", $ramptime));
$starttime = empty($starttime) ? time() : $starttime;
$endtime = empty($endtime) ? time() : $endtime;
$this->add_child(new longprop("{$tgstr}.start_time", $starttime));
$this->add_child(new longprop("{$tgstr}.end_time", $endtime));
$this->add_child(new boolprop("{$tgstr}.scheduler", $scheduler));
$this->add_child(new stringprop("{$tgstr}.on_sample_error", $on_sample_error));
$this->add_child(new stringprop("{$tgstr}.duration", $duration));
$this->add_child(new stringprop("{$tgstr}.delay", $delay));
}
}
class loopcontroller extends main_element {
function __construct($name, $loops=1, $loop_forever='false') {
parent::__construct('LoopController', 'LoopControlPanel', 'LoopController', "{$name} Loop Controller");
$this->add_child(new boolprop("LoopController.continue_forever", $loop_forever));
$this->add_child(new stringprop("LoopController.loops", $loops));
}
}
class hashtree extends xml_element {
function __construct() {
parent::__construct('hashTree');
}
}
class cookiemanager extends main_element {
function __construct() {
$cmstr = 'CookieManager';
// Create the CookieManager
parent::__construct($cmstr, 'CookiePanel', $cmstr, 'HTTP Cookie Manager');
// Create it's child elements
$this->add_child(new colprop("{$cmstr}.cookies"));
$this->add_child(new boolprop("{$cmstr}.clearEachIteration", 'true'));
// Create the sibling hashtree
$this->add_sibling(new hashtree());
}
}
class randomvariable extends main_element {
function __construct($name, $min, $max, $outputformat) {
$cmstr = 'RandomVariableConfig';
// Create the CookieManager
parent::__construct($cmstr, 'TestBeanGUI', $cmstr, 'Random Variable');
// Create it's child elements
$this->add_child(new stringprop("variableName", $name));
$this->add_child(new stringprop("minimumValue", $min));
$this->add_child(new stringprop("maximumValue", $max));
$this->add_child(new stringprop("outputFormat", $outputformat));
$this->add_child(new boolprop("perThread", 'true'));
// Create the sibling hashtree
$this->add_sibling(new hashtree());
}
}
class moodle_login extends xml_element {
function __construct($name_part) {
$arguments = array('username'=>'${username}', 'password'=>'${password}', 'FromURL' => '', 'Proceed1' => 'Sign in');
// Create a new HTTPSampler
$sampler = new httpsampler($name_part.' Login to site', 'login/index.php', $arguments, (object) array('method'=>'POST'));
$this->copy_to_this($sampler);
}
}
class moodle_logout extends xml_element {
function __construct($name_part) {
$arguments = array('sesskey'=>'${sesskey}');
// Create a new HTTPSampler
$sampler = new httpsampler($name_part.' Logout from site', 'login/index.php', $arguments, (object) array('method'=>'GET'));
$this->copy_to_this($sampler);
}
}
class master_test extends xml_element {
var $threadgroup;
var $threadgroup_hashtree;
var $name;
var $courseid;
var $wordgenerator;
function __construct() {
$this->wordgenerator = new LoremIpsumGenerator;
}
protected function convert_to_xml_element() {
$data = new xml_element($this->threadgroup->tagname, $this->threadgroup->attributes, $this->threadgroup->value, $this->threadgroup->children, array($this->threadgroup_hashtree));
unset($this->threadgroup);
unset($this->threadgroup_hashtree);
// Now I need to turn this entire object into an xml element
$this->copy_to_this($data);
}
protected function test_startup() {
if(empty($this->name)) {
$this->name = '[NOTSET]';
}
// This shouldn't ever be empty!
if(empty($this->courseid)) {
throw Exception('Error: Course id not set!');
}
// Start a new thread group
$this->threadgroup = new threadgroup($this->name, LOOPS, $loop_forever='false', USERS, USERS);
// Add in the threadgroup hashtree
$this->threadgroup_hashtree = new hashtree();
// Add in the cookie manager for this test
$this->threadgroup_hashtree->add_child(new cookiemanager());
// Now we need to add in the site login
$this->threadgroup_hashtree->add_child(new moodle_login($this->name));
// Add in the URLRewrite Modifier which automatically adds in sesskey to all post or get forms
$this->threadgroup_hashtree->add_child(new url_rewrite($this->name));
// View course page
$this->threadgroup_hashtree->add_child(new httpsampler($this->name.' View Course', 'course/view.php', array('id'=>$this->courseid)));
}
protected function test_finish() {
// Perform logout
$this->threadgroup_hashtree->add_child(new moodle_logout($this->name));
// Now add in random timer element
$this->threadgroup_hashtree->add_child(new random_timer($this->name));
}
}
class url_rewrite extends main_element {
function __construct($name_part) {
parent::__construct('URLRewritingModifier', 'URLRewritingModifierGui', 'URLRewritingModifier', $name_part.' HTTP URL Re-writing Modifier');
$this->add_child(new stringprop('argument_name', 'sesskey'));
$this->add_child(new boolprop('path_extension'));
$this->add_child(new boolprop('path_extension_no_equals'));
$this->add_child(new boolprop('path_extension_no_questionmark'));
$this->add_child(new boolprop('cache_value', 'true'));
// Add in empty hashtree
$this->add_sibling(new hashtree());
}
}
class random_timer extends main_element {
function __construct($name_part, $enabled='true', $delay=300, $range=100) {
parent::__construct('GaussianRandomTimer', 'GaussianRandomTimerGui', 'GaussianRandomTimer', $name_part.' Gaussian Random Timer', false, $enabled);
$this->add_child(new stringprop('ConstantTimer.delay', $delay));
$this->add_child(new stringprop('RandomTimer.range', sprintf ("%0.1f",$range)));
// Add in empty hashtree
$this->add_sibling(new hashtree());
}
}
class regex extends main_element {
function __construct($name, $refname, $regex, $match=false, $template='$1$', $default=false, $useheaders = 'false') {
$restr = 'RegexExtractor';
parent::__construct($restr, "{$restr}Gui", $restr, $name);
$this->add_child(new stringprop("{$restr}.useHeaders", $useheaders));
$this->add_child(new stringprop("{$restr}.refname", $refname));
$this->add_child(new stringprop("{$restr}.regex", $regex));
$this->add_child(new stringprop("{$restr}.template", $template));
$this->add_child(new stringprop("{$restr}.default", $default));
$this->add_child(new stringprop("{$restr}.match_number", $match));
// Add in empty hashtree
$this->add_sibling(new hashtree());
}
}
class results_collector extends main_element {
function __construct($name, $guiclass) {
parent::__construct('ResultCollector', $guiclass, 'ResultCollector', $name);
$this->add_child(new boolprop('ResultCollector.error_logging', 'false'));
$obj = new xml_element('objProp');
$obj->add_child(new xml_element('name', false, 'saveConfig'));
$value = new xml_element('value', array('class'=>'SampleSaveConfiguration'));
$value->add_child(new xml_element('time', false, 'true'));
$value->add_child(new xml_element('latency', false, 'true'));
$value->add_child(new xml_element('timestamp', false, 'true'));
$value->add_child(new xml_element('success', false, 'true'));
$value->add_child(new xml_element('label', false, 'true'));
$value->add_child(new xml_element('code', false, 'true'));
$value->add_child(new xml_element('message', false, 'true'));
$value->add_child(new xml_element('threadName', false, 'true'));
$value->add_child(new xml_element('dataType', false, 'true'));
$value->add_child(new xml_element('encoding', false, 'false'));
$value->add_child(new xml_element('assertions', false, 'true'));
$value->add_child(new xml_element('subresults', false, 'true'));
$value->add_child(new xml_element('responseData', false, 'false'));
$value->add_child(new xml_element('samplerData', false, 'false'));
$value->add_child(new xml_element('xml', false, 'true'));
$value->add_child(new xml_element('fieldNames', false, 'false'));
$value->add_child(new xml_element('responseHeaders', false, 'false'));
$value->add_child(new xml_element('requestHeaders', false, 'false'));
$value->add_child(new xml_element('responseDataOnError', false, 'false'));
$value->add_child(new xml_element('saveAssertionResultsFailureMessage', false, 'false'));
$value->add_child(new xml_element('assertionsResultsToSave', false, '0'));
$value->add_child(new xml_element('bytes', false, 'true'));
$obj->add_child($value);
$this->add_child($obj);
$this->add_child(new stringprop('filename'));
$this->add_sibling(new hashtree());
}
}
class tree_results extends results_collector {
function __construct() {
parent::__construct('View Results Tree', 'ViewResultsFullVisualizer');
}
}
class summary_report extends results_collector {
function __construct() {
parent::__construct('Summary Report', 'SummaryReport');
}
}
class aggregate_report extends results_collector {
function __construct() {
parent::__construct('Aggregate Report', 'StatVisualizer');
}
}
class aggregate_graph extends results_collector {
function __construct() {
parent::__construct('Aggregate Graph', 'StatGraphVisualizer');
}
}
class graph_results extends results_collector {
function __construct() {
parent::__construct('Graph Results', 'GraphVisualizer');
}
}
abstract class test_setup {
/**
* Store of the course_modules found for this test class
*/
public $cms = array();
/**
* The name of the test class
*/
private $name;
public $count = 0;
/**
* Constructor for the test_setup class
*/
public function __construct() {
$this->name = preg_replace('/_test_setup$/', '', get_class($this));
}
/**
* Retrieve a list of the course module instances for the specified course
*
* @param integer $course The course ID number
* @return array The list of course module instances
*/
public function get_cms($course) {
global $DB;
$cms = $DB->get_records_sql('
SELECT
cm.id AS cmid,
{' . $this->get_table_name() . '}.name AS name,
{' . $this->get_table_name() . '}.id AS id,
course.fullname AS course_name,
course.id AS course_id
FROM {course_modules} AS cm
JOIN {' . $this->get_table_name() . '}
ON {' . $this->get_table_name() . '}.id = cm.instance
JOIN {course} course
ON course.id = cm.course
JOIN {modules} modules
ON cm.module = modules.id
WHERE cm.visible = 1
AND cm.course = ?
AND modules.name = ?
', array($course, $this->get_name()));
return $cms;
}
/**
* The name of the test class
*
* @return String the name of the course module
*/
public function get_name() {
return $this->name;
}
/**
* The name of the table in the moodle database for this course module.
* This is used in the get_cms() function
*
* @return String the name of the table for the course module
*/
public function get_table_name() {
return $this->get_name();
}
/**
* The test class name
*
* @return String the name of the test class to use
*/
public function get_test_name() {
return $this->get_name() . '_test';
}
/**
* Any optional settings
*
* @return String HTML to output any optional settings
*/
public function optional_settings() {
return '';
}
public function process_optional_settings($data) {
}
/**
* Create a test plan for the specified activities
*
* @param object $jmeter The jmeter class
* @param array $activities The list of activities
* @return object The completed test plan
*/
public function create_testplan($jmeter, $activities) {
$class = $this->get_test_name();
foreach($activities as $activity) {
$jmeter->testplan_hashtree_constructor->add_child(new $class($activity));
}
}
}
class jmeter {
var $xml_element_pointer;
var $main_hashtree_element_pointer;
var $testplan_hashtree_element_pointer;
var $constructor;
function __construct($jmeter_data) {
global $CFG, $testclasses;
// Define the moodle path
$site = preg_replace('_https?://_', '', $CFG->wwwroot);
list($site, $path) = explode('/', $site, 2);
define('MOODLE_PATH', $path);
define('MOODLE_SITE', $site);
define('USERS', $jmeter_data['users']);
define('LOOPS', $jmeter_data['loops']);
define('IMAGE_PARSER', $jmeter_data['image_parser']);
// Start XML
$xmlstr = "<?xml version='1.0' encoding=\"UTF-8\"?>\n".
"<jmeterTestPlan version=\"1.2\" properties=\"2.1\"></jmeterTestPlan>";
// create the SimpleXMLElement object with an empty <book> element
$this->xml_element_pointer = new SimpleXMLElement($xmlstr);
$this->main_hashtree_element_pointer = $this->add_hashTree_to_xml_dom($this->xml_element_pointer);
$this->constructor = new simple_xml_constructor($this->main_hashtree_element_pointer);
// Main testplan element
$this->constructor->add_child(new testplan());
// Now we need to generate the hashTree under the testplan. This is
// where all our test threads go
$this->testplan_hashtree_element_pointer = $this->add_hashTree_to_xml_dom($this->main_hashtree_element_pointer);
// Now we need to create the $this->testplan_hashtree_constructor
$this->testplan_hashtree_constructor = new simple_xml_constructor($this->testplan_hashtree_element_pointer);
// Now we've got to add in any global stuff for the test(s) selected
$this->testplan_hashtree_constructor->add_child(new csv_dataset('csv_file.csv', 'username,password'));
// Add in the HTTP Defaults
$this->testplan_hashtree_constructor->add_child(new http_request_defaults());
foreach ($jmeter_data['activities'] as $type => $activities) {
$testclasses[$type]->create_testplan($this, $activities);
}
// Now add in the listeners
$this->testplan_hashtree_constructor->add_child(new tree_results());
$this->testplan_hashtree_constructor->add_child(new summary_report());
$this->testplan_hashtree_constructor->add_child(new aggregate_report());
$this->testplan_hashtree_constructor->add_child(new aggregate_graph());
$this->testplan_hashtree_constructor->add_child(new graph_results());
}
function add_hashTree_to_xml_dom($xml_parent_pointer) {
return $xml_parent_pointer->addChild('hashTree');
}
function get_xml() {
return $this->xml_element_pointer;
}
}
require_once(dirname(__FILE__) . '/../../config.php');
require_once($CFG->dirroot.'/report/loadtesting/LoremIpsum.class.php');
require_once($CFG->libdir.'/adminlib.php');
require_once($CFG->libdir.'/enrollib.php');
require_once($CFG->dirroot . '/enrol/self/lib.php');
// The list of available test classes
$testclasses = array();
// Include all of the possible test classes
$classdir = dirname(__FILE__) . '/classes/';
$dirlist = scandir($classdir);
foreach ($dirlist as $class) {
$testclassdir = $classdir . $class;
if (is_dir($testclassdir)) {
if (file_exists($testclassdir . '/lib.php')) {
require_once($testclassdir . '/lib.php');
$classname = $class . '_test_setup';
if (class_exists($classname)) {
$testclasses[$class] = new $classname();
}
}
}
}
// Check to see if we have had the form posted, if so we want to send the zip file!
if (!empty($_POST['action']) && $_POST['action'] == 'users') {
$user_count = intval($_POST['users']);
$userids = array();
if(empty($user_count)) {
print_error('You have not specified how many users to test with');
}
$csv = '';
for($i=1; $i<=$user_count; $i++) {
// We need to produce a user row.
$username = "testaccount$i";
$password = "password$i";
// We should only add the users if we are running moodle tests.
if(!empty($_POST['generate_users'])) {
$user = new stdClass();
$user->username = $username;
$user->firstname = 'Test';
$user->lastname = "Account [AC$i]";
$user->email = "[email protected]";
$user->country = 'GB';
$user->mnethostid = $CFG->mnet_localhost_id;
$user->lang = $CFG->lang;
$user->confirmed = 1;
$user->auth = 'manual';
$user->firstaccess = time();
$user->password = hash_internal_user_password($password);
$user->trackforums = 1;
// If we have been asked to generate the moodle accounts generate them
// now. First we need to check the account doesn't already exist
if($existinguser = $DB->get_record('user', array('email' => $user->email))) {
$userids[] = $existinguser->id;
} else {
$userids[] = $DB->insert_record('user', $user);
}
}
$csv .= "{$username},{$password}\n";
}
$jmeter_data = array();
// Now we need to add the user and loop info into the jmeter data
$jmeter_data['users'] = $user_count;
$jmeter_data['loops'] = intval($_POST['loops']);
$jmeter_data['image_parser'] = $_POST['image_parser'];
$jmeter_data['courses'] = array();
$jmeter_data['activities'] = array();
//$jmeter_data['login'] = $_POST['user_type'];
// Now we need to work out which activities we are doing
foreach($_POST['data'] as $type => $data) {
// Lazyness!!!!!
$data = (object) $data;
// Now we need to check what the user has requested
if(!empty($data->all)) {
// The user wants to do all of this type!
$jmeter_data['activities'][$type] = $_SESSION['loadtesting_data'][$type];
} else if(!empty($data->count)) {
// The user has selected to do x random selection of the type
$count = $_SESSION['loadtesting_data']["{$type}_count"];
// We need to do some bound checking to make sure they haven't
// requested to do more activities than there is
if($data->count >= $count) {
// We actally want to do all activities of this type
$jmeter_data['activities'][$type] = $_SESSION['loadtesting_data'][$type];
} else {
// We want to select a random $count of the activity type
$selected = array();
for($i=1; $i<=$data->count; $i++) {
$rand = rand(0, $count-1);
while(in_array($rand, $selected)) {
$rand = rand(0, $count-1);
}
$jmeter_data['activities'][$type][] = $_SESSION['loadtesting_data'][$type][$rand];
$selected[] = $rand;
}
}
} else {
// We don't want to do random ones, and we haven't said how many we
// want to do. Check to see if the user has selected specific ones.
if(!empty($data->todo)) {
// The user has selected some activities....
foreach($data->todo as $cmid) {
// Find the object... this could be slow should do a different way!
foreach($_SESSION['loadtesting_data'][$type] as $activity) {
if($activity->cmid == $cmid) {
$jmeter_data['activities'][$type][] = $activity;
}
}
}
}
}
$testclasses[$type]->process_optional_settings($data);
// Build the complete list of courses
if (isset($jmeter_data['activities'][$type])) {
foreach ($jmeter_data['activities'][$type] as $element) {
$jmeter_data['courses'][$element->course_id] = $element->course_id;
}
}
}
if (isset($_POST['enrol']) && $_POST['enrol'] == 1) {
$selfplugin = enrol_get_plugin('self');
$role = $DB->get_record('role', array('shortname' => 'student'));
foreach ($jmeter_data['courses'] as $courseid) {
$course = $DB->get_record('course', array('id' => $courseid));
// Check whether the loadtesting enrolment mechanism exists
$instances = enrol_get_instances($course->id, false);
$enrolinstance = null;
foreach ($instances as $instance) {
if ($instance->enrol === 'self' && $instance->name === 'loadtesting') {
$enrolinstance = $instance;
}
}
if ($enrolinstance === null) {
// Create a new enrol instance
$fields = array(
'status' => 1,
'name' => 'loadtesting',
);
$enrolinstance = $selfplugin->add_instance($course, $fields);
$enrolinstance = $DB->get_record('enrol', array('id' => $enrolinstance));
}
// Ensure that it's enabled
$selfplugin->update_status($enrolinstance, ENROL_INSTANCE_ENABLED);
// Assign all of the test users to the instance
foreach ($userids as $userid) {
$selfplugin->enrol_user($enrolinstance, $userid, $role->id);
}
}
}
// Make the jQuery script
try {
// insert the header to tell the browser how to read the document
//header("Content-type: text/html");
$jmeter = new jmeter($jmeter_data);
// print the SimpleXMLElement as a XML well-formed string
$dom_sxe = dom_import_simplexml($jmeter->get_xml());
$dom = new DOMDocument('1.0', 'UTF-8');
$dom->formatOutput = true;
$dom_sxe = $dom->importNode($dom_sxe, true);
$dom_sxe = $dom->appendChild($dom_sxe);