Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions FORK.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ git pull --ff-only origin master
The build script reads the source in the current checkout and writes Linux and macOS binaries for
amd64 and arm64 into `dist/`. It does not install or deploy them. Its displayed `VERSION` value is
informational; use the binary's `version` command and SHA-256 checksum to identify an artifact.
Filenames include the build-start timestamp in UTC after `-breakingit`, for example
`k6-linux-amd64-breakingit-20261013T143000Z`. All four binaries from one invocation share the
same timestamp.

For the complete change history, compare the `master` branch with the upstream
[grafana/k6](https://github.com/grafana/k6) repository.
Expand Down
8 changes: 5 additions & 3 deletions build-linux-binaries.sh
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
#!/bin/bash
# Build k6 Linux and macOS binaries (amd64 and arm64) from the current code.
# Usage: ./build-linux-binaries.sh [output-dir]
# Default output dir is "dist". Binaries: k6-linux-*-breakingit, k6-darwin-*-breakingit
# Default output dir is "dist". Binaries end in -breakingit-YYYYMMDDTHHMMSSZ (UTC).

set -euo pipefail

OUT_DIR="${1:-dist}"
VERSION="${VERSION:-$(git describe --tags --always --dirty)}"
BUILD_TIMESTAMP="$(date -u +%Y%m%dT%H%M%SZ)"

echo "--- Building k6 binaries: ${VERSION}"
echo "--- Build timestamp (UTC): ${BUILD_TIMESTAMP}"
echo "--- Output directory: ${OUT_DIR}"

mkdir -p "$OUT_DIR"
Expand All @@ -17,15 +19,15 @@ LDFLAGS='-s -w'

build_linux() {
local arch="$1"
local name="k6-linux-${arch}-breakingit"
local name="k6-linux-${arch}-breakingit-${BUILD_TIMESTAMP}"
echo "Building ${name}..."
GOOS=linux GOARCH="${arch}" CGO_ENABLED=0 go build -tags lean -trimpath -ldflags "${LDFLAGS}" -o "${OUT_DIR}/${name}" .
echo " -> ${OUT_DIR}/${name}"
}

build_darwin() {
local arch="$1"
local name="k6-darwin-${arch}-breakingit"
local name="k6-darwin-${arch}-breakingit-${BUILD_TIMESTAMP}"
echo "Building ${name}..."
GOOS=darwin GOARCH="${arch}" CGO_ENABLED=0 go build -tags lean -trimpath -ldflags "${LDFLAGS}" -o "${OUT_DIR}/${name}" .
echo " -> ${OUT_DIR}/${name}"
Expand Down
21 changes: 21 additions & 0 deletions internal/output/breakingit/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,24 @@ and context, while keeping any business checks and throwing on failures.
PG-proxy connection. The default remains `true` for compatibility with existing
private proxy deployments. This setting applies only to output delivery, not to
HTTP requests generated by the test script.

### Binary delivery and batching

The Breaking-IT output sends PostgreSQL binary COPY parts (`X-Format: binary`)
through `/ingest/batch`, matching the JMeter pg-proxy writer. Requests,
transactions, errors, and virtual users retain their existing table and column
order, including the synthetic monitoring tables. The proxy must support binary
COPY. Screenshots continue to use the separate JSON upsert endpoint.

The first complete row triggers an immediate batch so BreakTest can detect that
the test is running. Subsequent batches wait 1,000 ms after the previous send
finishes, for both load tests and synthetic monitoring. Slow sends and retries
never overlap. Shutdown drains incomplete HTTP groups and sends the remaining
rows immediately. The 100 ms HTTP-group cleanup timer only assembles rows; it
does not control network delivery.

Multipart bodies use fast gzip compression. Sending detaches the current buffers,
so producers can fill the next batch during retries. Exhausted retries discard
only the failed batch; newly collected rows remain queued for the next send.
Synthetic run IDs use UUID binary fields, with the same deterministic fallback
as JMeter for legacy identifiers that are not UUIDs.
4 changes: 3 additions & 1 deletion internal/output/breakingit/batch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,12 @@ func TestBatchRetryPreservesConcurrentSamples(t *testing.T) {
defer server.Close()
logger.proxyURL, logger.httpClient = server.URL, server.Client()
logger.flush()
require.Len(t, requests, 2)
logger.flush()
for _, want := range []string{"original\n", "original\n", "arrived during retry\n"} {
select {
case got := <-requests:
assert.Equal(t, want, got)
assert.Equal(t, binaryCopyHeader+want+"\xff\xff", got)
case <-time.After(5 * time.Second):
t.Fatal("missing batch")
}
Expand Down
133 changes: 133 additions & 0 deletions internal/output/breakingit/binary.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package breakingit

import (
"bytes"
"crypto/md5" //nolint:gosec // Java UUID.nameUUIDFromBytes compatibility, not cryptography.
"encoding/binary"
"fmt"
"math"
"strconv"
"time"

"github.com/gofrs/uuid/v5"
)

const (
binaryCopyHeader = "PGCOPY\n\xff\r\n\x00\x00\x00\x00\x00\x00\x00\x00\x00"
postgresEpochMicros int64 = 946684800000000
)

// Column types follow the pg-proxy/JMeter COPY contract: timestamp, text, bool, int4, UUID.
func (l *Logger) binarySchema(buffer *bytes.Buffer) string {
switch buffer {
case l.bufHTTP:
if l.isSynthetic {
return "tssssssbiisiiiu"
}
return "tssssbiisiii"
case l.bufTrans:
if l.isSynthetic {
return "tsssssbiiiu"
}
return "tsssbiii"
case l.bufErrors:
if l.isSynthetic {
return "tsssssssiissssssuu"
}
return "tssssssiissssssu"
default:
return "tsssiii"
}
}

// syntheticRunUUID matches Java's nameUUIDFromBytes fallback for legacy run identifiers.
func syntheticRunUUID(value string) uuid.UUID {
if id, err := uuid.FromString(value); err == nil {
return id
}
if value == "" {
value = "unknown"
}
id := uuid.UUID(md5.Sum([]byte(value))) //nolint:gosec // Protocol compatibility.
id[6] = (id[6] & 0x0f) | 0x30
id[8] = (id[8] & 0x3f) | 0x80
return id
}

// writeBinaryRow is called with mu held. Buffers contain rows only; framing is added per batch.
func (l *Logger) writeBinaryRow(buffer *bytes.Buffer, row []any) {
schema := l.binarySchema(buffer)
// Reserve enough room for common rows, and write directly into unused buffer capacity.
size := 2 + len(row)*12
for _, value := range row {
if text, ok := value.(string); ok {
size += len(text)
}
}
buffer.Grow(size)
data := binary.BigEndian.AppendUint16(buffer.AvailableBuffer(), uint16(len(row))) //nolint:gosec
for i, value := range row {
var err error
data, err = appendBinaryField(data, value, schema[i])
if err != nil {
_, _ = fmt.Fprintf(l.out, "breakingit: cannot encode COPY row: %v\n", err)
return
}
}

_, _ = buffer.Write(data)
if !l.firstQueued && l.firstData != nil {
l.firstQueued = true
l.firstData <- struct{}{}
}
}

func appendBinaryField(data []byte, value any, kind byte) ([]byte, error) {
switch v := value.(type) {
case time.Time:
data = binary.BigEndian.AppendUint32(data, 8)
return binary.BigEndian.AppendUint64(data, uint64(v.UnixMicro()-postgresEpochMicros)), nil //nolint:gosec
case int64:
if v < math.MinInt32 || v > math.MaxInt32 {
return nil, fmt.Errorf("int4 out of range: %d", v)
}
data = binary.BigEndian.AppendUint32(data, 4)
return binary.BigEndian.AppendUint32(data, uint32(v)), nil //nolint:gosec // Signed int4 bits.
case string:
return appendBinaryString(data, v, kind)
default:
return nil, fmt.Errorf("unsupported field type %T", value)
}
}

func appendBinaryString(data []byte, value string, kind byte) ([]byte, error) {
// Preserve CSV's NULL semantics for empty fields, except JMeter's run UUID fallback.
if value == "" && kind != 'u' {
return binary.BigEndian.AppendUint32(data, ^uint32(0)), nil
}
switch kind {
case 'b':
v, err := strconv.ParseBool(value)
if err != nil {
return nil, err
}
data = binary.BigEndian.AppendUint32(data, 1)
if v {
return append(data, 1), nil
}
return append(data, 0), nil
case 'i':
n, err := strconv.ParseInt(value, 10, 32)
if err != nil {
return nil, err
}
return appendBinaryField(data, n, kind)
case 'u':
id := syntheticRunUUID(value)
data = binary.BigEndian.AppendUint32(data, 16)
return append(data, id[:]...), nil
default:
data = binary.BigEndian.AppendUint32(data, uint32(len(value))) //nolint:gosec // COPY field length.
return append(data, value...), nil
}
}
33 changes: 33 additions & 0 deletions internal/output/breakingit/binary_benchmark_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package breakingit

import (
"bytes"
"encoding/csv"
"strconv"
"testing"
"time"
)

// Compare row serialization with the previous per-row CSV writer, excluding network and gzip.
func BenchmarkTransactionEncoding(b *testing.B) {
at := time.Date(2026, 10, 13, 12, 0, 0, 123456000, time.UTC)
b.Run("CSV", func(b *testing.B) {
var buffer bytes.Buffer
b.ReportAllocs()
for b.Loop() {
buffer.Reset()
row := []string{at.UTC().Format(time.RFC3339Nano), "run-123", "Amsterdam", "Checkout", "true", "", "", strconv.FormatInt(123, 10)}
writer := csv.NewWriter(&buffer)
_ = writer.Write(row)
writer.Flush()
}
})
b.Run("Binary", func(b *testing.B) {
logger := newTransactionTestLogger(false)
b.ReportAllocs()
for b.Loop() {
logger.bufTrans.Reset()
logger.writeBinaryRow(logger.bufTrans, []any{at, "run-123", "Amsterdam", "Checkout", "true", "", "", int64(123)})
}
})
}
Loading