2019-05-02 17:17:27 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-08-04 16:22:00 -05:00
|
|
|
# can be used to generate a mock db for profiling purposes
|
|
|
|
|
2013-08-15 01:59:38 -05: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(1098109928029800)
|
|
|
|
|
|
|
|
def sample
|
|
|
|
self[RNG.rand(size)]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-08-04 23:29:04 -05:00
|
|
|
# based on https://gist.github.com/zaius/2643079
|
2013-08-04 23:26:11 -05:00
|
|
|
def unbundled_require(gem)
|
|
|
|
if defined?(::Bundler)
|
|
|
|
spec_path = Dir.glob("#{Gem.dir}/specifications/#{gem}-*.gemspec").last
|
|
|
|
if spec_path.nil?
|
|
|
|
raise LoadError
|
|
|
|
end
|
|
|
|
|
|
|
|
spec = Gem::Specification.load spec_path
|
|
|
|
spec.activate
|
|
|
|
end
|
|
|
|
|
|
|
|
begin
|
|
|
|
require gem
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-08-04 16:22:00 -05:00
|
|
|
def sentence
|
|
|
|
@gabbler ||= Gabbler.new.tap do |gabbler|
|
|
|
|
story = File.read(File.dirname(__FILE__) + "/alice.txt")
|
|
|
|
gabbler.learn(story)
|
|
|
|
end
|
|
|
|
|
2019-09-09 02:38:37 -05:00
|
|
|
sentence = +""
|
2013-08-04 16:22:00 -05:00
|
|
|
until sentence.length > 800 do
|
|
|
|
sentence << @gabbler.sentence
|
|
|
|
sentence << "\n"
|
|
|
|
end
|
|
|
|
sentence
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_admin(seq)
|
|
|
|
User.new.tap { |admin|
|
2017-04-14 11:58:35 -05:00
|
|
|
admin.email = "admin@localhost#{seq}.fake"
|
2013-08-04 16:22:00 -05:00
|
|
|
admin.username = "admin#{seq}"
|
2018-02-18 17:12:51 -06:00
|
|
|
admin.password = "password12345abc"
|
2017-09-13 02:33:59 -05:00
|
|
|
admin.save!
|
2013-08-04 16:22:00 -05:00
|
|
|
admin.grant_admin!
|
2014-09-05 00:20:39 -05:00
|
|
|
admin.change_trust_level!(TrustLevel[4])
|
2017-09-13 02:33:59 -05:00
|
|
|
admin.activate
|
2013-08-04 16:22:00 -05:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
require File.expand_path(File.dirname(__FILE__) + "/../config/environment")
|
|
|
|
|
2019-03-14 09:47:38 -05:00
|
|
|
Jobs.run_immediately!
|
2014-01-08 22:56:03 -06:00
|
|
|
|
2013-08-04 16:22:00 -05:00
|
|
|
unless Rails.env == "profile"
|
|
|
|
puts "This script should only be used in the profile environment"
|
|
|
|
exit
|
|
|
|
end
|
|
|
|
|
2019-03-14 19:30:17 -05:00
|
|
|
def ensure_perf_test_topic_has_right_title!
|
2020-01-07 23:33:45 -06:00
|
|
|
title = "I am a topic used for perf tests"
|
|
|
|
# in case we have an old run and picked the wrong topic
|
|
|
|
Topic.where(title: title).update_all(title: "Test topic #{SecureRandom.hex}")
|
|
|
|
t = Topic.where(archetype: :regular, posts_count: 30).order(id: :desc).first
|
|
|
|
t.title = title
|
|
|
|
t.save!
|
2019-03-14 19:30:17 -05:00
|
|
|
end
|
|
|
|
|
2017-09-04 22:23:03 -05:00
|
|
|
# by default, Discourse has a "system" and `discobot` account
|
|
|
|
if User.count > 2
|
2014-01-02 18:51:12 -06:00
|
|
|
puts "Only run this script against an empty DB"
|
2019-03-14 19:30:17 -05:00
|
|
|
|
|
|
|
ensure_perf_test_topic_has_right_title!
|
2014-01-02 18:51:12 -06:00
|
|
|
exit
|
|
|
|
end
|
|
|
|
|
|
|
|
require 'optparse'
|
|
|
|
begin
|
|
|
|
unbundled_require 'gabbler'
|
|
|
|
rescue LoadError
|
|
|
|
puts "installing gabbler gem"
|
|
|
|
puts `gem install gabbler`
|
|
|
|
unbundled_require 'gabbler'
|
|
|
|
end
|
|
|
|
|
2013-08-04 16:22:00 -05:00
|
|
|
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
|
2019-09-09 02:38:37 -05:00
|
|
|
post = PostCreator.create(users.sample, raw: sentence, title: sentence[0..50].strip, category: categories.sample.id, skip_validations: true)
|
2014-11-10 17:28:17 -06:00
|
|
|
|
2013-08-04 16:22:00 -05:00
|
|
|
putc "."
|
2014-11-10 17:28:17 -06:00
|
|
|
post.topic_id
|
2013-08-04 16:22:00 -05:00
|
|
|
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
|
2014-01-08 22:56:03 -06:00
|
|
|
|
|
|
|
# no sidekiq so update some stuff
|
|
|
|
Category.update_stats
|
|
|
|
Jobs::PeriodicalUpdates.new.execute(nil)
|
2019-03-14 19:30:17 -05:00
|
|
|
|
|
|
|
ensure_perf_test_topic_has_right_title!
|