You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@usergrid.apache.org by sf...@apache.org on 2014/12/10 18:13:55 UTC

[06/41] incubator-usergrid git commit: Added extractor helpers

Added extractor helpers

Updated UsersScenario to use helpers and constants


Project: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/commit/6709158f
Tree: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/tree/6709158f
Diff: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/diff/6709158f

Branch: refs/heads/UG-rest-test-framework-overhaul
Commit: 6709158fc40a011807007717f432d1d324319e26
Parents: 2372701
Author: Todd Nine <tn...@apigee.com>
Authored: Thu Dec 4 15:14:33 2014 -0700
Committer: Todd Nine <tn...@apigee.com>
Committed: Thu Dec 4 15:14:33 2014 -0700

----------------------------------------------------------------------
 .../apache/usergrid/helpers/Extractors.scala    |  72 ++++++++++
 .../usergrid/scenarios/UserScenarios.scala      | 132 ++++++++++++-------
 2 files changed, 153 insertions(+), 51 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/6709158f/stack/loadtests/src/main/scala/org/apache/usergrid/helpers/Extractors.scala
----------------------------------------------------------------------
diff --git a/stack/loadtests/src/main/scala/org/apache/usergrid/helpers/Extractors.scala b/stack/loadtests/src/main/scala/org/apache/usergrid/helpers/Extractors.scala
new file mode 100644
index 0000000..055bf8d
--- /dev/null
+++ b/stack/loadtests/src/main/scala/org/apache/usergrid/helpers/Extractors.scala
@@ -0,0 +1,72 @@
+package org.apache.usergrid.helpers
+
+import io.gatling.core.Predef._
+import io.gatling.core.session._
+import io.gatling.http.Predef._
+
+import scala.util.parsing.json.JSONArray
+
+/**
+ * Helper object that will perform extractions
+ */
+object Extractors {
+
+//  /**
+//   * Will extract the cursor from the response.  If the cursor is not present, an empty string will be set
+//   */
+//  val maybeExtractCursor = jsonPath("$..cursor").transformOption( extract => {
+//    //it may or may not be present.  If it is, save it, otherwise save it as an empty string
+//   extract.orElse(Some(""))
+//  }).saveAs("cursor")
+//
+//  /**
+//   * Will extract entities as a json array into the session. If they do not exist, it will set to an empty list
+//   */
+//  val maybeExtractEntities = jsonPath("..entities").transformOption(extract => {
+//      extract.orElse(Some(JSONArray(List())));
+//    }).saveAs("entities")
+
+  /**
+   * Will extract the cursor from the response.  If the cursor is not present, an empty string will be set
+   */
+  def maybeExtractCursor(saveAsName : String) = {
+    jsonPath("$..cursor").transformOption(extract => {
+      //it may or may not be present.  If it is, save it, otherwise save it as an empty string
+      extract.orElse(Some(""))
+    }).saveAs(saveAsName)
+  }
+
+
+  /**
+   * tries to extract the cursor from the session, if it exists, it returns true. if it's the default, returns false
+   * @param nameInSession The name of the variable in the session
+   * @return
+   */
+  def stringParamExists (nameInSession: String) : Expression[Boolean] = {
+    session => session(nameInSession) != null && session(nameInSession).as[String] != ""
+  }
+
+  /**
+   * Will extract entities as a json array into the session. If they do not exist, it will set to an empty list
+   * @param saveAsName The name to use when saving to the session
+   */
+  def maybeExtractEntities(saveAsName: String) = {
+    jsonPath("$..entities").ofType[Seq[Any]].transformOption(extract => {
+      extract.orElse(Some(Seq()));
+    }).saveAs(saveAsName)
+  }
+
+  /**
+   * Returns true if sequence is not null and has elements.  Expects a seq object
+   * @param nameInSession  The name ot use when saving to the session
+   * @return
+   */
+  def sequenceHasElements(nameInSession: String) : Expression[Boolean] = {
+    session => session(nameInSession) != null && session(nameInSession).as[Seq[Any]].length > 0
+  }
+
+
+
+
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/6709158f/stack/loadtests/src/main/scala/org/apache/usergrid/scenarios/UserScenarios.scala
----------------------------------------------------------------------
diff --git a/stack/loadtests/src/main/scala/org/apache/usergrid/scenarios/UserScenarios.scala b/stack/loadtests/src/main/scala/org/apache/usergrid/scenarios/UserScenarios.scala
index 2bbe9a3..13fb96b 100755
--- a/stack/loadtests/src/main/scala/org/apache/usergrid/scenarios/UserScenarios.scala
+++ b/stack/loadtests/src/main/scala/org/apache/usergrid/scenarios/UserScenarios.scala
@@ -14,28 +14,49 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
- package org.apache.usergrid.scenarios
+
+package org.apache.usergrid.scenarios
 
 import io.gatling.core.Predef._
- import io.gatling.http.Predef._
- import io.gatling.http.request.StringBody
- import org.apache.usergrid.datagenerators.FeederGenerator
- import org.apache.usergrid.settings.{Headers, Settings, Utils}
+import io.gatling.http.Predef._
+import io.gatling.http.request.StringBody
+import org.apache.usergrid.datagenerators.FeederGenerator
+import org.apache.usergrid.settings.{Headers, Settings, Utils}
+import org.apache.usergrid.helpers.Extractors._
+
+import scala.util.parsing.json.JSONArray
+
+object UserScenarios {
+
+   /**
+    * Naming constants used in the scenarios for saving values into the sessions
+    */
+
+   //The value for the cursor
+   val SessionVarCursor: String = "cursor"
 
- object UserScenarios {
+   //the value for the json array of users
+   val SessionVarUsers: String = "users"
+
+  //the value for the users uuid
+   val SessionVarUserId: String = "userId"
+
+  //the value for HTTP response code after requests
+   val SessionVarUserStatus: String = "userStatus"
+
+   val getRandomUser = exec(
+     http("GET user")
+       .get("/users/user" + Utils.generateRandomInt(1, Settings.numEntities))
+       .headers(Headers.jsonAuthorized)
+       .check(status.is(200))
+   )
 
-  val getRandomUser = exec(
-    http("GET user")
-      .get("/users/user" + Utils.generateRandomInt(1, Settings.numEntities))
-      .headers(Headers.jsonAuthorized)
-      .check(status.is(200))
-  )
 
    val getUserByUsername = exec(
      http("GET user")
        .get("/users/${username}")
        .headers(Headers.jsonAuthorized)
-       .check(status.saveAs("userStatus"), jsonPath("$..entities[0]").exists, jsonPath("$..entities[0].uuid").exists, jsonPath("$..entities[0].uuid").saveAs("userId"))
+       .check(status.saveAs(SessionVarUserStatus), jsonPath("$..entities[0]").exists, jsonPath("$..entities[0].uuid").exists, jsonPath("$..entities[0].uuid").saveAs(SessionVarUserId))
    )
 
 
@@ -45,31 +66,30 @@ import io.gatling.core.Predef._
    val postUser =
 
      exec(
-            http("POST geolocated Users")
-              .post("/users")
-              .body(new StringBody("""{"location":{"latitude":"${latitude}","longitude":"${longitude}"},"username":"${username}",
+       http("POST geolocated Users")
+         .post("/users")
+         .body(new StringBody( """{"location":{"latitude":"${latitude}","longitude":"${longitude}"},"username":"${username}",
            "displayName":"${displayName}","age":"${age}","seen":"${seen}","weight":"${weight}",
            "height":"${height}","aboutMe":"${aboutMe}","profileId":"${profileId}","headline":"${headline}",
            "showAge":"${showAge}","relationshipStatus":"${relationshipStatus}","ethnicity":"${ethnicity}","password":"password"}"""))
-              .check(status.saveAs("userStatus"))
-              .check(status.is(200),jsonPath("$..entities[0].uuid").saveAs("userId"))
-          )
-
+         .check(status.saveAs(SessionVarUserStatus))
+         .check(status.is(200), jsonPath("$..entities[0].uuid").saveAs(SessionVarUserId))
+     )
 
 
    /**
-     * Try to get a user, if it returns a 404, create the user
-     */
+    * Try to get a user, if it returns a 404, create the user
+    */
    val postUserIfNotExists =
      exec(getUserByUsername)
-       .doIf ("${userStatus}", "404") {
-      exec(postUser)
+       .doIf("${userStatus}", "404") {
+       exec(postUser)
      }
 
 
    val putUser =
      exec(getUserByUsername)
-     .doIf("${userStatus}", "200") {
+       .doIf("${userStatus}", "200") {
        exec(
          http("POST geolocated Users")
            .put("/users")
@@ -77,10 +97,10 @@ import io.gatling.core.Predef._
         "displayName":"${displayName}","age":"${age}","seen":"${seen}","weight":"${weight}",
         "height":"${height}","aboutMe":"${aboutMe}","profileId":"${profileId}","headline":"${headline}",
         "showAge":"${showAge}","relationshipStatus":"${relationshipStatus}","ethnicity":"${ethnicity}","password":"password"}"""))
-           .check(status.is(200), jsonPath("$..entities[0].uuid").saveAs("userId"))
+           .check(status.is(200), jsonPath("$..entities[0].uuid").saveAs(SessionVarUserId))
 
        )
-      }
+     }
 
    /**
     * Get a collection of users without a cursor.  Sets the cursor and entities array as "users"
@@ -89,59 +109,69 @@ import io.gatling.core.Predef._
      http("GET user")
        .get("/users")
        .headers(Headers.jsonAuthorized)
-       .check(status.is(200),  jsonPath("$..entities").saveAs("users"), jsonPath("$..cursor").saveAs("cursor"))
+       .check(status.is(200), maybeExtractEntities(SessionVarUsers), maybeExtractCursor(SessionVarCursor))
    )
 
    /**
     * Get the next page of users with the cursor, expects the value "cursor" to be present in teh session
     */
+   //maybe doif for detecting empty session?
    val getUsersWithCursor = exec(
-        http("GET user")
-          .get("/users?cursor=${cursor}")
-          .headers(Headers.jsonAuthorized)
-          .check(status.is(200),  jsonPath("$..entities").saveAs("users"), jsonPath("$..cursor").saveAs("cursor"))
-      )
+     http("GET user")
+       .get("/users?cursor=${" + SessionVarCursor + "}")
+       .headers(Headers.jsonAuthorized)
+       .check(status.is(200), maybeExtractEntities(SessionVarUsers), maybeExtractCursor(SessionVarCursor))
+   ) /**
+     * Debugging block
+
+          .exec(session => {
+
+          val cursor = session.get(SessionVarCursor)
+          val users = session.get(SessionVarUsers)
+
+          session
+        })    */
 
 
    val deleteUserByUsername = exec(
      http("DELETE user")
        .delete("/users/${username}")
        .headers(Headers.jsonAuthorized)
-       .check(status.is(200), jsonPath("$..entities[0].uuid").saveAs("userId"))
+       .check(status.is(200), jsonPath("$..entities[0].uuid").saveAs(SessionVarUserId))
    )
 
    /**
     * Logs in as the admin user.  Checks if a user exists, if not, creates the user
     * Logs in as the user, then creates 2 devices if they do not exist
     */
-   val createUsersWithDevicesScenario =  scenario("Create Users")
+   val createUsersWithDevicesScenario = scenario("Create Users")
      .feed(Settings.getInfiniteUserFeeder())
      .exec(TokenScenarios.getManagementToken)
      .exec(UserScenarios.postUserIfNotExists)
      .exec(TokenScenarios.getUserToken)
      .exec(UserScenarios.getUserByUsername)
-     .repeat(2){
-       feed(FeederGenerator.generateEntityNameFeeder("device", Settings.numDevices))
-         .exec( DeviceScenarios.maybeCreateDevices)
-     }
+     .repeat(2) {
+     feed(FeederGenerator.generateEntityNameFeeder("device", Settings.numDevices))
+       .exec(DeviceScenarios.maybeCreateDevices)
+   }
 
    /**
     * Posts a new user every time
     */
-   val postUsersInfinitely =  scenario("Post Users")
-        .feed(Settings.getInfiniteUserFeeder())
-        .exec(UserScenarios.postUser)
+   val postUsersInfinitely = scenario("Post Users")
+     .feed(Settings.getInfiniteUserFeeder())
+     .exec(UserScenarios.postUser)
 
    /**
     * Get the users a page at a time until exhausted
     */
-   val getUsersToEnd = scenario("Get Users").exec(
-   //get the management token
-     TokenScenarios.getManagementToken,
-   //get users without a cursor
-     getUsersWithoutCursor
-   //as long as we have a cursor, keep getting results
-   ).asLongAs(session => session("cursor").as[String] != "") {
-     exec(getUsersWithCursor)
-   }
+   val getUsersToEnd = scenario("Get Users")
+     //get the management token
+     .exec(TokenScenarios.getManagementToken)
+     //get users without a cursor
+     .exec(getUsersWithoutCursor)
+     //as long as we have a cursor, keep getting results
+     .asLongAs(stringParamExists(SessionVarCursor)) {
+        exec(getUsersWithCursor)
+      }
  }