benchmark.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. /*
  2. * Created by Wu Jian Ping on - 2022/07/22.
  3. */
  4. const Benchmark = require('benchmark')
  5. const path = require('path')
  6. const Searcher = require('..')
  7. const dbPath = path.join(__dirname, '..', '..', '..', 'data', 'ip2region.xdb')
  8. const buffer = Searcher.loadContentFromFile(dbPath)
  9. const searcher1 = Searcher.newWithBuffer(buffer)
  10. const vectorIndex = Searcher.loadVectorIndexFromFile(dbPath)
  11. const searcher2 = Searcher.newWithVectorIndex(dbPath, vectorIndex)
  12. const searcher3 = Searcher.newWithFileOnly(dbPath)
  13. const suite = new Benchmark.Suite()
  14. suite
  15. .add('#缓存整个xdb数据【搜索218.4.167.70】', async () => {
  16. const ip = '218.4.167.70'
  17. return searcher1.search(ip)
  18. })
  19. .add('#缓存VectorIndex索引【搜索218.4.167.70】', async () => {
  20. const ip = '218.4.167.70'
  21. return searcher2.search(ip)
  22. })
  23. .add('#完全基于文件的查询【搜索218.4.167.70】', async () => {
  24. const ip = '218.4.167.70'
  25. return searcher3.search(ip)
  26. })
  27. .on('cycle', function (event) {
  28. console.log(String(event.target)) // eslint-disable-line
  29. })
  30. .on('complete', function () {
  31. console.log('Fastest is ' + this.filter('fastest').map('name')) // eslint-disable-line
  32. })
  33. .run({ async: true })