sse

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2024 License: Apache-2.0 Imports: 21 Imported by: 0

README

xk6-sse

A k6 extension for Server-Sent Events (SSE) using the xk6 system.

See the K6 SSE Extension design.

k6 version

This extension is tested with k6 version v0.50.0 last release is v0.1.0.

Build

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

Then:

  1. Install xk6:
go install go.k6.io/xk6/cmd/xk6@latest
  1. Build the binary:
xk6 build master \
--with github.com/phymbert/xk6-sse

Example

import sse from "k6/x/sse"
import {check} from "k6"

export default function () {
    const url = "https://echo.websocket.org/.sse"
    const params = {
        method: 'GET',
        headers: {
            "Authorization": "Bearer XXXX"
        },
        tags: {"my_k6s_tag": "hello sse"}
    }

    const response = sse.open(url, params, function (client) {
        client.on('open', function open() {
            console.log('connected')
        })

        client.on('event', function (event) {
            console.log(`event id=${event.id}, name=${event.name}, data=${event.data}`)
            if (parseInt(event.id) === 4) {
                client.close()
            }
        })

        client.on('error', function (e) {
            console.log('An unexpected error occurred: ', e.error())
        })
    })

    check(response, {"status is 200": (r) => r && r.status === 200})
}
License
                             Apache License
                       Version 2.0, January 2004
                    http://www.apache.org/licenses/

Documentation

Overview

Package sse implements a k6/x/sse javascript module extension for k6. It provides basic functionality to handle Server-Sent Event over http that *blocks* the event loop while the http connection is opened. [SSE API design document]: https://github.com/phymbert/xk6-sse/blob/master/docs/design/021-sse-api.md#proposed-solution

Index

Constants

View Source
const MetricEventName = "sse_event"

MetricEventName is the sse event metric of the module

Variables

View Source
var ErrSSEInInitContext = common.NewInitContextError("using sse in the init context is not supported")

ErrSSEInInitContext is returned when sse are using in the init context

Functions

This section is empty.

Types

type Client

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

Client is the representation of the sse returned to the js.

func (*Client) Close

func (c *Client) Close() error

Close the event loop

func (*Client) On

func (c *Client) On(event string, handler goja.Value)

On is used to configure what the client should do on each event.

type Event

type Event struct {
	ID      string
	Comment string
	Name    string
	Data    string
}

Event represents a Server-Sent Event

type HTTPResponse

type HTTPResponse struct {
	URL     string            `json:"url"`
	Status  int               `json:"status"`
	Headers map[string]string `json:"headers"`
	Error   string            `json:"error"`
}

HTTPResponse is the http response returned by sse.open.

type RootModule

type RootModule struct{}

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

func New

func New() *RootModule

New returns a pointer to a new RootModule instance.

func (*RootModule) NewModuleInstance

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

NewModuleInstance implements the modules.Module interface to return a new instance for each VU.

Jump to

Keyboard shortcuts

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