Keine Beschreibung
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

cbl.sh 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. #!/bin/bash
  2. #
  3. # CBL : Curl Benchmark on Lodel
  4. #
  5. # Usage : $0 [HOSTNAME] [INSTANCE_LIST_FILE] [N_CREATE] [N_EDIT] [N_DELETE]
  6. #
  7. # Instance_list_file is expected to be a file containing instances name (1 per
  8. # line). Uses by default /tmp/lodel2_instance_list.txt
  9. #
  10. # Instances base URL are generated given the current webui implementation :
  11. # http://HOST/INSTANCE_NAME/
  12. #
  13. #
  14. # Scenario description :
  15. #
  16. # mass_creation instance_name iteration_count :
  17. # Create iteration_count time a random leobject in given instance_name
  18. # note : do note create relation between objects, only populate content
  19. #
  20. # step 1 : fetch all non abstract class name
  21. # step 2 : loop on creation (using bash function curl_opt_create_CLSNAME)
  22. # that return the POST fields (populated with random values)
  23. #
  24. # mass_get instance_name :
  25. # Create all possible get for an instance
  26. # step 1 : fetch all classes
  27. # step 2 : for each class fetch all id
  28. # step 3 : for all ids generate the gets
  29. #
  30. # mass_deletion instance_name iteration_count :
  31. # Foreach non asbtracty class delete iteration_count time an object of
  32. # current class in current instance
  33. #
  34. # step 1 : fetch all non abstract class name
  35. # step 2 : loop on non abstract class name
  36. # step 3 : loop iteration_count time on deletion
  37. #
  38. # mass_link_edit instance_name iteration_count :
  39. # Foreach non abstract class make iteration_count time edition of an
  40. # object in current class
  41. # note : only implemented for Person for the moment
  42. # note : can maybe ask for invalid modifications
  43. #
  44. # step 1 : fetch all non abstract class name
  45. # step 2 : loop on non abstract class name
  46. # step 3 : depends on curent class :
  47. # - fetch all existing id of current class
  48. # - fetch all existing id in class that can be linked with
  49. # current class
  50. # step 4 : loop iteration_count time :
  51. # - choose a random id in current class
  52. # - choose random ids from linkable classes
  53. # - trigger edition using curl (with datas from the same
  54. # bash function than creation : curl_opt_create_CLSNAME)
  55. #
  56. # Current way to run scenarios :
  57. #
  58. # using the function run_bg_with_param FUNCTION_NAME INSTANCE_LIST_FILE *ARGS
  59. #
  60. # The function name can be one of the scenario functions
  61. # INSTANCE_LIST_FILE is the file containing instances list
  62. # *ARGS are args given as it to FUNCTION_NAME after the instance_name argument
  63. #
  64. # function call : FUN_NAME INSTANCE_NAME *ARGS
  65. #
  66. # The run_bg_with_param run a scenario in background for each instance allowing
  67. # to send a lot of request at the same time
  68. #
  69. #
  70. usage() {
  71. echo "Usage : $0 [HOSTNAME] [INSTANCE_LIST_FILE] [CREATE_COUNT] [EDIT_COUNT] [DELETE_COUNT]"
  72. exit
  73. }
  74. host=$1
  75. host=${host:=localhost}
  76. instance_list=$2
  77. instance_list=${instance_list:=/tmp/lodel2_instance_list.txt}
  78. #A modifier for requests count
  79. n_create=$3
  80. n_create=${n_create:=50}
  81. n_edit=$4
  82. n_edit=${n_edit:=10}
  83. n_delete=$5
  84. n_delete=${n_delete:=10}
  85. for i in $(seq $#)
  86. do
  87. echo $1 |grep -E "^-?-h" &>/dev/null
  88. shift
  89. done
  90. #curl_options='--silent -o /dev/null -s -w %{http_code}:%{time_connect}:%{time_starttransfer}:%{time_total}\n'
  91. curl_options='--silent -o /dev/null -s -w %{url_effective};%{http_code};%{time_connect};%{time_starttransfer};%{time_total}\n'
  92. curl_debug_opt='-v -w %{url_effective};%{http_code};%{time_connect};%{time_starttransfer};%{time_total}\n'
  93. curl_cmd="curl $curl_options"
  94. curl_raw="curl --silent"
  95. curl_debug="curl $curl_debug_opt"
  96. curcurl="$curl_cmd"
  97. cmktemp="mktemp -t lodel2_cbl_XXXXXXXX"
  98. _base_uri() {
  99. echo -n "http://$host/$1"
  100. }
  101. rnd_str() {
  102. len=$1
  103. cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w $len | head -n 1
  104. }
  105. rnd_str_len() {
  106. minlen=$1
  107. maxlen=$2
  108. cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w $(shuf -i${minlen}-${maxlen} -n1) | head -n 1
  109. }
  110. mass_creation() {
  111. #mass creation scenario
  112. #$1 is instance name
  113. #$2 is iteration count (1 iteration is 1 creation of a random class)
  114. instance_name=$1
  115. iteration_count=$2
  116. base_uri=$(_base_uri $1)
  117. cls_list_file=$(fetch_all_classes $1)
  118. if [ -z "$(cat $cls_list_file)" ]
  119. then
  120. echo "Failed to fetch class list for instance $1. Abording..." >&2
  121. exit
  122. fi
  123. if [ "$iteration_count" -le "0" ]
  124. then
  125. return
  126. fi
  127. for i in $(seq $iteration_count)
  128. do
  129. cls=$(shuf -n1 $cls_list_file)
  130. echo "${base_uri}$(uri_create $cls) POST $(curl_opt_create_$cls)"
  131. done
  132. rm -v $cls_list_file >&2
  133. }
  134. mass_get() {
  135. #mass get scenario
  136. #$1 is instance name
  137. cls_list_file=$(fetch_all_classes $1)
  138. base_uri=$(_base_uri $1)
  139. #Get the site root
  140. echo "$base_uri/"
  141. #Get the site class list
  142. echo "$base_uri/list_classes"
  143. for clsname in $(cat $cls_list_file)
  144. do
  145. #Get instances list for a class
  146. echo "$base_uri/show_class?classname=$clsname"
  147. ids_file=$(fetch_all_ids $1 $clsname)
  148. for id in $(cat $ids_file)
  149. do
  150. #Get details on an instance
  151. echo "$base_uri/show_object_detailled?classname=${clsname}&lodel_id=$id"
  152. #Get base informations on an instance
  153. echo "$base_uri/show_object?classname=${clsname}&lodel_id=$id"
  154. done
  155. done
  156. }
  157. mass_link_edit() {
  158. #mass linking & edition scenarion
  159. #$1 is instance name
  160. #$2 is iteration count
  161. instance_name=$1
  162. iteration_count=$2
  163. base_uri=$(_base_uri $1)
  164. cls_list_file=$(fetch_all_classes $1)
  165. for cls in $(cat $cls_list_file)
  166. do
  167. case $cls in
  168. Person)
  169. person_ids=$(fetch_all_ids $1 Person)
  170. section_ids=$(fetch_all_ids $1 Section)
  171. subsection_ids=$(fetch_all_ids $1 Subsection)
  172. text_ids=$($cmktemp)
  173. cat $section_ids $subsection_ids | shuf > $text_ids
  174. for i in $(seq $iteration_count)
  175. do
  176. cur_id=$(shuf -n1 $person_ids)
  177. alias_count=$(shuf -i1-5 -n1)
  178. ltext_count=$(shuf -i1-5 -n1)
  179. alias_param=$(head -n $(expr $alias_count \* $i) $person_ids| tail -n$alias_count|tr -s "\n" "," | sed 's/,$//')
  180. txt_param=$(head -n $(expr $ltext_count \* $i) $text_ids | tail -n$ltext_count|tr -s "\n" "," | sed 's/,$//')
  181. echo "$base_uri/admin/update?classname=$cls&lodel_id=$cur_id POST $(curl_opt_create_$cls $alias_param $txt_param)&uid=$cur_id"
  182. done
  183. rm -v $text_ids $person_ids $section_ids $subsection_ids >&2
  184. ;;
  185. Collection)
  186. collections_ids=$(fetch_all_ids $1 Collection)
  187. publication_ids=$(fetch_all_ids $1 Publication)
  188. for i in $(seq $iteration_count)
  189. do
  190. cur_id=$(shuf -n1 $collections_ids)
  191. publications_count=$(shuf -i1-5 -n1)
  192. publication_param=$(head -n $(expr $publications_count \* $i) $publication_ids| tail -n$publications_count|tr -s "\n" ",")
  193. echo "$base_uri/admin/update?classname=$cls&lodel_id=$cur_id POST $(curl_opt_create_$cls $publication_param)&uid=$cur_id"
  194. done
  195. rm -v $collections_ids $publication_ids >&2
  196. ;;
  197. Publication)
  198. publication_ids=$(fetch_all_ids $1 Publication)
  199. collection_ids=$(fetch_all_ids $1 Collection)
  200. for i in $(seq $iteration_count)
  201. do
  202. cur_id=$(shuf -n1 $publication_ids)
  203. collections_count=$(shuf -i1-5 -n1)
  204. collection_param=$(head -n $(expr $collections_count \* $i) $collection_ids| tail -n$collections_count|tr -s "\n" ",")
  205. echo "$base_uri/admin.update?classname=$cls&lodel_id=$cur_id POST $(curl_opt_create_$cls $collection_param)&uid=$cur_id"
  206. done
  207. rm -v $publication_ids $collection_ids >&2
  208. ;;
  209. Section)
  210. section_ids=$(fetch_all_ids $1 Section)
  211. child_ids=$(fetch_all_ids $1 Subsection)
  212. person_ids=$(fetch_all_ids $1 Person)
  213. for i in $(seq $iteration_count)
  214. do
  215. cur_id=$(shuf -n1 $section_ids)
  216. child_count=$(shuf -i1-5 -n1)
  217. person_count=$(shuf -i1-5 -n1)
  218. child_param=$(head -n $(expr $child_count \* $i) $child_ids| tail -n$child_count|tr -s "\n" ",")
  219. person_param=$(head -n $(expr $person_count \* $i) $person_ids| tail -n$person_count|tr -s "\n" ",")
  220. echo "$base_uri/admin/update?classname=$cls&lodel_id=$cur_id POST $(curl_opt_create_$cls $child_param $person_param)&uid=$cur_id"
  221. done
  222. rm -v $section_ids $child_ids $person_ids >&2
  223. ;;
  224. Subsection)
  225. subsection_ids=$(fetch_all_ids $1 Subsection)
  226. section_ids=$(fetch_all_ids $1 Section)
  227. persons_ids=$(fetch_all_ids $1 Person)
  228. parent_ids=$($cmktemp)
  229. cat $section_ids $subsection_ids | shuf > $parent_ids
  230. child_ids=$subsection_ids
  231. for i in $(seq $iteration_count)
  232. do
  233. cur_id=$(shuf -n1 $subsection_ids)
  234. child_count=$(shuf -i1-5 -n1)
  235. parent_count=$(shuf -i1-5 -n1)
  236. person_count=$(shuf -i1-5 -n1)
  237. child_param=$(head -n $(expr $child_count \* $i) $child_ids| tail -n$child_count|tr -s "\n" ",")
  238. parent_param=$(head -n $(expr $parent_count \* $i) $parent_ids| tail -n$parent_count|tr -s "\n" ",")
  239. person_param=$(head -n $(expr $person_count \* $i) $persons_ids| tail -n$person_count|tr -s "\n" ",")
  240. echo "$base_uri/admin/update?classname=$cls&lodel_id=$cur_id POST $(curl_opt_create_$cls $child_param $person_param $parent_param)&uid=$cur_id"
  241. done
  242. rm -v $subsection_ids $parent_ids $section_ids $persons_ids >&2
  243. ;;
  244. *)
  245. ;;
  246. esac
  247. done
  248. rm -v $cls_list_file >&2
  249. }
  250. mass_deletion() {
  251. #mass deletion scenario
  252. #$1 is instance name
  253. #$2 number of deletion per classes !
  254. instance_name=$1
  255. iteration_count=$2
  256. base_uri=$(_base_uri $1)
  257. cls_list_file=$(fetch_all_classes $1)
  258. for cls in $(cat $cls_list_file)
  259. do
  260. id_list_file=$(fetch_all_ids $1 $cls)
  261. if [ "$iteration_count" -gt "$(wc -l $id_list_file | cut -d " " -f1)" ]
  262. then
  263. max_iter=$(wc -l $id_list_file | cut -d " " -f1)
  264. else
  265. max_iter="$iteration_count"
  266. fi
  267. for i in $(seq $max_iter)
  268. do
  269. id=$(tail -n $i $id_list_file | head -n1)
  270. echo "${base_uri}/admin/delete?classname=$cls&lodel_id=$id"
  271. done
  272. rm -v $id_list_file >&2
  273. done
  274. rm -v $cls_list_file >&2
  275. }
  276. fetch_all_classes() {
  277. #$1 is intance name
  278. cls_list_file=$($cmktemp)
  279. $curl_raw "$(_base_uri $1)/list_classes" | grep -v Abstract |sed -nE 's/^ *<li> +<a href="show_class([^"]+)".*$/\1/p'|cut -d"=" -f2 > $cls_list_file
  280. if [ -z "$(cat $cls_list_file)" ]
  281. then
  282. echo "Unable to fetch class list for $1" >&2
  283. echo "Request was : $curl_raw '$(_base_uri $1)/list_classes'" >&2
  284. rm $cls_list_file >&2
  285. exit 1
  286. fi
  287. echo $cls_list_file
  288. }
  289. fetch_all_ids() {
  290. # Fetch all ids of a class in an instance and shuffle them
  291. instance_name=$1
  292. classname=$2
  293. idfile=$($cmktemp)
  294. $curl_raw "$(_base_uri $1)/show_class?classname=$2" | sed -nE 's/^.*<li><a href="[^=]+=[^=]+=([0-9]+)".*$/\1/p' |shuf > $idfile
  295. echo $idfile
  296. }
  297. uri_create() {
  298. clsname=$1
  299. echo -n "/admin/create?classname=$1"
  300. }
  301. curl_opt_create_Person() {
  302. #$1 is alias id
  303. #$2 is linked_texts id (comma separated)
  304. 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"
  305. }
  306. curl_opt_create_User() {
  307. 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"
  308. }
  309. curl_opt_create_Collection() {
  310. #$1 is publications id (comma separated)
  311. echo "field_input_title=$(rnd_str_len 20 50)&field_input_publications=$1&classname=Collection"
  312. }
  313. curl_opt_create_Publication() {
  314. #$1 collections id comma separated
  315. echo "field_input_collection=$1&classname=Publication"
  316. }
  317. curl_opt_create_Section() {
  318. #$1 childs id (comma separated)
  319. #$2 linked_persons id (comma separated)
  320. 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"
  321. }
  322. curl_opt_create_Subsection() {
  323. #$1 childs id (comma separated)
  324. #$2 linked_persons id (comma separated)
  325. #$3 parants id (comma separated)
  326. 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"
  327. }
  328. run_bg_with_param() {
  329. #$1 is the function name to run
  330. #$2 is the instance_list filename
  331. #other parameters are given to the function
  332. fun=$1
  333. instance_list=$2
  334. shift;shift
  335. pidlist=$($cmktemp)
  336. for iname in $(cat $instance_list)
  337. do
  338. $fun $iname $@ &
  339. echo $! >> $pidlist
  340. sleep 1
  341. done
  342. for pid in $(cat $pidlist)
  343. do
  344. wait $pid
  345. done
  346. rm -v $pidlist >&2
  347. }
  348. get_queries_with_params() {
  349. #$1 is the function name to run
  350. #$2 is the instance_list filename
  351. #other parameters are given to the function
  352. fun=$1
  353. instance_list=$2
  354. shift;shift
  355. counter=0
  356. for iname in $(cat $instance_list| sort)
  357. do
  358. echo "Running $fun $iname $@" >&2
  359. beg=$(date "+%s")
  360. $fun $iname $@
  361. tsecs=$(expr $(date "+%s") - $beg)
  362. left=$(expr $(cat $instance_list |wc -l) - $counter)
  363. counter=$(expr $counter + 1)
  364. tleft=$(expr $left \* $tsecs)
  365. percent_done=$(echo "2k ${counter}.0 100.0 * $(cat $instance_list |wc -l).0 2k/ f" | dc)
  366. echo -e "Done in ${tsecs}s\t$fun ${percent_done}% done ~$tleft secs" >&2
  367. done | shuf
  368. }
  369. get_queries_with_params "mass_get" $instance_list
  370. [ "$n_create" -gt 0 ] && get_queries_with_params "mass_creation" $instance_list $n_create
  371. [ "$n_edit" -gt 0 ] && get_queries_with_params "mass_link_edit" $instance_list $n_edit
  372. [ "$n_delete" -gt 0 ] && get_queries_with_params "mass_deletion" $instance_list $n_delete