No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

cbl.sh 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. #!/bin/bash
  2. #
  3. # CBL : Curl Benchmark on Lodel
  4. #
  5. # Usage : $0 [HOSTNAME] [INSTANCE_LIST_FILE]
  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_deletion instance_name iteration_count :
  25. # Foreach non asbtracty class delete iteration_count time an object of
  26. # current class in current instance
  27. #
  28. # step 1 : fetch all non abstract class name
  29. # step 2 : loop on non abstract class name
  30. # step 3 : loop iteration_count time on deletion
  31. #
  32. # mass_link_edit instance_name iteration_count :
  33. # Foreach non abstract class make iteration_count time edition of an
  34. # object in current class
  35. # note : only implemented for Person for the moment
  36. # note : can maybe ask for invalid modifications
  37. #
  38. # step 1 : fetch all non abstract class name
  39. # step 2 : loop on non abstract class name
  40. # step 3 : depends on curent class :
  41. # - fetch all existing id of current class
  42. # - fetch all existing id in class that can be linked with
  43. # current class
  44. # step 4 : loop iteration_count time :
  45. # - choose a random id in current class
  46. # - choose random ids from linkable classes
  47. # - trigger edition using curl (with datas from the same
  48. # bash function than creation : curl_opt_create_CLSNAME)
  49. #
  50. # Current way to run scenarios :
  51. #
  52. # using the function run_bg_with_param FUNCTION_NAME INSTANCE_LIST_FILE *ARGS
  53. #
  54. # The function name can be one of the scenario functions
  55. # INSTANCE_LIST_FILE is the file containing instances list
  56. # *ARGS are args given as it to FUNCTION_NAME after the instance_name argument
  57. #
  58. # function call : FUN_NAME INSTANCE_NAME *ARGS
  59. #
  60. # The run_bg_with_param run a scenario in background for each instance allowing
  61. # to send a lot of request at the same time
  62. #
  63. #
  64. usage() {
  65. echo "Usage : $0 [HOSTNAME] [INSTANCE_LIST_FILE] [CREATE_COUNT] [EDIT_COUNT] [DELETE_COUNT]"
  66. exit
  67. }
  68. host=$1
  69. host=${host:=localhost}
  70. instance_list=$2
  71. instance_list=${instance_list:=/tmp/lodel2_instance_list.txt}
  72. #A modifier for requests count
  73. n_create=$3
  74. n_create=${n_create:=50}
  75. n_edit=$4
  76. n_edit=${n_edit:=10}
  77. n_delete=$5
  78. n_delete=${n_delete:=10}
  79. for i in $(seq $#)
  80. do
  81. echo $1 |grep -E "^-?-h" &>/dev/null && usage
  82. shift
  83. done
  84. logdir="/tmp/lodel2_cbl_logs"
  85. if [ -d "$logdir" ]
  86. then
  87. echo "WARNING : $logdir allready exists. It's a better idea to delete it before running this script again"
  88. echo "waiting 3s"
  89. sleep 3
  90. fi
  91. mkdir -p $logdir
  92. #curl_options='--silent -o /dev/null -s -w %{http_code}:%{time_connect}:%{time_starttransfer}:%{time_total}\n'
  93. curl_options='--silent -o /dev/null -s -w %{url_effective};%{http_code};%{time_connect};%{time_starttransfer};%{time_total}\n'
  94. curl_debug_opt='-v -w %{url_effective};%{http_code};%{time_connect};%{time_starttransfer};%{time_total}\n'
  95. curl_cmd="curl $curl_options"
  96. curl_raw="curl --silent"
  97. curl_debug="curl $curl_debug_opt"
  98. curcurl="$curl_cmd"
  99. cmktemp="mktemp -t lodel2_cbl_XXXXXXXX"
  100. _base_uri() {
  101. echo -n "http://$host/$1"
  102. }
  103. rnd_str() {
  104. len=$1
  105. cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w $len | head -n 1
  106. }
  107. rnd_str_len() {
  108. minlen=$1
  109. maxlen=$2
  110. cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w $(shuf -i${minlen}-${maxlen} -n1) | head -n 1
  111. }
  112. mass_creation() {
  113. #mass creation scenario
  114. #$1 is instance name
  115. #$2 is iteration count (1 iteration is 1 creation of a random class)
  116. instance_name=$1
  117. iteration_count=$2
  118. base_uri=$(_base_uri $1)
  119. logfile="$logdir/mass_creation_${instance_name}.log"
  120. cls_list_file=$(fetch_all_classes $1)
  121. if [ "$iteration_count" -le "0" ]
  122. then
  123. return
  124. fi
  125. for i in $(seq $iteration_count)
  126. do
  127. cls=$(shuf -n1 $cls_list_file)
  128. $curcurl -d "$(curl_opt_create_$cls)" "${base_uri}$(uri_create $cls)" | tee -a $logfile
  129. done
  130. rm -v $cls_list_file
  131. }
  132. mass_link_edit() {
  133. #mass linking & edition scenarion
  134. #$1 is instance name
  135. #$2 is iteration count
  136. instance_name=$1
  137. iteration_count=$2
  138. base_uri=$(_base_uri $1)
  139. logfile="$logdir/mass_link_edit_${instance_name}.log"
  140. cls_list_file=$(fetch_all_classes $1)
  141. for cls in $(cat $cls_list_file)
  142. do
  143. case $cls in
  144. Person)
  145. person_ids=$(fetch_all_ids $1 Person)
  146. section_ids=$(fetch_all_ids $1 Section)
  147. subsection_ids=$(fetch_all_ids $1 Subsection)
  148. text_ids=$($cmktemp)
  149. cat $section_ids $subsection_ids | shuf > $text_ids
  150. for i in $(seq $iteration_count)
  151. do
  152. cur_id=$(shuf -n1 $person_ids)
  153. alias_count=$(shuf -i1-5 -n1)
  154. ltext_count=$(shuf -i1-5 -n1)
  155. alias_param=$(head -n $(expr $alias_count \* $i) $person_ids| tail -n$alias_count|tr -s "\n" ",")
  156. txt_param=$(head -n $(expr $ltext_count \* $i) $text_ids | tail -n$ltext_count|tr -s "\n" ",")
  157. $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
  158. done
  159. rm -v $text_ids $person_ids $section_ids $subsection_ids
  160. ;;
  161. *)
  162. ;;
  163. esac
  164. done
  165. rm -v $cls_list_file
  166. }
  167. mass_deletion() {
  168. #mass deletion scenario
  169. #$1 is instance name
  170. #$2 number of deletion per classes !
  171. instance_name=$1
  172. iteration_count=$2
  173. base_uri=$(_base_uri $1)
  174. logfile="$logdir/mass_deletion_${instance_name}.log"
  175. cls_list_file=$(fetch_all_classes $1)
  176. for cls in $(cat $cls_list_file)
  177. do
  178. id_list_file=$(fetch_all_ids $1 $cls)
  179. if [ "$iteration_count" -gt "$(wc -l $id_list_file | cut -d " " -f1)" ]
  180. then
  181. max_iter=$(wc -l $id_list_file | cut -d " " -f1)
  182. else
  183. max_iter="$iteration_count"
  184. fi
  185. for i in $(seq $max_iter)
  186. do
  187. id=$(tail -n $i $id_list_file | head -n1)
  188. $curcurl "${base_uri}/admin/delete?classname=$cls&lodel_id=$id" | tee -a $logfile
  189. done
  190. rm -v $id_list_file
  191. done
  192. rm -v $cls_list_file
  193. }
  194. fetch_all_classes() {
  195. #$1 is intance name
  196. cls_list_file=$($cmktemp)
  197. $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
  198. echo $cls_list_file
  199. }
  200. fetch_all_ids() {
  201. # Fetch all ids of a class in an instance and shuffle them
  202. instance_name=$1
  203. classname=$2
  204. idfile=$($cmktemp)
  205. $curl_raw "$(_base_uri $1)/show_class?classname=$2" | sed -nE 's/^.*<li><a href="[^=]+=[^=]+=([0-9]+)".*$/\1/p' |shuf > $idfile
  206. echo $idfile
  207. }
  208. uri_create() {
  209. clsname=$1
  210. echo -n "/admin/create?classname=$1"
  211. }
  212. curl_opt_create_Person() {
  213. #$1 is alias id
  214. #$2 is linked_texts id (comma separated)
  215. 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"
  216. }
  217. curl_opt_create_User() {
  218. 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"
  219. }
  220. curl_opt_create_Collection() {
  221. #$1 is publications id (comma separated)
  222. echo "field_input_title=$(rnd_str_len 20 50)&field_input_publications=$1&classname=Collection"
  223. }
  224. curl_opt_create_Publication() {
  225. #$1 collections id comma separated
  226. echo "field_input_collection=$1&classname=Publication"
  227. }
  228. curl_opt_create_Section() {
  229. #$1 childs id (comma separated)
  230. #$2 linked_persons id (comma separated)
  231. 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"
  232. }
  233. curl_opt_create_Subsection() {
  234. #$1 childs id (comma separated)
  235. #$2 linked_persons id (comma separated)
  236. #$3 parants id (comma separated)
  237. 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"
  238. }
  239. run_bg_with_param() {
  240. #$1 is the function name to run
  241. #$2 is the instance_list filename
  242. #other parameters are given to the function
  243. fun=$1
  244. instance_list=$2
  245. shift;shift
  246. pidlist=$($cmktemp)
  247. for iname in $(cat $instance_list)
  248. do
  249. $fun $iname $@ &
  250. echo $! >> $pidlist
  251. done
  252. for pid in $(cat $pidlist)
  253. do
  254. wait $pid
  255. done
  256. rm -v $pidlist
  257. }
  258. run_bg_with_param "mass_creation" $instance_list $n_create
  259. run_bg_with_param "mass_link_edit" $instance_list $n_edit
  260. run_bg_with_param "mass_deletion" $instance_list $n_delete
  261. echo ""
  262. echo "Logs can be found in $logdir"