Skip to content

Instantly share code, notes, and snippets.

@ArtemGr
Created April 17, 2009 09:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ArtemGr/96944 to your computer and use it in GitHub Desktop.
Save ArtemGr/96944 to your computer and use it in GitHub Desktop.
Neodatis micro-benchmark
object main {
import org.neodatis.odb._
import org.neodatis.odb.core.query.criteria._
import org.neodatis.odb.impl.core.query.criteria._
import org.neodatis.odb.impl.core.query.values._
import org.neodatis.odb.core.query.nq._
import Speed._
case class Test (var key: Long, var value: Int)
val random = new java.util.Random (System.currentTimeMillis)
def main (args: Array[String]) = {
OdbConfiguration.setDatabaseCharacterEncoding ("UTF-8")
OdbConfiguration.setDefaultIndexBTreeDegree (20) // Default is 20.
val odb = ODBFactory.open("test.odb")
val testClass = odb.getClassRepresentation (classOf[Test])
if (!testClass.existIndex ("Test.key")) testClass.addUniqueIndexOn ("Test.key", Array ("key"), true)
println ("insert+commit: " + measure (1) {odb.store (new Test (random.nextLong, random.nextInt)); odb.commit})
println ("insert: " + measure (1) {odb.store (new Test (random.nextLong, random.nextInt))})
var start = System.currentTimeMillis
odb.commit
var time = System.currentTimeMillis - start
println ("commit: " + (time.toDouble/1000) + " sec")
println ("select: " + measure (1) {
val objects = odb.getObjects[Test] (new CriteriaQuery (classOf[Test], Where.equal ("key", random.nextLong)))
assert (objects.size >= 0)
})
println ("value select: " + measure (1) {
val query = new ValuesCriteriaQuery (classOf[Test], Where.equal ("key", random.nextLong)) .field ("value")
val values = odb.getValues (query)
assert (values.size >= 0)
})
println ("native select: " + measure (1) {
val key = random.nextLong
val objects = odb.getObjects[Test] (new SimpleNativeQuery {def `match`(obj: Test): Boolean = {obj.key == key}})
assert (objects.size >= 0)
})
odb.close
}
}
object Speed {
object cpuSpeed {
val mf = java.lang.management.ManagementFactory.getThreadMXBean
assert (mf.isCurrentThreadCpuTimeSupported)
mf.setThreadCpuTimeEnabled (true)
}
/**
* Run <code>fun</code> for some time and return <i>operations per second</i>.<br>
* The time is measured using thread CPU time counter.
*/
def measureCPU (seconds: Double) (fun: => Unit) = {import cpuSpeed._
var left = 99; while (left != 0) {left -= 1; fun} // Warm-up cycle.
val t1 = mf.getCurrentThreadCpuTime; var t2 = t1; var count = 0
do {
var left = 999; count += left; while (left != 0) {left -= 1; fun}
t2 = mf.getCurrentThreadCpuTime
} while (t2 - t1 < seconds * 1000000000)
(count.toDouble / (t2 - t1) * 1000000000).toInt + " o/s"
}
/**
* Run <code>fun</code> for some time and return <i>operations per second</i>.<br>
* The time is measured using the system time counter (<code>System.nanoTime</code>).
*/
def measure (seconds: Double) (fun: => Unit) = {
var left = 99; while (left != 0) {left -= 1; fun} // Warm-up cycle.
val t1 = System.nanoTime; var t2 = t1; var count = 0
do {
var left = 999; count += left; while (left != 0) {left -= 1; fun}
t2 = System.nanoTime
} while (t2 - t1 < seconds * 1000000000)
(count.toDouble / (t2 - t1) * 1000000000).toInt + " o/s"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment