11import http from 'k6/http' ;
2- import { textSummary } from 'https://jslib.k6.io/k6-summary/0.0.2/index.js' ;
3- import { check , sleep , fail } from 'k6' ;
4- import exec from 'k6/execution' ;
5- import { Trend } from 'k6/metrics' ;
2+ import { Trend } from 'k6/metrics' ;
63
74const HTTP = {
85 withZen : {
@@ -16,59 +13,58 @@ const HTTP = {
1613}
1714
1815function test ( name , fn ) {
19- const duration = tests [ name ] . duration ;
20- const overhead = tests [ name ] . overhead ;
21-
2216 const withZen = fn ( HTTP . withZen ) ;
2317 const withoutZen = fn ( HTTP . withoutZen ) ;
18+ const timeWithZen = withZen . timings . duration ;
19+ const timeWithoutZen = withoutZen . timings . duration ;
2420
25- const timeWithZen = withZen . timings . duration ,
26- timeWithoutZen = withoutZen . timings . duration ;
27-
28- duration . add ( timeWithZen - timeWithoutZen ) ;
21+ tests [ name ] . delta . add ( timeWithZen - timeWithoutZen ) ;
22+ tests [ name ] . overhead . add ( 100 * ( timeWithZen - timeWithoutZen ) / timeWithoutZen )
2923
30- const ratio = withZen . timings . duration / withoutZen . timings . duration ;
31- overhead . add ( 100 * ( timeWithZen - timeWithoutZen ) / timeWithoutZen )
24+ tests [ name ] . with_zen . add ( timeWithZen ) ;
25+ tests [ name ] . without_zen . add ( timeWithoutZen ) ;
3226}
3327
34- const defaultHeaders = {
35- "User-Agent" : "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36" ,
36- } ;
28+ function buildTestTrends ( prefix ) {
29+ return {
30+ delta : new Trend ( `${ prefix } _delta` ) ,
31+ with_zen : new Trend ( `${ prefix } _with_zen` ) ,
32+ without_zen : new Trend ( `${ prefix } _without_zen` ) ,
33+ overhead : new Trend ( `${ prefix } _overhead` )
34+ } ;
35+ }
3736
3837const tests = {
39- test_post_page_with_json_body : {
40- duration : new Trend ( "test_post_page_with_json_body" ) ,
41- overhead : new Trend ( "test_overhead_with_json_body" )
42- } ,
43- test_get_page_without_attack : {
44- duration : new Trend ( "test_get_page_without_attack" ) ,
45- overhead : new Trend ( "test_overhead_without_attack" )
46- } ,
47- test_get_page_with_sql_injection : {
48- duration : new Trend ( "test_get_page_with_sql_injection" ) ,
49- overhead : new Trend ( "test_overhead_with_sql_injection" ) ,
50- }
38+ test_post_page_with_json_body : buildTestTrends ( "test_post_page_with_json_body" ) ,
39+ test_get_page_without_attack : buildTestTrends ( "test_get_page_without_attack" ) ,
40+ test_get_page_with_sql_injection : buildTestTrends ( "test_get_page_with_sql_injection" )
5141}
5242export const options = {
5343 vus : 1 , // Number of virtual users
5444 iterations : 200 ,
5545 thresholds : {
56- test_post_page_with_json_body : [ "med<10" ] ,
57- test_get_page_without_attack : [ "med<10" ] ,
58- test_get_page_with_sql_injection : [ "med<10" ] ,
46+ http_req_failed : [ 'rate==0' ] , // we are marking the attacks as expected, so we should have no errors
47+ test_post_page_with_json_body_delta : [ "med<10" ] ,
48+ test_get_page_without_attack_delta : [ "med<10" ] ,
49+ test_get_page_with_sql_injection_delta : [ "med<10" ] ,
5950 }
6051} ;
6152
62- const expectAttack = http . expectedStatuses ( 500 ) ;
53+ const expectAttack = http . expectedStatuses ( 200 , 500 ) ;
6354
6455export default function ( ) {
6556 test ( "test_post_page_with_json_body" ,
6657 ( http ) => http . post ( "/cats" , JSON . stringify ( { cat : { name : "Féline Dion" } } ) , {
67- headers : { "Content-Type" : "application/json" }
58+ headers : {
59+ "Content-Type" : "application/json" ,
60+ "Accept" : "application/json"
61+ }
6862 } )
6963 )
64+
7065 test ( "test_get_page_without_attack" , ( http ) => http . get ( "/cats" ) )
66+
7167 test ( "test_get_page_with_sql_injection" , ( http ) =>
72- http . get ( "/cats/1'%20OR%20''='" , { responseCallback : expectAttack } )
68+ http . get ( "/cats/1'%20OR%20''='" , { responseCallback : expectAttack } )
7369 )
7470}
0 commit comments