class HDRHistogram

Constants

VERSION

Attributes

multiplier[R]
unit[R]

Public Class Methods

new(p1, p2, p3, p4 = v4) click to toggle source
static VALUE histogram_new(int argc, VALUE* argv, VALUE class) {
  VALUE                    self, lowest_value, highest_value, significant_figures;
  VALUE                    opt;
  
  struct hdr_histogram    *hdrh;
  int                      ret;
  
  rb_scan_args(argc, argv, "31", &lowest_value, &highest_value, &significant_figures, &opt);
  
  lowest_value = rb_funcall(class, rb_intern("adjusted_boundary_val"), 2, lowest_value, opt);
  highest_value = rb_funcall(class, rb_intern("adjusted_boundary_val"), 2, highest_value, opt);
  
  ret = hdr_init(NUM2INT(lowest_value), NUM2INT(highest_value), NUM2INT(significant_figures), &hdrh);
  if(ret == EINVAL) {
    rb_raise(HDRHistogramError, "%s", "lowest_trackable_value must be >= 1");
  }
  else if(ret == ENOMEM) {
    rb_raise(HDRHistogramError, "%s", "no memory");
  }
  
  self = Data_Wrap_Struct(class, NULL, histogram_free, hdrh);
  rb_obj_call_init(self, argc, argv);
  
  return self;
}
new(lowest, highest, sig, opt={}) click to toggle source
# File lib/HDRHistogram.rb, line 5
def initialize(lowest, highest, sig, opt={})
  @multiplier = opt[:multiplier] || 1
  @unit = opt[:unit] || opt[:units]
  
  if opt[:unserialized]
    m=opt[:unserialized]
    self.unit_magnitude= m[:unit_magnitude].to_i
    self.sub_bucket_half_count_magnitude= m[:sub_bucket_half_count_magnitude].to_i
    self.sub_bucket_half_count= m[:sub_bucket_half_count].to_i
    self.sub_bucket_mask= m[:sub_bucket_mask].to_i
    self.sub_bucket_count= m[:sub_bucket_count].to_i
    self.bucket_count= m[:bucket_count].to_i
    self.min_value= m[:min_value].to_i
    self.max_value= m[:max_value].to_i
    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|
      nf="~!@#$%^&*"
      m = count.match /^([#{nf}])(\d+)$/
      if m && nf.index(m[1])
        shorted += 1
        m[2].to_i.times do
          set_raw_count(i, nf.index(m[1]))
          i+=1
        end
      else
        set_raw_count(i, count.to_i)
        i+=1
      end
    end
    if counts_len != i
      raise HDRHistogramError, "invalid serialization pattern: total count doesn't match, expected: #{counts_len}, found #{i} (diff: #{counts_len - i}), counts.count: #{counts.count}, shorted: #{shorted}"
    end
  end
end
unserialize(str, opt={}) click to toggle source
# File lib/HDRHistogram.rb, line 150
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+ )+)\]( \((?<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 * multiplier
  high = m[:highest_trackable_value].to_i * multiplier
  hdrh = self.new(low, high, m[:significant_figures].to_i, opt)
  
  return hdrh
end

Private Class Methods

adjusted_boundary_val(val, opt={}) click to toggle source
# File lib/HDRHistogram.rb, line 145
def self.adjusted_boundary_val(val, opt={})
  return opt ? val * 1/(opt[:multiplier] || 1) : val
end

Public Instance Methods

clone() click to toggle source
static VALUE histogram_clone(VALUE self_src) {
  GET_HDRHIST(hdr_src, self_src);
  VALUE                    self;
  struct hdr_histogram    *hdrh;
  int                      ret, i;
  ret = hdr_init(hdr_src->lowest_trackable_value, hdr_src->highest_trackable_value, hdr_src->significant_figures, &hdrh);
  if(ret == EINVAL) {
    rb_raise(HDRHistogramError, "%s", "lowest_trackable_value must be >= 1");
  }
  else if(ret == ENOMEM) {
    rb_raise(HDRHistogramError, "%s", "no memory");
  }
  else if(hdr_src->counts_len != hdrh->counts_len) {
    rb_raise(HDRHistogramError, "%s", "bad hdrhistogram ccopy");
  }
  self = Data_Wrap_Struct(HDRHistogram, NULL, histogram_free, hdrh);
  
  hdrh->lowest_trackable_value = hdr_src->lowest_trackable_value;
  hdrh->highest_trackable_value = hdr_src->highest_trackable_value;
  hdrh->unit_magnitude = hdr_src->unit_magnitude;
  hdrh->significant_figures = hdr_src->significant_figures;
  hdrh->sub_bucket_half_count_magnitude = hdr_src->sub_bucket_half_count_magnitude;
  hdrh->sub_bucket_half_count = hdr_src->sub_bucket_half_count;
  hdrh->sub_bucket_mask = hdr_src->sub_bucket_mask;
  hdrh->sub_bucket_count = hdr_src->sub_bucket_count;
  hdrh->bucket_count = hdr_src->bucket_count;
  hdrh->min_value = hdr_src->min_value;
  hdrh->max_value = hdr_src->max_value;
  hdrh->normalizing_index_offset = hdr_src->normalizing_index_offset;
  hdrh->conversion_ratio = hdr_src->conversion_ratio;
  hdrh->counts_len = hdr_src->counts_len;
  hdrh->total_count = hdr_src->total_count;
  
  for(i=0; i<hdrh->counts_len; i++) {
    hdrh->counts[i] = hdr_src->counts[i];
  }
  
  VALUE lowest = INT2NUM(hdr_src->lowest_trackable_value);
  VALUE highest = INT2NUM(hdr_src->highest_trackable_value);
  VALUE sig = INT2NUM(hdr_src->significant_figures);
  VALUE opt = rb_hash_new();
  rb_hash_aset(opt, ID2SYM(rb_intern("multiplier")), rb_iv_get(self_src, "@multiplier"));
  rb_hash_aset(opt, ID2SYM(rb_intern("unit")), rb_iv_get(self_src, "@unit"));
  
  VALUE argv[4];
  VALUE argc = 4;
  argv[0]=lowest;
  argv[1]=highest;
  argv[2]=sig;
  argv[3]=opt;
  
  rb_obj_call_init(self, argc, argv);
  return self;
}
Also aliased as: dup
count() click to toggle source
static VALUE histogram_count(VALUE self) {
  GET_HDRHIST(hdr, self);
  return INT2NUM(hdr->total_count);
}
dup()
Alias for: clone
latency_stats() click to toggle source
# File lib/HDRHistogram.rb, line 93
def latency_stats
  str = "Latency Stats\n"
  str << stats([ 50.0, 75.0, 90.0, 99.0, 99.9, 99.99, 99.999, 100.0 ])
  
end
max() click to toggle source
# File lib/HDRHistogram.rb, line 63
def max
  raw_max * @multiplier
end
mean() click to toggle source
# File lib/HDRHistogram.rb, line 66
def mean
  raw_mean * @multiplier
end
memsize() click to toggle source
static VALUE histogram_memsize(VALUE self) {
  GET_HDRHIST(hdr, self);
  return INT2NUM(hdr_get_memory_size(hdr));
}
merge!(other) click to toggle source
# File lib/HDRHistogram.rb, line 75
def merge!(other)
  if self == other 
    raise HDRHistogramError, "can't merge histogram with itself"
  end
  if other.multiplier != multiplier
    raise HDRHistogramError, "can't merge histograms with different multipliers"
  end
  if other.unit != unit
    raise HDRHistogramError, "can't merge histograms with different units"
  end
  raw_merge other
  self
end
min() click to toggle source
# File lib/HDRHistogram.rb, line 60
def min
  raw_min * @multiplier
end
percentile(pct) click to toggle source
# File lib/HDRHistogram.rb, line 72
def percentile(pct)
  raw_percentile(pct) * @multiplier
end
record(val) click to toggle source
# File lib/HDRHistogram.rb, line 54
def record(val)
  raw_record(val * 1/@multiplier)
end
record_corrected(val, expected_interval) click to toggle source
# File lib/HDRHistogram.rb, line 57
def record_corrected(val, expected_interval)
  raw_record_corrected(val * 1/@multiplier, expected_interval * 1/@multiplier)
end
reset() click to toggle source
static VALUE histogram_reset(VALUE self) {
  GET_HDRHIST(hdr, self);
  hdr_reset(hdr);
  return self;
}
serialize() click to toggle source
# File lib/HDRHistogram.rb, line 108
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
stats(percentiles = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]) click to toggle source
# File lib/HDRHistogram.rb, line 99
def stats(percentiles = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100])
  str = ""
  pctf = @multiplier < 1 ? "%12.#{Math.log(0.001, 10).abs.ceil}f" : "%12u"
  percentiles.each do |pct|
    str << sprintf("%7.3f%% #{pctf}%s\n", pct, percentile(pct), unit)
  end
  str
end
stddev() click to toggle source
# File lib/HDRHistogram.rb, line 69
def stddev
  raw_stddev * @multiplier
end
to_s() click to toggle source
# File lib/HDRHistogram.rb, line 89
def to_s
  stats
end

Private Instance Methods

get_raw_count(p1) click to toggle source
static VALUE histogram_get_raw_count(VALUE self, VALUE index) {
  VALUE   count;
  int     i = NUM2INT(index);
  GET_HDRHIST(hdr, self);
  if(i >= hdr->counts_len) {
    return Qnil;
  }
  else {
    count = LL2NUM(hdr->counts[i]);
    return count;
  }
}
raw_max() click to toggle source
static VALUE histogram_max(VALUE self) {
  return generic_histogram_intval(self, hdr_max);
}
raw_mean() click to toggle source
static VALUE histogram_mean(VALUE self) {
  return generic_histogram_floatval(self, hdr_mean);
}
raw_merge(p1) click to toggle source
static VALUE histogram_merge(VALUE self, VALUE another ) {
  GET_HDRHIST(hdr, self);
  GET_HDRHIST(hdr2, another);
  return INT2NUM(hdr_add(hdr, hdr2));
}
raw_min() click to toggle source
static VALUE histogram_min(VALUE self) {
  return generic_histogram_intval(self, hdr_min);
}
raw_percentile(p1) click to toggle source
static VALUE histogram_percentile(VALUE self, VALUE percentile ) {
  GET_HDRHIST(hdr, self);
  return INT2NUM(hdr_value_at_percentile(hdr, NUM2DBL(percentile)));
}
raw_record(p1) click to toggle source
static VALUE histogram_record_value(VALUE self, VALUE val) {
  GET_HDRHIST(hdr, self);
  return hdr_record_value(hdr, NUM2INT(val)) ? Qtrue : Qfalse;
}
raw_record_corrected(p1, p2) click to toggle source
static VALUE histogram_record_corrected_value(VALUE self, VALUE val, VALUE expected_interval) {
  GET_HDRHIST(hdr, self);
  return hdr_record_corrected_value(hdr, NUM2INT(val), NUM2INT(expected_interval)) ? Qtrue : Qfalse;
}
raw_stddev() click to toggle source
static VALUE histogram_stddev(VALUE self) {
  return generic_histogram_floatval(self, hdr_stddev);
}
set_raw_count(p1, p2) click to toggle source
static VALUE histogram_set_raw_count(VALUE self, VALUE index, VALUE count) {
  GET_HDRHIST(hdr, self);
  int     i = NUM2INT(index);
  int64_t c = NUM2LL(count);
  hdr->counts[i]=c;
  return Qtrue;
}