Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON.parse SyntaxError when running a duration test #358

Closed
gewfy opened this issue Nov 2, 2017 · 4 comments
Closed

JSON.parse SyntaxError when running a duration test #358

gewfy opened this issue Nov 2, 2017 · 4 comments

Comments

@gewfy
Copy link

gewfy commented Nov 2, 2017

This issue is related to #339 and #340
But I've reproduced this without using stages or rambda. The error happens if you set any form of duration e.g. k6 run --duration 5s test.js

This tiny test:

import { check } from 'k6';
import http from 'k6/http';

export default function () {
  const res = http.get('https://api.dev.peachworks.com/v1/products');

  check(res, {
    'status is 200': res => res.status === 200
  });

  const products = JSON.parse(res.body).results;

  check(products, {
    'products is not empty': products => products.length > 0
  });
}

The above test produces the SyntaxError problem but no check errors:
image

@ragnarlonn
Copy link

@liclac @gewfy This issue appears at the end of the test, and if you use a script that either idles the VUs at the end, or sleeps after the last HTTP request of the iteration, the bug is not triggered (e.g. running a 5-second test and not issuing any HTTP requests past the first 4 seconds of the test will not trigger it).

This makes it seem like it may be a matter of a faulty teardown order somewhere, in a code path that only happens when you use --duration (--iterations n does not trigger the bug).

This the smallest script that will trigger the EOF error (k6 run --duration 5s script.js):

import http from 'k6/http';

export default function () {
  const res = http.get('https://api.dev.peachworks.com/v1/products');
  const products = JSON.parse(res.body).results;
}

Now, if I add a sleep anywhere after the HTTP request the error disappears:

import http from 'k6/http';
import { sleep } from "k6";

export default function () {
  const res = http.get('https://api.dev.peachworks.com/v1/products');
  // sleep(1); // error goes away
  const products = JSON.parse(res.body).results;
  sleep(1); // error goes away
}

...but if I move the sleep to before the HTTP request the error is still triggered:

import http from 'k6/http';
import { sleep } from "k6";

export default function () {
  sleep(1); // error still there
  const res = http.get('https://api.dev.peachworks.com/v1/products');
  const products = JSON.parse(res.body).results;
}

So, my conclusion is that the error is triggered when duration ends while k6 either still has HTTP requests to make (as in when there is a sleep before the HTTP request), or is in the process of making a request (as in when there is no sleep at all, which means the HTTP request is the likely place the VU code will be when duration ends - because it is the longest-running function call in the script)

Wild guess: could it be something introduced with the new buffer pool code -
b002f2c ?

@liclac
Copy link
Contributor

liclac commented Nov 15, 2017

This looks like an issue wrt how VUs are scheduled, I'll do some poking around.

@liclac liclac closed this as completed in 682a341 Nov 15, 2017
@elimatripathy
Copy link

elimatripathy commented Jan 28, 2023

I am using k6 to check performance for one of our endpoints and the endpoint requires using access token so the script fails very intermittently at line var obj_accesstoken = JSON.parse(res_accesstoken.body, sleep(1.0));
with error as

ERRO[0004] SyntaxError: EOF
running at parse (native)
default at file:///Users/tripate/Documents/k6_perf_POC/notificationServiceRequest.js:32:33(22)
at native executor=constant-vus scenario=default source=stacktrace

I tried adding sleep after http call and after json parse still the exception happens . Below is my code
var payload = { email: username, password: password }; var res_accesstoken = http.post(url, JSON.stringify(payload), { headers: { "Content-Type": "application/json" } }, sleep(1.0)); //console.log("Endpoint response for token is = ", res_accesstoken); try { var obj_accesstoken = JSON.parse(res_accesstoken.body, sleep(1.0)); } catch(err) { //console.error(res_accesstoken.body could not be parsed as JSON. Received:\n${res_accesstoken.body}); var res_accesstoken = http.post(url, JSON.stringify(payload), { headers: { "Content-Type": "application/json" } }, sleep(1.0)); var obj_accesstoken = JSON.parse(res_accesstoken.body, sleep(1.0)); } var token = obj_accesstoken["data"]["access_token"];
I don't understand if this is okay because my checks at the end are working expected and no threshold error is shown to user

@na--
Copy link
Member

na-- commented Jan 30, 2023

@elimatripathy, please do not comment in issues that have been closed for more than 5 years... Moreover, the GitHub issues are not the place to ask such questions, please use the community forum at https://community.k6.io/

@grafana grafana locked as resolved and limited conversation to collaborators Jan 30, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants