ATuin

500k docs inserted into mongoDB

Sep 9th, 2012
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 1.90 KB | None | 0 0
  1. package com.sbs.hotins
  2.  
  3. import com.mongodb.casbah.Imports._
  4. import scala.actors.Actor
  5.  
  6. object HotInsertApp {
  7.   import HotInsert._
  8.   def main(args: Array[String]):Unit = time(test3)
  9. }
  10.  
  11.  
  12. object HotInsert {
  13.   val mongo = MongoConnection("localhost",27017)("HotInsDB")("HotTest")
  14.  
  15.   val data:Stream[() => DBObject] =
  16.     (() => MongoDBObject("_id" -> new ObjectId,
  17.              "Data" -> "X" * 20)) #:: data
  18.  
  19.   def time[R](block: => R): R = {
  20.     val t0 = System.nanoTime()
  21.     val result = block    // call-by-name
  22.     val t1 = System.nanoTime()
  23.     println("Elapsed time: %f secs".format((t1.toFloat - t0) / 1000000000))
  24.     result
  25.   }
  26.  
  27.   def test1: Unit = {
  28.     (1 to 500000) foreach ( _ =>
  29.       mongo.insert(MongoDBObject("_id" -> new ObjectId,
  30.                  "Data" -> "X" * 20))
  31.     )
  32.   }
  33.  
  34.   def test2: Unit = {
  35.     Range(1, 500000, 250).foreach(_ =>
  36.       mongo.insert(data.map(_.apply).take(250).toList))
  37.   }
  38.  
  39.   def test3: Unit = {
  40.     Range(1, 500000, 250) foreach(s =>
  41.       new Actor { override def act() = {
  42.     mongo.insert(data.map(_.apply).take(250).toList)}}.start)
  43.   }
  44. }
  45.  
  46.  
  47. /*
  48. Results for test3:
  49.  
  50. ~/Projects/MongoDB/hotinsertioncontest  ᐅ sbt run
  51. [info] Loading global plugins from /home/eof/.sbt/plugins
  52. [info] Loading project definition from /home/eof/Projects/MongoDB/hotinsertioncontest/project
  53. [info] Set current project to HotInsertionContest (in build file:/home/eof/Projects/MongoDB/hotinsertioncontest/)
  54.  
  55. Multiple main classes detected, select one to run:
  56.  
  57.  [1] JettyLauncher
  58.  [2] com.sbs.hotins.HotInsertApp
  59.  
  60. Enter number: 2
  61.  
  62. [info] Running com.sbs.hotins.HotInsertApp
  63. SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
  64. SLF4J: Defaulting to no-operation (NOP) logger implementation
  65. SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
  66. Elapsed time: 0,108003 secs
  67. [success] Total time: 7 s, completed 09-sep-2012 20:18:39
  68. */
Advertisement
Add Comment
Please, Sign In to add comment