Skip to content

Commit 955cf60

Browse files
Use in-built stats instead on helper in tests
Use in-built `crud.stats()` info instead on `storage_stat` helper in tests to track map reduce calls. Part of #224
1 parent 303556a commit 955cf60

File tree

5 files changed

+48
-188
lines changed

5 files changed

+48
-188
lines changed

test/helper.lua

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,4 +409,14 @@ function helpers.reload_roles(srv)
409409
t.assert_equals({ok, err}, {true, nil})
410410
end
411411

412+
function helpers.get_map_reduces_stat(router, space_name)
413+
return router:eval([[
414+
local stats = require('crud').stats(...)
415+
if stats.select == nil then
416+
return 0
417+
end
418+
return stats.select.details.map_reduces
419+
]], { space_name })
420+
end
421+
412422
return helpers

test/helpers/storage_stat.lua

Lines changed: 0 additions & 110 deletions
This file was deleted.

test/integration/ddl_sharding_key_test.lua

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ local crud = require('crud')
33
local t = require('luatest')
44

55
local helpers = require('test.helper')
6-
local storage_stat = require('test.helpers.storage_stat')
76

87
local ok = pcall(require, 'ddl')
98
if not ok then
@@ -35,12 +34,9 @@ pgroup.before_all(function(g)
3534
t.assert_equals(type(result), 'table')
3635
t.assert_equals(err, nil)
3736

38-
helpers.call_on_storages(g.cluster, function(server)
39-
server.net_box:eval([[
40-
local storage_stat = require('test.helpers.storage_stat')
41-
storage_stat.init_on_storage()
42-
]])
43-
end)
37+
g.cluster.main_server.net_box:eval([[
38+
crud.enable_stats()
39+
]])
4440
end)
4541

4642
pgroup.after_all(function(g) helpers.stop_cluster(g.cluster) end)
@@ -349,45 +345,39 @@ for name, case in pairs(cases) do
349345
pgroup[('test_%s_wont_lead_to_map_reduce'):format(name)] = function(g)
350346
case.prepare_data(g, case.space_name)
351347

352-
local stat_a = storage_stat.collect(g.cluster)
348+
local router = g.cluster:server('router').net_box
349+
local map_reduces_before = helpers.get_map_reduces_stat(router, case.space_name)
353350

354-
local result, err = g.cluster.main_server.net_box:call('crud.select', {
351+
local result, err = router:call('crud.select', {
355352
case.space_name, case.conditions
356353
})
357354
t.assert_equals(err, nil)
358355
t.assert_not_equals(result, nil)
359356
t.assert_equals(#result.rows, 1)
360357

361-
local stat_b = storage_stat.collect(g.cluster)
362-
363-
-- Check a number of select() requests made by CRUD on cluster's storages
364-
-- after calling select() on a router. Make sure only a single storage has
365-
-- a single select() request. Otherwise we lead to map-reduce.
366-
local stats = storage_stat.diff(stat_b, stat_a)
367-
t.assert_equals(storage_stat.total(stats), 1, 'Select request was not a map reduce')
358+
local map_reduces_after = helpers.get_map_reduces_stat(router, case.space_name)
359+
local diff = map_reduces_after - map_reduces_before
360+
t.assert_equals(diff, 0, 'Select request was not a map reduce')
368361
end
369362
end
370363

371364
pgroup.test_select_for_part_of_sharding_key_will_lead_to_map_reduce = function(g)
372365
local space_name = 'customers_name_age_key_different_indexes'
373366
prepare_data_name_age_sharding_key(g, space_name)
374367

375-
local stat_a = storage_stat.collect(g.cluster)
368+
local router = g.cluster:server('router').net_box
369+
local map_reduces_before = helpers.get_map_reduces_stat(router, space_name)
376370

377-
local result, err = g.cluster.main_server.net_box:call('crud.select', {
371+
local result, err = router:call('crud.select', {
378372
space_name, {{'==', 'age', 58}},
379373
})
380374
t.assert_equals(err, nil)
381375
t.assert_not_equals(result, nil)
382376
t.assert_equals(#result.rows, 1)
383377

384-
local stat_b = storage_stat.collect(g.cluster)
385-
386-
-- Check a number of select() requests made by CRUD on cluster's storages
387-
-- after calling select() on a router. Make sure it was a map-reduce
388-
-- since we do not have sharding key values in conditions.
389-
local stats = storage_stat.diff(stat_b, stat_a)
390-
t.assert_equals(storage_stat.total(stats), 2, 'Select request was a map reduce')
378+
local map_reduces_after = helpers.get_map_reduces_stat(router, space_name)
379+
local diff = map_reduces_after - map_reduces_before
380+
t.assert_equals(diff, 1, 'Select request was a map reduce')
391381
end
392382

393383
pgroup.test_select_secondary_idx = function(g)

test/integration/pairs_test.lua

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ local t = require('luatest')
55
local crud_utils = require('crud.common.utils')
66

77
local helpers = require('test.helper')
8-
local storage_stat = require('test.helpers.storage_stat')
98

109
local pgroup = t.group('pairs', {
1110
{engine = 'memtx'},
@@ -27,12 +26,9 @@ pgroup.before_all(function(g)
2726

2827
g.space_format = g.cluster.servers[2].net_box.space.customers:format()
2928

30-
helpers.call_on_storages(g.cluster, function(server)
31-
server.net_box:eval([[
32-
local storage_stat = require('test.helpers.storage_stat')
33-
storage_stat.init_on_storage()
34-
]])
35-
end)
29+
g.cluster.main_server.net_box:eval([[
30+
crud.enable_stats()
31+
]])
3632
end)
3733

3834
pgroup.after_all(function(g) helpers.stop_cluster(g.cluster) end)
@@ -842,10 +838,11 @@ pgroup.test_pairs_no_map_reduce = function(g)
842838

843839
table.sort(customers, function(obj1, obj2) return obj1.id < obj2.id end)
844840

845-
local stat_a = storage_stat.collect(g.cluster)
841+
local router = g.cluster:server('router').net_box
842+
local map_reduces_before = helpers.get_map_reduces_stat(router, 'customers')
846843

847844
-- Case: no conditions, just bucket id.
848-
local rows = g.cluster.main_server.net_box:eval([[
845+
local rows = router:eval([[
849846
local crud = require('crud')
850847
851848
return crud.pairs(...):totable()
@@ -858,15 +855,9 @@ pgroup.test_pairs_no_map_reduce = function(g)
858855
{3, 2804, 'David', 'Smith', 33, 'Los Angeles'},
859856
})
860857

861-
local stat_b = storage_stat.collect(g.cluster)
862-
t.assert_equals(storage_stat.diff(stat_b, stat_a), {
863-
['s-1'] = {
864-
select_requests = 1,
865-
},
866-
['s-2'] = {
867-
select_requests = 0,
868-
},
869-
})
858+
local map_reduces_after_1 = helpers.get_map_reduces_stat(router, 'customers')
859+
local diff_1 = map_reduces_after_1 - map_reduces_before
860+
t.assert_equals(diff_1, 0, 'Select request was not a map reduce')
870861

871862
-- Case: EQ on secondary index, which is not in the sharding
872863
-- index (primary index in the case).
@@ -883,13 +874,7 @@ pgroup.test_pairs_no_map_reduce = function(g)
883874
{4, 1161, 'William', 'White', 81, 'Chicago'},
884875
})
885876

886-
local stat_c = storage_stat.collect(g.cluster)
887-
t.assert_equals(storage_stat.diff(stat_c, stat_b), {
888-
['s-1'] = {
889-
select_requests = 0,
890-
},
891-
['s-2'] = {
892-
select_requests = 1,
893-
},
894-
})
877+
local map_reduces_after_2 = helpers.get_map_reduces_stat(router, 'customers')
878+
local diff_2 = map_reduces_after_2 - map_reduces_after_1
879+
t.assert_equals(diff_2, 0, 'Select request was not a map reduce')
895880
end

test/integration/select_test.lua

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ local crud = require('crud')
66
local crud_utils = require('crud.common.utils')
77

88
local helpers = require('test.helper')
9-
local storage_stat = require('test.helpers.storage_stat')
109

1110
local pgroup = t.group('select', {
1211
{engine = 'memtx'},
@@ -28,12 +27,9 @@ pgroup.before_all(function(g)
2827

2928
g.space_format = g.cluster.servers[2].net_box.space.customers:format()
3029

31-
helpers.call_on_storages(g.cluster, function(server)
32-
server.net_box:eval([[
33-
local storage_stat = require('test.helpers.storage_stat')
34-
storage_stat.init_on_storage()
35-
]])
36-
end)
30+
g.cluster.main_server.net_box:eval([[
31+
crud.enable_stats()
32+
]])
3733
end)
3834

3935
pgroup.after_all(function(g) helpers.stop_cluster(g.cluster) end)
@@ -1615,7 +1611,8 @@ pgroup.test_select_no_map_reduce = function(g)
16151611

16161612
table.sort(customers, function(obj1, obj2) return obj1.id < obj2.id end)
16171613

1618-
local stat_a = storage_stat.collect(g.cluster)
1614+
local router = g.cluster:server('router').net_box
1615+
local map_reduces_before = helpers.get_map_reduces_stat(router, 'customers')
16191616

16201617
-- Case: no conditions, just bucket id.
16211618
local result, err = g.cluster.main_server.net_box:call('crud.select', {
@@ -1628,15 +1625,9 @@ pgroup.test_select_no_map_reduce = function(g)
16281625
{3, 2804, 'David', 'Smith', 33, 'Los Angeles'},
16291626
})
16301627

1631-
local stat_b = storage_stat.collect(g.cluster)
1632-
t.assert_equals(storage_stat.diff(stat_b, stat_a), {
1633-
['s-1'] = {
1634-
select_requests = 1,
1635-
},
1636-
['s-2'] = {
1637-
select_requests = 0,
1638-
},
1639-
})
1628+
local map_reduces_after_1 = helpers.get_map_reduces_stat(router, 'customers')
1629+
local diff_1 = map_reduces_after_1 - map_reduces_before
1630+
t.assert_equals(diff_1, 0, 'Select request was not a map reduce')
16401631

16411632
-- Case: EQ on secondary index, which is not in the sharding
16421633
-- index (primary index in the case).
@@ -1650,13 +1641,7 @@ pgroup.test_select_no_map_reduce = function(g)
16501641
{4, 1161, 'William', 'White', 81, 'Chicago'},
16511642
})
16521643

1653-
local stat_c = storage_stat.collect(g.cluster)
1654-
t.assert_equals(storage_stat.diff(stat_c, stat_b), {
1655-
['s-1'] = {
1656-
select_requests = 0,
1657-
},
1658-
['s-2'] = {
1659-
select_requests = 1,
1660-
},
1661-
})
1644+
local map_reduces_after_2 = helpers.get_map_reduces_stat(router, 'customers')
1645+
local diff_2 = map_reduces_after_2 - map_reduces_after_1
1646+
t.assert_equals(diff_2, 0, 'Select request was not a map reduce')
16621647
end

0 commit comments

Comments
 (0)