Class: StreamStats::Stream

Inherits:
Object
  • Object
show all
Defined in:
lib/stream_stats/stream.rb,
ext/stream_stats/stream_stats.c

Instance Method Summary collapse

Constructor Details

#initialize(rb_eps, rb_quantiles) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'ext/stream_stats/stream_stats.c', line 13

static VALUE strstat_timer_init(VALUE self, VALUE rb_eps, VALUE rb_quantiles) {

  timer *i_timer = (timer *) malloc(sizeof(timer));

  double eps = NUM2DBL(rb_eps);
  double *quantiles;
  uint32_t num_quantiles;

  switch (TYPE(rb_quantiles)) {
    case T_ARRAY:
      rb_iv_set(self, "@quantiles", rb_quantiles);
      num_quantiles = RARRAY_LEN(rb_quantiles);
      if (num_quantiles < 1)
        rb_raise(rb_eRuntimeError, "no quantiles defined");
      quantiles = malloc(sizeof(double) * num_quantiles);
      for (int i = 0; i < num_quantiles; i++) {
        quantiles[i] = NUM2DBL(rb_ary_entry(rb_quantiles, i));
      }
      break;
    default:
      /* raise exception */
      rb_raise(rb_eTypeError, "not valid value");
      break;
  }

  init_timer(eps, quantiles, num_quantiles, i_timer);

  VALUE data = Data_Wrap_Struct(timer_class, NULL, strstat_timer_free, i_timer);
  rb_ivar_set(self, rb_intern("internal_struct"), data);

  return Qnil;
}

Instance Method Details

#<<(rb_sample) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'ext/stream_stats/stream_stats.c', line 54

static VALUE strstat_timer_add_sample(VALUE self, VALUE rb_sample) {

  double sample = NUM2DBL(rb_sample);

  timer *i_timer = (timer*) strstat_get_struct(self);

  int returned = timer_add_sample(i_timer, sample);
  if (returned != 0) {
    rb_raise(rb_eRuntimeError, "add sample returned %d", returned);
  }

  return Qnil;
}

#countObject



68
69
70
71
72
# File 'ext/stream_stats/stream_stats.c', line 68

static VALUE strstat_timer_count(VALUE self) {
  timer *i_timer = (timer*) strstat_get_struct(self);

  return LONG2NUM(timer_count(i_timer));
}

#get_quantilesObject



4
5
6
7
8
# File 'lib/stream_stats/stream.rb', line 4

def get_quantiles
  Hash[@quantiles.map do |q|
    [q, quantile(q)]
  end]
end

#inspectObject



10
11
12
13
14
15
# File 'lib/stream_stats/stream.rb', line 10

def inspect
  attr_list = [:count, :sum, :min, :max, :mean, :stddev].map do |method|
    "#{method.to_s}: #{self.send(method)}"
  end * ', '
  "#{self.to_s} {#{attr_list}, quantiles: #{get_quantiles.to_s}}"
end

#maxObject



99
100
101
# File 'ext/stream_stats/stream_stats.c', line 99

static VALUE strstat_timer_max(VALUE self) {
  return strstat_timer_commoncall(self, timer_max);
}

#meanObject



102
103
104
# File 'ext/stream_stats/stream_stats.c', line 102

static VALUE strstat_timer_mean(VALUE self) {
  return strstat_timer_commoncall(self, timer_mean);
}

#minObject



96
97
98
# File 'ext/stream_stats/stream_stats.c', line 96

static VALUE strstat_timer_min(VALUE self) {
  return strstat_timer_commoncall(self, timer_min);
}

#percentile(rb_percentile) ⇒ Object



83
84
85
86
87
88
89
# File 'ext/stream_stats/stream_stats.c', line 83

static VALUE strstat_timer_percentile(VALUE self, VALUE rb_percentile) {
  int percentile = NUM2INT(rb_percentile);
  if (percentile < 0 || percentile > 100)
    rb_raise(rb_eRuntimeError, "invalid percentile");

  return strstat_timer_query(self, DBL2NUM(percentile / 100.0));
}

#quantile(rb_query) ⇒ Object



74
75
76
77
78
79
80
81
# File 'ext/stream_stats/stream_stats.c', line 74

static VALUE strstat_timer_query(VALUE self, VALUE rb_query) {
  double query = NUM2DBL(rb_query);
  if (query < 0 || query > 1)
    rb_raise(rb_eRuntimeError, "invalid quantile");

  timer *i_timer = (timer*) strstat_get_struct(self);
  return DBL2NUM(timer_query(i_timer, query));
}

#squared_sumObject



111
112
113
# File 'ext/stream_stats/stream_stats.c', line 111

static VALUE strstat_timer_squared_sum(VALUE self) {
  return strstat_timer_commoncall(self, timer_squared_sum);
}

#stddevObject



105
106
107
# File 'ext/stream_stats/stream_stats.c', line 105

static VALUE strstat_timer_stddev(VALUE self) {
  return strstat_timer_commoncall(self, timer_stddev);
}

#sumObject



108
109
110
# File 'ext/stream_stats/stream_stats.c', line 108

static VALUE strstat_timer_sum(VALUE self) {
  return strstat_timer_commoncall(self, timer_sum);
}