Skip to content

Instantly share code, notes, and snippets.

@hisui
Last active December 2, 2016 07:41
Show Gist options
  • Save hisui/de66529734c8e289118f88be7e93e3d7 to your computer and use it in GitHub Desktop.
Save hisui/de66529734c8e289118f88be7e93e3d7 to your computer and use it in GitHub Desktop.
name := "ds"
scalaVersion := "2.11.8"
// https://mvnrepository.com/artifact/com.google.cloud/google-cloud-datastore
libraryDependencies += "com.google.cloud" % "google-cloud-datastore" % "0.6.0"
import com.google.cloud.datastore._
import com.google.cloud.datastore.testing._
object Main extends App {
def benchmark[A](title: String)(f: => A): A = {
val t0 = System.currentTimeMillis()
val o: A = f
val t1 = System.currentTimeMillis()
println(title)
println("-----")
println(s" - duration: ${t1 - t0}ms")
println(s" - returned: $o")
println("-----")
println("")
o
}
val ds = benchmark("get a service instance") {
DatastoreOptions.getDefaultInstance().getService()
}
val keyFactory = benchmark("create a key factory") {
ds.newKeyFactory().kind("Room")
}
val key = benchmark("create a key") {
ds.allocateId(keyFactory.newKey())
}
val entity = Entity
.builder(key)
.set("title", StringValue.builder("untitled").build())
.set("createdAt", DateTime.now())
.set("updatedAt", DateTime.now())
.build()
benchmark("put an entity") {
ds.put(entity)
}
val key1 = ds.newKeyFactory().setKind("Session").newKey(5629499534213120L)
benchmark("get an entity by a key: 5629499534213120") {
ds.get(key1, Array[ReadOption]() :_*)
}
val key2 = ds.newKeyFactory().setKind("Session").newKey(5649391675244544L)
benchmark("get an entity by a key: 5649391675244544") {
ds.get(key2, Array[ReadOption]() :_*)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment