Guest User

MTA: SA file read methods performance tests

a guest
May 16th, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.23 KB | None | 0 0
  1. -- ------
  2. -- CONFIG
  3. -- ------
  4. local smallBufferBytes, bigBufferBytes = 5000, 500 -- Arbitrary bytes to read into the buffer each iteration
  5. local smallFileSize, bigFileSize = 2048, 5242880 -- File sizes in bytes (2 KB, 5 MB)
  6.  
  7. -- Do not abort execution
  8. debug.sethook(nil)
  9.  
  10. -- Create files to test with
  11. local smallFile = fileCreate("small.dat")
  12. fileWrite(smallFile, string.rep("A", smallFileSize))
  13.  
  14. local bigFile = fileCreate("big.dat")
  15. fileWrite(bigFile, string.rep("A", bigFileSize))
  16.  
  17. -- Reset file read position
  18. fileSetPos(smallFile, 0)
  19. fileSetPos(bigFile, 0)
  20.  
  21. outputServerLog"*****************************************"
  22. outputServerLog"** FILE READ METHODS PERFORMANCE TESTS **"
  23. outputServerLog"*****************************************"
  24. outputServerLog"~ Information"
  25. outputServerLog("Small file size is " .. smallFileSize / 1024 .. " KB, big file size is " .. bigFileSize / 1024 .. " KB")
  26. outputServerLog("Small file loop iterations: " .. math.ceil(smallFileSize / smallBufferBytes) .. " (" .. smallBufferBytes .. " bytes buffer)")
  27. outputServerLog("Big file loop iterations: " .. math.ceil(bigFileSize / bigBufferBytes) .. " (" .. bigBufferBytes .. " bytes buffer)")
  28. outputServerLog"~ Test results"
  29.  
  30. -- Force GC to collect everything we could have created
  31. collectgarbage()
  32.  
  33. -- ---------------------------------------------------
  34. -- METHOD 1: READ ALL THE BYTES DIRECTLY INTO A STRING
  35. -- ---------------------------------------------------
  36. -- Small file
  37. local memoryBefore, ticksBefore = collectgarbage("count"), getTickCount()
  38. local fileContents = fileRead(smallFile, fileGetSize(smallFile))
  39. -- Output results
  40. local memoryNow, ticksNow = collectgarbage("count"), getTickCount()
  41. outputServerLog("Test 1: small file, read into string directly\n" .. memoryNow - memoryBefore .. " KB of memory | " .. ticksNow - ticksBefore .. " ms")
  42. memoryBefore, ticksBefore, memoryNow, ticksNow, fileContents = nil
  43. collectgarbage()
  44.  
  45. -- Big file
  46. local memoryBefore, ticksBefore = collectgarbage("count"), getTickCount()
  47. local fileContents = fileRead(bigFile, fileGetSize(bigFile))
  48. -- Output results
  49. local memoryNow, ticksNow = collectgarbage("count"), getTickCount()
  50. outputServerLog("Test 2: big file, read into string directly\n" .. memoryNow - memoryBefore .. " KB of memory | " .. ticksNow - ticksBefore .. " ms")
  51. memoryBefore, ticksBefore, memoryNow, ticksNow, fileContents = nil
  52. collectgarbage()
  53.  
  54. fileSetPos(smallFile, 0)
  55. fileSetPos(bigFile, 0)
  56.  
  57. -- -------------------------------------------------------------------------
  58. -- METHOD 2: CONCATENATING AN ARBITRARY NUMBER OF BYTES INTO A BUFFER STRING
  59. -- -------------------------------------------------------------------------
  60. -- Small file
  61. local memoryBefore, ticksBefore = collectgarbage("count"), getTickCount()
  62. local buffer = ""
  63. while not fileIsEOF(smallFile) do
  64.     buffer = buffer .. fileRead(smallFile, smallBufferBytes)
  65. end
  66. -- Output results
  67. local memoryNow, ticksNow = collectgarbage("count"), getTickCount()
  68. outputServerLog("Test 3: small file, concatenating into buffer string\n" .. memoryNow - memoryBefore .. " KB of memory | " .. ticksNow - ticksBefore .. " ms")
  69. memoryBefore, ticksBefore, memoryNow, ticksNow, buffer = nil
  70. collectgarbage()
  71.  
  72. -- Big file
  73. local memoryBefore, ticksBefore = collectgarbage("count"), getTickCount()
  74. local buffer = ""
  75. while not fileIsEOF(bigFile) do
  76.     buffer = buffer .. fileRead(bigFile, bigBufferBytes)
  77. end
  78. -- Output results
  79. local memoryNow, ticksNow = collectgarbage("count"), getTickCount()
  80. outputServerLog("Test 4: big file, concatenating into buffer string\n" .. memoryNow - memoryBefore .. " KB of memory | " .. ticksNow - ticksBefore .. " ms")
  81. memoryBefore, ticksBefore, memoryNow, ticksNow, buffer = nil
  82. collectgarbage()
  83.  
  84. fileSetPos(smallFile, 0)
  85. fileSetPos(bigFile, 0)
  86.  
  87. -- ------------------------------------------------------------------------
  88. -- METHOD 3: CONCATENATING AN ARBITRARY NUMBER OF BYTES INTO A BUFFER ARRAY
  89. -- ------------------------------------------------------------------------
  90. -- Small file
  91. local memoryBefore, ticksBefore = collectgarbage("count"), getTickCount()
  92. local buffer = {}
  93. while not fileIsEOF(smallFile) do
  94.     buffer[#buffer + 1] = fileRead(smallFile, smallBufferBytes)
  95. end
  96. local fileContents = table.concat(buffer)
  97. -- Output results
  98. local memoryNow, ticksNow = collectgarbage("count"), getTickCount()
  99. outputServerLog("Test 5: small file, concatenating into buffer array\n" .. memoryNow - memoryBefore .. " KB of memory | " .. ticksNow - ticksBefore .. " ms")
  100. memoryBefore, ticksBefore, memoryNow, ticksNow, buffer, fileContents = nil
  101. collectgarbage()
  102.  
  103. -- Big file
  104. local memoryBefore, ticksBefore = collectgarbage("count"), getTickCount()
  105. local buffer = {}
  106. while not fileIsEOF(bigFile) do
  107.     buffer[#buffer + 1] = fileRead(bigFile, bigBufferBytes)
  108. end
  109. local fileContents = table.concat(buffer)
  110. -- Output results
  111. local memoryNow, ticksNow = collectgarbage("count"), getTickCount()
  112. outputServerLog("Test 6: big file, concatenating into buffer array\n" .. memoryNow - memoryBefore .. " KB of memory | " .. ticksNow - ticksBefore .. " ms")
  113. memoryBefore, ticksBefore, memoryNow, ticksNow, buffer, fileContents = nil
  114.  
  115. -- Dereference the files
  116. -- (the GC will eventually take care of closing them)
  117. smallFile, bigFile = nil
Advertisement
Add Comment
Please, Sign In to add comment