microsoft / playwright

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.
https://playwright.dev
Apache License 2.0
66.2k stars 3.62k forks source link

[Question] Is there a way to return an api request response time? #19621

Open BekaOkromchedlidze opened 1 year ago

BekaOkromchedlidze commented 1 year ago

I am using the request fixture for API testing and I am just wondering is there a way to find what the api response time was?

pavelfeldman commented 1 year ago

I don't think we have a feature like this. But request API is not done through the browser, so we don't have access to the detailed timing anyways.

klepik1990 commented 1 year ago

Did you see this info? Maybe it can helps you https://playwright.dev/docs/api/class-request#request-timing

BekaOkromchedlidze commented 1 year ago

@pavelfeldman I think this would be a very nice feature to have for api testing. It would be used to test performance sanity to make sure we get a response in an acceptable time.

@klepik1990 this is for the page fixture. We don't have this option for the request fixture

brunojbarros commented 1 year ago

It's not completely accurate, but you can do something like:

const requestDate = new Date();

// your request: await request ...

const responseDate = new Date();

console.log(responseDate - requestDate);

I agree that this feature will be very useful 😄

srieas commented 1 year ago

+1

fayasfb commented 1 year ago

i agree this feature should be implemented in request fixture , as we need to do the performance of the api

brunojbarros commented 1 year ago

Another workaround is to use testStep.duration: https://playwright.dev/docs/api/class-teststep#test-step-duration

shashwatahalder01 commented 10 months ago

Feature ought to be integrated within the request fixture.

Skn0tt commented 4 weeks ago

Hey folks! Could you share a little more about your usecases? Specifically I'm curious about the granularity of timing you need. Do you care about the full roundtrip time, or also about time to first byte or time taken by a potential TLS handshake?

brunojbarros commented 3 weeks ago

Hey folks! Could you share a little more about your usecases? Specifically I'm curious about the granularity of timing you need. Do you care about the full roundtrip time, or also about time to first byte or time taken by a potential TLS handshake?

It would be ideal to have something similar to what k6 has: https://grafana.com/docs/k6/latest/examples/get-timings-for-an-http-metric/

At least just the duration.

Skn0tt commented 1 week ago

@brunojbarros thanks for getting back to me. I'm curious, are you performing load testing? Could you share a little bit more about your usecase?

brunojbarros commented 1 week ago

@brunojbarros thanks for getting back to me. I'm curious, are you performing load testing? Could you share a little bit more about your usecase?

Not really.

Currently, we do not have robust performance testing with any tool, but we want to know how APIs perform under low load (we have over 500 API tests with 8 workers), so we take the opportunity to find out when we run automated tests expecting that the time stays under a second.

Skn0tt commented 1 week ago

Alright, thanks! Playwright doesn't have elaborate performance testing tooling currently, but I think in your specific case you could write a wrapper method on top of APIRequestContext#fetch that records the time before and after the request and then do soft assertion on the duration.