HDRHistogram 0.1.7 → 0.1.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d475c4c4cbb66898aeff2e9dfdeccbaf9b8e7e000920332076a63b05191ccc6f
4
- data.tar.gz: a409b91d6a9fe2dd87ed4e4e74f2f6425dbea2743901540bcd0908915b1861ac
3
+ metadata.gz: 99f2a4bb0a67363e2249f38122fc2b18bf5cd49045386030c969b2bf8ac6edb1
4
+ data.tar.gz: 6bcfe26d7ed7c0abf786b100824a476d534c31aa86d94c9116d803d7f38d57f8
5
5
  SHA512:
6
- metadata.gz: 4733bc0361f91f457b3fe462cac735296e9190a69f95cb8aab09ec864dec9e4da91e3935aae26f240397e7504bf3310da2e3f29d842dfb75c4c8a631efd39e4c
7
- data.tar.gz: 4f1ec7b4af487e4a7034e1d552959dd3326757a9ac3760bb664779ec2ef73cc5284a644b241d43d76cb2bee47b4b48e33f70087cd9da020a640df086b0d39067
6
+ metadata.gz: 7b2e8a471d12a7689200361eb5a23197ca194ea11aa4d8e05beaa966877a37d84740018a6e72336ee2d092fdfdafdccd101440e946f4f06bdf549476f5aae20a
7
+ data.tar.gz: ab07d7649ded6ee83d6ac733a4ba3e534334e5a974b41ea622a8303c004cba6feac980fdab079908e44df79f60f1ac8510ae112367fc4f8f4711242013d65c6a
data/README.md CHANGED
@@ -77,8 +77,9 @@ Create new HDRHistogram object.
77
77
  - `significant_figures`: The level of precision for this histogram, i.e. the number of figures in a decimal number that will be maintained. E.g. a value of 3 will mean the results from the histogram will be accurate up to the first three digits. Must be a value between 1 and 5 (inclusive).
78
78
  - `:multiplier`: A multiplier to adjust all incoming values. If present, the raw value recorded in the histogram for `record(val)` will be `val * 1/multiplier`. Similarly, `percentile(pctl) => val * multiplier`. If `multiplier` < 1, `lowest_value` can be < 1 so that `lowest_value` * 1/`multiplier` == 1.
79
79
  - `:unit`: A unit for labeling histogram values. Useful for outputting things.
80
-
81
-
80
+
81
+ #### `hdr = HDRHistogram.unserialize(serialized_histogram_string)`
82
+ restore an HDRHistogram object and its data from serialized string.
82
83
 
83
84
  #### `hdr.record(value)`
84
85
  Records a `value` in the histogram, will round this `value` of to a precision at or better than the `significant_figures` specified at construction time.
@@ -146,4 +147,9 @@ Get a formatted string with percentile stats of the histogram useful for latency
146
147
  # 99.999% 1000.447ms
147
148
  # 100.000% 1000.447ms
148
149
  ```
150
+
149
151
  The above output assumes a :multiplier of 0.001 and a :unit of `:ms`
152
+
153
+ #### `hdr.serialize`
154
+ serialize the HDRHistogram object and data into a string
155
+
@@ -41,6 +41,61 @@ static VALUE histogram_new(int argc, VALUE* argv, VALUE class) {
41
41
  return self;
42
42
  }
43
43
 
44
+ static VALUE histogram_clone(VALUE self_src) {
45
+ GET_HDRHIST(hdr_src, self_src);
46
+ VALUE self;
47
+ struct hdr_histogram *hdrh;
48
+ int ret, i;
49
+ ret = hdr_init(hdr_src->lowest_trackable_value, hdr_src->highest_trackable_value, hdr_src->significant_figures, &hdrh);
50
+ if(ret == EINVAL) {
51
+ rb_raise(HDRHistogramError, "%s", "lowest_trackable_value must be >= 1");
52
+ }
53
+ else if(ret == ENOMEM) {
54
+ rb_raise(HDRHistogramError, "%s", "no memory");
55
+ }
56
+ else if(hdr_src->counts_len != hdrh->counts_len) {
57
+ rb_raise(HDRHistogramError, "%s", "bad hdrhistogram ccopy");
58
+ }
59
+ self = Data_Wrap_Struct(HDRHistogram, NULL, histogram_free, hdrh);
60
+
61
+ hdrh->lowest_trackable_value = hdr_src->lowest_trackable_value;
62
+ hdrh->highest_trackable_value = hdr_src->highest_trackable_value;
63
+ hdrh->unit_magnitude = hdr_src->unit_magnitude;
64
+ hdrh->significant_figures = hdr_src->significant_figures;
65
+ hdrh->sub_bucket_half_count_magnitude = hdr_src->sub_bucket_half_count_magnitude;
66
+ hdrh->sub_bucket_half_count = hdr_src->sub_bucket_half_count;
67
+ hdrh->sub_bucket_mask = hdr_src->sub_bucket_mask;
68
+ hdrh->sub_bucket_count = hdr_src->sub_bucket_count;
69
+ hdrh->bucket_count = hdr_src->bucket_count;
70
+ hdrh->min_value = hdr_src->min_value;
71
+ hdrh->max_value = hdr_src->max_value;
72
+ hdrh->normalizing_index_offset = hdr_src->normalizing_index_offset;
73
+ hdrh->conversion_ratio = hdr_src->conversion_ratio;
74
+ hdrh->counts_len = hdr_src->counts_len;
75
+ hdrh->total_count = hdr_src->total_count;
76
+
77
+ for(i=0; i<hdrh->counts_len; i++) {
78
+ hdrh->counts[i] = hdr_src->counts[i];
79
+ }
80
+
81
+ VALUE lowest = INT2NUM(hdr_src->lowest_trackable_value);
82
+ VALUE highest = INT2NUM(hdr_src->highest_trackable_value);
83
+ VALUE sig = INT2NUM(hdr_src->significant_figures);
84
+ VALUE opt = rb_hash_new();
85
+ rb_hash_aset(opt, rb_intern("multiplier"), rb_iv_get(self, "@multiplier"));
86
+ rb_hash_aset(opt, rb_intern("unit"), rb_iv_get(self, "@unit"));
87
+
88
+ VALUE argv[4];
89
+ VALUE argc = 4;
90
+ argv[0]=lowest;
91
+ argv[1]=highest;
92
+ argv[2]=sig;
93
+ argv[3]=opt;
94
+
95
+ rb_obj_call_init(self, argc, argv);
96
+ return self;
97
+ }
98
+
44
99
  static VALUE histogram_reset(VALUE self) {
45
100
  GET_HDRHIST(hdr, self);
46
101
  hdr_reset(hdr);
@@ -103,42 +158,38 @@ static VALUE histogram_merge(VALUE self, VALUE another ) {
103
158
  return INT2NUM(hdr_add(hdr, hdr2));
104
159
  }
105
160
 
106
- #define HISTOGRAM_GETINT_METHOD(int_name) \
107
- static VALUE histogram_##int_name(VALUE self) { \
161
+ #define HISTOGRAM_GETSETNUM_METHOD(num_name, num_type) \
162
+ static VALUE histogram_get_##num_name(VALUE self) { \
108
163
  GET_HDRHIST(hdr, self); \
109
- return INT2NUM(hdr->int_name); \
110
- }
111
-
112
- #define HISTOGRAM_SETNUM_METHOD(num_name, num_type) \
164
+ return num_type##2NUM(hdr->num_name); \
165
+ } \
166
+ \
113
167
  static VALUE histogram_set_##num_name(VALUE self, VALUE num) { \
114
168
  GET_HDRHIST(hdr, self); \
115
169
  hdr->num_name = NUM2##num_type(num); \
116
170
  return Qtrue; \
117
171
  }
118
172
 
119
- HISTOGRAM_SETNUM_METHOD(lowest_trackable_value, LL)
120
- HISTOGRAM_SETNUM_METHOD(highest_trackable_value, LL)
121
- HISTOGRAM_SETNUM_METHOD(unit_magnitude, LONG)
122
- HISTOGRAM_SETNUM_METHOD(significant_figures, LONG)
123
- HISTOGRAM_SETNUM_METHOD(sub_bucket_half_count_magnitude, LONG)
124
- HISTOGRAM_SETNUM_METHOD(sub_bucket_half_count, LONG)
125
- HISTOGRAM_SETNUM_METHOD(sub_bucket_mask, LL)
126
- HISTOGRAM_SETNUM_METHOD(sub_bucket_count, LONG)
127
- HISTOGRAM_SETNUM_METHOD(bucket_count, LONG)
128
- HISTOGRAM_SETNUM_METHOD(min_value, LL)
129
- HISTOGRAM_SETNUM_METHOD(max_value, LL)
130
- HISTOGRAM_SETNUM_METHOD(normalizing_index_offset, LONG)
131
- HISTOGRAM_SETNUM_METHOD(conversion_ratio, DBL)
132
- HISTOGRAM_SETNUM_METHOD(counts_len, LONG)
133
- HISTOGRAM_SETNUM_METHOD(total_count, LL)
134
-
135
- HISTOGRAM_GETINT_METHOD(lowest_trackable_value)
136
- HISTOGRAM_GETINT_METHOD(highest_trackable_value)
137
- HISTOGRAM_GETINT_METHOD(unit_magnitude)
138
- HISTOGRAM_GETINT_METHOD(significant_figures)
139
- HISTOGRAM_GETINT_METHOD(bucket_count)
140
- HISTOGRAM_GETINT_METHOD(sub_bucket_count)
141
- HISTOGRAM_GETINT_METHOD(counts_len)
173
+ HISTOGRAM_GETSETNUM_METHOD(lowest_trackable_value, LL)
174
+ HISTOGRAM_GETSETNUM_METHOD(highest_trackable_value, LL)
175
+ HISTOGRAM_GETSETNUM_METHOD(unit_magnitude, LONG)
176
+ HISTOGRAM_GETSETNUM_METHOD(significant_figures, LONG)
177
+ HISTOGRAM_GETSETNUM_METHOD(sub_bucket_half_count_magnitude, LONG)
178
+ HISTOGRAM_GETSETNUM_METHOD(sub_bucket_half_count, LONG)
179
+ HISTOGRAM_GETSETNUM_METHOD(sub_bucket_mask, LL)
180
+ HISTOGRAM_GETSETNUM_METHOD(sub_bucket_count, LONG)
181
+ HISTOGRAM_GETSETNUM_METHOD(bucket_count, LONG)
182
+ HISTOGRAM_GETSETNUM_METHOD(min_value, LL)
183
+ HISTOGRAM_GETSETNUM_METHOD(max_value, LL)
184
+ HISTOGRAM_GETSETNUM_METHOD(normalizing_index_offset, LONG)
185
+ HISTOGRAM_GETSETNUM_METHOD(conversion_ratio, DBL)
186
+ HISTOGRAM_GETSETNUM_METHOD(counts_len, LONG)
187
+ HISTOGRAM_GETSETNUM_METHOD(total_count, LL)
188
+
189
+ #define HISTOGRAM_RUBY_PRIVATE_GETSETTERS(num_name) \
190
+ rb_define_private_method(HDRHistogram, #num_name "=", histogram_set_##num_name, 1);\
191
+ rb_define_private_method(HDRHistogram, #num_name, histogram_get_##num_name, 0)
192
+
142
193
 
143
194
  static VALUE histogram_set_raw_count(VALUE self, VALUE index, VALUE count) {
144
195
  GET_HDRHIST(hdr, self);
@@ -147,6 +198,18 @@ static VALUE histogram_set_raw_count(VALUE self, VALUE index, VALUE count) {
147
198
  hdr->counts[i]=c;
148
199
  return Qtrue;
149
200
  }
201
+ static VALUE histogram_get_raw_count(VALUE self, VALUE index) {
202
+ VALUE count;
203
+ int i = NUM2INT(index);
204
+ GET_HDRHIST(hdr, self);
205
+ if(i >= hdr->counts_len) {
206
+ return Qnil;
207
+ }
208
+ else {
209
+ count = LL2NUM(hdr->counts[i]);
210
+ return count;
211
+ }
212
+ }
150
213
 
151
214
  void Init_ruby_hdr_histogram() {
152
215
  HDRHistogram = rb_define_class("HDRHistogram", rb_cObject);
@@ -160,6 +223,9 @@ void Init_ruby_hdr_histogram() {
160
223
  rb_define_method(HDRHistogram, "memsize", histogram_memsize, 0);
161
224
  rb_define_method(HDRHistogram, "count", histogram_count, 0);
162
225
 
226
+ rb_define_method(HDRHistogram, "clone", histogram_clone, 0);
227
+ rb_define_method(HDRHistogram, "dup", histogram_clone, 0);
228
+
163
229
  rb_define_private_method(HDRHistogram, "raw_record", histogram_record_value, 1);
164
230
  rb_define_private_method(HDRHistogram, "raw_record_corrected", histogram_record_corrected_value, 2);
165
231
  rb_define_private_method(HDRHistogram, "raw_min", histogram_min, 0);
@@ -169,34 +235,23 @@ void Init_ruby_hdr_histogram() {
169
235
  rb_define_private_method(HDRHistogram, "raw_percentile", histogram_percentile, 1);
170
236
  rb_define_private_method(HDRHistogram, "raw_merge", histogram_merge, 1);
171
237
 
172
- rb_define_private_method(HDRHistogram, "lowest_trackable_value=", histogram_set_lowest_trackable_value, 1);
173
- rb_define_private_method(HDRHistogram, "highest_trackable_value=", histogram_set_highest_trackable_value, 1);
174
- rb_define_private_method(HDRHistogram, "unit_magnitude=", histogram_set_unit_magnitude, 1);
175
- rb_define_private_method(HDRHistogram, "significant_figures=", histogram_set_significant_figures, 1);
176
- rb_define_private_method(HDRHistogram, "sub_bucket_half_count_magnitude=", histogram_set_sub_bucket_half_count_magnitude, 1);
177
- rb_define_private_method(HDRHistogram, "sub_bucket_half_count=", histogram_set_sub_bucket_half_count, 1);
178
- rb_define_private_method(HDRHistogram, "sub_bucket_mask=", histogram_set_sub_bucket_mask, 1);
179
- rb_define_private_method(HDRHistogram, "sub_bucket_count=", histogram_set_sub_bucket_count, 1);
180
- rb_define_private_method(HDRHistogram, "bucket_count=", histogram_set_bucket_count, 1);
181
- rb_define_private_method(HDRHistogram, "min_value=", histogram_set_min_value, 1);
182
- rb_define_private_method(HDRHistogram, "max_value=", histogram_set_max_value, 1);
183
- rb_define_private_method(HDRHistogram, "normalizing_index_offset=", histogram_set_normalizing_index_offset, 1);
184
- rb_define_private_method(HDRHistogram, "conversion_ratio=", histogram_set_conversion_ratio, 1);
185
- rb_define_private_method(HDRHistogram, "counts_len=", histogram_set_counts_len, 1);
186
- rb_define_private_method(HDRHistogram, "total_count=", histogram_set_total_count, 1);
187
- rb_define_private_method(HDRHistogram, "set_raw_count", histogram_set_raw_count, 2);
188
-
189
- rb_define_method(HDRHistogram, "lowest_trackable_value", histogram_lowest_trackable_value, 0);
190
- rb_define_method(HDRHistogram, "highest_trackable_value", histogram_highest_trackable_value, 0);
191
- rb_define_private_method(HDRHistogram, "unit_magnitude", histogram_unit_magnitude, 0);
192
- rb_define_method(HDRHistogram, "significant_figures", histogram_significant_figures, 0);
193
- rb_define_private_method(HDRHistogram, "bucket_count", histogram_bucket_count, 0);
194
- rb_define_private_method(HDRHistogram, "sub_bucket_count", histogram_sub_bucket_count, 0);
195
- rb_define_private_method(HDRHistogram, "counts_len", histogram_counts_len, 0);
196
-
238
+ HISTOGRAM_RUBY_PRIVATE_GETSETTERS(lowest_trackable_value);
239
+ HISTOGRAM_RUBY_PRIVATE_GETSETTERS(highest_trackable_value);
240
+ HISTOGRAM_RUBY_PRIVATE_GETSETTERS(unit_magnitude);
241
+ HISTOGRAM_RUBY_PRIVATE_GETSETTERS(significant_figures);
242
+ HISTOGRAM_RUBY_PRIVATE_GETSETTERS(sub_bucket_half_count_magnitude);
243
+ HISTOGRAM_RUBY_PRIVATE_GETSETTERS(sub_bucket_half_count);
244
+ HISTOGRAM_RUBY_PRIVATE_GETSETTERS(sub_bucket_mask);
245
+ HISTOGRAM_RUBY_PRIVATE_GETSETTERS(sub_bucket_count);
246
+ HISTOGRAM_RUBY_PRIVATE_GETSETTERS(bucket_count);
247
+ HISTOGRAM_RUBY_PRIVATE_GETSETTERS(min_value);
248
+ HISTOGRAM_RUBY_PRIVATE_GETSETTERS(max_value);
249
+ HISTOGRAM_RUBY_PRIVATE_GETSETTERS(normalizing_index_offset);
250
+ HISTOGRAM_RUBY_PRIVATE_GETSETTERS(conversion_ratio);
251
+ HISTOGRAM_RUBY_PRIVATE_GETSETTERS(counts_len);
252
+ HISTOGRAM_RUBY_PRIVATE_GETSETTERS(total_count);
197
253
 
198
- rb_define_private_method(HDRHistogram, "unit_magnitude", histogram_unit_magnitude, 0);
199
-
200
- //rb_define_private_method(HDRHistogram, "histogram_spectrum", histogram_spectrum, 2);
254
+ rb_define_private_method(HDRHistogram, "set_raw_count", histogram_set_raw_count, 2);
255
+ rb_define_private_method(HDRHistogram, "get_raw_count", histogram_get_raw_count, 1);
201
256
  }
202
257
 
@@ -21,6 +21,12 @@ class HDRHistogram
21
21
  counts_len = m[:counts_len].to_i
22
22
  self.counts_len= counts_len
23
23
  self.total_count= m[:total_count].to_i
24
+ if !opt[:multiplier] && m[:multiplier]
25
+ @multiplier = m[:multiplier].to_f
26
+ end
27
+ if !@unit && m[:unit]
28
+ @unit = m[:unit]
29
+ end
24
30
 
25
31
  counts = m[:counts].split " "
26
32
  i=0
@@ -99,22 +105,60 @@ class HDRHistogram
99
105
  str
100
106
  end
101
107
 
108
+ def serialize
109
+ 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]
110
+
111
+ raw_counts = []
112
+ numrun="~!@#$%^&*"
113
+
114
+ for i in 0...counts_len do
115
+ raw_counts << get_raw_count(i)
116
+ end
117
+
118
+ counts = []
119
+
120
+ while raw_counts.length > 0 do
121
+ num = raw_counts.shift
122
+ n = 1
123
+ if num < numrun.length
124
+ while raw_counts[0] == num
125
+ raw_counts.shift
126
+ n+=1
127
+ end
128
+ if n > 1
129
+ counts << "#{numrun[num]}#{n}"
130
+ else
131
+ counts << num
132
+ end
133
+ else
134
+ counts << num
135
+ end
136
+ end
137
+
138
+ out = "#{attrs.join " "} [#{counts.join " "} ]"
139
+ if @unit || @multiplier != 1
140
+ out << " (#{unit} #{multiplier})"
141
+ end
142
+ out
143
+ end
144
+
102
145
  def self.adjusted_boundary_val(val, opt={})
103
146
  return opt ? val * 1/(opt[:multiplier] || 1) : val
104
147
  end
105
148
  private_class_method :adjusted_boundary_val
106
149
 
107
150
  def self.unserialize(str, opt={})
108
- 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+ )+)\]/
151
+ 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+)\))?/
109
152
 
110
153
  m = str.match regex
111
154
 
112
155
  raise HDRHistogramError, "invalid serialization pattern" if m.nil?
113
156
 
114
157
  opt[:unserialized]=m
158
+ multiplier = opt[:multiplier] || 1
115
159
 
116
- low = m[:lowest_trackable_value].to_i * (opt[:multiplier] || 1)
117
- high = m[:highest_trackable_value].to_i * (opt[:multiplier] || 1)
160
+ low = m[:lowest_trackable_value].to_i * multiplier
161
+ high = m[:highest_trackable_value].to_i * multiplier
118
162
  hdrh = self.new(low, high, m[:significant_figures].to_i, opt)
119
163
 
120
164
  return hdrh
@@ -1,3 +1,3 @@
1
1
  class HDRHistogram
2
- VERSION = "0.1.7"
2
+ VERSION = "0.1.8"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: HDRHistogram
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.7
4
+ version: 0.1.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Leo P.
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-09-30 00:00:00.000000000 Z
11
+ date: 2018-10-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler