lib/HDRHistogram.rb in HDRHistogram-0.1.7 vs lib/HDRHistogram.rb in HDRHistogram-0.1.8

- old
+ new

@@ -19,10 +19,16 @@ self.normalizing_index_offset= m[:normalizing_index_offset].to_i self.conversion_ratio= m[:conversion_ratio].to_f counts_len = m[:counts_len].to_i self.counts_len= counts_len self.total_count= m[:total_count].to_i + if !opt[:multiplier] && m[:multiplier] + @multiplier = m[:multiplier].to_f + end + if !@unit && m[:unit] + @unit = m[:unit] + end counts = m[:counts].split " " i=0 shorted = 0 counts.each do |count| @@ -97,25 +103,63 @@ str << sprintf("%7.3f%% #{pctf}%s\n", pct, percentile(pct), unit) end str end + def serialize + attrs = [lowest_trackable_value, highest_trackable_value, unit_magnitude, significant_figures, sub_bucket_half_count_magnitude, sub_bucket_half_count, sub_bucket_mask, sub_bucket_count, bucket_count, min_value, max_value, normalizing_index_offset, ("%f" % conversion_ratio), counts_len, total_count] + + raw_counts = [] + numrun="~!@#$%^&*" + + for i in 0...counts_len do + raw_counts << get_raw_count(i) + end + + counts = [] + + while raw_counts.length > 0 do + num = raw_counts.shift + n = 1 + if num < numrun.length + while raw_counts[0] == num + raw_counts.shift + n+=1 + end + if n > 1 + counts << "#{numrun[num]}#{n}" + else + counts << num + end + else + counts << num + end + end + + out = "#{attrs.join " "} [#{counts.join " "} ]" + if @unit || @multiplier != 1 + out << " (#{unit} #{multiplier})" + end + out + end + def self.adjusted_boundary_val(val, opt={}) return opt ? val * 1/(opt[:multiplier] || 1) : val end private_class_method :adjusted_boundary_val def self.unserialize(str, opt={}) - regex = /^(?<lowest_trackable_value>\d+) (?<highest_trackable_value>\d+) (?<unit_magnitude>\d+) (?<significant_figures>\d+) (?<sub_bucket_half_count_magnitude>\d+) (?<sub_bucket_half_count>\d+) (?<sub_bucket_mask>\d+) (?<sub_bucket_count>\d+) (?<bucket_count>\d+) (?<min_value>\d+) (?<max_value>\d+) (?<normalizing_index_offset>\d+) (?<conversion_ratio>\S+) (?<counts_len>\d+) (?<total_count>\d+) \[(?<counts>([~!@#$%^&*]?\d+ )+)\]/ + regex = /^(?<lowest_trackable_value>\d+) (?<highest_trackable_value>\d+) (?<unit_magnitude>\d+) (?<significant_figures>\d+) (?<sub_bucket_half_count_magnitude>\d+) (?<sub_bucket_half_count>\d+) (?<sub_bucket_mask>\d+) (?<sub_bucket_count>\d+) (?<bucket_count>\d+) (?<min_value>\d+) (?<max_value>\d+) (?<normalizing_index_offset>\d+) (?<conversion_ratio>\S+) (?<counts_len>\d+) (?<total_count>\d+) \[(?<counts>([~!@#$%^&*]?\d+ )+)\]( \((?<unit>.*) (?<multiplier>\S+)\))?/ m = str.match regex raise HDRHistogramError, "invalid serialization pattern" if m.nil? opt[:unserialized]=m + multiplier = opt[:multiplier] || 1 - low = m[:lowest_trackable_value].to_i * (opt[:multiplier] || 1) - high = m[:highest_trackable_value].to_i * (opt[:multiplier] || 1) + low = m[:lowest_trackable_value].to_i * multiplier + high = m[:highest_trackable_value].to_i * multiplier hdrh = self.new(low, high, m[:significant_figures].to_i, opt) return hdrh end end