Skip to content

Instantly share code, notes, and snippets.

@kiszk
Last active February 27, 2018 16:26
Show Gist options
  • Save kiszk/94f75b506c93a663bbbc372ffe8f05de to your computer and use it in GitHub Desktop.
Save kiszk/94f75b506c93a663bbbc372ffe8f05de to your computer and use it in GitHub Desktop.
Benchmark program for MemoryBlock
package org.apache.spark.sql
import org.apache.spark.SparkFunSuite
import org.apache.spark.unsafe.Platform
import org.apache.spark.unsafe.memory._
import org.apache.spark.util.Benchmark
class MemoryBlockBenchmark extends SparkFunSuite {
test("benchmark") {
val N = 128 * 1024 * 1024
val iters = 2
val benchmark = new Benchmark("Memory access benchmarks", N, minNumIters = 20)
val bmemory = new Array[Byte](N * 8)
val bobj: Any = bmemory.asInstanceOf[Any]
val lmemory = new Array[Long](N)
val lobj: Any = lmemory.asInstanceOf[Any]
var memoryBlock: MemoryBlock = null
benchmark.addCase("ByteArrayMemoryBlock get/putInt()") { _: Int =>
memoryBlock = ByteArrayMemoryBlock.fromArray(bmemory).asInstanceOf[MemoryBlock]
for (_ <- 0L until iters) {
var sum = 0
var i = 0
while (i < N) {
memoryBlock.putInt(Platform.BYTE_ARRAY_OFFSET + i * 4, i)
i += 1
}
i = 0
while (i < N) {
sum += memoryBlock.getInt(Platform.BYTE_ARRAY_OFFSET + i * 4)
i += 1
}
}
}
benchmark.addCase("Platform get/putInt(byte[])") { _: Int =>
for (_ <- 0L until iters) {
var i = 0
var sum = 0
while (i < N) {
Platform.putInt(bmemory, Platform.BYTE_ARRAY_OFFSET + i * 4, i)
i += 1
}
i = 0
while (i < N) {
sum += Platform.getInt(bmemory, Platform.BYTE_ARRAY_OFFSET + i * 4)
i += 1
}
}
}
benchmark.addCase("Platform get/putInt(Object)") { _: Int =>
for (_ <- 0L until iters) {
var i = 0
var sum = 0
while (i < N) {
Platform.putInt(bobj, Platform.BYTE_ARRAY_OFFSET + i * 4, i)
i += 1
}
i = 0
while (i < N) {
sum += Platform.getInt(bobj, Platform.BYTE_ARRAY_OFFSET + i * 4)
i += 1
}
}
}
benchmark.addCase("OnHeapMemoryBlock get/putLong()") { _: Int =>
memoryBlock = OnHeapMemoryBlock.fromArray(lmemory).asInstanceOf[MemoryBlock]
for (_ <- 0L until iters) {
var sum = 0L
var i = 0
while (i < N) {
memoryBlock.putLong(Platform.LONG_ARRAY_OFFSET + i * 8, i)
i += 1
}
i = 0
while (i < N) {
sum += memoryBlock.getLong(Platform.LONG_ARRAY_OFFSET + i * 8)
i += 1
}
}
}
benchmark.addCase("long[]") { _: Int =>
for (_ <- 0L until iters) {
var i = 0
var sum = 0L
while (i < N) {
lmemory(i) = i
i += 1
}
i = 0
while (i < N) {
sum += lmemory(i)
i += 1
}
}
}
benchmark.addCase("Platform get/putLong(long[])") { _: Int =>
for (_ <- 0L until iters) {
var i = 0
var sum = 0L
while (i < N) {
Platform.putLong(lmemory, Platform.LONG_ARRAY_OFFSET + i * 8, i)
i += 1
}
i = 0
while (i < N) {
sum += Platform.getLong(lmemory, Platform.LONG_ARRAY_OFFSET + i * 8)
i += 1
}
}
}
benchmark.addCase("Platform get/putLong(Object)") { _: Int =>
for (_ <- 0L until iters) {
var i = 0
var sum = 0L
while (i < N) {
Platform.putLong(lobj, Platform.LONG_ARRAY_OFFSET + i * 8, i)
i += 1
}
i = 0
while (i < N) {
sum += Platform.getLong(lobj, Platform.LONG_ARRAY_OFFSET + i * 8)
i += 1
}
}
}
benchmark.run()
}
}
package org.apache.spark.sql
import org.apache.spark.SparkFunSuite
import org.apache.spark.unsafe.Platform
import org.apache.spark.unsafe.memory._
import org.apache.spark.util.Benchmark
class MemoryBlockBenchmark extends SparkFunSuite {
test("benchmark") {
val N = 128 * 1024 * 1024
val iters = 2
val benchmark = new Benchmark("Memory access benchmarks", N, minNumIters = 20)
val bmemory = new Array[Byte](N * 8)
val bobj: Any = bmemory.asInstanceOf[Any]
val lmemory = new Array[Long](N)
val lobj: Any = lmemory.asInstanceOf[Any]
var memoryBlock: MemoryBlock = null
benchmark.addCase("ByteArrayMemoryBlock get/putInt()") { _: Int =>
memoryBlock = ByteArrayMemoryBlock.fromArray(bmemory).asInstanceOf[MemoryBlock]
for (_ <- 0L until iters) {
var sum = 0
var i = 0
while (i < N) {
memoryBlock.putInt(Platform.BYTE_ARRAY_OFFSET + i * 4, i)
i += 1
}
i = 0
while (i < N) {
sum += memoryBlock.getInt(Platform.BYTE_ARRAY_OFFSET + i * 4)
i += 1
}
}
}
benchmark.addCase("Platform get/putInt(byte[])") { _: Int =>
for (_ <- 0L until iters) {
var i = 0
var sum = 0
while (i < N) {
Platform.putInt(bmemory, Platform.BYTE_ARRAY_OFFSET + i * 4, i)
i += 1
}
i = 0
while (i < N) {
sum += Platform.getInt(bmemory, Platform.BYTE_ARRAY_OFFSET + i * 4)
i += 1
}
}
}
benchmark.addCase("Platform get/putInt(Object)") { _: Int =>
for (_ <- 0L until iters) {
var i = 0
var sum = 0
while (i < N) {
Platform.putInt(bobj, Platform.BYTE_ARRAY_OFFSET + i * 4, i)
i += 1
}
i = 0
while (i < N) {
sum += Platform.getInt(bobj, Platform.BYTE_ARRAY_OFFSET + i * 4)
i += 1
}
}
}
benchmark.addCase("OnHeapMemoryBlock get/putLong()") { _: Int =>
memoryBlock = OnHeapMemoryBlock.fromArray(lmemory).asInstanceOf[MemoryBlock]
for (_ <- 0L until iters) {
var sum = 0L
var i = 0
while (i < N) {
memoryBlock.putLong(Platform.LONG_ARRAY_OFFSET + i * 8, i)
i += 1
}
i = 0
while (i < N) {
sum += memoryBlock.getLong(Platform.LONG_ARRAY_OFFSET + i * 8)
i += 1
}
}
}
benchmark.addCase("long[]") { _: Int =>
for (_ <- 0L until iters) {
var i = 0
var sum = 0L
while (i < N) {
lmemory(i) = i
i += 1
}
i = 0
while (i < N) {
sum += lmemory(i)
i += 1
}
}
}
benchmark.addCase("Platform get/putLong(long[])") { _: Int =>
for (_ <- 0L until iters) {
var i = 0
var sum = 0L
while (i < N) {
Platform.putLong(lmemory, Platform.LONG_ARRAY_OFFSET + i * 8, i)
i += 1
}
i = 0
while (i < N) {
sum += Platform.getLong(lmemory, Platform.LONG_ARRAY_OFFSET + i * 8)
i += 1
}
}
}
benchmark.addCase("Platform get/putLong(Object)") { _: Int =>
for (_ <- 0L until iters) {
var i = 0
var sum = 0L
while (i < N) {
Platform.putLong(lobj, Platform.LONG_ARRAY_OFFSET + i * 8, i)
i += 1
}
i = 0
while (i < N) {
sum += Platform.getLong(lobj, Platform.LONG_ARRAY_OFFSET + i * 8)
i += 1
}
}
}
benchmark.run()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment