File tree Expand file tree Collapse file tree 2 files changed +34
-12
lines changed Expand file tree Collapse file tree 2 files changed +34
-12
lines changed Original file line number Diff line number Diff line change 1
- local fiber = require (' fiber' )
2
1
local ffi = require (' ffi' )
3
2
4
3
local quantile = {}
5
4
6
- ffi .cdef [[
7
- typedef struct {int Delta , Width ; double Value ; } sample;
8
- ]]
5
+ if not pcall (ffi .typeof , " sample" ) then
6
+ ffi .cdef [[
7
+ typedef struct sample {int Delta , Width ; double Value ; } sample;
8
+ ]]
9
+ end
9
10
10
11
local sample_constructor = ffi .typeof (' sample' )
11
12
@@ -134,8 +135,8 @@ function stream:merge(samples, len)
134
135
local i = 1
135
136
local r = 0
136
137
for z = 1 , len do
137
- if i % 1000 == 0 then
138
- fiber .yield ()
138
+ if i % 1000 == 0 then
139
+ require ' fiber' .yield ()
139
140
end
140
141
local sample = samples [z - 1 ]
141
142
for j = i , s .l_len do
@@ -165,9 +166,6 @@ function stream:query(q)
165
166
local p = s .l [0 ]
166
167
local r = 0
167
168
for i = 1 , s .l_len do
168
- if i % 500 == 0 then
169
- fiber .yield ()
170
- end
171
169
local c = s .l [i ]
172
170
if r + c .Width + c .Delta > t then
173
171
return p .Value
@@ -189,9 +187,6 @@ function stream:compress()
189
187
local r = s .n - x .Width
190
188
191
189
for i = s .l_len - 1 , 1 , - 1 do
192
- if i % 1000 == 0 then
193
- fiber .yield ()
194
- end
195
190
local c = make_sample (0 )
196
191
sample_copy (c , s .l [i ])
197
192
if c .Width + x .Width + x .Delta <= s .f (s , r ) then
Original file line number Diff line number Diff line change 1
1
local quantile = require (' metrics.quantile' )
2
+ local fiber = require (' fiber' )
2
3
local ffi = require (' ffi' )
3
4
local t = require (' luatest' )
4
5
local g = t .group (' quantile' )
@@ -104,3 +105,29 @@ g.test_package_reload = function()
104
105
local ok , quantile_package = pcall (require , ' metrics.quantile' )
105
106
t .assert (ok , quantile_package )
106
107
end
108
+
109
+
110
+ g .test_fiber_yield = function ()
111
+ local
112
+ q1 = quantile .NewTargeted ({[0.5 ]= 0.01 , [0.9 ]= 0.01 , [0.99 ]= 0.01 })
113
+
114
+ for _ = 1 ,1e6 do
115
+ t .assert (q1 .b_len < q1 .__max_samples )
116
+ quantile .Insert (q1 , math.random (1 ))
117
+ end
118
+
119
+ for _ = 1 ,500 do
120
+ fiber .create (function ()
121
+ for _ = 1 ,1e2 do
122
+ t .assert (q1 .b_len < q1 .__max_samples )
123
+ quantile .Insert (q1 , math.random (1000 ))
124
+ end
125
+ end )
126
+ end
127
+
128
+ for _ = 1 ,1e6 do
129
+ t .assert (q1 .b_len < q1 .__max_samples )
130
+ quantile .Insert (q1 , math.random (1000 ))
131
+ end
132
+
133
+ end
You can’t perform that action at this time.
0 commit comments