Commit 161804bf authored by Timothy Andrew's avatar Timothy Andrew

Add a "populate metrics directly" option to the cycle analytics seed.

- The normal seed creates all the data for cycle analytics the "right"
  way. It creates issues, merge requests, commits, branches,
  deployments, etc. This is good, but too slow for perf testing.
  Generating a 1000 sets of records this way takes more than an hour.

- When the `CYCLE_ANALYTICS_POPULATE_METRICS_DIRECTLY` environment
  variable is passed in, the seed only creates issues and merge
  requests. It then adds the `metrics` for each issue and
  merge request directly, to save time.

- The seed now takes about 4 minutes to run for 1000 sets of records.
parent 40c17f2d
require 'sidekiq/testing' require 'sidekiq/testing'
require './spec/support/test_env'
class Gitlab::Seeder::CycleAnalytics class Gitlab::Seeder::CycleAnalytics
def initialize(project, perf: false) def initialize(project, perf: false)
...@@ -21,10 +22,64 @@ class Gitlab::Seeder::CycleAnalytics ...@@ -21,10 +22,64 @@ class Gitlab::Seeder::CycleAnalytics
end end
end end
def seed_metrics!
@issue_count.times do |index|
# Issue
Timecop.travel 5.days.from_now
title = "#{FFaker::Product.brand}-#{FFaker::Product.brand}-#{rand(1000)}"
issue = Issue.create(project: @project, title: title, author: @user)
issue_metrics = issue.metrics
# Milestones / Labels
Timecop.travel 5.days.from_now
if index.even?
issue_metrics.first_associated_with_milestone_at = rand(6..12).hours.from_now
else
issue_metrics.first_added_to_board_at = rand(6..12).hours.from_now
end
# Commit
Timecop.travel 5.days.from_now
issue_metrics.first_mentioned_in_commit_at = rand(6..12).hours.from_now
# MR
Timecop.travel 5.days.from_now
branch_name = "#{FFaker::Product.brand}-#{FFaker::Product.brand}-#{rand(1000)}"
@project.repository.add_branch(@user, branch_name, 'master')
merge_request = MergeRequest.create(target_project: @project, source_project: @project, source_branch: branch_name, target_branch: 'master', title: branch_name, author: @user)
merge_request_metrics = merge_request.metrics
# MR closing issues
Timecop.travel 5.days.from_now
MergeRequestsClosingIssues.create!(issue: issue, merge_request: merge_request)
# Merge
Timecop.travel 5.days.from_now
merge_request_metrics.merged_at = rand(6..12).hours.from_now
# Start build
Timecop.travel 5.days.from_now
merge_request_metrics.latest_build_started_at = rand(6..12).hours.from_now
# Finish build
Timecop.travel 5.days.from_now
merge_request_metrics.latest_build_finished_at = rand(6..12).hours.from_now
# Deploy to production
Timecop.travel 5.days.from_now
merge_request_metrics.first_deployed_to_production_at = rand(6..12).hours.from_now
issue_metrics.save!
merge_request_metrics.save!
print '.'
end
end
def seed! def seed!
Sidekiq::Testing.inline! do Sidekiq::Testing.inline! do
issues = create_issues(@project) issues = create_issues
print '.' puts '.'
# Stage 1 # Stage 1
Timecop.travel 5.days.from_now Timecop.travel 5.days.from_now
...@@ -62,7 +117,7 @@ class Gitlab::Seeder::CycleAnalytics ...@@ -62,7 +117,7 @@ class Gitlab::Seeder::CycleAnalytics
private private
def create_issues(project) def create_issues
Array.new(@issue_count) do Array.new(@issue_count) do
issue_params = { issue_params = {
title: "Cycle Analytics: #{FFaker::Lorem.sentence(6)}", title: "Cycle Analytics: #{FFaker::Lorem.sentence(6)}",
...@@ -182,6 +237,9 @@ Gitlab::Seeder.quiet do ...@@ -182,6 +237,9 @@ Gitlab::Seeder.quiet do
elsif ENV['CYCLE_ANALYTICS_PERF_TEST'] elsif ENV['CYCLE_ANALYTICS_PERF_TEST']
seeder = Gitlab::Seeder::CycleAnalytics.new(Project.order(:id).first, perf: true) seeder = Gitlab::Seeder::CycleAnalytics.new(Project.order(:id).first, perf: true)
seeder.seed! seeder.seed!
elsif ENV['CYCLE_ANALYTICS_POPULATE_METRICS_DIRECTLY']
seeder = Gitlab::Seeder::CycleAnalytics.new(Project.order(:id).first, perf: true)
seeder.seed_metrics!
else else
puts "Not running the cycle analytics seed file. Use the `SEED_CYCLE_ANALYTICS` environment variable to enable it." puts "Not running the cycle analytics seed file. Use the `SEED_CYCLE_ANALYTICS` environment variable to enable it."
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