David Chen
David Chen

Reputation: 15

jmeter vs python requests - different response time

I am running a load testing with Jmeter and python Requests package, but get different result when I try to access the same website.

target website: http://www.somewebsite.com/

request times: 100

avg response time for Jmeter: 1965ms

avg response time for python Requests: 4076ms

I have checked response html content of jmeter and python Requests are the same. So it means they all got the correct response from website. but not sure why it has 2 times difference with each other. Is there anyone know is there any deep reason for that?

the python Requests sample code:

repeat_time = 100
url = 'http://www.somewebsite.com/'
base_time = datetime.datetime.now()
time_cost = base_time
for i in range(repeat_time):
    start_time = datetime.datetime.now()
    r = requests.get(url, headers=headers)
    end_time = datetime.datetime.now()
    print str(r.status_code) + ';time cost: %s' % (end_time - start_time)
    time_cost += (end_time - start_time)
print 'total time: %s' % (time_cost - base_time)
print 'average time: %s' % ((time_cost - base_time).total_seconds() / repeat_time)

Upvotes: 1

Views: 415

Answers (1)

Ian Stapleton Cordasco
Ian Stapleton Cordasco

Reputation: 28707

Without your JMeter code, I can't tell you what the difference is, but let me give you an idea of what's happening in that one call to requests:

  • We create a Session object, plus the urllib3 connection pools we use
  • We do a DNS look-up for 'www.somewebsite.com' which shouldn't be too negatively affecting this request
  • We open a socket for 'www.somewebsite.com:80'
  • We send the request
  • We receive the first byte of the response
  • We determine if the user wanted to stream the body of the response, if not we read all of it and cache it locally.

Keep in mind that the three most intensive parts (usually) are:

  1. DNS lookup (for various reasons, but as I already said, it shouldn't be a problem here)
  2. Socket creation (this is always an expensive operation)
  3. Reading the entirety of the body and caching it locally.

That said, each response object should have an attribute, elapsed which will give you the time to the first byte of the response body. In other words, it will measure the time between when the request is actually sent and when the end of the headers is found.

That might give you far more accurate information than what you're measuring now, which is the time to the last byte of the message.

That said, keep in mind that what you're doing in that for-loop is also invoking the garbage collector a lot:

  1. Create Session, it's adapters, the adapters connection pools, etc.
  2. Create socket
  3. Discard socket
  4. Discard Session
  5. Goto 1

If you create a session once, your script will perform better in general.

Upvotes: 0

Related Questions