Giter VIP home page Giter VIP logo

xk6-pubsub's Introduction

xk6-pubsub

This is a k6 extension using the xk6 system.

❗ This is a proof of concept, isn't supported by the k6 team, and may break in the future. USE AT YOUR OWN RISK!

Build

To build a k6 binary with this extension, first ensure you have the prerequisites:

  1. Install xk6 framework for extending k6:
go install github.com/k6io/xk6/cmd/xk6@latest
  1. Build the binary:
#Required by Pub/Sub client
export CGO_ENABLED=1
xk6 build --with github.com/gera-cl/xk6-pubsub@latest

xk6 build --with github.com/k6io/xk6-redis="/Users/avpretty/pr/xk6-pubsub"

  1. Setup Google Pub/Sub configuration via environment variables:
export PUBSUB_PROJECT_ID=<project_id>
export PUBSUB_CREDENTIALS=<credentials>

Or use PubSub emulator for local development PUBSUB_EMULATOR_HOST environment variable must be present.

export PUBSUB_EMULATOR_HOST=<emulator_host>

k6 scripting

Required imports

import { check } from 'k6';
import pubsub from 'k6/x/pubsub';

Create a new publisher client

export default function () {
     /**
     * default values
     * 
     * publishTimeout: 5
     * debug: false
     * trace: false
     * doNotCreateTopicIfMissing: false
     */

     const client = pubsub.publisher({
          projectID: __ENV.PUBSUB_PROJECT_ID || "",
          credentials: __ENV.PUBSUB_CREDENTIALS || "",
          publishTimeout: 5,
          debug: true,
          trace: true,
          doNotCreateTopicIfMissing: false
     });

     ...
}

Publish a simple message (only data) and check

let error = pubsub.publish(client, 'topic_name', 'message_data');

check(error, {
     "is sent": err => err === null
});

Alternatively, you can also publish a message including data and attributes using the method publishWithAttributes

let myAttributes = {
     foo: 'bar'
}

let error = pubsub.publishWithAttributes(client, 'topic_name', 'message_data', myAttributes);

Close the client

client.close()

Execution

./k6 run example.js

Result output

          /\      |‾‾| /‾‾/   /‾‾/   
     /\  /  \     |  |/  /   /  /    
    /  \/    \    |     (   /   ‾‾\  
   /          \   |  |\  \ |  (‾)  | 
  / __________ \  |__| \__\ \_____/ .io

  execution: local
     script: example.js
     output: -

  scenarios: (100.00%) 1 scenario, 1 max VUs, 10m30s max duration (incl. graceful stop):
           * default: 1 iterations for each of 1 VUs (maxDuration: 10m0s, gracefulStop: 30s)

[watermill] 2021/03/17 22:17:04.450779 publisher.go:116: 	level=TRACE msg="Sending message to Google PubSub" message_uuid=Y3mriTgx4SuWo2ZxMgg8FF topic=topic_name 
[watermill] 2021/03/17 22:17:04.464942 publisher.go:131: 	level=TRACE msg="Message published to Google PubSub" message_uuid=Y3mriTgx4SuWo2ZxMgg8FF topic=topic_name 
[watermill] 2021/03/17 22:17:04.465082 publisher.go:139: 	level=INFO  msg="Closing Google PubSub publisher" 
[watermill] 2021/03/17 22:17:04.465128 publisher.go:153: 	level=INFO  msg="Google PubSub publisher closed" 

running (00m00.0s), 0/1 VUs, 1 complete and 0 interrupted iterations
default ✓ [======================================] 1 VUs  00m00.0s/10m0s  1/1 iters, 1 per VU

     ✓ is sent

     checks...............: 100.00% ✓ 1 ✗ 0
     data_received........: 0 B     0 B/s
     data_sent............: 0 B     0 B/s
     iteration_duration...: avg=27.14ms min=27.14ms med=27.14ms max=27.14ms p(90)=27.14ms p(95)=27.14ms
     iterations...........: 1       35.356928/s

xk6-pubsub's People

Contributors

olvod avatar mstoykov avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.