2015-11-10 100 views
1

我想創建一些AJAX自動完成功能的基本性能測試。Gatling模擬自動完成

在加特林做這件事的最好方法是什麼?

我有一個包含(許多)搜索詞的csv文件,我正在使用Gatling v2.1.7,我寫了以下內容。然而,我現在卡住了,無法從饋線獲取實際的字符串以生成ChainBuilder,建議/有可能在此時從會話中獲取它,還是有更簡單的方法?

def autoCompleteChain(existingChain: ChainBuilder, searchTerm: String): ChainBuilder = { 
    existingChain 
     .exec(http("autocomplete") 
      .get("http://localhost/autocomp?q=" + searchTerm) 
      .check(substring(searchTerm))) 
    .pause(1) 
} 

def autoCompleteTerm(term: String): ChainBuilder = { 
    // build a chain of auto complete searches 
    term.inits.toList.reverse.drop(1) 
     .foldLeft(exec())(autoCompleteChain(_, _)) 
} 

feed(feeder) 
    // goto page 
    .exec(http("home page").get("http://localhost")) 
    // type query 
    .exec(autoCompleteTerm("${term}")) 
    // search for term etc. 
    .exec(http("search").get("http://localhost/q=${term}")) 

回答

1

您錯過了如何構建Gatling場景:在加載時動作被一勞永逸地鏈接在一起,工作流然後是靜態的。

你試圖建立這種方式是錯誤的:你試圖建立請求序列,這取決於一些僅在運行時纔可用的信息,即稍後。

您必須使用Gatlin循環之一(如asLongAs),並在運行時計算不同的子字符串。

0

感謝斯特凡的澄清,我現在已經實現了以下內容:

feed(feeder) 
    // goto page 
    .exec(http("home page").get("http://localhost")) 
    // type query 
    .exec(
     foreach(session => session("term").as[String].toList, "char") { 
     exec(session => session.set(
      "currentTerm", 
      session("currentTerm").asOption[String].getOrElse("") + session("char").as[String])) 
     .exec(http("auto-complete") 
      .get("http://localhost/autocomp?q=${currentTerm}") 
      .check(substring("${currentTerm}")) 
     ) 
     } 
    ) 
    // search for the full term 
    .exec(http("search").get("http://localhost/q=${term}")) 

請評論,如果有什麼我可以做些什麼來改善這個代碼,尤其是從性能還是可讀性/風格角度。