performance.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #!/usr/bin/env node
  2. 'use strict'
  3. const fs = require('fs')
  4. const path = require('path')
  5. const check = require('check-types')
  6. const bfj = require('../src')
  7. const inPath = getDataPath('.json');
  8. let time = process.hrtime()
  9. if (process.argv.length === 4) {
  10. const stuff = []
  11. const stream = bfj.match(fs.createReadStream(inPath), process.argv[3])
  12. stream.on('data', thing => stuff.push(thing))
  13. stream.on('end', () => {
  14. reportTime()
  15. console.log('hooray!', stuff.length)
  16. fs.writeFileSync(getDataPath('-result.ndjson'), stuff.map(s => JSON.stringify(s)).join('\n'), {
  17. encoding: 'utf8',
  18. })
  19. process.exit(0)
  20. })
  21. stream.on('error', error => {
  22. console.error('error!', error.stack)
  23. process.exit(1)
  24. })
  25. stream.on('dataError', error => {
  26. console.error('dataError!', error.stack)
  27. process.exit(2)
  28. })
  29. } else {
  30. console.log('reading json')
  31. bfj.read(inPath)
  32. .then(data => {
  33. reportTime()
  34. console.log('writing json')
  35. return bfj.write(getDataPath('-result.json'), data)
  36. })
  37. .then(() => done('succeeded'))
  38. .catch(error => done(error.stack, 1))
  39. }
  40. function getDataPath (suffix) {
  41. return path.resolve(__dirname, process.argv[2] + suffix)
  42. }
  43. function reportTime () {
  44. let interimTime = process.hrtime(time)
  45. console.log('%d seconds and %d nanoseconds', interimTime[0], interimTime[1])
  46. time = process.hrtime()
  47. }
  48. function done (message, code) {
  49. reportTime()
  50. console.log(message)
  51. process.exit(code)
  52. }