Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import requests
- import string
- import random
- import json
- def random_string(N):
- return ''.join(random.SystemRandom().choice(string.ascii_uppercase + string.digits + string.ascii_lowercase) for _ in range(N))
- def random_sitemdata():
- sitemdata = json.loads('{ \
- "sitem": { \
- "sownershipId": 51, \
- "identifier": "b8e5d055-caf9-4dc9-befe-bbbcde9c5f08", \
- "modifyDate": "2015-07-17 21:32:55.0", \
- "coverImageURL": "/contentAsset/image/b8e5d055-caf9-4dc9-befe-bbbcde9c5f08/coverimage/?", \
- "realURL": "127.0.0.1:8080/assets/9/8/98809a6a-6fe6-41e9-af94-a6d2e8c77d60/coverimage/435cbb836ed92bf603aae03878440775c2584d4344b3e97c184e883b23f35e6e.jpg", \
- "description": "大學學習", \
- "pid": "cb0Nr2", \
- "title": "大學學習", \
- "thumbnailURL": "/contentAsset/image/b8e5d055-caf9-4dc9-befe-bbbcde9c5f08/coverimage/?&filter=Thumbnail&thumbnail_w=120&thumbnail_h=90" \
- }, \
- "shared": 2, \
- "creator": { \
- "birthday": "2002-10-01 00:00:00.0", \
- "gender": 0, \
- "modifyDate": "2015-07-09 19:04:57.0", \
- "roleId": 0, \
- "facebookId": "", \
- "userid": "dotcms.org.2839", \
- "userHeadURL": "/contentAsset/image/5d0d7573-8e51-4719-b46c-4da1fd21d54f/coverimage/?", \
- "twitterId": "", \
- "UserHeadId": "5d0d7573-8e51-4719-b46c-4da1fd21d54f", \
- "skypeId": "", \
- "name": "Hsing-Tai Chung", \
- "nickname": "htc", \
- "id": "5161307c-5483-4686-8d48-51a8c818da18", \
- "email": "htc.tku@gmail.com", \
- "createDate": "2015-03-31 15:07:37.0" \
- }, \
- "selfItemCount": 10, \
- "collectItemCount": 0, \
- "stagList": [ \
- { \
- "seqOrder": 0, \
- "stagName": "大學學習", \
- "categoryName": "教育啟智", \
- "categoryId": 41000, \
- "stagId": 11 \
- } \
- ], \
- "templateId": 0, \
- "collectSitemTimes": 0, \
- "totalItemCount": 10, \
- "sownershipId": 51, \
- "itemTagList": [], \
- "seqOrder": 410000000, \
- "modifyTime": "2015-07-17 21:32:55.0", \
- "createTime": "2015-07-07 18:10:44.0", \
- "category": { \
- "categroyName": "教育啟智", \
- "categoryId": 41000 \
- }, \
- "inbox": 0, \
- "isCollected": "self" \
- }')
- sitemdata['sitem']['pid'] = random_string(6)
- sitemdata['sitem']['description'] = random_string(60)
- sitemdata['sitem']['title'] = random_string(30)
- return sitemdata
- def random_itemdata():
- itemdata = json.loads('{ \
- "itemTagList": [], \
- "seqOrder": 50, \
- "shared": 0, \
- "creator": { \
- "birthday": "2002-10-01 00:00:00.0", \
- "gender": 0, \
- "modifyDate": "2015-07-09 19:04:57.0", \
- "roleId": 0, \
- "facebookId": "", \
- "userid": "dotcms.org.2839", \
- "userHeadURL": "/contentAsset/image/5d0d7573-8e51-4719-b46c-4da1fd21d54f/coverimage/?", \
- "twitterId": "", \
- "UserHeadId": "5d0d7573-8e51-4719-b46c-4da1fd21d54f", \
- "skypeId": "", \
- "name": "Hsing-Tai Chung", \
- "nickname": "htc", \
- "id": "5161307c-5483-4686-8d48-51a8c818da18", \
- "email": "htc.tku@gmail.com", \
- "createDate": "2015-03-31 15:07:37.0" \
- }, \
- "modifyTime": "2015-07-14 22:55:58.0", \
- "appreciationCount": 0, \
- "createTime": "2015-07-02 16:40:31.0", \
- "commentsCount": 0, \
- "commentRankAverage": 0, \
- "itemSetId": 2117, \
- "collectItemCount": 0, \
- "content": { \
- "identifier": "cb7c12d6-188a-4573-9078-acdeeea40db8", \
- "struc": "ZuArticle", \
- "coverImageURL": "/contentAsset/image/cb7c12d6-188a-4573-9078-acdeeea40db8/coverimage/?", \
- "realURL": "127.0.0.1:8080/assets/3/5/3532cc83-e87d-4176-94eb-418d85ae99bb/coverimage/adf1e82196e1e040a917c1f7b4884e8f3ce2273e8bf460dff853b3621a12e9f7.png", \
- "description": "", \
- "pid": "OzfXpR", \
- "eventtime": "2015-07-02 16:40:30.0", \
- "title": "[101-2] Dance Bear", \
- "content": "QQ", \
- "thumbnailURL": "/contentAsset/image/cb7c12d6-188a-4573-9078-acdeeea40db8/coverimage/?&filter=Thumbnail&thumbnail_w=120&thumbnail_h=90" \
- } \
- }')
- itemdata['content']['description'] = random_string(120)
- itemdata['content']['pid'] = random_string(6)
- itemdata['content']['title'] = random_string(20)
- itemdata['content']['content'] = random_string(200)
- return itemdata
- def random_userdata():
- userdata = {
- "birthday": "2002-10-01 00:00:00.0",
- "gender": 0,
- "modifyDate": "2015-07-09 19:04:57.0",
- "roleId": 0,
- "facebookId": "",
- "userid": "dotcms.org.2839",
- "userHeadURL": "/contentAsset/image/5d0d7573-8e51-4719-b46c-4da1fd21d54f/coverimage/?",
- "twitterId": "",
- "UserHeadId": "5d0d7573-8e51-4719-b46c-4da1fd21d54f",
- "skypeId": "",
- "name": "Hsing-Tai Chung",
- "nickname": "htc",
- "id": "5161307c-5483-4686-8d48-51a8c818da18",
- "email": "htc.tku@gmail.com",
- "createDate": "2015-03-31 15:07:37.0"
- }
- userdata['name'] = random_string(32)
- userdata['nickname'] = 'name_'+random_string(16)
- userdata['email'] = random_string(10) + '@gmail.com'
- return userdata
- def main():
- headers = {'Content-Type': 'Application/json'}
- URLPrefix = 'http://163.13.128.112:9200/performance-test'
- test_time = 3
- min_sitem = 1
- max_sitem = 3
- min_item_in_sitem = 1
- max_item_in_sitem = 5
- print("Start test: Post random data for %s times." % test_time)
- for i in range(test_time):
- sitem_count = random.randint(min_sitem, max_sitem)
- item_count = random.randint(min_item_in_sitem, max_item_in_sitem)
- print("%sth test: " % str(i+1))
- print("Posting %s sitems with %s items inside." % (sitem_count, item_count))
- user = random_userdata()
- print("Username: %s" % user['nickname'])
- URL = "%s/users/%s" % (URLPrefix, user['nickname'])
- print("Posting to %s" % URL)
- r = requests.post(URL, data=json.dumps(user), headers=headers)
- print(r.text)
- print("")
- for j in range(sitem_count):
- sitem = random_sitemdata()
- URL = "%s/sitem/%s?parent=%s" % (URLPrefix, sitem['sitem']['pid'], user['nickname'])
- print("Posting to %s" % URL)
- r = requests.post(URL, data=json.dumps(sitem), headers=headers)
- print(r.text)
- print("")
- for k in range(item_count):
- item = random_itemdata()
- URL = "%s/item/%s?parent=%s&routing=%s" % (URLPrefix, item['content']['pid'], sitem['sitem']['pid'], user['nickname'])
- print("Posting to %s" % URL)
- r = requests.post(URL, data=json.dumps(item), headers=headers)
- print(r.text)
- print("")
- print("")
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement