2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-08-04 17:22:00 -04:00
|
|
|
# can be used to generate a mock db for profiling purposes
|
|
|
|
|
2013-08-15 02:59:38 -04:00
|
|
|
# we want our script to generate a consistent output, to do so
|
|
|
|
# we monkey patch array sample so it always uses the same rng
|
|
|
|
class Array
|
|
|
|
RNG = Random.new(1_098_109_928_029_800)
|
|
|
|
|
|
|
|
def sample
|
|
|
|
self[RNG.rand(size)]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-08-05 00:29:04 -04:00
|
|
|
# based on https://gist.github.com/zaius/2643079
|
2013-08-05 00:26:11 -04:00
|
|
|
def unbundled_require(gem)
|
|
|
|
if defined?(::Bundler)
|
|
|
|
spec_path = Dir.glob("#{Gem.dir}/specifications/#{gem}-*.gemspec").last
|
|
|
|
raise LoadError if spec_path.nil?
|
|
|
|
|
|
|
|
spec = Gem::Specification.load spec_path
|
|
|
|
spec.activate
|
|
|
|
end
|
|
|
|
|
|
|
|
begin
|
|
|
|
require gem
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-08-04 17:22:00 -04:00
|
|
|
def sentence
|
|
|
|
@gabbler ||=
|
|
|
|
Gabbler.new.tap do |gabbler|
|
|
|
|
story = File.read(File.dirname(__FILE__) + "/alice.txt")
|
|
|
|
gabbler.learn(story)
|
|
|
|
end
|
|
|
|
|
2019-09-09 03:38:37 -04:00
|
|
|
sentence = +""
|
2013-08-04 17:22:00 -04:00
|
|
|
until sentence.length > 800
|
|
|
|
sentence << @gabbler.sentence
|
|
|
|
sentence << "\n"
|
|
|
|
end
|
|
|
|
sentence
|
|
|
|
end
|
|
|
|
|
2022-12-29 18:25:11 -05:00
|
|
|
def create_user(seq, admin: false, username: nil)
|
|
|
|
User.new.tap do |user|
|
|
|
|
user.email = "user@localhost#{seq}.fake"
|
|
|
|
user.username = username || "user#{seq}"
|
|
|
|
user.password = "password12345abc"
|
|
|
|
user.save!
|
|
|
|
|
|
|
|
if admin
|
|
|
|
user.grant_admin!
|
|
|
|
user.change_trust_level!(TrustLevel[4])
|
|
|
|
end
|
|
|
|
|
|
|
|
user.activate
|
|
|
|
end
|
2013-08-04 17:22:00 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
require File.expand_path(File.dirname(__FILE__) + "/../config/environment")
|
|
|
|
|
2019-03-14 10:47:38 -04:00
|
|
|
Jobs.run_immediately!
|
2014-01-08 23:56:03 -05:00
|
|
|
|
2013-08-04 17:22:00 -04:00
|
|
|
unless Rails.env == "profile"
|
|
|
|
puts "This script should only be used in the profile environment"
|
|
|
|
exit
|
|
|
|
end
|
|
|
|
|
2017-09-04 23:23:03 -04:00
|
|
|
# by default, Discourse has a "system" and `discobot` account
|
|
|
|
if User.count > 2
|
2014-01-02 19:51:12 -05:00
|
|
|
puts "Only run this script against an empty DB"
|
|
|
|
exit
|
|
|
|
end
|
|
|
|
|
|
|
|
require "optparse"
|
|
|
|
begin
|
|
|
|
unbundled_require "gabbler"
|
|
|
|
rescue LoadError
|
|
|
|
puts "installing gabbler gem"
|
|
|
|
puts `gem install gabbler`
|
|
|
|
unbundled_require "gabbler"
|
|
|
|
end
|
|
|
|
|
2022-12-29 18:25:11 -05:00
|
|
|
number_of_users = 100
|
|
|
|
puts "Creating #{number_of_users} users"
|
|
|
|
number_of_users.times.map do |i|
|
2013-08-04 17:22:00 -04:00
|
|
|
putc "."
|
2022-12-29 18:25:11 -05:00
|
|
|
create_user(i)
|
2013-08-04 17:22:00 -04:00
|
|
|
end
|
|
|
|
|
2022-12-29 18:25:11 -05:00
|
|
|
puts
|
|
|
|
puts "Creating 1 admin user"
|
|
|
|
admin_user = create_user(number_of_users + 1, admin: true, username: "admin1")
|
|
|
|
|
|
|
|
users = User.human_users.all
|
|
|
|
|
2013-08-04 17:22:00 -04:00
|
|
|
puts
|
|
|
|
puts "Creating 10 categories"
|
|
|
|
categories =
|
|
|
|
10.times.map do |i|
|
|
|
|
putc "."
|
2022-12-29 18:25:11 -05:00
|
|
|
Category.create(name: "category#{i}", text_color: "ffffff", color: "000000", user: admin_user)
|
2013-08-04 17:22:00 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
puts
|
|
|
|
puts "Creating 100 topics"
|
|
|
|
topic_ids =
|
|
|
|
100.times.map do
|
2022-12-29 18:25:11 -05:00
|
|
|
post =
|
|
|
|
PostCreator.create(
|
|
|
|
admin_user,
|
|
|
|
raw: sentence,
|
|
|
|
title: sentence[0..50].strip,
|
|
|
|
category: categories.sample.id,
|
|
|
|
skip_validations: true,
|
|
|
|
)
|
2013-08-04 17:22:00 -04:00
|
|
|
putc "."
|
2014-11-10 18:28:17 -05:00
|
|
|
post.topic_id
|
2013-08-04 17:22:00 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
puts
|
2022-12-29 18:25:11 -05:00
|
|
|
puts "Creating 2000 replies"
|
2013-08-04 17:22:00 -04:00
|
|
|
2000.times do
|
|
|
|
putc "."
|
|
|
|
PostCreator.create(
|
|
|
|
users.sample,
|
|
|
|
raw: sentence,
|
|
|
|
topic_id: topic_ids.sample,
|
|
|
|
skip_validations: true,
|
|
|
|
)
|
|
|
|
end
|
2014-01-08 23:56:03 -05:00
|
|
|
|
2022-12-29 18:25:11 -05:00
|
|
|
puts
|
|
|
|
puts "creating perf test topic"
|
|
|
|
first_post =
|
|
|
|
PostCreator.create(
|
|
|
|
users.sample,
|
|
|
|
raw: sentence,
|
|
|
|
title: "I am a topic used for perf tests",
|
|
|
|
category: categories.sample.id,
|
|
|
|
skip_validations: true,
|
|
|
|
)
|
|
|
|
|
|
|
|
puts
|
|
|
|
puts "Creating 100 replies for perf test topic"
|
|
|
|
100.times do
|
|
|
|
putc "."
|
|
|
|
PostCreator.create(
|
|
|
|
users.sample,
|
|
|
|
raw: sentence,
|
|
|
|
topic_id: first_post.topic_id,
|
|
|
|
skip_validations: true,
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2014-01-08 23:56:03 -05:00
|
|
|
# no sidekiq so update some stuff
|
|
|
|
Category.update_stats
|
|
|
|
Jobs::PeriodicalUpdates.new.execute(nil)
|