Commit c08c9294 by Rocky Duan

bulk generate benchmark

parent c1b96347
......@@ -30,6 +30,8 @@ def create_test_user(id)
User.create!(external_id: id, username: "user#{id}", email: "user#{id}@test.com")
end
Dir.glob('lib/tasks/*.rake').each { |r| import r }
task :console => :environment do
binding.pry
end
......@@ -130,10 +132,35 @@ namespace :db do
end
task :bulk_seed, [:num] => :environment do |t, args|
Mongoid.configure do |config|
config.connect_to("cs_comments_service_bulk_test")
end
connnection = Mongo::Connection.new("127.0.0.1", "27017")
db = Mongo::Connection.new.db("cs_comments_service_bulk_test")
CommentThread.create_indexes
Comment.create_indexes
Content.delete_all
coll = db.collection("contents")
args[:num].to_i.times do
doc = {"_type" => "CommentThread", "anonymous" => [true, false].sample, "at_position_list" => [],
"tags_array" => [],
"comment_count" => 0, "title" => Faker::Lorem.sentence(6), "author_id" => rand(1..10).to_s,
"body" => Faker::Lorem.paragraphs.join("\n\n"), "course_id" => COURSE_ID, "created_at" => Time.now,
"commentable_id" => COURSE_ID, "closed" => [true, false].sample, "updated_at" => Time.now, "last_activity_at" => Time.now,
"votes" => {"count" => 0, "down" => [], "down_count" => 0, "point" => 0, "up" => [], "up_count" => []}}
coll.insert(doc)
end
binding.pry
Tire.index('comment_threads').delete
CommentThread.create_elasticsearch_index
Tire.index('comment_threads') { import CommentThread.all }
end
task :seed_fast => :environment do
ADDITIONAL_COMMENTS_PER_THREAD = 20
config = YAML.load_file("config/mongoid.yml")[Sinatra::Base.environment]["sessions"]["default"]
connnection = Mongo::Connection.new(config["hosts"][0].split(":")[0], config["hosts"][0].split(":")[1])
db = Mongo::Connection.new.db(config["database"])
......
require 'spec_helper'
Mongoid.configure do |config|
config.connect_to "cs_comments_service_bulk_test"
end
describe "app" do
describe "benchmark" do
it "bulk generate" do
Comment.delete_all
CommentThread.delete_all
CommentThread.recalculate_all_context_tag_weights!
User.delete_all
Notification.delete_all
Subscription.delete_all
Comment.create_indexes
CommentThread.create_indexes
User.create_indexes
Notification.create_indexes
Subscription.create_indexes
Delayed::Backend::Mongoid::Job.create_indexes
COMMENTABLES = 20
USERS = 20
THREADS = 200
TOP_COMMENTS = 1000
SUB_COMMENTS = 1000
VOTES = 10000
TAGS = 1000
Benchmark.bm(31) do |x|
x.report "create users" do
(1..USERS).each do |user_id|
post "/api/v1/users", id: user_id, username: "user#{user_id}", email: "user#{user_id}@test.com"
end
end
x.report "create new threads" do
THREADS.times do
post "/api/v1/question_#{rand(COMMENTABLES).to_s}/threads", \
title: "Interesting question", body: "cool", anonymous: false, \
course_id: "1", user_id: (rand(USERS) + 1).to_s, \
tags: (1..5).map{|x| "tag#{rand(TAGS)}"}.join(",")
end
end
comment_thread_ids = CommentThread.all.to_a.map(&:id)
x.report("create top comments") do
TOP_COMMENTS.times do
post "/api/v1/threads/#{comment_thread_ids.sample}/comments", \
body: "lalala", anonymous: false,
course_id: "1", user_id: (rand(USERS) + 1).to_s
end
end
top_comment_ids = Comment.all.to_a.map(&:id)
x.report("create sub comments") do
SUB_COMMENTS.times do
post "/api/v1/comments/#{top_comment_ids.sample}", \
body: "lalala", anonymous: false,
course_id: "1", user_id: (rand(USERS) + 1).to_s
end
end
x.report("create votes") do
VOTES.times do
put "/api/v1/threads/#{comment_thread_ids.sample}", user_id: (rand(USERS) + 1).to_s, value: [:up, :down].sample
put "/api/v1/threads/#{top_comment_ids.sample}", user_id: (rand(USERS) + 1).to_s, value: [:up, :down].sample
end
end
end
end
end
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment