Advertisement
Guest User

Untitled

a guest
May 29th, 2015
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.88 KB | None | 0 0
  1. #run this file to create load a local mongo DB with about 1M documents. Takes about 6 minutes to run, prints gradual results.
  2. require 'mongo'
  3.  
  4. include Mongo
  5.  
  6. #$mongo = MongoClient.from_uri('mongodb://localhost:27017/mongoLoad').db('mongoLoad')
  7. $mongo = MongoClient.new('localhost', 27017, pool_size: 10, pool_timeout: 10).db('mongoLoad')
  8.  
  9.  
  10. $coll = $mongo.collection('users')
  11. $coll.remove
  12.  
  13. def insert_docs(n = 100000)
  14. thread_start = Time.now
  15. (1..n).each do |i|
  16. $coll.insert({index: i, text: 'Lorem Ipsum'});
  17. msg = "Thread #{Thread.current.object_id} - #{i}/#{n} done; time passed: #{Time.now - thread_start}"
  18. puts msg if i%1000 == 0
  19. end
  20. end
  21.  
  22. before = Time.now
  23. num_threads = 10
  24. threads = []
  25. (1..num_threads).each {|j| threads << Thread.new(nil) { insert_docs } }
  26. threads.each { |thr| thr.join }
  27.  
  28. puts "time taken: #{Time.now - before}; num_docs_now: #{$coll.count}"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement