Created
December 11, 2013 12:46
-
-
Save Hayena/7909827 to your computer and use it in GitHub Desktop.
Benchmark Test - Scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package models.database | |
import models.User | |
import org.apache.hadoop.hbase.HBaseConfiguration | |
import org.apache.hadoop.hbase.client._ | |
import scala.language.implicitConversions | |
object HBase extends Database { | |
val conf = HBaseConfiguration.create() | |
val admin = new HBaseAdmin(conf) | |
val table = new HTable(conf, "test") | |
override def create(user: User) { | |
val put = new Put(user.id) | |
put.add("cf", "latitude", user.latitude) | |
put.add("cf", "longitude", user.longitude) | |
put.add("cf", "time", user.time) | |
table.put(put) | |
} | |
override def search(id: Int) { | |
val get = new Get(id) | |
table.get(get) | |
} | |
implicit def intBytes(int: Int): Array[Byte] = { | |
int.toString.getBytes | |
} | |
implicit def stringBytes(string: String): Array[Byte] = { | |
string.getBytes | |
} | |
implicit def doubleBytes(double: Double): Array[Byte] = { | |
double.toString.getBytes | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package models.database | |
import models.User | |
import com.mongodb.casbah.MongoConnection | |
import com.mongodb.casbah.commons.MongoDBObject | |
import com.mongodb.casbah.commons.MongoDBList | |
object MongoDB extends Database { | |
val mongoConnection = MongoConnection() | |
val mongoDB = mongoConnection("speedtest")("mongodb") | |
override def create(user: User) { | |
val mongoObject = MongoDBObject( | |
"id" -> user.id, | |
"location" -> MongoDBList(user.longitude, user.latitude), | |
"time" -> user.time | |
) | |
mongoDB.insert(mongoObject) | |
} | |
override def search(id: Int) { | |
val result = mongoDB.find( | |
MongoDBObject("id" -> id)) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package neo4j | |
import org.anormcypher.Cypher | |
import models.User | |
object Neo4j { | |
def search(id: Int) { | |
Cypher(s""" | |
start n=node(*) | |
match (n)-[r]->(l), (n)-[r2]->(t) | |
where has(n.id) and (n.id = ${id}) | |
return n | |
""").execute | |
} | |
def create(user: User) { | |
Cypher(s""" | |
create (location { latitude: ${user.latitude}, longitude: ${user.longitude}}) | |
create (user{ id: ${user.id}}) | |
create (time{ time: ${user.time}}) | |
create user-[:isAt]->location | |
create user-[:at]->time | |
create location-[:during]->time | |
""").execute | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package models.database | |
import models.User | |
import com.scalapenos.riak.RiakClient | |
import concurrent.ExecutionContext.Implicits.global | |
object Riak extends Database { | |
val bucket = RiakClient("http://127.0.0.1:8098").bucket("service") | |
override def create(user: User) { | |
bucket.store(user.id.toString, user) | |
} | |
override def search(id: Int) { | |
bucket.fetch(id.toString) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package models | |
import models.database._ | |
object Test { | |
val times: Int = 3 | |
def executeWriteTests { | |
println("Write test - Riak:") | |
println("Amount 10:") | |
writeTest(Riak, 10) | |
println("Amount 500:") | |
writeTest(Riak, 500) | |
println("Amount 1000:") | |
writeTest(Riak, 1000) | |
println("=====================") | |
println("Write test - MongoDB:") | |
println("Amount 10:") | |
writeTest(MongoDB, 10) | |
println("Amount 500:") | |
writeTest(MongoDB, 500) | |
println("Amount 1000:") | |
writeTest(MongoDB, 1000) | |
println("=====================") | |
println("Write test - HBase:") | |
println("Amount 10:") | |
writeTest(HBase, 10) | |
println("Amount 500:") | |
writeTest(HBase, 500) | |
println("Amount 1000:") | |
writeTest(HBase, 1000) | |
println("=====================") | |
println("Write test - Neo4j:") | |
println("Amount 10:") | |
writeTest(Neo4j, 10) | |
println("Amount 500:") | |
writeTest(Neo4j, 500) | |
println("Amount 1000:") | |
writeTest(Neo4j, 1000) | |
println("=====================") | |
println("Read test - Riak:") | |
println("Amount 10:") | |
readTest(Riak, 10) | |
println("Amount 500:") | |
readTest(Riak, 500) | |
println("Amount 1000:") | |
readTest(Riak, 1000) | |
println("=====================") | |
println("Read test - MongoDB:") | |
println("Amount 10:") | |
readTest(MongoDB, 10) | |
println("Amount 500:") | |
readTest(MongoDB, 500) | |
println("Amount 1000:") | |
readTest(MongoDB, 1000) | |
println("=====================") | |
println("Read test - HBase:") | |
println("Amount 10:") | |
readTest(HBase, 10) | |
println("Amount 500:") | |
readTest(HBase, 500) | |
println("Amount 1000:") | |
readTest(HBase, 1000) | |
println("=====================") | |
println("Read test - Neo4j:") | |
println("Amount 10:") | |
readTest(Neo4j, 10) | |
println("Amount 500:") | |
readTest(Neo4j, 500) | |
println("Amount 1000:") | |
readTest(Neo4j, 1000) | |
} | |
def writeTest(database: Database, amount: Int) { | |
for (i <- 1 to times) | |
time { | |
for (j <- 1 to amount) | |
database.create(UserFactory.next) | |
} | |
} | |
def readTest(database: Database, amount: Int) { | |
for (i <- 1 to times) | |
time { | |
for (j <- 1 to amount) | |
database.search(1) | |
} | |
} | |
def time[R](block: => R): R = { | |
val start = System.nanoTime | |
val result = block | |
val end = System.nanoTime | |
println("Duration: " + toMillis(end - start) + "ms") | |
result | |
} | |
def toMillis(nanoseconds: Long): Long = { | |
(nanoseconds / 1000) / 1000 | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package models | |
import scala.xml._ | |
case class User ( | |
id: Int, | |
latitude: Double, | |
longitude: Double, | |
time: Int | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package models | |
import scala.util.Random | |
object UserFactory { | |
private def generateInt: Int = { | |
Random.nextInt(1000000) | |
} | |
private def generateDouble: Double = { | |
Random.nextDouble | |
} | |
def next: User = { | |
new User( | |
generateInt, | |
generateDouble, | |
generateDouble, | |
generateInt | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment