00-file-stress.test.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. 'use strict';
  2. /*
  3. * file-stress.test.js: Tests for stressing File transport: volume, ambient event loop lag.
  4. *
  5. * (C) 2016 Charlie Robbins
  6. * MIT LICENSE
  7. *
  8. */
  9. const fs = require('fs');
  10. const os = require('os');
  11. const path = require('path');
  12. const assume = require('assume');
  13. const helpers = require('../helpers');
  14. const split = require('split2');
  15. const winston = require('../../lib/winston');
  16. describe('File (stress)', function () {
  17. this.timeout(30 * 1000);
  18. const logPath = path.resolve(__dirname, '../fixtures/logs/file-stress-test.log');
  19. beforeEach(function () {
  20. try {
  21. fs.unlinkSync(logPath);
  22. } catch (ex) {
  23. if (ex && ex.code !== 'ENOENT') { return done(ex); }
  24. }
  25. });
  26. it('should handle a high volume of writes', function (done) {
  27. const logger = winston.createLogger({
  28. transports: [new winston.transports.File({
  29. filename: logPath
  30. })]
  31. });
  32. const counters = {
  33. write: 0,
  34. read: 0
  35. };
  36. const interval = setInterval(function () {
  37. logger.info(++counters.write);
  38. }, 0);
  39. setTimeout(function () {
  40. clearInterval(interval);
  41. helpers.tryRead(logPath)
  42. .on('error', function (err) {
  43. assume(err).false();
  44. logger.close();
  45. done();
  46. })
  47. .pipe(split())
  48. .on('data', function (d) {
  49. const json = JSON.parse(d);
  50. assume(json.level).equal('info');
  51. assume(json.message).equal(++counters.read);
  52. })
  53. .on('end', function () {
  54. assume(counters.write).equal(counters.read);
  55. logger.close();
  56. done();
  57. });
  58. }, 10000);
  59. });
  60. it('should handle a high volume of large writes', function (done) {
  61. const logger = winston.createLogger({
  62. transports: [new winston.transports.File({
  63. filename: logPath
  64. })]
  65. });
  66. const counters = {
  67. write: 0,
  68. read: 0
  69. };
  70. const interval = setInterval(function () {
  71. const msg = {
  72. counter: ++counters.write,
  73. message: 'a'.repeat(16384 - os.EOL.length - 1)
  74. };
  75. logger.info(msg);
  76. }, 0);
  77. setTimeout(function () {
  78. clearInterval(interval);
  79. helpers.tryRead(logPath)
  80. .on('error', function (err) {
  81. assume(err).false();
  82. logger.close();
  83. done();
  84. })
  85. .pipe(split())
  86. .on('data', function (d) {
  87. const json = JSON.parse(d);
  88. assume(json.level).equal('info');
  89. assume(json.message).equal('a'.repeat(16384 - os.EOL.length - 1));
  90. assume(json.counter).equal(++counters.read);
  91. })
  92. .on('end', function () {
  93. assume(counters.write).equal(counters.read);
  94. logger.close();
  95. done();
  96. });
  97. }, 10000);
  98. });
  99. it('should handle a high volume of large writes synchronous', function (done) {
  100. const logger = winston.createLogger({
  101. transports: [new winston.transports.File({
  102. filename: logPath
  103. })]
  104. });
  105. const counters = {
  106. write: 0,
  107. read: 0
  108. };
  109. const msgs = new Array(10).fill().map(() => ({
  110. counter: ++counters.write,
  111. message: 'a'.repeat(16384 - os.EOL.length - 1)
  112. }));
  113. msgs.forEach(msg => logger.info(msg));
  114. setTimeout(function () {
  115. helpers.tryRead(logPath)
  116. .on('error', function (err) {
  117. assume(err).false();
  118. logger.close();
  119. done();
  120. })
  121. .pipe(split())
  122. .on('data', function (d) {
  123. const json = JSON.parse(d);
  124. assume(json.level).equal('info');
  125. assume(json.message).equal('a'.repeat(16384 - os.EOL.length - 1));
  126. assume(json.counter).equal(++counters.read);
  127. })
  128. .on('end', function () {
  129. assume(counters.write).equal(counters.read);
  130. logger.close();
  131. done();
  132. });
  133. }, 10000);
  134. });
  135. });