You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by da...@apache.org on 2020/03/03 16:30:48 UTC

[couchdb] 02/03: WIP: Test generation

This is an automated email from the ASF dual-hosted git repository.

davisp pushed a commit to branch prototype/fdb-layer-couch-views-size-tests
in repository https://gitbox.apache.org/repos/asf/couchdb.git

commit b8742a7332511ad26aa33a305a798bed6ce71e82
Author: Paul J. Davis <pa...@gmail.com>
AuthorDate: Mon Mar 2 16:53:18 2020 -0600

    WIP: Test generation
---
 src/couch_views/test/couch_views_size_test.erl | 163 +++++++++++++++++++++++++
 1 file changed, 163 insertions(+)

diff --git a/src/couch_views/test/couch_views_size_test.erl b/src/couch_views/test/couch_views_size_test.erl
new file mode 100644
index 0000000..888a7ea
--- /dev/null
+++ b/src/couch_views/test/couch_views_size_test.erl
@@ -0,0 +1,163 @@
+% Licensed under the Apache License, Version 2.0 (the "License"); you may not
+% use this file except in compliance with the License. You may obtain a copy of
+% the License at
+%
+%   http://www.apache.org/licenses/LICENSE-2.0
+%
+% Unless required by applicable law or agreed to in writing, software
+% distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+% WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+% License for the specific language governing permissions and limitations under
+% the License.
+
+-module(couch_views_size_test).
+
+-include_lib("eunit/include/eunit.hrl").
+-include_lib("couch/include/couch_db.hrl").
+-include_lib("couch/include/couch_eunit.hrl").
+-include_lib("couch_mrview/include/couch_mrview.hrl").
+
+% N.B., we should move to couch_ejson_size instead
+% of erlang:external_size
+%
+% to calculate view size:
+% total = 0
+% for (fdb_k, fdb_v) in VIEW_MAP_RANGE:
+%   {EncUserKey, EncUserval} = erlfdb_tuple:unpack(fdb_v),
+%   UserKey = couch_views_encoding:decode(EncUserKey),
+%   UserVal = couch_views_encoding:decode(EncUserVal),
+%   total += erlang:external_size(UserKey),
+%   total += erlang:external_size(UserVal)
+%
+% Our goal in checking the size calculations is that we cover
+% as much of the possible key mutation space as possible while
+% not relying on fuzzing out the edge cases. Conceptually we have
+% two sets of keys E and U. E is keys as currently exist in the
+% view, and U is the new set of keys corresponding to an update.
+%
+% Both sets E and U have the same possible set of state variables:
+%
+% 1. N unique keys, where 0 =< N =< infinity
+% 2. D keys with duplicates, where 0 =< D =< N,
+% 3. R repeats for each member of D, for 2 =< R =< infinity
+%
+% Given two sets S1 and S2, we then have a set of transition variables:
+%
+% 1. deltaN - shared unique keys, where 0 =< deltaN =< N
+% 2. deltaD - shared duplicates, where 0 =< deltaD =< N
+% 3. deltaR - shared repeats for each D, where 2 =< deltaR =< infinity
+%
+% To search our state transition space, we can create two functions to
+% first define our start and end states, and for each transition we have
+% a function that defines the shared overlap between states.
+%
+% Given a list of transitions are checks then become simple in that
+% we can iterate over each transition checking that our index is valid
+% after each one. Index validation will purely look at the existing
+% state of the index in fdb and validate correctness.
+
+
+-define(N_DOMAIN, [0, 1, 2, 5]).
+-define(D_DOMAIN, [0, 1, 2, 5]).
+-define(R_DOMAIN, [2, 4]).
+
+-define(DELTA_N_DOMAIN, [0, 1, 2, 5]).
+-define(DELTA_D_DOMAIN, [0, 1, 2, 5]).
+-define(DELTA_R_DOMAIN, [1, 2, 4]).
+
+
+generate_sets() ->
+    lists:foldl(fun(N, NAcc) ->
+        lists:foldl(fun(D, DAcc) ->
+            if
+                D > N ->
+                    DAcc;
+                D == 0 ->
+                    [{N, 0, 0} | DAcc];
+                D > 0 ->
+                    lists:foldl(fun(R, RAcc) ->
+                        [{N, D, R} | RAcc]
+                    end, DAcc, ?R_DOMAIN)
+            end
+        end, NAcc, ?D_DOMAIN)
+    end, [], ?N_DOMAIN).
+
+
+-define(I_HEART_EUNIT(Tests), [{with, [T]} || T <- Tests]).
+
+
+size_test_() ->
+    {
+        "Test view size tracking",
+        {
+            setup,
+            fun setup/0,
+            fun cleanup/1,
+            {
+                foreach,
+                fun foreach_setup/0,
+                fun foreach_teardown/1,
+                ?I_HEART_EUNIT([
+                    fun single_view/1
+                ])
+            }
+        }
+    }.
+
+
+setup() ->
+    Ctx = test_util:start_couch([
+            fabric,
+            couch_jobs,
+            couch_js,
+            couch_views
+        ]),
+    Ctx.
+
+
+cleanup(Ctx) ->
+    test_util:stop_couch(Ctx).
+
+
+foreach_setup() ->
+    {ok, Db} = fabric2_db:create(?tempdb(), [{user_ctx, ?ADMIN_USER}]),
+    Db.
+
+
+foreach_teardown(Db) ->
+    ok = fabric2_db:delete(fabric2_db:name(Db), []).
+
+
+single_view(_Db) ->
+    Sets = generate_sets(),
+    Pairs = [{Set1, Set2} || Set1 <- Sets, Set2 <- Sets],
+    Transitions = lists:flatmap(fun({{N1, D1, R1} = S1, {N2, D2, R2} = S2}) ->
+        lists:foldl(fun(DeltaN, DNAcc) ->
+            case DeltaN > min(N1, N2) of
+                true ->
+                    DNAcc;
+                false when DeltaN == 0 ->
+                    [{S1, S2, {0, 0, 0}}];
+                false ->
+                    lists:foldl(fun(DeltaD, DDAcc) ->
+                        case DeltaD > min(D1, D2) orelse DeltaD > DeltaN of
+                            true ->
+                                DDAcc;
+                            false when DeltaD == 0 ->
+                                [{S1, S2, {DeltaN, 0, 0}} | DDAcc];
+                            false ->
+                                lists:foldl(fun(DeltaR, DRAcc) ->
+                                    case DeltaR > min(R1, R2) of
+                                        true ->
+                                            DRAcc;
+                                        false ->
+                                            T = {DeltaN, DeltaD, DeltaR},
+                                            [{S1, S2, T} | DRAcc]
+                                    end
+                                end, DDAcc, ?DELTA_R_DOMAIN)
+                        end
+                    end, DNAcc, ?DELTA_D_DOMAIN)
+            end
+        end, [], ?DELTA_N_DOMAIN)
+    end, Pairs),
+      io:format(standard_error, "~n~n~p~n~n", [lists:sort(Transitions)]).