11import { Counter , Trend } from "k6/metrics" ;
22
3- const metrics = {
4- responseTime : new Trend ( "response_time" , true ) ,
5- notSent : new Counter ( "not_sent" ) ,
6- noResponse : new Counter ( "no_response" )
7- } ;
8- const metricsData = {
9- sentAt : null ,
10- respondedAt : null
11- } ;
3+ const responseTime = new Trend ( "response_time" , true ) ;
4+ const notSent = new Counter ( "not_sent" ) ;
5+ const noResponse = new Counter ( "no_response" ) ;
6+ let sentAt , respondedAt ;
127
138function onMessage ( socket , parsedData ) {
149 if ( parsedData . type === "SYSTEM_OUT" && parsedData . value === "What's your name?" ) {
@@ -17,24 +12,21 @@ function onMessage(socket, parsedData) {
1712 message : "Ben"
1813 } ) ;
1914 socket . send ( message ) ;
20- metricData . sentAt = Date . now ( ) ;
15+ sentAt = Date . now ( ) ;
2116 }
2217
2318 if ( parsedData . type === "SYSTEM_OUT" && parsedData . value === "Hello Ben!" ) {
24- metricData . respondedAt = Date . now ( ) ;
19+ respondedAt = Date . now ( ) ;
2520 }
2621}
2722
2823function onClose ( ) {
29- const { responseTime, notSent, noResponse} = metrics ;
30- const { sentAt, respondedAt} = metricData ;
31-
32- if ( ! metricData . sentAt ) {
33- metrics . notSent . add ( 1 ) ;
34- } else if ( ! metricData . respondedAt ) {
35- metrics . noResponse . add ( 1 ) ;
24+ if ( ! sentAt ) {
25+ notSent . add ( 1 ) ;
26+ } else if ( ! respondedAt ) {
27+ noResponse . add ( 1 ) ;
3628 } else {
37- metrics . responseTime . add ( respondedAt - sentAt ) ;
29+ responseTime . add ( respondedAt - sentAt ) ;
3830 }
3931}
4032
0 commit comments