title: How to measure Event loop blocking in Nodejs?
body: <p>I can't get real an event loop blocking time. I have searched in Google answers (<a href="https://stackoverflow.com/questions/28563354/how-to-detect-and-measure-event-loop-blocking-in-node-js">here</a>), but they didn't help for me. I got different results. </p>
  
  <p>I have created Node/Express app. And try to detect event loop blocking with different tools. I used hrtime, pm2, <a href="https://www.npmjs.com/package/blocked-at" rel="nofollow noreferrer">blocked_at</a>.</p>
  
  <p><strong>1 test:</strong></p>
  
  <p>server.js</p>
  
  <pre><code>require('./routes')(app, passport, mongoData)
  </code></pre>
  
  <p>routes/index.js </p>
  
  <pre><code>router
    .get('/articles/:articleId(\\d+)', (req, res, next) =&gt; { 
        const blockedAt = require('blocked-at')
        blockedAt((time, stack) =&gt; {
           console.log(`Blocked for ${time}ms, operation started here:`, stack)
              },  {threshold:12})
  
        // my blocking script
        for (let i = 0; i &lt;= 1000000000; i++) {
            let z = 10000 / Math.random()         
        }
  
        let ArticleController = require(path + 'app/controllers/ArticleController')
        let articleController = new ArticleController()
        articleController.index(req, res, next)
      })
  </code></pre>
  
  <p>I got:</p>
  
  <blockquote>
    <p>Blocked for 15.5994921875ms, operation started here: [ '    at ',
      '    at ArticleService.getArticle (/app/services/article/ArticleService.js:79:44)' ]
    Blocked for 14.0350537109375ms, operation started here: [ '    at Promise.then ()',
      '    at ExpressHandlebars.render (node_modules/express-handlebars/lib/express-handlebars.js:157:8)',
      '    at ExpressHandlebars. (node_modules/express-handlebars/lib/express-handlebars.js:226:29)' ]</p>
  </blockquote>
  
  <p>But nothing about my blocking script!</p>
  
  <p><strong>2 test:</strong></p>
  
  <p>With pm2:</p>
  
  <ul>
  <li>Event Loop Latency - 0.56ms</li>
  <li>Event Loop Latency p95 - 4.5ms</li>
  </ul>
  
  <p>After remove my blocking script I get same results.</p>
  
  <p><strong>3 test:</strong></p>
  
  <p>With hrtime I measure inside ArticleController.index. Index method loads 3 services in async mode. There are a lot of I/O operations, and there works worker_threads. Some code created into setImmediate. </p>
  
  <p>inside into index:</p>
  
  <pre><code>let hrstart = process.hrtime()
  
  // there works all my services 
  //...
  
  let hrend = process.hrtime(hrstart)
   console.info('Execution time (hr): %ds ir ms: %dms', hrend[0], hrend[1] / 1000000)
  
  res.render('home', data)
  </code></pre>
  
  <p>There I got 1s, 233ms. I got big time, but I confused - because there all operations work in async mode, Event loop ain't blocking? </p>
  
  <p>How I can measure Event loop blocks?</p>
  
  <p>I expect the output: "event loop blocking in routes/index.js:12 for 5000ms", but actual output doesn't catch my blocking script.</p>