Commit 376aa1dc by Rocky Duan

weird

parent 11784608
......@@ -39,6 +39,8 @@ gem 'nokogiri'
gem 'tire'
gem 'tire-contrib'
gem 'rest-client'
group :test do
gem 'rspec'
gem 'rack-test', :require => "rack/test"
......
......@@ -30,7 +30,7 @@ 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 }
#Dir.glob('lib/tasks/*.rake').each { |r| import r }
task :console => :environment do
binding.pry
......@@ -204,8 +204,8 @@ namespace :db do
# user.subscribe(other_user)
# end
generate_comments_for("video_1")
generate_comments_for("lab_1")
generate_comments_for("lab_2")
#generate_comments_for("lab_1")
#generate_comments_for("lab_2")
end_time = Time.now
......
......@@ -20,9 +20,10 @@ put "#{APIPREFIX}/threads/:thread_id" do |thread_id|
end
post "#{APIPREFIX}/threads/:thread_id/comments" do |thread_id|
comment = thread.comments.new(params.slice(*%w[body course_id]))
comment = Comment.new(params.slice(*%w[body course_id]))
comment.anonymous = bool_anonymous || false
comment.author = user
comment.comment_thread = thread
comment.save
if comment.errors.any?
error 400, comment.errors.full_messages.to_json
......
......@@ -10,6 +10,7 @@ get "#{APIPREFIX}/:commentable_id/threads" do |commentable_id|
end
post "#{APIPREFIX}/:commentable_id/threads" do |commentable_id|
puts "creating thread"
thread = CommentThread.new(params.slice(*%w[title body course_id]).merge(commentable_id: commentable_id))
thread.anonymous = bool_anonymous || false
thread.tags = params["tags"] || ""
......@@ -21,4 +22,5 @@ post "#{APIPREFIX}/:commentable_id/threads" do |commentable_id|
user.subscribe(thread) if bool_auto_subscribe
thread.to_hash.to_json
end
puts CommentThread.count
end
......@@ -46,11 +46,7 @@ require './api/notifications_and_subscriptions'
if environment.to_s == "development"
get "#{APIPREFIX}/clean" do
Comment.delete_all
CommentThread.delete_all
User.delete_all
Notification.delete_all
Subscription.delete_all
[Comment, CommentThread, User, Notification, Subscription, Activity].each(&:delete_all)
{}.to_json
end
end
......
seed_size:
commentables: 20
users: 20
threads: 20
top_comments: 100
sub_comments: 100
votes: 100
tags: 1000
require 'rest_client'
namespace :benchmark do
task :bulk_generate do
#[Comment, CommentThread, User, Notification, Subscription].each(&:delete_all).each(&:create_indexes)
#Delayed::Backend::Mongoid::Job.create_indexes
seed_config = YAML.load_file("config/benchmark.yml").with_indifferent_access
COMMENTABLES = seed_config[:seed_size][:commentables]
USERS = seed_config[:seed_size][:users]
THREADS = seed_config[:seed_size][:threads]
TOP_COMMENTS = seed_config[:seed_size][:top_comments]
SUB_COMMENTS = seed_config[:seed_size][:sub_comments]
VOTES = seed_config[:seed_size][:votes]
TAGS = seed_config[:seed_size][:tags]
PREFIX = "http://localhost:4567/api/v1"
#Benchmark.bm(31) do |x|
RestClient.get "#{PREFIX}/clean"
#x.report "create users via api" do
(1..USERS).each do |user_id|
data = { id: user_id, username: "user#{user_id}", email: "user#{user_id}@test.com" }
RestClient.post "#{PREFIX}/users", data
end
#end
#x.report "create new threads via api" do
(1..THREADS).each do |t|
data = {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(",")}
RestClient.post "#{PREFIX}/question_#{rand(COMMENTABLES).to_s}/threads", data
end
#end
#comment_thread_ids = CommentThread.all.to_a.map(&:id)
#binding.pry
#x.report("create top comments via api") do
TOP_COMMENTS.times do
data = {body: "lalala", anonymous: false,
course_id: "1", user_id: (rand(USERS) + 1).to_s}
RestClient.post "#{PREFIX}/threads/#{comment_thread_ids.sample}/comments", data
end
#end
#top_comment_ids = Comment.all.to_a.map(&:id)
#x.report("create sub comments") do
SUB_COMMENTS.times do
data = {body: "lalala", anonymous: false,
course_id: "1", user_id: (rand(USERS) + 1).to_s}
RestClient.post "#{PREFIX}/comments/#{top_comment_ids.sample}", data
end
#end
#x.report("create votes") do
VOTES.times do
data = {user_id: (rand(USERS) + 1).to_s, value: [:up, :down].sample}
RestClient.put "#{PREFIX}/threads/#{comment_thread_ids.sample}/votes", data
RestClient.put "#{PREFIX}/comments/#{top_comment_ids.sample}/votes", data
end
#end
end
#end
end
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, CommentThread, User, Notification, Subscription].each(&:delete_all).each(&: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