Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- ------
- -- CONFIG
- -- ------
- local smallBufferBytes, bigBufferBytes = 5000, 500 -- Arbitrary bytes to read into the buffer each iteration
- local smallFileSize, bigFileSize = 2048, 5242880 -- File sizes in bytes (2 KB, 5 MB)
- -- Do not abort execution
- debug.sethook(nil)
- -- Create files to test with
- local smallFile = fileCreate("small.dat")
- fileWrite(smallFile, string.rep("A", smallFileSize))
- local bigFile = fileCreate("big.dat")
- fileWrite(bigFile, string.rep("A", bigFileSize))
- -- Reset file read position
- fileSetPos(smallFile, 0)
- fileSetPos(bigFile, 0)
- outputServerLog"*****************************************"
- outputServerLog"** FILE READ METHODS PERFORMANCE TESTS **"
- outputServerLog"*****************************************"
- outputServerLog"~ Information"
- outputServerLog("Small file size is " .. smallFileSize / 1024 .. " KB, big file size is " .. bigFileSize / 1024 .. " KB")
- outputServerLog("Small file loop iterations: " .. math.ceil(smallFileSize / smallBufferBytes) .. " (" .. smallBufferBytes .. " bytes buffer)")
- outputServerLog("Big file loop iterations: " .. math.ceil(bigFileSize / bigBufferBytes) .. " (" .. bigBufferBytes .. " bytes buffer)")
- outputServerLog"~ Test results"
- -- Force GC to collect everything we could have created
- collectgarbage()
- -- ---------------------------------------------------
- -- METHOD 1: READ ALL THE BYTES DIRECTLY INTO A STRING
- -- ---------------------------------------------------
- -- Small file
- local memoryBefore, ticksBefore = collectgarbage("count"), getTickCount()
- local fileContents = fileRead(smallFile, fileGetSize(smallFile))
- -- Output results
- local memoryNow, ticksNow = collectgarbage("count"), getTickCount()
- outputServerLog("Test 1: small file, read into string directly\n" .. memoryNow - memoryBefore .. " KB of memory | " .. ticksNow - ticksBefore .. " ms")
- memoryBefore, ticksBefore, memoryNow, ticksNow, fileContents = nil
- collectgarbage()
- -- Big file
- local memoryBefore, ticksBefore = collectgarbage("count"), getTickCount()
- local fileContents = fileRead(bigFile, fileGetSize(bigFile))
- -- Output results
- local memoryNow, ticksNow = collectgarbage("count"), getTickCount()
- outputServerLog("Test 2: big file, read into string directly\n" .. memoryNow - memoryBefore .. " KB of memory | " .. ticksNow - ticksBefore .. " ms")
- memoryBefore, ticksBefore, memoryNow, ticksNow, fileContents = nil
- collectgarbage()
- fileSetPos(smallFile, 0)
- fileSetPos(bigFile, 0)
- -- -------------------------------------------------------------------------
- -- METHOD 2: CONCATENATING AN ARBITRARY NUMBER OF BYTES INTO A BUFFER STRING
- -- -------------------------------------------------------------------------
- -- Small file
- local memoryBefore, ticksBefore = collectgarbage("count"), getTickCount()
- local buffer = ""
- while not fileIsEOF(smallFile) do
- buffer = buffer .. fileRead(smallFile, smallBufferBytes)
- end
- -- Output results
- local memoryNow, ticksNow = collectgarbage("count"), getTickCount()
- outputServerLog("Test 3: small file, concatenating into buffer string\n" .. memoryNow - memoryBefore .. " KB of memory | " .. ticksNow - ticksBefore .. " ms")
- memoryBefore, ticksBefore, memoryNow, ticksNow, buffer = nil
- collectgarbage()
- -- Big file
- local memoryBefore, ticksBefore = collectgarbage("count"), getTickCount()
- local buffer = ""
- while not fileIsEOF(bigFile) do
- buffer = buffer .. fileRead(bigFile, bigBufferBytes)
- end
- -- Output results
- local memoryNow, ticksNow = collectgarbage("count"), getTickCount()
- outputServerLog("Test 4: big file, concatenating into buffer string\n" .. memoryNow - memoryBefore .. " KB of memory | " .. ticksNow - ticksBefore .. " ms")
- memoryBefore, ticksBefore, memoryNow, ticksNow, buffer = nil
- collectgarbage()
- fileSetPos(smallFile, 0)
- fileSetPos(bigFile, 0)
- -- ------------------------------------------------------------------------
- -- METHOD 3: CONCATENATING AN ARBITRARY NUMBER OF BYTES INTO A BUFFER ARRAY
- -- ------------------------------------------------------------------------
- -- Small file
- local memoryBefore, ticksBefore = collectgarbage("count"), getTickCount()
- local buffer = {}
- while not fileIsEOF(smallFile) do
- buffer[#buffer + 1] = fileRead(smallFile, smallBufferBytes)
- end
- local fileContents = table.concat(buffer)
- -- Output results
- local memoryNow, ticksNow = collectgarbage("count"), getTickCount()
- outputServerLog("Test 5: small file, concatenating into buffer array\n" .. memoryNow - memoryBefore .. " KB of memory | " .. ticksNow - ticksBefore .. " ms")
- memoryBefore, ticksBefore, memoryNow, ticksNow, buffer, fileContents = nil
- collectgarbage()
- -- Big file
- local memoryBefore, ticksBefore = collectgarbage("count"), getTickCount()
- local buffer = {}
- while not fileIsEOF(bigFile) do
- buffer[#buffer + 1] = fileRead(bigFile, bigBufferBytes)
- end
- local fileContents = table.concat(buffer)
- -- Output results
- local memoryNow, ticksNow = collectgarbage("count"), getTickCount()
- outputServerLog("Test 6: big file, concatenating into buffer array\n" .. memoryNow - memoryBefore .. " KB of memory | " .. ticksNow - ticksBefore .. " ms")
- memoryBefore, ticksBefore, memoryNow, ticksNow, buffer, fileContents = nil
- -- Dereference the files
- -- (the GC will eventually take care of closing them)
- smallFile, bigFile = nil
Advertisement
Add Comment
Please, Sign In to add comment