Figure 1. Sequence diagram แสดงการเรียกข้อมูล Favourite Books

JMeter – Process JSON response with BeanShell Processor

jmeter-logo      บ่อยครั้งที่ request message นั้นต้องการเอาค่าจาก response ของอีก service หนึ่ง. ยกตัวอย่างเช่น เว็บไซต์ห้องสมุดต้องการแสดงรายการ favourite books, จะต้องมีการเรียกข้อมูล id หนังสือจาก FavouriteBookService ก่อนแล้วจึงเรียกข้อมูลหนังสือจาก BookInfoService  ดัง sequence diagram ใน Figure 1.

Figure 1. Sequence diagram แสดงการเรียกข้อมูล Favourite Books
Figure 1. Sequence diagram แสดงการเรียกข้อมูล Favourite Books

JMeter ได้เตรียมเครื่องมือเอาไว้สำหรับ extract data จาก response หลายแบบเช่น Regular Expression, XPath, JSON. แต่ทว่าในกรณีที่ response นั้นมีโครงสร้างซับซ้อน, เราอาจจะต้องเขียนโปรแกรมเพื่อประมวลผล response ด้วยตัวเอง.
ในบทความนี้จะแสดงตัวอย่างการประมวลผล response ที่เป็น JSON โดยใช้ BeanShell Processor.

เตรียม Library เพื่อประมวลผล JSON

  1. เริ่มต้นด้วยการไปโหลด library สำหรับจัดการ JSON มาก่อนนะฮะ. โดยส่วนตัวผมชอบใช้ json-simple, สามารถ download JAR ได้จาก https://code.google.com/p/json-simple/ มองหาคำว่า Downloads จากเมนูซ้ายมือ ให้เลือกโหลดไฟล์ .jar นะฮะ. ยกตัวอย่างเช่น json-simple-1.1.1.jar.
  2. จากนั้นให้ copy json-simple-1.1.1.jar ไปวางใน folder lib ที่อยู่ภายใน folder ของ JMeter. เช่น \apache-jmeter-2.12\lib ดัง Figure 2.

    Figure 1. การ copy json library ไปวางใน JMeter
    Figure 2. การ copy json library ไปวางใน JMeter

เป็นอันเสร็จสิ้นการเตรียมการ ต่อไปให้เปิด JMeter ขึ้นมาเลยฮะ.

การประมวลผล JSON

ในตัวอย่างนี้ บัวบานจะประมวลผล JSON ที่ได้จาก auto-suggest service ของ google ให้ชม. โดยจะใช้ keyword ว่า “json parser example” ดัง Figure 3.

Figure 2. ตัวอย่าง auto-suggest ของ google
Figure 3. ตัวอย่าง auto-suggest ของ google
  1. สร้าง HTTP Sampler
    – ใส่ server เป็น www.google.com.sg
    – ใส่ path เป็น

    /s?biw=1600&bih=809&sclient=psy-ab&q=json%20parser%20example&oq=&gs_l=&pbx=1&bav=on.2,or.r_qf.&bvm=bv.85970519,d.c2E&fp=a91f5977af3367b7&es_sm=0&pf=p&sugexp=msedr&gs_rn=61&gs_ri=psy-ab&pq=json%20parser%20example&cp=20&gs_id=1e&xhr=t&es_nrs=true&tch=1&ech=2&psi=XHTlVOPiIonmuQSD7oLIBg.1424323680476.3
  2. คลิกขวาที่ HTTP Sampler ที่เพิ่งสร้างขึ้นมา แล้วเลือก Add > Post Processors > BeanShell PostProcessor
  3. Copy เอา script ข้างล่างไปใส่ใน BeanShell PostProcessor (บรรทัดที่ Highlight เอาไว้นั่นคือส่วนที่เรียกใช้ JSON Simple เพื่อ parse JSON string ที่เราเตรียมไว้).  ดู Figure 4 ประกอบ.
    import org.json.simple.JSONObject;
    import org.json.simple.JSONArray;
    import org.json.simple.parser.JSONParser;
    import org.json.simple.parser.ParseException;
    
    try {
        /////// Start prepare JSON string //////
        // get response from last response message
        String response = prev.getResponseDataAsString();
        
        // Google returns a lot of JSON object in one response, each object is separated by /*""*/ 
        // So we split response by /*""*/ 
        response = response.substring(0,response.length()-6);
        String[] res = response.split("/\\*\\\"\\\"\\*/");
        
        // Walk through array of JSON object
        for(int i=0; i < res.length; i++)
        {
            // Some nodes in JSON contains "[" and "]" which are invalid so we replace them
            // Replace \" with "
            // Replace "[ with [  
            // Replace ]" with ] 
            String tmpRes = res[i];
            tmpRes = tmpRes.replace("\\\"","\"");
            tmpRes = tmpRes.replace("\"[","[");
            tmpRes = tmpRes.replace("]\"","]");
            
            // Finally, JSON string is ready for us. Let's parse it
            log.info(tmpRes);
            JSONParser parser = new JSONParser(); 
            Object obj = parser.parse(tmpRes);
            JSONObject jsonObj = (JSONObject) obj; 
            // Auto-suggest results are in JSON node "d"
            JSONArray dArr = (JSONArray) jsonObj.get("d");
            JSONArray sugestArr = (JSONArray) dArr.get(1);
            
            // Print each result to Log
            for(int j=0; j < sugestArr.size();j++)
            {
                log.info(""+sugestArr.get(j)); 
            }
        }
    }
    catch(ParseException e) {
        e.printStackTrace();
    }
    

     

    Figure 3. การสร้าง BeanShell PostProcessor
    Figure 4. การสร้าง BeanShell PostProcessor

     

  4. จากนั้นให้เปิด Log Viewer ขึ้นมาโดยไปกดที่เมนู Options > Log Viewer. พอกดแล้วจะมีกรอบยื่นออกมาทางด้านล่าง.
  5. กด Start Test แล้วรอดูผลจาก Log Viewer. ถ้าไม่มีอะไรผิดพลาด น่าจะได้ผลลัพธ์ดัง Figure 5 นะฮะ. จะเห็นว่าสีแดงๆที่วงไว้ ตรงกับผลที่ได้ดัง Figure 3 เลย หุหุ.

    ผลลัพธ์จากการ parse response จาก google
    Figure 5. ผลลัพธ์จากการ parse response จาก google

 

 

ท่านผู้ชมสามารถ download ตัวอย่างได้จาก: https://drive.google.com/file/d/0B69Rt-ghTQqyMkFISDlLNHpuOE0/view?usp=sharing

Leave a Reply

Your email address will not be published. Required fields are marked *