You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.coffee 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. builder = require('../src/index')
  2. git = require('git-state')
  3. fs = require('fs')
  4. path = require('path')
  5. { performance, PerformanceObserver } = require('perf_hooks')
  6. global.xml = builder.create
  7. global.doc = builder.begin
  8. global.perf = (description, count, func) ->
  9. totalTime = 0
  10. callback = (userFunction) ->
  11. startTime = performance.now()
  12. for i in [1..count]
  13. userFunction()
  14. endTime = performance.now()
  15. totalTime += endTime - startTime
  16. func(callback)
  17. averageTime = totalTime / count
  18. version = require('../package.json').version
  19. working = gitWorking(gitDir)
  20. if working then version = version + "*"
  21. if not perfObj[version] then perfObj[version] = { }
  22. perfObj[version][description] = averageTime.toFixed(4)
  23. readPerf = (filename) ->
  24. if not fs.existsSync(filename) then fs.closeSync(fs.openSync(filename, 'w'))
  25. str = fs.readFileSync(filename, 'utf8')
  26. if str then JSON.parse(str) else { }
  27. runPerf = (dirPath) ->
  28. for file from walkDir(dirPath)
  29. filename = path.basename(file)
  30. if filename is "index.coffee" or filename is "perf.list" then continue
  31. require(file)
  32. walkDir = (dirPath) ->
  33. for file in fs.readdirSync(dirPath)
  34. filePath = path.join(dirPath, file)
  35. stat = fs.statSync(filePath)
  36. if stat.isFile() then yield filePath else if stat.isDirectory() then yield from walkDir(filePath)
  37. return undefined
  38. gitWorking = (dirPath) ->
  39. return git.isGitSync(dirPath) and git.dirtySync(dirPath)
  40. printPerf = (perfObj) ->
  41. sorted = sortByVersion(perfObj)
  42. for sortedItems in sorted
  43. version = sortedItems.version
  44. items = sortedItems.item
  45. sortedItem = sortByDesc(items)
  46. if parseVersion(version)[3]
  47. console.log "\x1b[4mv%s (Working Tree):\x1b[0m", version
  48. else
  49. console.log "\x1b[4mv%s:\x1b[0m", version
  50. longestDescription = 0
  51. for item in sortedItem
  52. descriptionLength = item.description.length
  53. if descriptionLength > longestDescription
  54. longestDescription = descriptionLength
  55. for item in sortedItem
  56. description = item.description
  57. averageTime = item.averageTime
  58. prevItem = findPrevPerf(sorted, version, description)
  59. if prevItem
  60. if averageTime < prevItem.item[description]
  61. console.log " - \x1b[36m%s\x1b[0m \x1b[1m\x1b[32m%s\x1b[0m ms (v%s was \x1b[1m%s\x1b[0m ms, -\x1b[1m%s\x1b[0m%)", padRight(description, longestDescription), averageTime, prevItem.version, prevItem.item[description], (-100*(averageTime - prevItem.item[description]) / prevItem.item[description]).toFixed(0)
  62. else if averageTime > prevItem.item[description]
  63. console.log " - \x1b[36m%s\x1b[0m \x1b[1m\x1b[31m%s\x1b[0m ms (v%s was \x1b[1m%s\x1b[0m ms, +\x1b[1m%s\x1b[0m%)", padRight(description, longestDescription), averageTime, prevItem.version, prevItem.item[description], (100*(averageTime - prevItem.item[description]) / prevItem.item[description]).toFixed(0)
  64. else
  65. console.log " - \x1b[36m%s\x1b[0m \x1b[1m%s\x1b[0m ms (v%s was \x1b[1m%s\x1b[0m ms, \x1b[1m%s\x1b[0m%)", padRight(description, longestDescription), averageTime, prevItem.version, prevItem.item[description], (100*(averageTime - prevItem.item[description]) / prevItem.item[description]).toFixed(0)
  66. else
  67. console.log " - \x1b[36m%s\x1b[0m \x1b[1m%s\x1b[0m ms (no previous result)", padRight(description, longestDescription), averageTime
  68. padRight = (str, len) ->
  69. str + " ".repeat(len - str.length)
  70. writePerf = (filename, perfObj) ->
  71. writePerfObj = { }
  72. for version, items of perfObj
  73. if not parseVersion(version)[3]
  74. writePerfObj[version] = items
  75. fs.writeFileSync(filename, JSON.stringify(writePerfObj, null, 2) , 'utf-8')
  76. findPrevPerf = (sorted, version, description) ->
  77. prev = undefined
  78. for item in sorted
  79. if compareVersion(item.version, version) is -1
  80. if item.item[description]
  81. prev = item
  82. return prev
  83. sortByVersion = (perfObj) ->
  84. sorted = []
  85. for version, items of perfObj
  86. sorted.push
  87. version: version
  88. item: items
  89. sorted.sort (item1, item2) ->
  90. compareVersion(item1.version, item2.version)
  91. sortByDesc = (item) ->
  92. sorted = []
  93. for description, averageTime of item
  94. sorted.push
  95. description: description
  96. averageTime: averageTime
  97. sorted.sort (item1, item2) ->
  98. if item1.description < item2.description then -1 else 1
  99. parseVersion = (version) ->
  100. isDirty = version[version.length - 1] is "*"
  101. if isDirty then version = version.substr(0, version.length - 1)
  102. v = version.split('.')
  103. v.push(isDirty)
  104. return v
  105. compareVersion = (v1, v2) ->
  106. v1 = parseVersion(v1)
  107. v2 = parseVersion(v2)
  108. if v1[0] < v2[0]
  109. -1
  110. else if v1[0] > v2[0]
  111. 1
  112. else # v1[0] = v2[0]
  113. if v1[1] < v2[1]
  114. -1
  115. else if v1[1] > v2[1]
  116. 1
  117. else # v1[1] = v2[1]
  118. if v1[2] < v2[2]
  119. -1
  120. else if v1[2] > v2[2]
  121. 1
  122. else # v1[2] = v2[2]
  123. if v1[3] and not v2[3]
  124. 1
  125. else if v2[3] and not v1[3]
  126. -1
  127. else
  128. 0
  129. perfDir = __dirname
  130. gitDir = path.resolve(__dirname, '..')
  131. perfFile = path.join(perfDir, './perf.list')
  132. perfObj = readPerf(perfFile)
  133. runPerf(perfDir)
  134. printPerf(perfObj)
  135. writePerf(perfFile, perfObj)