DigitalMag

file watch mark

Feb 19th, 2020
358
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const fs = require("fs");
  2.  
  3. const performance = require('perf_hooks').performance;
  4.  
  5. var flname = "tst.txt"
  6.  
  7. var start;
  8. var i = 0;
  9. var aver = [];
  10.  
  11. fs.watchFile(flname, { interval: 10 }, (curr, prev) => {
  12.    
  13.     let perf = performance.now() - start;  
  14.    
  15.     console.timeEnd('watch');  
  16.    
  17.     console.log(perf)
  18.     aver.push(perf);
  19.    
  20.     var sum = 0;
  21.     for(var c = 0;c<aver.length;c++)sum+=aver[c];
  22.    
  23.     console.log('average: ' + sum/aver.length + ' by ' + aver.length)
  24.  
  25.     // console.log(`${flname} file Changed`);
  26.    
  27.     console.log('--------------------')
  28.    
  29. });
  30.  
  31. setInterval(function(){
  32.    
  33.     console.time('writeFileSync');
  34.  
  35.     fs.writeFileSync(flname, 'test - ' + ++i)
  36.  
  37.     console.timeEnd("writeFileSync");  
  38.    
  39.    
  40.     start = performance.now()
  41.    
  42.     console.time('watch'); 
  43.        
  44.    
  45. }, 1500);
Add Comment
Please, Sign In to add comment