Skip to content

Commit

Permalink
Improve compression eunit test suite
Browse files Browse the repository at this point in the history
Use the simpler `?TDEF_FE` macro and fix timeout usage bug. EUnit timeout is in
seconds, while test_util:wait/2 takes milliseconds. This was revealed due a
slow disk IO speed on the new s390x worker.

To reduce flakiness and to ensure db / view size before compaction is greater
than the size after compaction, even with `none` compression option, make db
update batches smaller and use more frequent index commits.

Issue: #4521
  • Loading branch information
nickva committed Apr 11, 2023
1 parent 1432717 commit e32cfd6
Showing 1 changed file with 55 additions and 61 deletions.
116 changes: 55 additions & 61 deletions src/couch/test/eunit/couchdb_file_compression_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,38 @@
-define(DDOC_ID, <<"_design/test">>).
-define(DOCS_COUNT, 1000).
-define(TIMEOUT, 60).
% Smaller doc batches will generate more garbage
-define(BATCH_SIZE, 100).

setup_all() ->
Ctx = test_util:start_couch(),
config:set("query_server_config", "commit_freq", "0", false),
config:set("couchdb", "file_compression", "none", false),
Ctx.

teardown_all(Ctx) ->
config:delete("couchdb", "file_compression", false),
config:delete("query_server_config", "commit_freq", false),
test_util:stop_couch(Ctx).

couch_file_compression_test_() ->
{
"CouchDB file compression tests",
{
setup,
fun setup_all/0,
fun teardown_all/1,
{foreach, fun setup/0, fun teardown/1, [
?TDEF_FE(should_use_none, ?TIMEOUT),
?TDEF_FE(should_use_deflate_1, ?TIMEOUT),
?TDEF_FE(should_use_deflate_9, ?TIMEOUT),
?TDEF_FE(should_use_snappy, ?TIMEOUT),
?TDEF_FE(should_compare_compression_methods, ?TIMEOUT)
]}
}
}.

setup() ->
config:set("couchdb", "file_compression", "none", false),
DbName = ?tempdb(),
{ok, Db} = couch_db:create(DbName, [?ADMIN_CTX]),
Expand All @@ -40,58 +69,34 @@ setup_all() ->
),
{ok, _} = couch_db:update_doc(Db, DDoc, []),
ok = couch_db:close(Db),
{Ctx, DbName}.
ok = refresh_index(DbName),
DbName.

teardown_all({Ctx, DbName}) ->
ok = couch_server:delete(DbName, [?ADMIN_CTX]),
test_util:stop_couch(Ctx).
teardown(DbName) ->
config:delete("couchdb", "file_compression", false),
couch_server:delete(DbName, [?ADMIN_CTX]).

couch_file_compression_test_() ->
{
"CouchDB file compression tests",
{
setup,
fun setup_all/0,
fun teardown_all/1,
{with, [
fun should_use_none/1,
fun should_use_deflate_1/1,
fun should_use_deflate_9/1,
fun should_use_snappy/1,
fun should_compare_compression_methods/1
]}
}
}.
should_use_none(DbName) ->
config:set("couchdb", "file_compression", "none", false),
compact_db(DbName),
compact_view(DbName).

should_use_none({_, DbName}) -> run_test(DbName, "none").
should_use_deflate_1({_, DbName}) -> run_test(DbName, "deflate_1").
should_use_deflate_9({_, DbName}) -> run_test(DbName, "deflate_9").
should_use_snappy({_, DbName}) -> run_test(DbName, "snappy").

should_compare_compression_methods({_, DbName}) ->
TestDb = setup_db(DbName),
Name = "none > snappy > deflate_1 > deflate_9",
try
{Name, {timeout, ?TIMEOUT, ?_test(compare_methods(TestDb))}}
after
couch_server:delete(TestDb, [?ADMIN_CTX])
end.
should_use_deflate_1(DbName) ->
config:set("couchdb", "file_compression", "deflate_1", false),
compact_db(DbName),
compact_view(DbName).

run_test(DbName, Comp) ->
config:set("couchdb", "file_compression", Comp, false),
Timeout = 5 + ?TIMEOUT,
TestDb = setup_db(DbName),
Tests = [
{"compact database", {timeout, Timeout, ?_test(compact_db(DbName))}},
{"compact view", {timeout, Timeout, ?_test(compact_view(DbName))}}
],
try
{"Use compression: " ++ Comp, Tests}
after
ok = couch_server:delete(TestDb, [?ADMIN_CTX])
end.
should_use_deflate_9(DbName) ->
config:set("couchdb", "file_compression", "deflate_9", false),
compact_db(DbName),
compact_view(DbName).

should_use_snappy(DbName) ->
config:set("couchdb", "file_compression", "deflate_9", false),
compact_db(DbName),
compact_view(DbName).

compare_methods(DbName) ->
should_compare_compression_methods(DbName) ->
config:set("couchdb", "file_compression", "none", false),
ExternalSizePreCompact = db_external_size(DbName),
compact_db(DbName),
Expand Down Expand Up @@ -149,21 +154,10 @@ populate_db(Db, NumDocs) ->
]}
)
end,
lists:seq(1, 500)
lists:seq(1, ?BATCH_SIZE)
),
{ok, _} = couch_db:update_docs(Db, Docs, []),
populate_db(Db, NumDocs - 500).

setup_db(SrcDbName) ->
TgtDbName = ?tempdb(),
TgtDbFileName = binary_to_list(TgtDbName) ++ ".couch",
couch_util:with_db(SrcDbName, fun(Db) ->
OldPath = couch_db:get_filepath(Db),
NewPath = filename:join(filename:dirname(OldPath), TgtDbFileName),
{ok, _} = file:copy(OldPath, NewPath)
end),
refresh_index(TgtDbName),
TgtDbName.
populate_db(Db, NumDocs - ?BATCH_SIZE).

refresh_index(DbName) ->
{ok, Db} = couch_db:open_int(DbName, []),
Expand Down Expand Up @@ -226,7 +220,7 @@ wait_compaction(DbName, Kind, Line) ->
false -> ok
end
end,
case test_util:wait(WaitFun, ?TIMEOUT) of
case test_util:wait(WaitFun, ?TIMEOUT * 1000) of
timeout ->
erlang:error(
{assertion_failed, [
Expand Down

0 comments on commit e32cfd6

Please sign in to comment.