From 10b02b1288945f3adb51f3592d5f42d3edb11c0f Mon Sep 17 00:00:00 2001 From: Sam Date: Mon, 5 Aug 2013 07:22:00 +1000 Subject: [PATCH] Create profile db generator for use when benchmarking a local install http://meta.discourse.org/t/benchmarking-discourse-locally/9070 --- config/database.yml.development-sample | 2 +- script/profile_db_generator.rb | 73 ++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 script/profile_db_generator.rb diff --git a/config/database.yml.development-sample b/config/database.yml.development-sample index 74106f2638b..ab3b878e7c9 100644 --- a/config/database.yml.development-sample +++ b/config/database.yml.development-sample @@ -39,7 +39,7 @@ production: profile: adapter: postgresql - database: discourse_development + database: discourse_profile min_messages: warning host: localhost pool: 5 diff --git a/script/profile_db_generator.rb b/script/profile_db_generator.rb new file mode 100644 index 00000000000..3dbe3d245b9 --- /dev/null +++ b/script/profile_db_generator.rb @@ -0,0 +1,73 @@ +# can be used to generate a mock db for profiling purposes + +require 'optparse' +require 'gabbler' + +user_id = nil + +def sentence + @gabbler ||= Gabbler.new.tap do |gabbler| + story = File.read(File.dirname(__FILE__) + "/alice.txt") + gabbler.learn(story) + end + + sentence = "" + until sentence.length > 800 do + sentence << @gabbler.sentence + sentence << "\n" + end + sentence +end + +def create_admin(seq) + User.new.tap { |admin| + admin.email = "admin@localhost#{seq}" + admin.username = "admin#{seq}" + admin.password = "password" + admin.save + admin.grant_admin! + admin.change_trust_level!(:regular) + admin.email_tokens.update_all(confirmed: true) + } +end + +require File.expand_path(File.dirname(__FILE__) + "/../config/environment") + +unless Rails.env == "profile" + puts "This script should only be used in the profile environment" + exit +end + +unless User.count == 0 + puts "Only run this script against an empty DB" + exit +end + +puts "Creating 100 users" +users = 100.times.map do |i| + putc "." + create_admin(i) +end + +puts +puts "Creating 10 categories" +categories = 10.times.map do |i| + putc "." + Category.create(name: "category#{i}", text_color: "ffffff", color: "000000", user: users.first) +end + +puts +puts "Creating 100 topics" + +topic_ids = 100.times.map do + topic = PostCreator.create(users.sample, raw: sentence, title: sentence[0..50].strip, category: categories.sample.name, skip_validations: true) + putc "." + topic.id +end + +puts +puts "creating 2000 replies" +2000.times do + putc "." + PostCreator.create(users.sample, raw: sentence, topic_id: topic_ids.sample, skip_validations: true) +end