Skip to content

Commit

Permalink
feat: timeout setup
Browse files Browse the repository at this point in the history
  • Loading branch information
takehaya committed Mar 19, 2024
1 parent d9a9e91 commit 1d9c797
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 4 deletions.
69 changes: 69 additions & 0 deletions example/non_tested_examples/s5s8pgw-burst-csr.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { check } from 'k6';
import exec from 'k6/execution';
import gtpv2 from 'k6/x/gtpv2';

// sgw:8, concurrent:100 senario (vut:100)
function get_random(n, m){
return Math.floor(Math.random() * (m + 1 - n)) + n;
};

const sgwip_addr_list = [
"127.0.0.1",
"127.0.0.2",
"127.0.0.3",
"127.0.0.4",
"127.0.0.5",
"127.0.0.6",
"127.0.0.7",
"127.0.0.8",
];

const client = {};
export default function (){
const saddr = `${sgwip_addr_list[Math.floor(exec.vu.idInTest%sgwip_addr_list.length)]}:2124`;
if (!(`${saddr}` in client)) {
console.log(saddr);
let opts = {
saddr: saddr,
daddr: "127.0.0.1:2123",
count: 3,
if_type_name: "IFTypeS5S8SGWGTPC"
}
client[saddr] = new gtpv2.K6GTPv2ClientWithConnect(opts);
client[saddr].setTimeout(10);
}
const randkey = get_random(0, 10000)
const teid = 10000 + randkey;
const imsi = 123451234567890 + randkey;
const csr_res = client[saddr].checkSendCreateSessionRequestS5S8(
"127.0.0.1:2123",
{
imsi: `${imsi}`,
msisdn: "123451234567891",
mei: "123451234567891",
mcc: "123",
mnc: "123",
tac: 1,
rat: "EUTRAN",
apn: "ocx",
eci: 1,
pdntype: 1,
epsbearerid: 1,
uplane_ie: {
teid: teid,
},
ambrul: 100000000,
ambrdl: 100000000,
}
)
check (csr_res, {
'csr is success': (res) => true === res,
});
}

export function teardown(data) {
for (let key in client) {
client[key].close();
client[key] = null;
}
}
16 changes: 12 additions & 4 deletions pkg/gtpv2/gtpv2.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,15 @@ type K6GTPv2Client struct {
vu modules.VU
Conn *gtpv2.Conn
sessions *sync.Map
timeout int64
}

// NewClient is the JS constructor for the grpc Client.
func (c *ModuleInstance) NewK6GTPv2Client(call goja.ConstructorCall) *goja.Object {
cli := &K6GTPv2Client{
vu: c.vu,
sessions: &sync.Map{},
timeout: 3,
}
rt := c.vu.Runtime()
return rt.ToValue(cli).ToObject(rt)
Expand All @@ -106,6 +108,7 @@ func (c *ModuleInstance) NewK6GTPv2ClientWithConnect(call goja.ConstructorCall)
cli = &K6GTPv2Client{
vu: c.vu,
sessions: &sync.Map{},
timeout: 3,
}
_, err := cli.Connect(options)
if err != nil {
Expand Down Expand Up @@ -191,6 +194,10 @@ func (c *K6GTPv2Client) Connect(options ConnectionOptions) (bool, error) {
return false, nil
}

func (c *K6GTPv2Client) SetTimeout(timeout int64) {
c.timeout = timeout
}

func GetMessage[PT *T, T any](ctx context.Context, sessions *sync.Map, msgType uint8, seq uint32) (PT, error) {
for {
// TODO reduce cpu usage
Expand Down Expand Up @@ -257,7 +264,7 @@ func (c *K6GTPv2Client) CheckSendEchoRequestWithReturnResponse(daddr string) (bo
}

func (c *K6GTPv2Client) CheckRecvEchoResponse(seq uint32) (bool, error) {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(c.timeout)*time.Second)
defer cancel()
_, err := GetMessage[*message.EchoResponse](ctx, c.sessions, message.MsgTypeEchoResponse, seq)
if err != nil {
Expand All @@ -271,8 +278,9 @@ func (c *K6GTPv2Client) CheckRecvCreateSessionResponse(seq uint32, imsi string)
if err != nil {
return false, err
}
fmt.Println(c.timeout)

ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(c.timeout)*time.Second)
defer cancel()
res, err := GetMessage[*message.CreateSessionResponse](ctx, c.sessions, message.MsgTypeCreateSessionResponse, seq)
if err != nil {
Expand All @@ -293,7 +301,7 @@ func (c *K6GTPv2Client) CheckRecvCreateSessionResponse(seq uint32, imsi string)
}

func (c *K6GTPv2Client) CheckRecvDeleteSessionResponse(seq uint32) (bool, error) {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(c.timeout)*time.Second)
defer cancel()
_, err := GetMessage[*message.DeleteSessionResponse](ctx, c.sessions, message.MsgTypeDeleteSessionResponse, seq)
if err != nil {
Expand All @@ -303,7 +311,7 @@ func (c *K6GTPv2Client) CheckRecvDeleteSessionResponse(seq uint32) (bool, error)
}

func (c *K6GTPv2Client) CheckRecvModifyBearerResponse(seq uint32) (bool, error) {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(c.timeout)*time.Second)
defer cancel()
_, err := GetMessage[*message.ModifyBearerResponse](ctx, c.sessions, message.MsgTypeModifyBearerResponse, seq)
if err != nil {
Expand Down

0 comments on commit 1d9c797

Please sign in to comment.