gatling、scala、svn 测试案例

package svnperformance

import akka.actor.Props
import akka.actor.ActorRef
import io.gatling.core.scenario.configuration.Simulation
import io.gatling.core.action.builder.ActionBuilder
import io.gatling.core.structure.ChainBuilder
import io.gatling.core.config.ProtocolConfigurationRegistry
import io.gatling.core.Predef._
import io.gatling.core.action.Action
import io.gatling.core.action.system
import io.gatling.core.result.writer.DataWriter
import io.gatling.core.result.message.RequestMessage
import io.gatling.core.result.message.{ KO, OK }
import org.tmatesoft.svn.core.SVNURL
import org.tmatesoft.svn.core.internal.io.dav.DAVRepositoryFactory
import org.tmatesoft.svn.core.wc.SVNWCUtil
import org.tmatesoft.svn.core.io.SVNRepositoryFactory
import org.tmatesoft.svn.core.wc.SVNClientManager
import org.tmatesoft.svn.core.wc.SVNRevision
import org.tmatesoft.svn.core.ISVNLogEntryHandler
import java.io.File
import java.util.Date
import scala.util.Random

class SVNTest extends Simulation {
  val checkout = new ActionBuilder {
    override def build(next: ActorRef, protocolConfigurationRegistry: ProtocolConfigurationRegistry) = system.actorOf(Props(new CheckoutAction(next)))
  }
  val checkoutList = List(checkout)
  val checktry = new ChainBuilder(checkoutList)

  val importfiles = new ActionBuilder {
    override def build(next: ActorRef, protocolConfigurationRegistry: ProtocolConfigurationRegistry) = system.actorOf(Props(new ImportAction(next)))
  }
  val importfilesList = List(importfiles)
  val importtry = new ChainBuilder(importfilesList)
 
  val logcheck = new ActionBuilder {
    override def build(next: ActorRef, protocolConfigurationRegistry: ProtocolConfigurationRegistry) = system.actorOf(Props(new LogAction(next)))
  }
  val logcheckList = List(logcheck)
  val logtry = new ChainBuilder(logcheckList)
 
  //val scn_checkout = scenario("checkout").during(60,"",false){checktry} // error!
  //val scn_checkout = scenario("checkout").tryMax(10) { checktry } // cant stop!
  //val scn_checkout = scenario("checkout").exitBlockOnFail(checktry) // cant stop!
  //val scn_checkout = scenario("checkout").repeat(5){checktry}.exitHereIfFailed
  val scn_checkout = scenario("checkout").repeat(10) { checktry } //repeat 5 times
  val scn_import = scenario("importfiles").repeat(10) { importtry } //repeat 5 times
  val scn_log = scenario("logcheck").repeat(10) { logtry } //repeat 5 times

  setUp(scn_checkout.inject(ramp(20 users).over(10)),
    scn_import.inject(ramp(20 users).over(10)),
    scn_log.inject(ramp(10 users).over(10))
  )
}

class CheckoutAction(val next: ActorRef) extends Action() {
  /*val svnhost = "http://svn-dr.paic.com.cn/svn/svntest/trunk/smallDir2/"
  val user = "svntest002"
  val pass = "Paic1234"*/
  val svnhost = "https://192.168.1.199/svn/projects/trunk/Data2/"
  val user = "orj"
  val pass = "123"

  override def execute(session: Session) {
    try {
      var start = new Date().getTime

      val aa = new Random().nextInt(10) + 1
      val bb = new Random().nextInt(10) + 1
      val url0 = svnhost + "tmp" + aa.toString + "/tmp" + bb.toString //
      println(url0)
      val url = SVNURL.parseURIEncoded(url0) //svnurl

      val cc = new Random().nextInt(100000) + 1
      val desDir0 = "D:/tmp/tmp00/tmp" + start + cc.toString //
      println(desDir0)
      val desDir = new File(desDir0) //local path

      DAVRepositoryFactory.setup
      val options = SVNWCUtil.createDefaultOptions(true)
      val authManager = SVNWCUtil.createDefaultAuthenticationManager(user, pass)
      val repos = SVNRepositoryFactory.create(url)
      repos.setAuthenticationManager(authManager)
      val svnclient = SVNClientManager.newInstance
      val updateclient = svnclient.getUpdateClient
      updateclient.setIgnoreExternals(false)
      val revision = repos.getLatestRevision
      println(revision);

      println("checkout")
      val checkout = updateclient.doCheckout(url, desDir, SVNRevision.UNDEFINED, SVNRevision.HEAD, true)
      println(checkout.toString());
      var end = new Date().getTime
      DataWriter.tell(RequestMessage(session.scenarioName, session.userId, Nil, "checkout", start, end, end, end, OK, None, Nil))
    } catch {
      case e: Throwable => {
        var start = new Date().getTime
        e.printStackTrace
        var end = new Date().getTime
        DataWriter.tell(RequestMessage(session.scenarioName, session.userId, Nil, "checkout", start, end, end, end, KO, None, Nil))
      }
    }
    next ! session
  }
}

class ImportAction(val next: ActorRef) extends Action() {
  /*val svnhost = "http://svn-dr.paic.com.cn/svn/svntest/trunk/commit/tmp0/"
  val user = "svntest002"
  val pass = "Paic1234"*/
  val svnhost = "https://192.168.1.199/svn/projects/trunk/Data3/"
  val user = "orj"
  val pass = "123"

  override def execute(session: Session) {
    try {
      var start = new Date().getTime
      val aa = new Random().nextInt(100000) + 1
      val svnurl0 = svnhost + "tmp" + start + aa.toString //
      println(svnurl0)
      val svnurl = SVNURL.parseURIEncoded(svnurl0) //svnurl

      val bb = new Random().nextInt(10) + 1
      val localDir0 = "D:/tmp/Data/tmp" + bb.toString //
      println(localDir0)
      val localDir = new File(localDir0)

      DAVRepositoryFactory.setup
      val options = SVNWCUtil.createDefaultOptions(true)
      val authManager = SVNWCUtil.createDefaultAuthenticationManager(user, pass)
      val repos = SVNRepositoryFactory.create(svnurl)
      repos.setAuthenticationManager(authManager)
      val svnClient = SVNClientManager.newInstance
      val importClient = svnClient.getCommitClient()
      importClient.setIgnoreExternals(false)
      val revision = repos.getLatestRevision
      println(revision);

      println("importFiles")
      val importFiles = importClient.doImport(localDir, svnurl, "", true)
      println(importFiles.toString());
      var end = new Date().getTime
      DataWriter.tell(RequestMessage(session.scenarioName, session.userId, Nil, "importFiles", start, end, end, end, OK, None, Nil))
    } catch {
      case e: Throwable => {
        var start = new Date().getTime
        e.printStackTrace
        var end = new Date().getTime
        DataWriter.tell(RequestMessage(session.scenarioName, session.userId, Nil, "importFiles", start, end, end, end, KO, None, Nil))
      }
    }
    next ! session
  }
}

class LogAction(val next: ActorRef) extends Action() {
  /*val svnhost = "http://svn-dr.paic.com.cn/svn/svntest/trunk/commit/tmp0/"
  val user = "svntest002"
  val pass = "Paic1234"*/
  val svnhost = "https://192.168.1.199/svn/projects/trunk/Data3/"
  val user = "orj"
  val pass = "123"

  override def execute(session: Session) {
    try {
      var start = new Date().getTime

      val svnurl = SVNURL.parseURIEncoded(svnhost) //svnurl

      val bb = new Random().nextInt(10) + 1
      val localDir0 = "D:/tmp/"
      println(localDir0)
      val localDir = new File(localDir0)

      DAVRepositoryFactory.setup
      val options = SVNWCUtil.createDefaultOptions(true)
      val authManager = SVNWCUtil.createDefaultAuthenticationManager(user, pass)
      val repos = SVNRepositoryFactory.create(svnurl)
      repos.setAuthenticationManager(authManager)
      val svnClient = SVNClientManager.newInstance
      val logClient = svnClient.getLogClient()
      logClient.setIgnoreExternals(false)
      val revision = repos.getLatestRevision
      println(revision);
      val r = SVNRevision.create(revision)

      println("importFiles")
      val logCheck = logClient.doLog(svnurl, new Array[String](0), r, r, r, false, false, true, 1, new Array[String](0), null)
      println(logCheck.toString());
      var end = new Date().getTime
      DataWriter.tell(RequestMessage(session.scenarioName, session.userId, Nil, "logCheck", start, end, end, end, OK, None, Nil))
    } catch {
      case e: Throwable => {
        var start = new Date().getTime
        e.printStackTrace
        var end = new Date().getTime
        DataWriter.tell(RequestMessage(session.scenarioName, session.userId, Nil, "logCheck", start, end, end, end, KO, None, Nil))
      }
    }
    next ! session
  }
}

你可能感兴趣的:(自动化测试)