PageRenderTime 27ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/benchmark/src/main/scala/org/beaucatcher/benchmark/backends/PlainCasbah.scala

http://github.com/havocp/beaucatcher
Scala | 96 lines | 76 code | 16 blank | 4 comment | 5 complexity | 32ca58b5876078960548f69aada1f803 MD5 | raw file
Possible License(s): Apache-2.0
  1. package org.beaucatcher.benchmark.backends
  2. import java.util.concurrent.Executors
  3. import scala.collection.Map
  4. import scala.collection.TraversableOnce
  5. import com.mongodb.casbah.Imports._
  6. import com.mongodb.casbah.util.bson.conversions.RegisterJodaTimeConversionHelpers
  7. import org.beaucatcher.benchmark.MongoBenchmark
  8. import org.beaucatcher.benchmark.BenchmarkFuture
  9. import java.util.concurrent.ExecutorService
  10. class PlainCasbahBenchmark extends MongoBenchmark[MongoCollection] {
  11. override val name = "Casbah"
  12. private var connection : MongoConnection = null
  13. private var db : MongoDB = null
  14. private var threads : ExecutorService = null
  15. override def openDatabase(host : String, port : Int, dbname : String) : Unit = {
  16. // goal is to have enough threads that we don't bottleneck. resource usage doesn't matter.
  17. threads = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors * 2)
  18. RegisterJodaTimeConversionHelpers()
  19. connection = MongoConnection(host, port)
  20. connection.setWriteConcern(WriteConcern.Safe)
  21. connection.dropDatabase(dbname)
  22. db = connection(dbname)
  23. }
  24. override def cleanupDatabase() : Unit = {
  25. val name = db.name
  26. connection.dropDatabase(name)
  27. threads.shutdown()
  28. threads = null
  29. }
  30. private def convertObject(obj : Map[String, Any]) : MongoDBObject = {
  31. val builder = MongoDBObject.newBuilder
  32. for (kv <- obj.iterator) {
  33. kv match {
  34. case (k, v : Map[_, _]) =>
  35. builder += Pair(k, convertObject(v.asInstanceOf[Map[String, Any]]))
  36. case _ =>
  37. builder += kv
  38. }
  39. }
  40. builder.result
  41. }
  42. override def createCollection(collectionName : String, data : TraversableOnce[Map[String, Any]]) : MongoCollection = {
  43. val c = db(collectionName)
  44. require(c.count == 0)
  45. for (obj <- data) {
  46. c.insert(convertObject(obj))
  47. }
  48. c
  49. }
  50. override def findOne(collection : MongoCollection) = {
  51. val maybeOne = collection.findOne()
  52. // throw if it's not there
  53. maybeOne.get
  54. }
  55. override def findAll(collection : MongoCollection, numberExpected : Int) = {
  56. val all = collection.find()
  57. // force the cursor to all come through
  58. // (done imperative-style!)
  59. var num = 0
  60. while (all.hasNext) {
  61. all.next
  62. num += 1
  63. }
  64. require(numberExpected == num)
  65. }
  66. override def findAllAsync(collection : MongoCollection, numberExpected : Int) : BenchmarkFuture = {
  67. BenchmarkFuture(threads.submit(new Runnable() {
  68. override def run() = {
  69. findAll(collection, numberExpected)
  70. }
  71. }))
  72. }
  73. override def findOneAsync(collection : MongoCollection) : BenchmarkFuture = {
  74. BenchmarkFuture(threads.submit(new Runnable() {
  75. override def run() = {
  76. findOne(collection)
  77. }
  78. }))
  79. }
  80. }