Переглянути джерело

V1 of CBL script

Tree 'scenario' are available :
- mass insertion
- mass edition
- mass deletion
For the moment scenario parameters and order are harcoded :
- 50 iteration for mass_insertion
- 10 iteration for mass_edition
- 10 iteration on deletion
Yann Weber 7 роки тому
джерело
коміт
8f5e33cb7f
1 змінених файлів з 221 додано та 0 видалено
  1. 221
    0
      scripts/cbl.sh

+ 221
- 0
scripts/cbl.sh Переглянути файл

@@ -0,0 +1,221 @@
1
+#!/bin/bash
2
+
3
+usage() {
4
+	echo "Usage : $0 [HOST] [INSTANCE_LIST_FILE]"
5
+	exit
6
+}
7
+
8
+host=$1
9
+host=${host:=localhost}
10
+instance_list=$2
11
+instance_list=${instance_list:=/tmp/lodel2_instance_list.txt}
12
+
13
+
14
+logdir="/tmp/lodel2_cbl_logs"
15
+if [ -d "$logdir" ]
16
+then
17
+	echo "WARNING : $logdir allready exists. It's a better idea to delete it before running this script again"
18
+	echo "waiting 3s"
19
+	sleep 3
20
+fi
21
+mkdir -p $logdir
22
+
23
+random_word_file="/usr/share/dict/words"
24
+
25
+#curl_options='--silent -o /dev/null -s -w %{http_code}:%{time_connect}:%{time_starttransfer}:%{time_total}\n'
26
+curl_options='--silent -o /dev/null -s -w %{url_effective};%{http_code};%{time_connect};%{time_starttransfer};%{time_total}\n'
27
+curl_debug_opt='-v -w %{url_effective};%{http_code};%{time_connect};%{time_starttransfer};%{time_total}\n'
28
+curl_cmd="curl $curl_options"
29
+curl_raw="curl --silent"
30
+curl_debug="curl $curl_debug_opt"
31
+
32
+curcurl="$curl_cmd"
33
+
34
+cmktemp="mktemp -t lodel2_cbl_XXXXXXXX"
35
+
36
+_base_uri() {
37
+	echo -n "http://$host/$1"
38
+}
39
+
40
+rnd_str() {
41
+	len=$1
42
+	cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w $len | head -n 1
43
+}
44
+
45
+rnd_str_len() {
46
+	minlen=$1
47
+	maxlen=$2
48
+	cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w $(shuf -i${minlen}-${maxlen} -n1) | head -n 1
49
+}
50
+
51
+mass_creation() {
52
+	#mass creation scenario
53
+	#$1 is instance name
54
+	#$2 is iteration count (1 iteration is 1 creation of a random class)
55
+	instance_name=$1
56
+	iteration_count=$2
57
+	base_uri=$(_base_uri $1)
58
+	logfile="$logdir/mass_creation_${instance_name}.log"
59
+	cls_list_file=$(fetch_all_classes $1)
60
+
61
+	for i in $(seq $iteration_count)
62
+	do
63
+		cls=$(shuf -n1 $cls_list_file)
64
+		$curcurl -d "$(curl_opt_create_$cls)" "${base_uri}$(uri_create $cls)" | tee -a $logfile
65
+	done
66
+
67
+	rm -v $cls_list_file
68
+}
69
+
70
+mass_link_edit() {
71
+	#mass linking & edition scenarion
72
+	#$1 is instance name
73
+	#$2 is iteration count
74
+	instance_name=$1
75
+	iteration_count=$2
76
+	base_uri=$(_base_uri $1)
77
+	logfile="$logdir/mass_link_edit_${instance_name}.log"
78
+	cls_list_file=$(fetch_all_classes $1)
79
+
80
+	for cls in $(cat $cls_list_file)
81
+	do
82
+		case $cls in
83
+			Person)
84
+				person_ids=$(fetch_all_ids $1 Person)
85
+				section_ids=$(fetch_all_ids $1 Section)
86
+				subsection_ids=$(fetch_all_ids $1 Section)
87
+				text_ids=$($cmktemp)
88
+				cat $section_ids $subsection_ids | shuf > $text_ids
89
+				for i in $(seq $iteration_count)
90
+				do
91
+					cur_id=$(shuf -n1 $person_ids)
92
+					alias_count=$(shuf -i1-5 -n1)
93
+					ltext_count=$(shuf -i1-5 -n1)
94
+					alias_param=$(head -n $(expr $alias_count \* $i) $person_ids| tail -n$alias_count|tr -s "\n" ",")
95
+					txt_param=$(head -n $(expr $ltext_count \* $i) $text_ids | tail -n$ltext_count|tr -s "\n" ",")
96
+					$curcurl -d "$(curl_opt_create_$cls $alias_param $txt_param)&uid=$cur_id" "$base_uri/admin/update?classname=$cls&lodel_id=$cur_id" | tee -a $logfile
97
+				done
98
+				rm -v $text_ids $person_ids $section_ids $subsection_ids
99
+				;;
100
+
101
+			*)
102
+				;;
103
+			
104
+		esac
105
+	done
106
+	rm -v $cls_list_file
107
+}
108
+
109
+mass_deletion() {
110
+	#mass deletion scenario
111
+	#$1 is instance name
112
+	#$2 number of deletion per classes !
113
+	instance_name=$1
114
+	iteration_count=$2
115
+	base_uri=$(_base_uri $1)
116
+	logfile="$logdir/mass_deletion_${instance_name}.log"
117
+	cls_list_file=$(fetch_all_classes $1)
118
+	
119
+	for cls in $(cat $cls_list_file)
120
+	do
121
+		id_list_file=$(fetch_all_ids $1 $cls)
122
+		if [ "$iteration_count" -gt "$(wc -l $id_list_file | cut -d " " -f1)" ]
123
+		then
124
+			max_iter=$(wc -l $id_list_file | cut -d " " -f1)
125
+		else
126
+			max_iter="$iteration_count"
127
+		fi
128
+
129
+		for i in $(seq $max_iter)
130
+		do
131
+			id=$(tail -n $i $id_list_file | head -n1)
132
+			$curcurl "${base_uri}/admin/delete?classname=$cls&lodel_id=$id" | tee -a $logfile
133
+		done
134
+		rm -v $id_list_file
135
+	done
136
+	rm -v $cls_list_file
137
+}
138
+
139
+
140
+fetch_all_classes() {
141
+	#$1 is intance name
142
+	cls_list_file=$($cmktemp)
143
+	$curl_raw "$(_base_uri $1)/list_classes" | grep -v Abstract |sed -nE 's/^ *<li> +<a href="([^"]+)" target="_blank".*$/\1/p'|cut -d"=" -f2 > $cls_list_file
144
+	echo $cls_list_file
145
+}
146
+
147
+fetch_all_ids() {
148
+	# Fetch all ids of a class in an instance and shuffle them
149
+	instance_name=$1
150
+	classname=$2
151
+	idfile=$($cmktemp)
152
+	$curl_raw "$(_base_uri $1)/show_class?classname=$2" | sed -nE 's/^.*<li><a href="[^=]+=[^=]+=([0-9]+)".*$/\1/p' |shuf > $idfile
153
+	echo $idfile
154
+}
155
+
156
+uri_create() {
157
+	clsname=$1
158
+	echo -n "/admin/create?classname=$1"
159
+}
160
+
161
+curl_opt_create_Person() {
162
+	#$1 is alias id
163
+	#$2 is linked_texts id (comma separated)
164
+	echo "field_input_lastname=$(rnd_str_len 10 20)&field_input_firstname=$(rnd_str_len 10 20)&field_input_alias=$1&field_input_linked_texts=$2&classname=Person"
165
+}
166
+
167
+curl_opt_create_User() {
168
+	echo "field_input_lastname=$(rnd_str_len 10 20)&field_input_firstname=$(rnd_str_len 10 20)&field_input_password=$(rnd_str 50)&field_input_login=$(rnd_str_len 5 20)&classname=User"
169
+}
170
+
171
+curl_opt_create_Collection() {
172
+	#$1 is publications id (comma separated)
173
+	echo "field_input_title=$(rnd_str_len 20 50)&field_input_publications=$1&classname=Collection"
174
+}
175
+
176
+curl_opt_create_Publication() {
177
+	#$1 collections id comma separated
178
+	echo "field_input_collection=$1&classname=Publication"
179
+}
180
+
181
+curl_opt_create_Section() {
182
+	#$1 childs id (comma separated)
183
+	#$2 linked_persons id (comma separated)
184
+	echo "field_input_title=$(rnd_str_len 20 50)&field_input_subtitle=$(rnd_str_len 20 50)&field_input_childs=$1&field_input_linked_persons=$2&classname=Section"
185
+}
186
+
187
+curl_opt_create_Subsection() {
188
+	#$1 childs id (comma separated)
189
+	#$2 linked_persons id (comma separated)
190
+	#$3 parants id (comma separated)
191
+	echo "field_input_title=$(rnd_str_len 20 50)&field_input_subtitle=$(rnd_str_len 20 50)&field_input_childs=$1&field_input_linked_persons=$2&field_input_parent=$3&classname=Subsection"
192
+}
193
+
194
+run_bg_with_param() {
195
+	#$1 is the function name to run
196
+	#$2 is the instance_list filename
197
+	#other parameters are given to the function
198
+	fun=$1
199
+	instance_list=$2
200
+	shift;shift
201
+
202
+	pidlist=$($cmktemp)
203
+	for iname in $(cat $instance_list)
204
+	do
205
+		$fun $iname $@ &
206
+		echo $! >> $pidlist
207
+	done
208
+	for pid in $(cat $pidlist)
209
+	do
210
+		wait $pid
211
+	done
212
+	rm -v $pidlist
213
+}
214
+
215
+run_bg_with_param "mass_creation" $instance_list 50
216
+run_bg_with_param "mass_link_edit" $instance_list 10
217
+run_bg_with_param "mass_deletion" $instance_list 10
218
+
219
+echo ""
220
+echo "Logs can be found in $logdir"
221
+

Loading…
Відмінити
Зберегти