Skip to content

Commit

Permalink
Merge pull request #5 from kelseyaubrecht/feat/datagrams
Browse files Browse the repository at this point in the history
Feat/datagrams
  • Loading branch information
kelseyaubrecht authored May 8, 2024
2 parents 4413580 + e8a8925 commit 7379e11
Show file tree
Hide file tree
Showing 9 changed files with 294 additions and 162 deletions.
7 changes: 3 additions & 4 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,19 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.22
go-version: 1.21

- name: Run prettier
uses: creyD/prettier_action@v4.3
with:
dry: True

- name: Lint code issues
uses: golangci/golangci-lint-action@v3.7.1
with:
skip-pkg-cache: true

- name: Build k6 with xk6-webtransport
run: |
go install go.k6.io/xk6/cmd/xk6@latest
xk6 build --with github.com/kelseyaubrecht/xk6-webtransport@latest
37 changes: 23 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
![ ](https://github.com/kelseyaubrecht/xk6-webtransport/actions/workflows/test.yaml/badge.svg)

# xk6-webtransport

[k6](https://github.com/grafana/k6) extension to k6 extension to use the WebTransport protocol. Currently only supports bidirectional streams.
![ ](https://github.com/kelseyaubrecht/xk6-webtransport/actions/workflows/test.yaml/badge.svg)

[k6](https://github.com/grafana/k6) extension to k6 extension to use the WebTransport protocol.
Implemented using the [xk6](https://github.com/grafana/xk6) system and [webtransport-go](https://github.com/quic-go/webtransport-go).

Supports:

- bidirectional streams
- datagrams

## Work in progress

This project is a work in progress. Feedback and contributions are welcome!
Expand All @@ -25,13 +30,13 @@ To build a `k6` binary with this extension, first ensure you have the prerequisi

Then:

1. Install `xk6`:
- Install `xk6`:

```shell
go install go.k6.io/xk6/cmd/xk6@latest
```

2. Build the binary:
- Build the binary:

```shell
xk6 build --with github.com/kelseyaubrecht/xk6-webtransport@latest
Expand All @@ -47,7 +52,7 @@ xk6 build --with github.com/kelseyaubrecht/xk6-webtransport@latest

## Usage example

An example of using the extension to create a bidirection stream.
An example of using the extension to create a bidirection stream. More in [examples](examples).

```javascript
import wt from "k6/x/webtransport";
Expand All @@ -68,11 +73,15 @@ export default function () {

## Metrics

| Metric | Type | Description |
| ------------------------ | ------- | ---------------------------------- |
| webtransport_read_bytes | counter | Total bytes read |
| webtransport_read_count | counter | Total read count |
| webtransport_read_size | trend | Trends of read size per operation |
| webtransport_write_bytes | counter | Total bytes written |
| webtransport_write_count | counter | Total write count |
| webtransport_write_size | trend | Trends of write size per operation |
| Metric | Type | Description |
| ------------------------------------- | ------- | ---------------------------------- |
| webtransport_read_bytes | counter | Total bytes read |
| webtransport_read_count | counter | Total read count |
| webtransport_read_size | trend | Trends of read size per operation |
| webtransport_write_bytes | counter | Total bytes written |
| webtransport_write_count | counter | Total write count |
| webtransport_write_size | trend | Trends of write size per operation |
| webtransport_datagrams_sent_count | counter | Number of sent datagrams |
| webtransport_datagrams_sent_bytes | counter | Total bytes sent in datagrams |
| webtransport_datagrams_received_count | counter | Number of received datagrams |
| webtransport_datagrams_received_bytes | counter | Total bytes received in datagrams |
77 changes: 77 additions & 0 deletions datagrams.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package wt

import (
"log"
"time"

"go.k6.io/k6/metrics"
)

func (c *Connection) SendDatagram(p []byte) {
err := c.Session.SendDatagram(p)
defer c.logSendDatagramMetrics(len(p))
if err != nil {
log.Println("SendDatagram error: " + err.Error())
}
}

func (c *Connection) ReceiveDatagram() []byte {
p, err := c.Session.ReceiveDatagram(c.Session.Context())
if err != nil {
log.Println("ReceiveDatagram error: " + err.Error())
}
defer c.logRecvDatagramMetrics(len(p))
return p
}

func (c *Connection) logSendDatagramMetrics(n int) {
state := c.vu.State()
ctx := c.vu.Context()
if state == nil || ctx == nil {
return
}

now := time.Now()

metrics.PushIfNotDone(ctx, state.Samples, metrics.ConnectedSamples{
Samples: []metrics.Sample{
{
Time: now,
TimeSeries: metrics.TimeSeries{Metric: c.metrics.DatagramsSentCount},
Value: 1,
},
{
Time: now,
TimeSeries: metrics.TimeSeries{Metric: c.metrics.DatagramsSentBytes},
Value: float64(n),
},
},
Time: now,
})
}

func (c *Connection) logRecvDatagramMetrics(n int) {
state := c.vu.State()
ctx := c.vu.Context()
if state == nil || ctx == nil {
return
}

now := time.Now()

metrics.PushIfNotDone(ctx, state.Samples, metrics.ConnectedSamples{
Samples: []metrics.Sample{
{
Time: now,
TimeSeries: metrics.TimeSeries{Metric: c.metrics.DatagramsRecvCount},
Value: 1,
},
{
Time: now,
TimeSeries: metrics.TimeSeries{Metric: c.metrics.DatagramsRecvBytes},
Value: float64(n),
},
},
Time: now,
})
}
13 changes: 13 additions & 0 deletions examples/datagrams.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import wt from "k6/x/webtransport";

export default function () {
const url = "https://localhost:443/webtransport";
let data = [0, 1, 2, 3, 4];

wt.connect(url);

wt.sendDatagram(data);

const response = wt.receiveDatagram();
// handle response
}
76 changes: 39 additions & 37 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,57 +1,59 @@
module github.com/kelseyaubrecht/xk6-webtransport

go 1.22
go 1.21

require (
github.com/quic-go/webtransport-go v0.6.0
go.k6.io/k6 v0.48.0
github.com/quic-go/webtransport-go v0.8.0
go.k6.io/k6 v0.50.0
)

require (
github.com/cenkalti/backoff/v4 v4.2.1 // indirect
github.com/dlclark/regexp2 v1.9.0 // indirect
github.com/dop251/goja v0.0.0-20231027120936-b396bb4c349d // indirect
github.com/fatih/color v1.15.0 // indirect
github.com/go-logr/logr v1.2.4 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-sourcemap/sourcemap v2.1.4-0.20211119122758-180fcef48034+incompatible // indirect
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 // indirect
github.com/go-task/slim-sprig/v3 v3.0.0 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/pprof v0.0.0-20230821062121-407c9e7a662f // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.16.0 // indirect
go.opentelemetry.io/otel v1.21.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.21.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.21.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.21.0 // indirect
go.opentelemetry.io/otel/metric v1.21.0 // indirect
go.opentelemetry.io/otel/sdk v1.21.0 // indirect
go.opentelemetry.io/otel/trace v1.21.0 // indirect
go.opentelemetry.io/proto/otlp v1.0.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20231106174013-bbf56f31fb17 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20231120223509-83a465c0220f // indirect
google.golang.org/grpc v1.60.0 // indirect
google.golang.org/protobuf v1.33.0 // indirect
)

require (
github.com/dlclark/regexp2 v1.11.0 // indirect
github.com/dop251/goja v0.0.0-20240220182346-e401ed450204 // indirect
github.com/fatih/color v1.16.0 // indirect
github.com/go-sourcemap/sourcemap v2.1.4+incompatible // indirect
github.com/google/pprof v0.0.0-20240507183855-6f11f98ebb1c // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mstoykov/atlas v0.0.0-20220811071828-388f114305dd // indirect
github.com/onsi/ginkgo v1.16.5 // indirect
github.com/onsi/ginkgo/v2 v2.12.0 // indirect
github.com/onsi/ginkgo/v2 v2.17.3 // indirect
github.com/quic-go/qpack v0.4.0 // indirect
github.com/quic-go/qtls-go1-20 v0.4.1 // indirect
github.com/quic-go/quic-go v0.40.0 // indirect
github.com/quic-go/quic-go v0.43.1 // indirect
github.com/serenize/snaker v0.0.0-20201027110005-a7ad2135616e // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/spf13/afero v1.1.2 // indirect
go.opentelemetry.io/otel v1.19.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.19.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.19.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.19.0 // indirect
go.opentelemetry.io/otel/metric v1.19.0 // indirect
go.opentelemetry.io/otel/sdk v1.19.0 // indirect
go.opentelemetry.io/otel/trace v1.19.0 // indirect
go.opentelemetry.io/proto/otlp v1.0.0 // indirect
go.uber.org/mock v0.3.0 // indirect
golang.org/x/crypto v0.14.0 // indirect
golang.org/x/exp v0.0.0-20230817173708-d852ddb80c63 // indirect
golang.org/x/mod v0.12.0 // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/text v0.13.0 // indirect
golang.org/x/time v0.3.0 // indirect
golang.org/x/tools v0.12.1-0.20230815132531-74c255bcf846 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20230711160842-782d3b101e98 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230731193218-e0aa005b6bdf // indirect
google.golang.org/grpc v1.58.3 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/guregu/null.v3 v3.3.0 // indirect
github.com/spf13/afero v1.11.0 // indirect
go.uber.org/mock v0.4.0 // indirect
golang.org/x/crypto v0.23.0 // indirect
golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842 // indirect
golang.org/x/mod v0.17.0 // indirect
golang.org/x/net v0.25.0 // indirect
golang.org/x/sys v0.20.0 // indirect
golang.org/x/text v0.15.0 // indirect
golang.org/x/time v0.5.0 // indirect
golang.org/x/tools v0.21.0 // indirect
gopkg.in/guregu/null.v3 v3.5.0 // indirect
)
Loading

0 comments on commit 7379e11

Please sign in to comment.