From 33d09dbb3b256fb824831a229abdb3189f168135 Mon Sep 17 00:00:00 2001 From: Nick LaMuro Date: Mon, 11 Feb 2019 13:14:18 -0600 Subject: [PATCH] [Service#power_state] Avoid update if values match As part of the getter for `Service#power_state`, avoid calling an `update_attributes` if the value is already what we are going to update it to. This avoids not only some SQL queries, but some YAML serialization that slows down API requests and reports that use this `virtual_attribute`. Benchmarks: ----------- These benchmarks are done against the API doing the following request: ``` url: /api/services attributes: expand: resources attributes: -picture - picture.image_href - chargeback_report - evm_owner.userid - v_total_vms - power_state - all_service_children - tags filter[]: ancestry=null sort_by: created_at sort_options: nil sort_order: desc limit: 20 offset: 0 ``` And about half of the attributes need to trigger an update. Because of the way the code is configured and that the metrics were taken against a database that is detached from the provider (the `power_states_match?` call is consistent every time as a result), this happens consistently across runs. **Before** | ms | queries | query (ms) | rows | | ---: | ---: | ---: | ---: | | 2060 | 322 | 152 | 1627 | | 799 | 321 | 132.5 | 196 | | 839 | 321 | 134.2 | 196 | **After** | ms | queries | query (ms) | rows | | ---: | ---: | ---: | ---: | | 1976 | 290 | 164 | 1625 | | 675 | 289 | 128.4 | 194 | | 686 | 289 | 128.4 | 194 | --- app/models/service.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app/models/service.rb b/app/models/service.rb index 807262235a2..f8fb100f364 100644 --- a/app/models/service.rb +++ b/app/models/service.rb @@ -262,7 +262,9 @@ def map_power_states(action) end def update_power_status(action) - options[:power_status] = "#{action}_complete" + expected_status = "#{action}_complete" + return true if options[:power_status] == expected_status + options[:power_status] = expected_status update_attributes(:options => options) end