From 96bc9487c49068a221161fba4b75f627d869095c Mon Sep 17 00:00:00 2001 From: Seth Call Date: Thu, 29 Jan 2015 16:13:40 -0600 Subject: [PATCH] * merged --- db/manifest | 3 ++- db/up/discard_scores_optimized.sql | 11 +++++++++ ruby/lib/jam_ruby.rb | 1 + ruby/lib/jam_ruby/resque/resque_hooks.rb | 1 - ruby/lib/jam_ruby/resque/stress_job.rb | 29 ++++++++++++++++++++++++ ruby/spec/jam_ruby/models/score_spec.rb | 10 ++++---- web/lib/tasks/stress_resque.rake | 8 +++++++ 7 files changed, 56 insertions(+), 7 deletions(-) create mode 100644 db/up/discard_scores_optimized.sql create mode 100644 ruby/lib/jam_ruby/resque/stress_job.rb create mode 100644 web/lib/tasks/stress_resque.rake diff --git a/db/manifest b/db/manifest index 0a15a0b99..fdfec768a 100755 --- a/db/manifest +++ b/db/manifest @@ -245,4 +245,5 @@ text_messages.sql text_message_migration.sql user_model_about_changes.sql performance_samples.sql -user_presences.sql \ No newline at end of file +user_presences.sql +discard_scores_optimized.sql \ No newline at end of file diff --git a/db/up/discard_scores_optimized.sql b/db/up/discard_scores_optimized.sql new file mode 100644 index 000000000..72eb582cf --- /dev/null +++ b/db/up/discard_scores_optimized.sql @@ -0,0 +1,11 @@ +DROP FUNCTION IF EXISTS discard_scores(); + +CREATE FUNCTION discard_scores (INTEGER keep) RETURNS VOID AS $$ +BEGIN + + DELETE FROM scores WHERE score_dt > + (SELECT score_dt FROM scores s WHERE s.alocidispid = scores.alocidispid AND s.blocidispid = scores.blocidispid ORDER BY DESC score_dt LIMIT 1 OFFSET (keep * 2)); + + RETURN; +END; +$$ LANGUAGE plpgsql; \ No newline at end of file diff --git a/ruby/lib/jam_ruby.rb b/ruby/lib/jam_ruby.rb index 084ae7d9e..91c2063ac 100755 --- a/ruby/lib/jam_ruby.rb +++ b/ruby/lib/jam_ruby.rb @@ -42,6 +42,7 @@ require "jam_ruby/resque/resque_hooks" require "jam_ruby/resque/audiomixer" require "jam_ruby/resque/quick_mixer" require "jam_ruby/resque/icecast_config_writer" +require "jam_ruby/resque/stress_job" require "jam_ruby/resque/scheduled/audiomixer_retry" require "jam_ruby/resque/scheduled/icecast_config_retry" require "jam_ruby/resque/scheduled/icecast_source_check" diff --git a/ruby/lib/jam_ruby/resque/resque_hooks.rb b/ruby/lib/jam_ruby/resque/resque_hooks.rb index e58298569..be09d0925 100644 --- a/ruby/lib/jam_ruby/resque/resque_hooks.rb +++ b/ruby/lib/jam_ruby/resque/resque_hooks.rb @@ -35,7 +35,6 @@ Resque.before_first_fork do end JamRuby::Stats.init(config) - end # https://devcenter.heroku.com/articles/forked-pg-connections Resque.before_fork do diff --git a/ruby/lib/jam_ruby/resque/stress_job.rb b/ruby/lib/jam_ruby/resque/stress_job.rb new file mode 100644 index 000000000..b5004dd11 --- /dev/null +++ b/ruby/lib/jam_ruby/resque/stress_job.rb @@ -0,0 +1,29 @@ +require 'resque' + +module JamRuby + + # this job exists as a way to manually test a bunch of jobs firing at once. It's not a real job. + class StressJob + extend JamRuby::ResqueStats + + @queue = :stress_job + + @@log = Logging.logger[StressJob] + + + def self.perform + + @@log.debug("STARTING") + 100.times do + user = User.first.id + diagnostic = Diagnostic.first.user_id + count = Diagnostic.all.count + end + @@log.debug("ENDING") + + end + end + +end + + diff --git a/ruby/spec/jam_ruby/models/score_spec.rb b/ruby/spec/jam_ruby/models/score_spec.rb index 0e87f433d..84010cc18 100644 --- a/ruby/spec/jam_ruby/models/score_spec.rb +++ b/ruby/spec/jam_ruby/models/score_spec.rb @@ -522,7 +522,7 @@ describe Score do it "works" do Score.createx(LOCA, NODEA, ADDRA, LOCB, NODEB, ADDRB, 20, nil) Score.count.should == 2 - Score.connection.execute("SELECT discard_scores()").check + Score.connection.execute("SELECT discard_scores(5)").check Score.count.should == 2 end @@ -535,12 +535,12 @@ describe Score do Score.createx(LOCA, NODEA, ADDRA, LOCB, NODEB, ADDRB, 20, nil) Score.count.should == 12 - Score.connection.execute("SELECT discard_scores()").check + Score.connection.execute("SELECT discard_scores(5)").check Score.count.should == 12 Score.createx(LOCA, NODEA, ADDRA, LOCB, NODEB, ADDRB, 26, nil) Score.connection.execute("UPDATE scores set created_at = TIMESTAMP '#{2.days.ago}' WHERE score = 26").cmdtuples.should == 2 - Score.connection.execute("SELECT discard_scores()").check + Score.connection.execute("SELECT discard_scores(5)").check Score.count.should == 12 Score.connection.execute("SELECT * FROM scores WHERE score = 20").ntuples.should == 12 Score.connection.execute("SELECT * FROM scores WHERE scorer = 0").ntuples.should == 6 @@ -555,12 +555,12 @@ describe Score do Score.createx(LOCB, NODEB, ADDRB, LOCA, NODEA, ADDRA, 22, nil) Score.count.should == 24 - Score.connection.execute("SELECT discard_scores()").check + Score.connection.execute("SELECT discard_scores(5)").check Score.count.should == 24 Score.createx(LOCB, NODEB, ADDRB, LOCA, NODEA, ADDRA, 36, nil) Score.connection.execute("UPDATE scores set created_at = TIMESTAMP '#{2.days.ago}' WHERE score = 36").cmdtuples.should == 2 - Score.connection.execute("SELECT discard_scores()").check + Score.connection.execute("SELECT discard_scores(5)").check Score.count.should == 24 Score.connection.execute("SELECT * FROM scores WHERE score = 22").ntuples.should == 12 Score.connection.execute("SELECT * FROM scores WHERE score = 22 AND scorer = 0").ntuples.should == 6 diff --git a/web/lib/tasks/stress_resque.rake b/web/lib/tasks/stress_resque.rake new file mode 100644 index 000000000..d59623120 --- /dev/null +++ b/web/lib/tasks/stress_resque.rake @@ -0,0 +1,8 @@ +task :stress_resque do + Rake::Task['environment'].invoke + + 10.times do + Resque.enqueue(StressJob) + end + +end