11import http from "k6/http" ;
22import { check } from "k6" ;
33
4- const PAYLOAD = JSON . stringify ( {
5- n : "pageview" ,
6- u : "http://dummy.site/some-page" ,
7- d : "dummy.site" ,
8- r : null ,
9- w : 1666 ,
10- } ) ;
4+ function payload ( pages ) {
5+ return JSON . stringify ( {
6+ n : "pageview" ,
7+ u : pages [ Math . floor ( Math . random ( ) * pages . length ) ] ,
8+ d : "dummy2.site" ,
9+ r : null ,
10+ w : 1666 ,
11+ } ) ;
12+ }
1113
1214function newParams ( ) {
1315 const ip =
@@ -29,24 +31,60 @@ function newParams() {
2931 } ;
3032}
3133
34+ function getClient ( clients ) {
35+ return clients [ Math . floor ( Math . random ( ) * clients . length ) ] ;
36+ }
37+
38+ function getClientSliding ( clients , start , runtime , windowSize ) {
39+ const now = Date . now ( ) ;
40+ const delta = windowSize + Math . floor ( ( now - start ) / 1000 ) ;
41+ const timeline = runtime + 2 * windowSize ;
42+
43+ const lowerBound = Math . max ( delta - windowSize , 0 ) ;
44+ const upperBound = Math . min ( lowerBound + 2 * windowSize , timeline ) ;
45+
46+ const lowerIndex = Math . floor ( ( lowerBound / timeline ) * clients . length ) ;
47+ const upperIndex = Math . floor ( ( upperBound / timeline ) * clients . length ) ;
48+ const indexWindow = upperIndex - lowerIndex ;
49+
50+ return clients [ lowerIndex + Math . floor ( Math . random ( ) * indexWindow ) ] ;
51+ }
52+
53+ export function setup ( ) {
54+ const start = Date . now ( ) ;
55+
56+ const clients = Array ( 2000 )
57+ . fill ( 0 )
58+ . map ( ( _ ) => newParams ( ) ) ;
59+
60+ const pages = Array ( 100 )
61+ . fill ( 0 )
62+ . map ( ( _ , i ) => `http://dummy.site/some-page-${ i } ` ) ;
63+
64+ return { clients : clients , pages : pages , start : start } ;
65+ }
66+
3267export const options = {
3368 scenarios : {
3469 constant_rps : {
3570 executor : "constant-arrival-rate" ,
36- rate : 12000 ,
71+ rate : 50 ,
3772 timeUnit : "1s" ,
38- duration : "15m " ,
39- preAllocatedVUs : 10000 ,
73+ duration : "10m " ,
74+ preAllocatedVUs : 0 ,
4075 maxVUs : 30000 ,
4176 } ,
4277 } ,
4378} ;
4479
45- export default function ( ) {
80+ export default function ( data ) {
81+ // const client = getClient(data.clients);
82+ const client = getClientSliding ( data . clients , data . start , 600 , 60 ) ;
83+
4684 const res = http . post (
47- "http://localhost:8000 /api/event" ,
48- PAYLOAD ,
49- newParams ( ) ,
85+ "http://localhost:4000 /api/event" ,
86+ payload ( data . pages ) ,
87+ client ,
5088 ) ;
5189
5290 check ( res , {
0 commit comments