Illustration Image

Cassandra.Link

The best knowledge base on Apache Cassandra®

Helping platform leaders, architects, engineers, and operators build scalable real time data platforms.

11/18/2019

Reading time:1 min

gatling-cql/GatlingCql

by John Doe

Gatling DSL support for Apache Cassandra CQLFeaturesBasic Gatling DSL for Apache Cassandra CQL, prepared statements are supported as wellclass CassandraSimulation extends Simulation { val keyspace = "test" val table_name = "test_table" val cluster = Cluster.builder().addContactPoint("127.0.0.1").build() val session = cluster.connect(s"$keyspace") //Your C* session val cqlConfig = cql.session(session) //Initialize Gatling DSL with your session //Setup session.execute(s"""CREATE KEYSPACE IF NOT EXISTS $keyspace WITH replication = { 'class' : 'SimpleStrategy', 'replication_factor': '1'}""") session.execute(s"""CREATE TABLE IF NOT EXISTS $table_name ( id timeuuid, num int, str text, PRIMARY KEY (id) ); """) //It's generally not advisable to use secondary indexes in you schema session.execute(f"""CREATE INDEX IF NOT EXISTS $table_name%s_num_idx ON $table_name%s (num)""") //Prepare your statement, we want to be effective, right? val prepared = session.prepare(s"""INSERT INTO $table_name (id, num, str) VALUES (now(), ?, ?)""") val random = new util.Random val feeder = Iterator.continually( // this feader will "feed" random data into our Sessions Map( "randomString" -> random.nextString(20), "randomNum" -> random.nextInt() )) val scn = scenario("Two statements").repeat(1) { //Name your scenario feed(feeder) .exec(cql("simple SELECT") // 'execute' can accept a string // and understands Gatling expression language (EL), i.e. ${randomNum} .execute("SELECT * FROM test_table WHERE num = ${randomNum}")) .exec(cql("prepared INSERT") // alternatively 'execute' accepts a prepared statement .execute(prepared) // you need to provide parameters for that (EL is supported as well) .withParams(Integer.valueOf(random.nextInt()), "${randomString}") // and set a ConsistencyLevel optionally .consistencyLevel(ConsistencyLevel.ANY)) } setUp(scn.inject(rampUsersPerSec(10) to 100 during (30 seconds))) .protocols(cqlConfig) after(cluster.close()) // close session and stop associated threads started by the Java/Scala driver}InstallationGet a release TGZUnpack into Gatling folder: tar -xjf GatlingCql-3.0.0-release.tar.gz -C gatling-charts-highcharts-bundle-3.0.3/Run Gatling and you should see cassandra.CassandraSimulation in your simulations listMore Informationhttp://gatling.io/docs/3.0/quickstarthttp://gatling.io/docs/3.0/cheat-sheet

Illustration Image

Gatling DSL support for Apache Cassandra CQL

Features

Basic Gatling DSL for Apache Cassandra CQL, prepared statements are supported as well

class CassandraSimulation extends Simulation {
  val keyspace = "test"
  val table_name = "test_table"
  val cluster = Cluster.builder().addContactPoint("127.0.0.1").build()
  val session = cluster.connect(s"$keyspace") //Your C* session
  val cqlConfig = cql.session(session) //Initialize Gatling DSL with your session
  //Setup
  session.execute(s"""CREATE KEYSPACE IF NOT EXISTS $keyspace 
                      WITH replication = { 'class' : 'SimpleStrategy', 
                                          'replication_factor': '1'}""")
  session.execute(s"""CREATE TABLE IF NOT EXISTS $table_name (
          id timeuuid,
          num int,
          str text,
          PRIMARY KEY (id)
        );
      """)
  //It's generally not advisable to use secondary indexes in you schema
  session.execute(f"""CREATE INDEX IF NOT EXISTS $table_name%s_num_idx 
                      ON $table_name%s (num)""")
  //Prepare your statement, we want to be effective, right?
  val prepared = session.prepare(s"""INSERT INTO $table_name 
                                      (id, num, str) 
                                      VALUES 
                                      (now(), ?, ?)""")
  val random = new util.Random
  val feeder = Iterator.continually( 
      // this feader will "feed" random data into our Sessions
      Map(
          "randomString" -> random.nextString(20), 
          "randomNum" -> random.nextInt()
          ))
  val scn = scenario("Two statements").repeat(1) { //Name your scenario
    feed(feeder)
    .exec(cql("simple SELECT") 
         // 'execute' can accept a string 
         // and understands Gatling expression language (EL), i.e. ${randomNum}
        .execute("SELECT * FROM test_table WHERE num = ${randomNum}")) 
    .exec(cql("prepared INSERT")
         // alternatively 'execute' accepts a prepared statement
        .execute(prepared)
         // you need to provide parameters for that (EL is supported as well)
        .withParams(Integer.valueOf(random.nextInt()), "${randomString}")
        // and set a ConsistencyLevel optionally
        .consistencyLevel(ConsistencyLevel.ANY)) 
  }
  setUp(scn.inject(rampUsersPerSec(10) to 100 during (30 seconds)))
    .protocols(cqlConfig)
  after(cluster.close()) // close session and stop associated threads started by the Java/Scala driver
}

Installation

  • Get a release TGZ
  • Unpack into Gatling folder: tar -xjf GatlingCql-3.0.0-release.tar.gz -C gatling-charts-highcharts-bundle-3.0.3/
  • Run Gatling and you should see cassandra.CassandraSimulation in your simulations list

More Information

Related Articles

cassandra
langchain
llamaindex

GitHub - michelderu/chat-with-your-data-in-cassandra: Chat with your data stored in DataStax Enterprise, Astra DB and Apache Cassandra - In Natural Language!

John Doe

3/26/2024

Checkout Planet Cassandra

Claim Your Free Planet Cassandra Contributor T-shirt!

Make your contribution and score a FREE Planet Cassandra Contributor T-Shirt! 
We value our incredible Cassandra community, and we want to express our gratitude by sending an exclusive Planet Cassandra Contributor T-Shirt you can wear with pride.

Join Our Newsletter!

Sign up below to receive email updates and see what's going on with our company

Explore Related Topics

AllKafkaSparkScyllaSStableKubernetesApiGithubGraphQl

Explore Further

stress