pitaya

package module
v0.0.0-...-5230ff3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 26, 2024 License: MIT Imports: 15 Imported by: 0

README

xk6-pitaya

xk6-pitaya is a k6 extension that provides a Pitaya client implementation.

Usage

Building the k6 binary

xk6 build --with github.com/topfreegames/xk6-pitaya=. --with github.com/topfreegames/pitaya/v2=../

Building the k6 docker image

docker build --build-arg pitaya_revision=ac6eb29bdb -t xk6-pitaya
# change the pitaya_revision to the revision you want to use

Example usage

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

export const options = {
  vus: 10,
  duration: '10s',
}

const opts = {
  handshakeData: {
    sys: {
      clientVersion: "1.0.1",
      clientBuildNumber: "1",
      platform: "android"
    },
    user: {
      fiu: "c0a78b27-dd34-4e0d-bff7-36168fce0df5",
      bundleId: "com.game.test",
      deviceType: "ios",
      language: "en",
      osVersion: "12.0",
      region: "US",
      stack: "green-stack"
    }
  },
  requestTimeoutMs: 1000,
  logLevel: "info",
  serializer: "json",
}

const pitayaClient = new pitaya.Client(opts)

export default async () => {
  if (!pitayaClient.isConnected()) {
    pitayaClient.connect("localhost:3250")
  }

  check(pitayaClient.isConnected(), { 'pitaya client is connected': (r) => r === true })

  var res = await pitayaClient.request("room.room.entry")
  check(res.result, { 'contains an result field': (r) => r !== undefined })
  check(res.result, { 'result is ok': (r) => r === "ok" })

  var res = await pitayaClient.request("room.room.setsessiondata", { data: {"testKey": "testVal"} })
  check(res, { 'res is success': (r) => String.fromCharCode.apply(null,r) === "success"} )
  var res = await pitayaClient.request("room.room.getsessiondata")
  check(res.Data, { 'res contains set data': (r) => r.testKey === "testVal"} )
  res = await pitayaClient.request("room.room.join")
  check(res.result, { 'result from join is successful': (r) => r === "success"} )
  res = await pitayaClient.consumePush("onMembers", 500)
  check(res.Members, { 'res contains a member group': (m) => m !== undefined } )
  res = await pitayaClient.request("room.room.leave")
  check(res, { 'result from leave is successful': (r) => String.fromCharCode.apply(null,r) === "success"})

 pitayaClient.disconnect()
}

export function teardown() {
}

Running the scenario 1 example


# spin up pitaya dependencies
make ensure-testing-deps

# run pitaya server, backend and frontend
make run-cluster-example-backend
make run-cluster-example-frontend

# run k6 scenario
./k6 run ./examples/scenario1.js

Metrics

This extension will add the following metrics to the k6 output:

  • pitaya_client_request_duration_ms: Histogram of request durations in milliseconds
    • success: If the request was successful or not
    • route: The route of the request
  • pitaya_client_request_timeout_count: Counter of timedout requests
    • route: The route of the request

Protobuf client support

This extension does not support pitaya running with protobuf serialization. For loadtesting your server with this, use the json serializer.

builder.Serializer = json.NewSerializer()

Or just don't set it, since json is the default serializer.

Additional Documentation

All k6 documentation also applies to this extension. See https://k6.io/docs/ for more information.

Running distributed tests

It is possible to run distributed tests using k6 and this extension. To do so you can refer to the k6 documentation and use the binary generated from this repo as the k6 binary. There's a prebuilt docker image available at tfgco/xk6-pitaya that you can use as well.

Documentation

Overview

Package pitays only exists to register the pitaya extension

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client is the pitaya client It is used to connect to a pitaya server and send requests and notifies It is also used to consume pushes

func (*Client) Connect

func (c *Client) Connect(addr string) error

Connect connects to the server addr is the address of the server to connect to

func (*Client) ConsumePush

func (c *Client) ConsumePush(route string, timeoutMs int) *goja.Promise

ConsumePush will return a promise that will be resolved when a push is received on the given route. The promise will be rejected if the timeout is reached before a push is received. The promise will be resolved with the push data.

func (*Client) Disconnect

func (c *Client) Disconnect()

Disconnect disconnects from the server

func (*Client) IsConnected

func (c *Client) IsConnected() bool

IsConnected returns true if the client is connected to the server

func (*Client) Notify

func (c *Client) Notify(route string, msg interface{}) error

Notify sends a notify to the server route is the route to send the notify to msg is the message to send returns an error if the notify could not be sent

func (*Client) Request

func (c *Client) Request(route string, msg interface{}) *goja.Promise

Request sends a request to the server route is the route to send the request to msg is the message to send returns a promise that will be resolved when the response is received the promise will be rejected if the timeout is reached before a response is received

type ModuleInstance

type ModuleInstance struct {
	*Client
	// contains filtered or unexported fields
}

ModuleInstance represents an instance of the JS module.

func (*ModuleInstance) Exports

func (mi *ModuleInstance) Exports() modules.Exports

Exports implements the modules.Instance interface and returns the exports of the JS module.

func (*ModuleInstance) NewClient

func (mi *ModuleInstance) NewClient(call goja.ConstructorCall) *goja.Object

NewClient is the JS constructor function for the Client type. It returns a new Client instance for each VU. The first argument is an options object with the following fields: - handshakeData: the handshake data to send to the server - requestTimeoutMs: the timeout for requests in milliseconds - logLevel: the log level to use

type Response

type Response interface{}

Response is the type of the response returned by the server

type RootModule

type RootModule struct{}

RootModule is the global module instance that will create Client instances for each VU.

func New

func New() *RootModule

New returns a pointer to a new RootModule instance

func (*RootModule) NewModuleInstance

func (*RootModule) NewModuleInstance(vu modules.VU) modules.Instance

NewModuleInstance implements the modules.Module interface and returns a new instance for each VU.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL