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

Avoid to log if the log is not set #776

Merged
merged 1 commit into from
Feb 28, 2018

Conversation

allevo
Copy link
Member

@allevo allevo commented Feb 11, 2018

loggerUtils.onResponseCallback logs only if there's a logger

Bench:

this branch:

[1] Running 5s test @ http://localhost:3000/
[1] 100 connections with 10 pipelining factor
[1] 
[0] server listening on 3000
[1] Stat         Avg     Stdev  Max     
[1] Latency (ms) 8.3     27.44  511.66  
[1] Req/Sec      11710   2804.9 13896   
[1] Bytes/Sec    1.75 MB 411 kB 2.07 MB 
[1] 
[1] 59k requests in 5s, 8.72 MB read
[1] 112 errors (0 timeouts)

master:

[1] Running 5s test @ http://localhost:3000/
[1] 100 connections with 10 pipelining factor
[1] 
[0] server listening on 3000
[1] Stat         Avg     Stdev   Max     
[1] Latency (ms) 8.78    29.27   340.12  
[1] Req/Sec      11079.6 3750.94 14926   
[1] Bytes/Sec    1.64 MB 535 kB  2.22 MB 
[1] 
[1] 55k requests in 5s, 8.25 MB read
[1] 130 errors (0 timeouts)

Can someone else confirm this improvement?

Checklist

  • run npm run test and npm run benchmark
  • tests and/or benchmarks are included
  • documentation is changed or added
  • commit message and code follows Code of conduct

fastify.js Outdated
@@ -26,6 +26,8 @@ const runHooks = require('./lib/hookRunner')

const DEFAULT_JSON_BODY_LIMIT = 1024 * 1024 // 1 MiB

function noop (anError, res) { }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think there's any reason for a no-op to have parameters.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Copy link
Member

@delvedor delvedor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we add the isAbstract check also here?

res._startTime = now()

@delvedor delvedor added enhancement performances Everything related to performances labels Feb 13, 2018
@allevo
Copy link
Member Author

allevo commented Feb 13, 2018

@delvedor yeah, _startTime should be considered as a private variable.

fastify.js Outdated
@@ -199,7 +203,9 @@ function build (options) {

req.log.info({ req }, 'incoming request')

res._startTime = now()
if (!isAbstract) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use isAbstract === false it should be faster.
Can you also paste the new benchmarks?

@allevo
Copy link
Member Author

allevo commented Feb 13, 2018

New bench:
master

754k requests in 50s, 112 MB read

this branch

787k requests in 50s, 117 MB read

I don't know if this bench are accurate or not. Is there someone that is able to confirm it?

@allevo
Copy link
Member Author

allevo commented Feb 13, 2018

Last commit removes two listener: if the logger is abstract and the current context has no onResponse hooks, those lines can be skipped

fastify/fastify.js

Lines 203 to 204 in 8aa6b1d

res.on('finish', onResFinished)
res.on('error', onResFinished)

fastify.js Outdated
@@ -206,8 +206,10 @@ function build (options) {
if (isAbstract === false) {
res._startTime = now()
}
res.on('finish', onResFinished)
res.on('error', onResFinished)
if (isAbstract === false || context.onResponse !== null) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can do the same for res._context = context :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

fastify.js Outdated
res.on('error', onResFinished)
if (isAbstract === false) {
res._startTime = now()
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might create some deopts and prevent some inlining if logging is indeed enable (which will be the majority of cases). Can you test if the throughput drops with logging enabled?

I fear that might be the case, as I've seen this happening when properties are added within an if.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't seen any drops.

NB: In the whole application isAbstract is a constant

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

fastify.js Outdated
@@ -40,12 +42,14 @@ function build (options) {
}

var log
var isAbstract = false
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you make this variable name more precise? hasLogger is a good one for example.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

fastify.js Outdated
res.on('finish', onResFinished)
res.on('error', onResFinished)
if (isAbstract === false) {
res._startTime = now()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can safely move res._startTime = now() inside the if below, since it is used only inside the onResponseCallback function.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

again, I would not touch this. The chances to have a change of shape is very high.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your suggestion is to remove all the if checks inside this function?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no. Adding a property within an if is a clear strategy to cause deopts.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@mcollina
Copy link
Member

@allevo any update on this?

@allevo
Copy link
Member Author

allevo commented Feb 26, 2018 via email

@allevo
Copy link
Member Author

allevo commented Feb 27, 2018

rebased

@mcollina
Copy link
Member

mcollina commented Feb 28, 2018

Can you please check if the improvement is still there?

@mcollina
Copy link
Member

A quick explanation on the Date.now() problem:

schermata 2018-02-28 alle 14 31 58

@allevo
Copy link
Member Author

allevo commented Feb 28, 2018

@mcollina Removing the call to now, we should remove that 5.8% on the top. Am I right?

Copy link
Member

@delvedor delvedor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@mcollina mcollina merged commit f545acc into fastify:master Feb 28, 2018
mcollina added a commit that referenced this pull request Mar 4, 2018
@mcollina mcollina mentioned this pull request Mar 4, 2018
3 tasks
mcollina added a commit that referenced this pull request Mar 4, 2018
@github-actions
Copy link

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 18, 2022
@Eomm Eomm added the semver-minor Issue or PR that should land as semver minor label Apr 1, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
performances Everything related to performances semver-minor Issue or PR that should land as semver minor
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants