SHARE
TWEET

Zuhzu elasticsearch performance test script

tom19960222 Dec 1st, 2015 78 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import requests
  2. import string
  3. import random
  4. import json
  5.  
  6.  
  7. def random_string(N):
  8.     return ''.join(random.SystemRandom().choice(string.ascii_uppercase + string.digits + string.ascii_lowercase) for _ in range(N))
  9.  
  10. def random_sitemdata():
  11.     sitemdata = json.loads('{ \
  12.          "sitem": { \
  13.            "sownershipId": 51, \
  14.            "identifier": "b8e5d055-caf9-4dc9-befe-bbbcde9c5f08", \
  15.            "modifyDate": "2015-07-17 21:32:55.0", \
  16.            "coverImageURL": "/contentAsset/image/b8e5d055-caf9-4dc9-befe-bbbcde9c5f08/coverimage/?", \
  17.            "realURL": "127.0.0.1:8080/assets/9/8/98809a6a-6fe6-41e9-af94-a6d2e8c77d60/coverimage/435cbb836ed92bf603aae03878440775c2584d4344b3e97c184e883b23f35e6e.jpg", \
  18.            "description": "大學學習", \
  19.            "pid": "cb0Nr2", \
  20.            "title": "大學學習", \
  21.            "thumbnailURL": "/contentAsset/image/b8e5d055-caf9-4dc9-befe-bbbcde9c5f08/coverimage/?&filter=Thumbnail&thumbnail_w=120&thumbnail_h=90" \
  22.          }, \
  23.          "shared": 2, \
  24.          "creator": { \
  25.            "birthday": "2002-10-01 00:00:00.0", \
  26.            "gender": 0, \
  27.            "modifyDate": "2015-07-09 19:04:57.0", \
  28.            "roleId": 0, \
  29.            "facebookId": "", \
  30.            "userid": "dotcms.org.2839", \
  31.            "userHeadURL": "/contentAsset/image/5d0d7573-8e51-4719-b46c-4da1fd21d54f/coverimage/?", \
  32.            "twitterId": "", \
  33.            "UserHeadId": "5d0d7573-8e51-4719-b46c-4da1fd21d54f", \
  34.            "skypeId": "", \
  35.            "name": "Hsing-Tai Chung", \
  36.            "nickname": "htc", \
  37.            "id": "5161307c-5483-4686-8d48-51a8c818da18", \
  38.            "email": "htc.tku@gmail.com", \
  39.            "createDate": "2015-03-31 15:07:37.0" \
  40.          }, \
  41.          "selfItemCount": 10, \
  42.          "collectItemCount": 0, \
  43.          "stagList": [ \
  44.            { \
  45.              "seqOrder": 0, \
  46.              "stagName": "大學學習", \
  47.              "categoryName": "教育啟智", \
  48.              "categoryId": 41000, \
  49.              "stagId": 11 \
  50.            } \
  51.          ], \
  52.          "templateId": 0, \
  53.          "collectSitemTimes": 0, \
  54.          "totalItemCount": 10, \
  55.          "sownershipId": 51, \
  56.          "itemTagList": [], \
  57.          "seqOrder": 410000000, \
  58.          "modifyTime": "2015-07-17 21:32:55.0", \
  59.          "createTime": "2015-07-07 18:10:44.0", \
  60.          "category": { \
  61.            "categroyName": "教育啟智", \
  62.            "categoryId": 41000 \
  63.          }, \
  64.          "inbox": 0, \
  65.          "isCollected": "self" \
  66.        }')
  67.  
  68.     sitemdata['sitem']['pid'] = random_string(6)
  69.     sitemdata['sitem']['description'] = random_string(60)
  70.     sitemdata['sitem']['title'] = random_string(30)
  71.  
  72.     return sitemdata
  73.  
  74. def random_itemdata():
  75.     itemdata = json.loads('{ \
  76.          "itemTagList": [], \
  77.          "seqOrder": 50, \
  78.          "shared": 0, \
  79.          "creator": { \
  80.            "birthday": "2002-10-01 00:00:00.0", \
  81.            "gender": 0, \
  82.            "modifyDate": "2015-07-09 19:04:57.0", \
  83.            "roleId": 0, \
  84.            "facebookId": "", \
  85.            "userid": "dotcms.org.2839", \
  86.            "userHeadURL": "/contentAsset/image/5d0d7573-8e51-4719-b46c-4da1fd21d54f/coverimage/?", \
  87.            "twitterId": "", \
  88.            "UserHeadId": "5d0d7573-8e51-4719-b46c-4da1fd21d54f", \
  89.            "skypeId": "", \
  90.            "name": "Hsing-Tai Chung", \
  91.            "nickname": "htc", \
  92.            "id": "5161307c-5483-4686-8d48-51a8c818da18", \
  93.            "email": "htc.tku@gmail.com", \
  94.            "createDate": "2015-03-31 15:07:37.0" \
  95.          }, \
  96.          "modifyTime": "2015-07-14 22:55:58.0", \
  97.          "appreciationCount": 0, \
  98.          "createTime": "2015-07-02 16:40:31.0", \
  99.          "commentsCount": 0, \
  100.          "commentRankAverage": 0, \
  101.          "itemSetId": 2117, \
  102.          "collectItemCount": 0, \
  103.          "content": { \
  104.            "identifier": "cb7c12d6-188a-4573-9078-acdeeea40db8", \
  105.            "struc": "ZuArticle", \
  106.            "coverImageURL": "/contentAsset/image/cb7c12d6-188a-4573-9078-acdeeea40db8/coverimage/?", \
  107.            "realURL": "127.0.0.1:8080/assets/3/5/3532cc83-e87d-4176-94eb-418d85ae99bb/coverimage/adf1e82196e1e040a917c1f7b4884e8f3ce2273e8bf460dff853b3621a12e9f7.png", \
  108.            "description": "", \
  109.            "pid": "OzfXpR", \
  110.            "eventtime": "2015-07-02 16:40:30.0", \
  111.            "title": "[101-2] Dance Bear", \
  112.            "content": "QQ", \
  113.            "thumbnailURL": "/contentAsset/image/cb7c12d6-188a-4573-9078-acdeeea40db8/coverimage/?&filter=Thumbnail&thumbnail_w=120&thumbnail_h=90" \
  114.          } \
  115.        }')
  116.     itemdata['content']['description'] = random_string(120)
  117.     itemdata['content']['pid'] = random_string(6)
  118.     itemdata['content']['title'] = random_string(20)
  119.     itemdata['content']['content'] = random_string(200)
  120.  
  121.     return itemdata
  122.  
  123. def random_userdata():
  124.     userdata = {
  125.         "birthday": "2002-10-01 00:00:00.0",
  126.         "gender": 0,
  127.         "modifyDate": "2015-07-09 19:04:57.0",
  128.         "roleId": 0,
  129.         "facebookId": "",
  130.         "userid": "dotcms.org.2839",
  131.         "userHeadURL": "/contentAsset/image/5d0d7573-8e51-4719-b46c-4da1fd21d54f/coverimage/?",
  132.         "twitterId": "",
  133.         "UserHeadId": "5d0d7573-8e51-4719-b46c-4da1fd21d54f",
  134.         "skypeId": "",
  135.         "name": "Hsing-Tai Chung",
  136.         "nickname": "htc",
  137.         "id": "5161307c-5483-4686-8d48-51a8c818da18",
  138.         "email": "htc.tku@gmail.com",
  139.         "createDate": "2015-03-31 15:07:37.0"
  140.     }
  141.  
  142.     userdata['name'] = random_string(32)
  143.     userdata['nickname'] = 'name_'+random_string(16)
  144.     userdata['email'] = random_string(10) + '@gmail.com'
  145.     return userdata
  146.  
  147. def main():
  148.  
  149.     headers = {'Content-Type': 'Application/json'}
  150.     URLPrefix = 'http://163.13.128.112:9200/performance-test'
  151.  
  152.     test_time = 3
  153.     min_sitem = 1
  154.     max_sitem = 3
  155.     min_item_in_sitem = 1
  156.     max_item_in_sitem = 5
  157.  
  158.     print("Start test: Post random data for %s times." % test_time)
  159.  
  160.     for i in range(test_time):
  161.         sitem_count = random.randint(min_sitem, max_sitem)
  162.         item_count = random.randint(min_item_in_sitem, max_item_in_sitem)
  163.         print("%sth test: " % str(i+1))
  164.         print("Posting %s sitems with %s items inside." % (sitem_count, item_count))
  165.  
  166.         user = random_userdata()
  167.         print("Username: %s" % user['nickname'])
  168.         URL = "%s/users/%s" % (URLPrefix, user['nickname'])
  169.  
  170.         print("Posting to %s" % URL)
  171.         r = requests.post(URL, data=json.dumps(user), headers=headers)
  172.         print(r.text)
  173.         print("")
  174.  
  175.         for j in range(sitem_count):
  176.             sitem = random_sitemdata()
  177.             URL = "%s/sitem/%s?parent=%s" % (URLPrefix, sitem['sitem']['pid'], user['nickname'])
  178.  
  179.             print("Posting to %s" % URL)
  180.             r = requests.post(URL, data=json.dumps(sitem), headers=headers)
  181.             print(r.text)
  182.             print("")
  183.  
  184.             for k in range(item_count):
  185.                 item = random_itemdata()
  186.                 URL = "%s/item/%s?parent=%s&routing=%s" % (URLPrefix, item['content']['pid'], sitem['sitem']['pid'], user['nickname'])
  187.                
  188.                 print("Posting to %s" % URL)
  189.                 r = requests.post(URL, data=json.dumps(item), headers=headers)
  190.                 print(r.text)
  191.                 print("")
  192.         print("")
  193.  
  194.  
  195. if __name__ == '__main__':
  196.     main()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top