1
1
import time
2
2
import json
3
+ import uuid
3
4
import hashlib
4
5
5
- from locust import HttpUser , task , between
6
+ import random
7
+ import string
6
8
7
- # FastHttpUser is 5-6X faster than HttpUser but does not support headers persist in on_start
8
- # https://github.com/locustio/locust/issues/2035
9
- # from locust.contrib.fasthttp import FastHttpUser
9
+ from locust import HttpUser , task , constant
10
+ from locust .contrib .fasthttp import FastHttpUser
10
11
11
12
KEYWORD = "tmc"
13
+ SURVEY_HASH = "2d2e36786c5f1dee206620413313dd8031d0ef8f"
14
+ SURVEY_ITEM_HASH = "5f3e2a52627e5902ed5987177ff546d47e645066"
15
+ TOKEN = "660383a9cd886a70c0bed7a37cc1e4a66cd8a48a"
12
16
13
- class QuickstartUser (HttpUser ):
14
- wait_time = between ( 1 , 5 )
17
+ class QuickstartUser (FastHttpUser ):
18
+ wait_time = constant ( 10000 )
15
19
16
20
def shaHash (self , string ):
17
21
m = hashlib .sha1 ()
18
22
m .update (string .encode ('utf-8' ))
19
23
return str (m .hexdigest ())
20
24
21
- @task
22
25
def on_start (self ):
23
- token = ""
24
- payload = {"data" :{"type" :"apiSession" ,"attributes" :{"app_id" :"wisembly_app" ,"hash" :"67608b0127f18b15cc8bee4c4f9caeb9cbde5b0c" },"relationships" :{}}}
25
- headers = {'content-type' : 'application/vnd.api+json' }
26
-
27
- clientParams = {
28
- "catch_response" : True ,
29
- "verify" : False ,
30
- "data" : json .dumps (payload ),
31
- "headers" : headers
32
- }
33
-
34
- with self .client .post ("/api/6/authentication" , ** clientParams ) as response :
35
- if response .status_code == 200 :
36
- json_response = json .loads (response .text )
37
- token = response .json ()['data' ]['attributes' ]['token' ]
38
26
39
- self .client .headers = {
40
- 'content-type' : 'application/vnd.api+json' ,
41
- 'Wisembly-Token' : token
42
- }
43
-
44
- response .success ()
45
- else :
46
- response .failure ("Could not get credentials" )
47
-
48
- @task (100 )
49
- def get_event (self ):
50
- self .client .get ("/api/6/event/%s" % KEYWORD )
51
- self .client .post ("/api/6/event/%s/watchers" % KEYWORD )
52
-
53
- clientParams = {
54
- "data" : json .dumps ({"event" : {"keyword" : KEYWORD }}),
55
- "headers" : {'content-type' : 'application/json' }
56
- }
57
-
58
- self .client .post ("/api/4/users/node/credentials" , ** clientParams )
59
-
60
- self .client .get ("/api/6/event/%s/medias?limit=25&offset=0&active=true" % KEYWORD )
61
- self .client .get ("/api/6/event/%s/surveys?limit=50" % KEYWORD )
62
- self .client .get ("/api/6/event/%s/quotes?sort=recent&limit=10&unmoderated=false" % KEYWORD )
63
-
64
- @task (5 )
65
- def post_quote (self ):
66
- hash = self .shaHash (str (time .time ()))
67
- payload = {"data" :{"id" : hash ,"type" :"quote" ,"attributes" :{"quote" :"Hello %s" % hash ,"username" :"Guillaume Potier" ,"via" :"tablet" },"relationships" :{}}}
68
- clientParams = {
69
- "catch_response" : True ,
70
- "verify" : False ,
71
- "data" : json .dumps (payload ),
72
- }
73
-
74
- with self .client .post ("/api/6/event/%s/quotes" % KEYWORD , ** clientParams ) as response :
75
- if response .status_code == 201 :
76
- response .success ()
77
- else :
78
- response .failure ("Could not post quote" )
27
+ message = '' .join (random .choices (string .ascii_lowercase , k = 10 ))
28
+ payload = {"data" :{"type" :"surveyAnswer" ,"attributes" :{"username" :"foo" ,"via" :"web" ,"items" :[{"hash" :SURVEY_ITEM_HASH ,"value" :message }]},"relationships" :{"survey" :{"data" :{"id" :SURVEY_HASH ,"type" :"survey" }}}}}
29
+ headers = {'content-type' : 'application/vnd.api+json' }
79
30
80
- @task (20 )
81
- def post_mood (self ):
82
- clientParams = {
83
- "catch_response" : True ,
84
- "verify" : False ,
31
+ newHeaders = {
32
+ 'content-type' : 'application/vnd.api+json' ,
33
+ 'Wisembly-Token' : TOKEN
85
34
}
86
35
87
- with self .client .get ("/api/4/event/%s/mood?mood=tada" % KEYWORD , ** clientParams ) as response :
88
- if response .status_code == 201 :
89
- response .success ()
90
- else :
91
- response .failure ("Could not post quote" )
92
-
36
+ self .client .post (f'/api/6/event/{ KEYWORD } /surveys/{ SURVEY_HASH } /answers' , data = json .dumps (payload ), headers = newHeaders )
93
37
38
+ @task
39
+ def do_nothing (self ):
40
+ pass
0 commit comments