Created
August 8, 2013 18:01
-
-
Save litch/6187015 to your computer and use it in GitHub Desktop.
MongoDB Logger
Request timing percentile finder
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PercentileFinder | |
attr_accessor :action, :controller, :limit, :calls, :dataset | |
def initialize(action, controller, limit = 1000) | |
self.action = action | |
self.controller = controller | |
self.limit = limit | |
end | |
def find(percentile_looking_for = 0.95) | |
populate_dataset unless dataset | |
return "No data" if dataset.size == 0 | |
percentile(dataset, percentile_looking_for) | |
end | |
def populate_dataset | |
db = Rails.logger.mongo_adapter.connection | |
collection = Rails.logger.mongo_adapter.collection | |
@calls = collection.where({action: action, controller: controller}).sort(request_time: -1).limit(limit) | |
self.dataset = [] | |
@calls.each do |call| | |
dataset << call["runtime"] | |
end | |
end | |
def percentile(values, percentile) | |
values_sorted = values.sort | |
k = (percentile*(values_sorted.length-1)+1).floor - 1 | |
f = (percentile*(values_sorted.length-1)+1).modulo(1) | |
values_sorted[k] + (f * (values_sorted[k+1] - values_sorted[k])) | |
end | |
end | |
>> require 'percentile_finder' | |
=> true | |
>> PercentileFinder.new('index', 'categories', 1000).find(0.95) | |
=> 1288.2999999999997 | |
>> PercentileFinder.new('index', 'categories', 1000).find(0.5) | |
=> 352.0 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment