k6-perf-reporter 1.5.0 → 1.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4,5 +4,9 @@
4
4
  "token": "${INFLUX_TOKEN}",
5
5
  "org": "my-org",
6
6
  "bucket": "k6"
7
+ },
8
+ "slack": {
9
+ "token": "${SLACK_TOKEN}",
10
+ "channel": "#k6-reports"
7
11
  }
8
12
  }
package/README.md CHANGED
@@ -1,13 +1,13 @@
1
1
  # k6-perf-reporter
2
2
 
3
- A comprehensive reporting tool for k6 performance tests with InfluxDB 2 integration. Generates beautiful HTML, PDF, CSV, and JSON reports with key performance metrics.
3
+ A comprehensive reporting tool for k6 performance tests with InfluxDB 2 integration. Generates beautiful CLI, JSON, and Slack reports with key performance metrics.
4
4
 
5
5
  ## Features
6
6
 
7
- - **Multiple Report Formats**: HTML, PDF, CSV, JSON, and CLI output
7
+ - **Multiple Report Formats**: CLI output, JSON, and Slack integration
8
8
  - **InfluxDB 2 Integration**: Query test metrics directly from InfluxDB
9
- - **Key Metrics**: Response time percentiles (p50, p95, p99), throughput, error rates, and error breakdown
10
- - **Professional Reports**: Beautiful, interactive HTML reports with responsive design
9
+ - **Key Metrics**: RPS, HTTP requests, checks, error rates, latencies, and more
10
+ - **Real-time Slack Notifications**: Send formatted test reports directly to Slack channels
11
11
  - **CLI Tool**: Command-line interface for automated report generation
12
12
  - **Library Support**: Use as a TypeScript/JavaScript library in your own tools
13
13
 
@@ -20,12 +20,11 @@ npm install
20
20
  # Build the project
21
21
  npm run build
22
22
 
23
- # Generate an HTML report
23
+ # Generate a CLI report for the last hour
24
24
  npx tsx src/cli.ts generate \
25
- --scenario api-load-test \
26
- --start-time "2024-01-01T00:00:00Z" \
27
- --end-time "2024-01-01T01:00:00Z" \
28
- --format html
25
+ --run-id 123456790121 \
26
+ -st -1h \
27
+ --format cli
29
28
  ```
30
29
 
31
30
  ## Installation
@@ -35,213 +34,122 @@ npm install
35
34
  npm run build
36
35
  ```
37
36
 
38
- ## Usage
37
+ ## Configuration
39
38
 
40
- ### As a CLI Tool
39
+ Configuration can be provided via config file (`.config.json`) or environment variables. Environment variables take precedence over config file values.
41
40
 
42
- #### HTML Report
43
- ```bash
44
- npx tsx src/cli.ts generate \
45
- --scenario api-load-test \
46
- --start-time "2024-01-01T00:00:00Z" \
47
- --end-time "2024-01-01T01:00:00Z" \
48
- --format html
41
+ ### Config File (.config.json)
42
+
43
+ ```json
44
+ {
45
+ "influx": {
46
+ "url": "http://localhost:8086",
47
+ "token": "${INFLUX_TOKEN}",
48
+ "org": "my-org",
49
+ "bucket": "k6"
50
+ },
51
+ "slack": {
52
+ "token": "${SLACK_TOKEN}",
53
+ "channel": "#k6-reports"
54
+ }
55
+ }
49
56
  ```
50
57
 
51
- Generates a beautiful, interactive HTML report with:
52
- - Color-coded performance metric cards
53
- - Responsive design that works on all devices
54
- - Executive summary section
55
- - Detailed error breakdown table
56
- - Professional styling with gradient headers
58
+ ### Environment Variables
57
59
 
58
- #### PDF Report
59
60
  ```bash
60
- npx tsx src/cli.ts generate \
61
- --scenario api-load-test \
62
- --start-time "2024-01-01T00:00:00Z" \
63
- --end-time "2024-01-01T01:00:00Z" \
64
- --format pdf
61
+ # InfluxDB Configuration (required)
62
+ export INFLUX_URL=http://localhost:8086
63
+ export INFLUX_TOKEN=your-influx-token
64
+ export INFLUX_ORG=your-org
65
+ export INFLUX_BUCKET=k6
66
+
67
+ # Slack Configuration (optional)
68
+ export SLACK_TOKEN=xoxb-your-slack-token
69
+ export SLACK_CHANNEL=#k6-reports
65
70
  ```
66
71
 
67
- Creates a printable PDF version of the HTML report for archival and sharing.
72
+ **Note:** Environment variables support `${ENV_VAR}` references in config file (e.g., `"token": "${INFLUX_TOKEN}"`).
68
73
 
69
- #### CSV Report
70
- ```bash
71
- npx tsx src/cli.ts generate \
72
- --scenario api-load-test \
73
- --start-time "2024-01-01T00:00:00Z" \
74
- --end-time "2024-01-01T01:00:00Z" \
75
- --format csv
76
- ```
74
+ ## Usage
77
75
 
78
- Exports metrics in CSV format for import into Excel, Google Sheets, or data analysis tools.
76
+ ### As a CLI Tool
79
77
 
80
- Example CSV output:
81
- ```
82
- k6 Performance Test Report
83
- Test Name,api-load-test
84
- Generated,2024-01-01T12:00:00Z
85
-
86
- RESPONSE TIME METRICS
87
- Metric,Value (ms)
88
- P50,125.45
89
- P95,385.67
90
- P99,892.34
91
-
92
- THROUGHPUT & REQUESTS
93
- Metric,Value
94
- Throughput (req/s),156.23
95
- Total Requests,10000
96
- Successful Requests,9950
97
- Failed Requests,50
98
- Error Rate (%),0.50
99
-
100
- ERROR BREAKDOWN
101
- Error Type,Count
102
- ConnectionError,30
103
- TimeoutError,15
104
- HTTPError500,5
105
- ```
78
+ #### CLI Output (Default)
79
+
80
+ Display formatted report in terminal:
106
81
 
107
- #### JSON Report
108
82
  ```bash
109
83
  npx tsx src/cli.ts generate \
110
- --scenario api-load-test \
111
- --start-time "2024-01-01T00:00:00Z" \
112
- --end-time "2024-01-01T01:00:00Z" \
113
- --format json
84
+ --run-id 123456790121 \
85
+ --start-time "-1h" \
86
+ --format cli
114
87
  ```
115
88
 
116
- Generates machine-readable JSON for programmatic access:
117
- ```json
118
- {
119
- "testName": "api-load-test",
120
- "generatedAt": "2024-01-01T12:00:00Z",
121
- "metrics": {
122
- "responseTime": {
123
- "p50": 125.45,
124
- "p95": 385.67,
125
- "p99": 892.34
126
- },
127
- "throughput": {
128
- "requestsPerSecond": 156.23
129
- },
130
- "requests": {
131
- "total": 10000,
132
- "successful": 9950,
133
- "failed": 50,
134
- "errorRate": 0.50
135
- },
136
- "errors": {
137
- "ConnectionError": 30,
138
- "TimeoutError": 15,
139
- "HTTPError500": 5
140
- }
141
- }
142
- }
143
- ```
89
+ Output includes:
90
+ - Run ID, start/end times
91
+ - Check pass rate with status
92
+ - RPS metrics (avg, p95, max)
93
+ - HTTP requests count and rate
94
+ - Request duration metrics
95
+ - Virtual users info
96
+ - Top slowest URLs
97
+ - RPS per URL
98
+ - Error requests breakdown
99
+ - Error responses with error types
100
+
101
+ #### JSON Report
144
102
 
145
- #### CLI Output
146
103
  ```bash
147
104
  npx tsx src/cli.ts generate \
148
- --scenario api-load-test \
149
- --start-time "2024-01-01T00:00:00Z" \
150
- --end-time "2024-01-01T01:00:00Z" \
151
- --format cli
105
+ --run-id 123456790121 \
106
+ --start-time "-1h" \
107
+ --format json \
108
+ --output report.json
152
109
  ```
153
110
 
154
- Displays a pretty-printed table in your terminal:
155
- ```
156
- ━━━ k6 Performance Test Report ━━━
157
-
158
- Test: api-load-test
159
- Generated: 1/1/2024, 12:00:00 PM
160
-
161
- 📊 Response Times
162
- ┌────────┬───────────┐
163
- │ Metric │ Value │
164
- ├────────┼───────────┤
165
- │ P50 │ 125.45 ms │
166
- │ P95 │ 385.67 ms │
167
- │ P99 │ 892.34 ms │
168
- └────────┴───────────┘
169
-
170
- ⚡ Throughput & Requests
171
- ┌──────────────────────┬──────────┐
172
- │ Metric │ Value │
173
- ├──────────────────────┼──────────┤
174
- │ Throughput │ 156.23 req/s │
175
- │ Total Requests │ 10000 │
176
- │ Successful Requests │ 9950 │
177
- │ Failed Requests │ 50 │
178
- └──────────────────────┴──────────┘
179
-
180
- ❌ Error Analysis
181
- ┌────────────┬─────────┐
182
- │ Metric │ Value │
183
- ├────────────┼─────────┤
184
- │ Error Rate │ 0.50 % │
185
- └────────────┴─────────┘
186
-
187
- Overall Status: ✓ PASS
188
-
189
- ━━━━━━━━━━━━━━━━━━━━━━━━━━━
190
- ```
111
+ Generates machine-readable JSON with all metrics for programmatic access and CI/CD integration.
191
112
 
192
- ### As a Library
113
+ #### Slack Report
193
114
 
194
- ```typescript
195
- import {
196
- InfluxQueryClient,
197
- ReportGenerator,
198
- CLIReporter,
199
- } from "k6-perf-reporter";
115
+ Send formatted report directly to Slack:
200
116
 
201
- const influxConfig = {
202
- url: "http://localhost:8086",
203
- token: "your-token",
204
- org: "your-org",
205
- bucket: "k6",
206
- };
117
+ ```bash
118
+ # Via config file (recommended)
119
+ npx tsx src/cli.ts generate \
120
+ --run-id 123456790121 \
121
+ --start-time "-1h" \
122
+ --format slack
207
123
 
208
- const influxClient = new InfluxQueryClient(influxConfig);
209
- const reportGenerator = new ReportGenerator(influxClient);
210
-
211
- // Generate metrics for the last hour
212
- const metrics = await reportGenerator.generateMetrics(
213
- "api-load-test",
214
- "-1h",
215
- "now()"
216
- );
217
-
218
- // Print to CLI
219
- const cliReporter = new CLIReporter();
220
- cliReporter.printMetricsTable(metrics, "api-load-test");
221
-
222
- // Or generate formatted reports
223
- const htmlReport = reportGenerator.generateHTMLReport(metrics, "api-load-test");
224
- const jsonReport = reportGenerator.generateJSONReport(metrics, "api-load-test");
225
- const csvReport = reportGenerator.generateCSVReport(metrics, "api-load-test");
226
-
227
- // Save to file
228
- import fs from "fs";
229
- fs.writeFileSync("report.html", htmlReport);
230
- fs.writeFileSync("report.json", jsonReport);
231
- fs.writeFileSync("report.csv", csvReport);
124
+ # Or via environment variables
125
+ SLACK_TOKEN=xoxb-... SLACK_CHANNEL=#k6-reports npx tsx src/cli.ts generate \
126
+ --run-id 123456790121 \
127
+ --start-time "-1h" \
128
+ --format slack
232
129
  ```
233
130
 
234
- ## Metrics Included
131
+ Slack message includes:
132
+ - Pass/Fail status with emoji (✅ PASS / ❌ FAIL)
133
+ - Key metrics (RPS, HTTP requests, iterations, VUs, duration)
134
+ - Error rate and failed checks
135
+ - Tables for:
136
+ - Top slowest URLs
137
+ - RPS per URL
138
+ - Error requests
139
+ - Error responses with error types
235
140
 
236
- - **Response Times**: p50, p95, p99 percentiles (milliseconds)
237
- - **Throughput**: Requests per second
238
- - **Requests**: Total, successful, and failed counts
239
- - **Error Rate**: Percentage of failed requests
240
- - **Error Breakdown**: Distribution of error types with counts
141
+ ### Command Options
241
142
 
242
- ## Time Format Examples
143
+ ```
144
+ --run-id <id> k6 test run ID (required)
145
+ -st, --start-time <time> Start time (relative: -1h, -30m, or ISO 8601)
146
+ -et, --end-time <time> End time (ISO 8601 format, defaults to now)
147
+ -c, --config <path> Path to config file (default: .config.json)
148
+ -f, --format <format> Output format: 'json', 'cli', or 'slack' (default: cli)
149
+ -o, --output <path> Output file path (for json format)
150
+ ```
243
151
 
244
- For InfluxDB time queries, use relative or absolute formats:
152
+ ### Time Format Examples
245
153
 
246
154
  ```bash
247
155
  # Last hour
@@ -250,56 +158,99 @@ For InfluxDB time queries, use relative or absolute formats:
250
158
  # Last 24 hours
251
159
  --start-time "-24h" --end-time "now()"
252
160
 
253
- # Specific date range (ISO 8601)
254
- --start-time "2024-01-01T00:00:00Z" --end-time "2024-01-02T00:00:00Z"
255
-
256
161
  # Last 30 minutes
257
162
  --start-time "-30m" --end-time "now()"
163
+
164
+ # Specific date range (ISO 8601)
165
+ --start-time "2024-01-01T00:00:00Z" --end-time "2024-01-02T00:00:00Z"
258
166
  ```
259
167
 
260
- ## Environment Variables
168
+ ## Metrics Included
261
169
 
262
- You can configure InfluxDB settings via environment variables. They can be used in two ways:
170
+ ### Response Time
171
+ - Min, max, average
172
+ - Percentiles: p50, p90, p95, p99
173
+ - Both overall and for successful responses only
263
174
 
264
- ### 1. Complete Configuration via Environment Variables
175
+ ### Throughput & Requests
176
+ - Requests per second (RPS)
177
+ - Total, successful, and failed request counts
178
+ - Request rate
265
179
 
266
- Set all four variables to use environment-only configuration:
180
+ ### Checks
181
+ - Pass rate percentage
182
+ - Passed and failed check counts
267
183
 
268
- ```bash
269
- export INFLUX_URL=http://localhost:8086
270
- export INFLUX_TOKEN=your-influx-token
271
- export INFLUX_ORG=your-org
272
- export INFLUX_BUCKET=k6
273
- ```
184
+ ### Error Analysis
185
+ - Error response count and rate
186
+ - Top error requests by status code
187
+ - Error responses grouped by type
188
+
189
+ ### Performance Metrics
190
+ - Iteration duration (min, max, avg, p90, p95)
191
+ - Virtual users (current, min, max)
192
+
193
+ ## Configuration Loading Priority
194
+
195
+ For each configuration value, the system checks in this order:
196
+
197
+ 1. **Environment variables** (highest priority)
198
+ - `INFLUX_URL`, `INFLUX_TOKEN`, `INFLUX_ORG`, `INFLUX_BUCKET`
199
+ - `SLACK_TOKEN`, `SLACK_CHANNEL`
200
+
201
+ 2. **Config file values**
202
+ - `.config.json` or custom path via `-c` option
203
+
204
+ 3. **Environment variable references in config**
205
+ - Syntax: `"${ENV_VAR_NAME}"`
206
+ - Example: `"token": "${INFLUX_TOKEN}"`
207
+
208
+ **Example:** Using production token via env var while keeping other settings in config file
274
209
 
275
- Then run the CLI without needing a config file:
276
210
  ```bash
277
- npx tsx src/cli.ts generate \
278
- --run-id 123456790121 \
279
- --start-time "-1h" \
280
- --end-time "now()" \
281
- --format cli
211
+ export INFLUX_TOKEN=prod-token
212
+ npx tsx src/cli.ts generate --run-id 123 --format cli
213
+ # Uses prod-token but other influx settings from .config.json
282
214
  ```
283
215
 
284
- ### 2. Override Specific Values from Config File
216
+ ## Full Example Workflow
285
217
 
286
- You can also use environment variables to override individual settings from `.config.json`:
218
+ 1. **Configure InfluxDB and Slack:**
287
219
 
288
220
  ```bash
289
- # .config.json exists with default settings
290
- export INFLUX_TOKEN=production-token
221
+ # Create .config.json
222
+ cat > .config.json << EOF
223
+ {
224
+ "influx": {
225
+ "url": "http://localhost:8086",
226
+ "token": "\${INFLUX_TOKEN}",
227
+ "org": "my-org",
228
+ "bucket": "k6"
229
+ },
230
+ "slack": {
231
+ "token": "\${SLACK_TOKEN}",
232
+ "channel": "#k6-reports"
233
+ }
234
+ }
235
+ EOF
291
236
 
292
- # Now the CLI will use the token from env var while other settings come from .config.json
293
- npx tsx src/cli.ts generate --run-id 123456790121 --format cli
237
+ # Set environment variables
238
+ export INFLUX_TOKEN=your-token
239
+ export SLACK_TOKEN=xoxb-your-slack-token
294
240
  ```
295
241
 
296
- **Note:** Environment variables always take precedence over values in `.config.json`.
242
+ 2. **Generate reports:**
297
243
 
298
- ## Prerequisites
244
+ ```bash
245
+ # CLI report
246
+ npx tsx src/cli.ts generate --run-id 123456 -st -1h --format cli
299
247
 
300
- - Node.js 16+
301
- - InfluxDB 2.x with k6 test data
302
- - For PDF generation: Puppeteer (included in dependencies)
248
+ # Save as JSON
249
+ npx tsx src/cli.ts generate --run-id 123456 -st -1h --format json -o report.json
250
+
251
+ # Send to Slack
252
+ npx tsx src/cli.ts generate --run-id 123456 -st -1h --format slack
253
+ ```
303
254
 
304
255
  ## InfluxDB Setup
305
256
 
@@ -328,102 +279,13 @@ export default function () {
328
279
  ```
329
280
 
330
281
  Run with InfluxDB output:
282
+
331
283
  ```bash
332
284
  k6 run script.js \
333
- -o xk6-influxdb=http://localhost:8086/testdata \
285
+ -o xk6-influxdb=http://localhost:8086/k6 \
334
286
  --tag testName=api-load-test
335
287
  ```
336
288
 
337
- ## Full Example Workflow
338
-
339
- 1. **Run your k6 test with InfluxDB output:**
340
- ```bash
341
- k6 run load-test.js \
342
- -o xk6-influxdb=http://localhost:8086/k6db \
343
- --tag testName=checkout-flow-test
344
- ```
345
-
346
- 2. **Wait for test to complete, then generate reports:**
347
- ```bash
348
- # Generate HTML report
349
- npx tsx src/cli.ts generate \
350
- --url http://localhost:8086 \
351
- --token your-token \
352
- --org your-org \
353
- --bucket k6db \
354
- --test-name checkout-flow-test \
355
- --start-time "-30m" \
356
- --end-time "now()" \
357
- --format html \
358
- --output reports/checkout-flow-test.html
359
-
360
- # Generate JSON for CI/CD integration
361
- npx tsx src/cli.ts generate \
362
- --url http://localhost:8086 \
363
- --token your-token \
364
- --org your-org \
365
- --bucket k6db \
366
- --test-name checkout-flow-test \
367
- --start-time "-30m" \
368
- --end-time "now()" \
369
- --format json \
370
- --output reports/checkout-flow-test.json
371
-
372
- # Generate CSV for spreadsheet analysis
373
- npx tsx src/cli.ts generate \
374
- --url http://localhost:8086 \
375
- --token your-token \
376
- --org your-org \
377
- --bucket k6db \
378
- --test-name checkout-flow-test \
379
- --start-time "-30m" \
380
- --end-time "now()" \
381
- --format csv \
382
- --output reports/checkout-flow-test.csv
383
-
384
- # Print summary to terminal
385
- npx tsx src/cli.ts generate \
386
- --url http://localhost:8086 \
387
- --token your-token \
388
- --org your-org \
389
- --bucket k6db \
390
- --test-name checkout-flow-test \
391
- --start-time "-30m" \
392
- --end-time "now()" \
393
- --format cli
394
- ```
395
-
396
- 3. **Integrate with CI/CD:**
397
- ```bash
398
- #!/bin/bash
399
- # ci-report.sh
400
-
401
- TEST_NAME="production-api-test"
402
- START_TIME="-1h"
403
- END_TIME="now()"
404
-
405
- # Generate report
406
- npx tsx src/cli.ts generate \
407
- --url $INFLUX_URL \
408
- --token $INFLUX_TOKEN \
409
- --org $INFLUX_ORG \
410
- --bucket $INFLUX_BUCKET \
411
- --scenario $TEST_NAME \
412
- --start-time $START_TIME \
413
- --end-time $END_TIME \
414
- --format json \
415
- --output test-results.json
416
-
417
- # Parse and check thresholds
418
- ERROR_RATE=$(jq '.metrics.requests.errorRate' test-results.json)
419
- if (( $(echo "$ERROR_RATE > 5.0" | bc -l) )); then
420
- echo "Error rate too high: $ERROR_RATE%"
421
- exit 1
422
- fi
423
-
424
- echo "Test passed! Error rate: $ERROR_RATE%"
425
- ```
426
-
427
289
  ## Development
428
290
 
429
291
  ```bash
@@ -441,19 +303,59 @@ npm run build
441
303
 
442
304
  # Development mode
443
305
  npm run dev
306
+
307
+ # Run example
308
+ npm run report:example
444
309
  ```
445
310
 
446
311
  ## Architecture
447
312
 
448
313
  ```
449
314
  src/
450
- ├── influx.ts # InfluxDB client and query logic
451
- ├── reports.ts # Report generation (HTML, CSV, JSON)
452
- ├── cli-reporter.ts # CLI table formatting
453
- ├── cli.ts # Command-line interface
454
- └── index.ts # Library exports
315
+ ├── config.ts # Unified config loading (Influx + Slack)
316
+ ├── data-collector.ts # Data collection from InfluxDB
317
+ ├── influx-client.ts # InfluxDB client
318
+ ├── influx-data-extractor.ts # Metric extraction
319
+ ├── reporters/
320
+ │ ├── cli-reporter.ts # CLI table formatting
321
+ │ ├── json-reporter.ts # JSON export
322
+ │ ├── slack-reporter.ts # Slack integration
323
+ │ └── index.ts # Reporter exports
324
+ ├── cli.ts # Command-line interface
325
+ └── index.ts # Library exports
455
326
  ```
456
327
 
328
+ ## Error Handling
329
+
330
+ ### Missing Configuration
331
+
332
+ If required configuration is missing, the CLI will provide a helpful error message:
333
+
334
+ ```
335
+ Error: InfluxDB configuration is incomplete.
336
+ Set INFLUX_URL, INFLUX_TOKEN, INFLUX_ORG, INFLUX_BUCKET via environment variables or config file.
337
+ ```
338
+
339
+ ### Slack Configuration
340
+
341
+ Slack integration is optional. If not configured, Slack format option will error:
342
+
343
+ ```
344
+ Error: Slack token not configured. Set SLACK_TOKEN environment variable or configure in config file.
345
+ ```
346
+
347
+ Channel is required if Slack is configured:
348
+
349
+ ```
350
+ Error: Slack channel is required and cannot be empty.
351
+ ```
352
+
353
+ ## Prerequisites
354
+
355
+ - Node.js 16+
356
+ - InfluxDB 2.x with k6 test data
357
+ - For Slack integration: Slack workspace with bot token
358
+
457
359
  ## License
458
360
 
459
361
  MIT