Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A script to calculate buffered byte-sized read and write times at various buffer sizes.
fileSize = 2 ** 16 * 1000
def getStatsWith(closure) {
buffSizes = (8..15)
iterations = (0..10)
buffSize2runtimes = buffSizes.collectEntries { [(2 ** it):[]] }
iterations.each {
buffSize2runtimes.each { buffSize, runtimes ->
runtimes << closure(buffSize) * 1000 / 1024 / 1024
}
}
println("Filesize: $fileSize")
println("Buffsize (Bytes): Mean (MB/s), Median (MB/s)")
buffSize2runtimes.each { buffSize, runtimes ->
mean = String.format("%.3f", runtimes.sum() / runtimes.size())
median = String.format("%.3f", runtimes.sort()[5])
println("$buffSize: $mean, $median")
}
}
def getWriteTime(buffSize) {
def rand = new Random()
def file = File.createTempFile("temp", ".tmp")
def wt = new BufferedWriter(new FileWriter(file), buffSize)
def i = 0
def startTime = System.currentTimeMillis()
while (i < fileSize) {
wt.write(rand.nextInt())
i += Character.BYTES
}
def stopTime = System.currentTimeMillis()
file.delete()
return fileSize / (stopTime - startTime)
}
def getWriteStats() {
println("Write Timing")
getStatsWith(this.&getWriteTime)
}
def getReadTime(file, buffSize) {
def rd = new BufferedReader(new FileReader(file), buffSize)
def bytes = 0
def startTime = System.currentTimeMillis()
while (rd.ready()) {
rd.read()
bytes += Character.BYTES
}
def stopTime = System.currentTimeMillis()
return bytes / (stopTime - startTime)
}
def getReadStats() {
println("Read Timing")
def rand = new Random()
def file = File.createTempFile("temp", ".tmp")
file.deleteOnExit()
file.withWriter { wt ->
(0..fileSize/Character.BYTES).each {
wt.write(rand.nextInt())
}
}
getStatsWith(this.&getReadTime.curry(file))
}
getWriteStats()
getReadStats()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment