Skip to content

Commit ea534ac

Browse files
fiber.yield() call removed from quantile
1 parent 18fb2ee commit ea534ac

File tree

2 files changed

+34
-12
lines changed

2 files changed

+34
-12
lines changed

metrics/quantile.lua

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
local fiber = require('fiber')
21
local ffi = require('ffi')
32

43
local quantile = {}
54

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
910

1011
local sample_constructor = ffi.typeof('sample')
1112

@@ -134,8 +135,8 @@ function stream:merge(samples, len)
134135
local i = 1
135136
local r = 0
136137
for z = 1, len do
137-
if i % 1000 == 0 then
138-
fiber.yield()
138+
if i % 1000 == 0 then
139+
require'fiber'.yield()
139140
end
140141
local sample = samples[z-1]
141142
for j = i, s.l_len do
@@ -165,9 +166,6 @@ function stream:query(q)
165166
local p = s.l[0]
166167
local r = 0
167168
for i = 1, s.l_len do
168-
if i % 500 == 0 then
169-
fiber.yield()
170-
end
171169
local c = s.l[i]
172170
if r + c.Width + c.Delta > t then
173171
return p.Value
@@ -189,9 +187,6 @@ function stream:compress()
189187
local r = s.n - x.Width
190188

191189
for i = s.l_len - 1, 1, -1 do
192-
if i % 1000 == 0 then
193-
fiber.yield()
194-
end
195190
local c = make_sample(0)
196191
sample_copy(c, s.l[i])
197192
if c.Width + x.Width + x.Delta <= s.f(s, r) then

test/quantile_test.lua

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
local quantile = require('metrics.quantile')
2+
local fiber = require('fiber')
23
local ffi = require('ffi')
34
local t = require('luatest')
45
local g = t.group('quantile')
@@ -104,3 +105,29 @@ g.test_package_reload = function()
104105
local ok, quantile_package = pcall(require, 'metrics.quantile')
105106
t.assert(ok, quantile_package)
106107
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

0 commit comments

Comments
 (0)