cronTriggerTest.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. var cronTrigger = require('../lib/cronTrigger');
  2. var SECOND = 0;
  3. var MIN = 1;
  4. var HOUR = 2;
  5. var DOM = 3;
  6. var MONTH = 4;
  7. var DOW = 5;
  8. function decoderTest(){
  9. var result = [];
  10. result.push(cronTrigger.decodeTrigger('12 2 3,4,5 4 5 1'));
  11. result.push(cronTrigger.decodeTrigger('* 1-3,2-9,4 3,4,5 4-9 5 1'));
  12. result.push(cronTrigger.decodeTrigger('12 2 3 4 5 1'));
  13. console.log(result);
  14. }
  15. function nextTimeTest(count){
  16. var timer = cronTrigger.decodeTrigger('0 0 0 1 0 2-5');
  17. var value = Date.now();
  18. console.log(timer);
  19. // console.log([1,2] instanceof Array);
  20. var r1, r2;
  21. var start = Date.now();
  22. for(var i = 0; i < count; i++)
  23. r1 = cronTrigger.nextTime(value, timer);
  24. var end = Date.now();
  25. console.log("first run time : " + (end-start));
  26. var start = Date.now();
  27. for(var i = 0; i < count; i++)
  28. r2 = nextExcuteTimeTest(value, timer);
  29. var end = Date.now();
  30. console.log("second run time : " + (end-start));
  31. console.log("first run time:" + r1);
  32. console.log("second run time:" + r2);
  33. }
  34. function nextExcuteTimeTest(time, cronTrigger){
  35. var next = new Date(time + 1000);
  36. while (true)
  37. {
  38. if (!timeMatch(next.getMonth(), cronTrigger[MONTH]))
  39. {
  40. next.setMonth(next.getMonth()+1);
  41. next.setDate(1);
  42. next.setHours(0);
  43. next.setMinutes(0);
  44. next.setSeconds(0);
  45. continue;
  46. }
  47. if (!timeMatch(next.getDate(), cronTrigger[DOM]))
  48. {
  49. next.setDate(next.getDate() + 1);
  50. next.setHours(0);
  51. next.setMinutes(0);
  52. next.setSeconds(0);
  53. continue;
  54. }
  55. if (!timeMatch(next.getDay(), cronTrigger[DOW]))
  56. {
  57. next.setDate(next.getDate() + 1);
  58. next.setHours(0);
  59. next.setMinutes(0);
  60. next.setSeconds(0);
  61. continue;
  62. }
  63. if (!timeMatch(next.getHours(), cronTrigger[HOUR]))
  64. {
  65. next.setHours(next.getHours() + 1);
  66. next.setMinutes(0);
  67. next.setSeconds(0);
  68. continue;
  69. }
  70. if (!timeMatch(next.getMinutes(), cronTrigger[MIN]))
  71. {
  72. next.setMinutes(next.getMinutes() + 1);
  73. next.setSeconds(0);
  74. continue;
  75. }
  76. if (!timeMatch(next.getSeconds(), cronTrigger[SECOND]))
  77. {
  78. next.setSeconds(next.getSeconds()+1);
  79. continue;
  80. }
  81. break;
  82. }
  83. return next;
  84. }
  85. function getDomLimitTest(y1,y2,m1,m2){
  86. for(var year = y1; year <= y2; year++)
  87. for(var month = m1; month <= m2; month++){
  88. console.log(year + "." + (month +1)+ " limit : " + cronTrigger.getDomLimit(year,month));
  89. }
  90. }
  91. function timeMatch(value, cronTime){
  92. // console.log("match value " + value + ' cronTime ' + cronTime);
  93. if(typeof(cronTime) == 'number'){
  94. if(cronTime == -1)
  95. return true;
  96. if(value == cronTime)
  97. return true;
  98. return false;
  99. }else if(typeof(cronTime) == 'object' && cronTime instanceof Array){
  100. if(value < cronTime[0] || value > cronTime[cronTime.length -1])
  101. return false;
  102. for(var i = 0; i < cronTime.length; i++)
  103. if(value == cronTime[i])
  104. return true;
  105. return false;
  106. }
  107. return null;
  108. }
  109. function test(){
  110. nextTimeTest(100);
  111. // getDomLimitTest(1800,2002,1,1);
  112. }
  113. test();