bbh_filter.lua 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. -- bbh_filter.lua
  2. --
  3. -- This script outputs info about 'interesting' skp files,
  4. -- where the definition of 'interesting' changes but is roughly:
  5. -- "Interesting for bounding box hierarchy benchmarks."
  6. --
  7. -- Currently, the approach is to output, in equal ammounts, the names of the files that
  8. -- have most commands, and the names of the files that use the least popular commands.
  9. function count_entries(table)
  10. local count = 0
  11. for _,_ in pairs(table) do
  12. count = count + 1
  13. end
  14. return count
  15. end
  16. verbCounts = {}
  17. function reset_current()
  18. -- Data about the skp in transit
  19. currentInfo = {
  20. fileName = '',
  21. verbs = {},
  22. numOps = 0
  23. }
  24. end
  25. reset_current()
  26. numOutputFiles = 10 -- This is per measure.
  27. globalInfo = {} -- Saves currentInfo for each file to be used at the end.
  28. output = {} -- Stores {fileName, {verb, count}} tables.
  29. function tostr(t)
  30. local str = ""
  31. for k, v in next, t do
  32. if #str > 0 then
  33. str = str .. ", "
  34. end
  35. if type(k) == "number" then
  36. str = str .. "[" .. k .. "] = "
  37. else
  38. str = str .. tostring(k) .. " = "
  39. end
  40. if type(v) == "table" then
  41. str = str .. "{ " .. tostr(v) .. " }"
  42. else
  43. str = str .. tostring(v)
  44. end
  45. end
  46. return str
  47. end
  48. function sk_scrape_startcanvas(c, fileName) end
  49. function sk_scrape_endcanvas(c, fileName)
  50. globalInfo[fileName] = currentInfo
  51. globalInfo[fileName].fileName = fileName
  52. reset_current()
  53. end
  54. function sk_scrape_accumulate(t)
  55. -- dump the params in t, specifically showing the verb first, which we
  56. -- then nil out so it doesn't appear in tostr()
  57. --
  58. verbCounts[t.verb] = (verbCounts[t.verb] or 0) + 1
  59. currentInfo.verbs[t.verb] = (currentInfo.verbs[t.verb] or 0) + 1
  60. currentInfo.numOps = currentInfo.numOps + 1
  61. t.verb = nil
  62. end
  63. function sk_scrape_summarize()
  64. verbWeights = {} -- {verb, weight}, where 0 < weight <= 1
  65. meta = {}
  66. for k,v in pairs(verbCounts) do
  67. table.insert(meta, {key=k, value=v})
  68. end
  69. table.sort(meta, function (a,b) return a.value > b.value; end)
  70. maxValue = meta[1].value
  71. io.write("-- ==================\n")
  72. io.write("------------------------------------------------------------------ \n")
  73. io.write("-- Command\t\t\tNumber of calls\t\tPopularity\n")
  74. io.write("------------------------------------------------------------------ \n")
  75. for k, v in pairs(meta) do
  76. verbWeights[v.key] = v.value / maxValue
  77. -- Poor man's formatting:
  78. local padding = "\t\t\t"
  79. if (#v.key + 3) < 8 then
  80. padding = "\t\t\t\t"
  81. end
  82. if (#v.key + 3) >= 16 then
  83. padding = "\t\t"
  84. end
  85. io.write ("-- ",v.key, padding, v.value, '\t\t\t', verbWeights[v.key], "\n")
  86. end
  87. meta = {}
  88. function calculate_weight(verbs)
  89. local weight = 0
  90. for name, count in pairs(verbs) do
  91. weight = weight + (1 / verbWeights[name]) * count
  92. end
  93. return weight
  94. end
  95. for n, info in pairs(globalInfo) do
  96. table.insert(meta, info)
  97. end
  98. local visitedFiles = {}
  99. -- Prints out information in lua readable format
  100. function output_with_metric(metric_func, description, numOutputFiles)
  101. table.sort(meta, metric_func)
  102. print(description)
  103. local iter = 0
  104. for i, t in pairs(meta) do
  105. if not visitedFiles[t.fileName] then
  106. visitedFiles[t.fileName] = true
  107. io.write ("{\nname = \"", t.fileName, "\", \nverbs = {\n")
  108. for verb,count in pairs(globalInfo[t.fileName].verbs) do
  109. io.write(' ', verb, " = ", count, ",\n")
  110. end
  111. io.write("}\n},\n")
  112. iter = iter + 1
  113. if iter >= numOutputFiles then
  114. break
  115. end
  116. end
  117. end
  118. end
  119. output_with_metric(
  120. function(a, b) return calculate_weight(a.verbs) > calculate_weight(b.verbs); end,
  121. "\n-- ================== skps with calling unpopular commands.", 10)
  122. output_with_metric(
  123. function(a, b) return a.numOps > b.numOps; end,
  124. "\n-- ================== skps with the most calls.", 50)
  125. local count = count_entries(visitedFiles)
  126. print ("-- Spat", count, "files")
  127. end