File size: 963 Bytes
23ac194
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
'use strict'

/**
 * Globals for benchmark.js
 */
global.proxyaddr = require('..')
global.createReq = createReq

/**
 * Module dependencies.
 */
const benchmark = require('benchmark')
const benchmarks = require('beautify-benchmark')

const suite = new benchmark.Suite()

suite.add({
  name: 're-compiling',
  minSamples: 100,
  fn: 'proxyaddr(req, "loopback")',
  setup: 'req = createReq("127.0.0.1", "10.0.0.1")'
})

suite.add({
  name: 'pre-compiling',
  minSamples: 100,
  fn: 'proxyaddr(req, trust)',
  setup: 'req = createReq("127.0.0.1", "10.0.0.1"); trust = proxyaddr.compile("loopback")'
})

suite.on('cycle', function onCycle (event) {
  benchmarks.add(event.target)
})

suite.on('complete', function onComplete () {
  benchmarks.log()
})

suite.run({ async: false })

function createReq (socketAddr, forwardedFor) {
  return {
    socket: {
      remoteAddress: socketAddr
    },
    headers: {
      'x-forwarded-for': (forwardedFor || '')
    }
  }
}