1
1
import ws from "k6/ws" ;
2
2
import { check } from "k6" ;
3
+ import { Trend } from "k6/metrics" ;
3
4
4
5
const numShapesPerClient = __ENV . SHAPES_PER_CLIENT || 1 ;
5
6
const roomID = __ENV . ROOM_ID ;
6
7
8
+ // e.g.: wss://replidraw.replicache.workers.dev/connect or ws://[::1]:8787/connect
9
+ // Note: it looks like wrangler only listens on ipv6!
10
+ // https://github.com/cloudflare/wrangler/issues/1198#issuecomment-1204690449
11
+ const socketBaseURL = __ENV . SOCKET_BASE_URL || "ws://[::1]:8787/connect" ;
12
+
7
13
if ( ! roomID ) {
8
14
throw new Error ( "Must specify a ROOM_ID env variable" ) ;
9
15
}
@@ -12,22 +18,29 @@ function randomID() {
12
18
return Math . random ( ) . toString ( 36 ) . substring ( 2 ) ;
13
19
}
14
20
15
- let lastMutationID = 0 ;
21
+ const sentMutations = [ ] ;
22
+ const receivedPokes = [ ] ;
23
+ const pokeWaitTrend = new Trend ( "poke_wait_time" ) ;
16
24
17
25
function sendMutation ( socket , clientID , name , args ) {
26
+ const lastMutation = sentMutations [ sentMutations . length - 1 ] ;
27
+ const lmid = lastMutation ? lastMutation . id : 0 ;
28
+ const id = lmid + 1 ;
29
+ const ts = Date . now ( ) ;
18
30
const mutation = {
19
- id : ++ lastMutationID ,
31
+ id,
20
32
name,
21
33
args,
22
- timestamp : Date . now ( ) ,
34
+ timestamp : ts ,
23
35
} ;
24
36
const pushBody = {
25
37
clientID,
26
38
mutations : [ mutation ] ,
27
39
pushVersion : 1 ,
28
40
schemaVersion : "" ,
29
- timestamp : Date . now ( ) ,
41
+ timestamp : ts ,
30
42
} ;
43
+ sentMutations . push ( { id, ts } ) ;
31
44
const msg = JSON . stringify ( [ "push" , pushBody ] ) ;
32
45
console . info ( "sending" , msg ) ;
33
46
socket . send ( msg ) ;
@@ -64,9 +77,6 @@ export default function () {
64
77
const clientID = randomID ( ) ;
65
78
const userID = randomID ( ) ;
66
79
67
- // Note: it looks like wrangler only listens on ipv6!
68
- // https://github.com/cloudflare/wrangler/issues/1198#issuecomment-1204690449
69
- const socketBaseURL = "ws://[::1]:8787/connect" ;
70
80
const url = `${ socketBaseURL } ?clientID=${ clientID } &roomID=${ roomID } &baseCookie=0&lmid=0&ts=${ Date . now ( ) } ` ;
71
81
const params = {
72
82
headers : {
@@ -92,6 +102,25 @@ export default function () {
92
102
socket . setInterval ( ( ) => {
93
103
scanShapes ( socket , clientID , numShapesPerClient ) ;
94
104
} , 16 ) ;
105
+ } else {
106
+ const lastPoke = receivedPokes [ receivedPokes . length - 1 ] ;
107
+ const lastLMID = lastPoke ? lastPoke . id : - 1 ;
108
+ const id = body . lastMutationID ;
109
+ const ts = Date . now ( ) ;
110
+ for ( const m of sentMutations . reverse ( ) ) {
111
+ if ( m . id <= id ) {
112
+ pokeWaitTrend . add ( ts - m . ts ) ;
113
+ break ;
114
+ }
115
+ }
116
+ check ( type , {
117
+ "message type was poke" : ( res ) => res === "poke" ,
118
+ } ) ;
119
+ check ( body , {
120
+ "body is an object" : ( res ) => typeof res === "object" ,
121
+ "received correct lmid" : ( res ) => id >= lastLMID + 1 ,
122
+ } ) ;
123
+ receivedPokes . push ( { id, ts : Date . now ( ) } ) ;
95
124
}
96
125
} ) ;
97
126
0 commit comments