1+ # Licensed under the Apache License, Version 2.0 (the "License"); you may not
2+ # use this file except in compliance with the License. You may obtain a copy of
3+ # the License at
4+ #
5+ # http://www.apache.org/licenses/LICENSE-2.0
6+ #
7+ # Unless required by applicable law or agreed to in writing, software
8+ # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9+ # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10+ # License for the specific language governing permissions and limitations under
11+ # the License.
12+
13+ defmodule ExecutionStatsTests do
14+ use CouchTestCase
15+ use ExUnit.Case
16+
17+ @ db_name "execution-stats-docs"
18+
19+ setup do
20+ UserDocs . setup ( @ db_name )
21+ end
22+
23+ test "simple json index" do
24+ selector = % { "age" => % { "$lt" => 35 } }
25+ { :ok , raw } = MangoDatabase . find ( @ db_name , selector , return_raw: true , executionStats: true )
26+
27+ assert length ( raw [ "docs" ] ) == 3
28+ assert raw [ "execution_stats" ] [ "total_keys_examined" ] == 3
29+ assert raw [ "execution_stats" ] [ "total_docs_examined" ] == 3
30+ assert raw [ "execution_stats" ] [ "total_quorum_docs_examined" ] == 0
31+ assert raw [ "execution_stats" ] [ "results_returned" ] == 3
32+ assert raw [ "execution_stats" ] [ "execution_time_ms" ] > 0
33+ end
34+
35+ test "no execution stats" do
36+ selector = % { "age" => % { "$lt" => 35 } }
37+ { :ok , raw } = MangoDatabase . find ( @ db_name , selector , return_raw: true , executionStats: false )
38+
39+ refute Map . has_key? ( raw , "execution_stats" )
40+ end
41+
42+ test "quorum json index" do
43+ selector = % { "age" => % { "$lt" => 35 } }
44+ { :ok , raw } = MangoDatabase . find (
45+ @ db_name ,
46+ selector ,
47+ return_raw: true ,
48+ r: 3 ,
49+ executionStats: true
50+ )
51+
52+ assert length ( raw [ "docs" ] ) == 3
53+ assert raw [ "execution_stats" ] [ "total_keys_examined" ] == 3
54+ assert raw [ "execution_stats" ] [ "total_docs_examined" ] == 0
55+ assert raw [ "execution_stats" ] [ "total_quorum_docs_examined" ] == 3
56+ assert raw [ "execution_stats" ] [ "results_returned" ] == 3
57+ assert raw [ "execution_stats" ] [ "execution_time_ms" ] > 0
58+ end
59+
60+ test "results returned limit" do
61+ selector = % { "age" => % { "$lt" => 35 } }
62+ { :ok , raw } = MangoDatabase . find (
63+ @ db_name ,
64+ selector ,
65+ return_raw: true ,
66+ limit: 2 ,
67+ executionStats: true
68+ )
69+
70+ assert raw [ "execution_stats" ] [ "results_returned" ] == length ( raw [ "docs" ] )
71+ end
72+
73+ test "no matches index scan" do
74+ selector = % { "age" => % { "$lt" => 35 } , "nomatch" => "me" }
75+ { :ok , raw } = MangoDatabase . find (
76+ @ db_name ,
77+ selector ,
78+ return_raw: true ,
79+ executionStats: true
80+ )
81+
82+ assert raw [ "execution_stats" ] [ "total_docs_examined" ] == 3
83+ assert raw [ "execution_stats" ] [ "results_returned" ] == 0
84+ end
85+
86+ test "covering json index" do
87+ selector = % { "age" => % { "$lt" => 35 } }
88+ { :ok , raw } = MangoDatabase . find (
89+ @ db_name ,
90+ selector ,
91+ fields: [ "_id" , "age" ] ,
92+ return_raw: true ,
93+ executionStats: true
94+ )
95+
96+ assert length ( raw [ "docs" ] ) == 3
97+ assert raw [ "execution_stats" ] [ "total_keys_examined" ] == 3
98+ assert raw [ "execution_stats" ] [ "total_docs_examined" ] == 0
99+ assert raw [ "execution_stats" ] [ "total_quorum_docs_examined" ] == 0
100+ assert raw [ "execution_stats" ] [ "results_returned" ] == 3
101+ end
102+
103+ # reporting consistency
104+ @ cases [
105+ % {
106+ title: "with limit" ,
107+ selector: % { "age" => % { "$lte" => 42 } } ,
108+ fields: [ "name" , "email" , "age" ] ,
109+ limit: 3 ,
110+ total_keys_examined: 4 ,
111+ total_docs_examined: 4 ,
112+ results_returned: 3
113+
114+ } ,
115+ % {
116+ title: "partial matches" ,
117+ selector: % { "favorites" => % { "$elemMatch" => % { "$eq" => "Erlang" } } } ,
118+ fields: [ "name" , "email" , "twitter" ] ,
119+ limit: 200 ,
120+ total_keys_examined: 15 ,
121+ total_docs_examined: 15 ,
122+ results_returned: 6 ,
123+ } ,
124+ % {
125+ title: "no matches, using _all_docs" ,
126+ selector: % { "foo" => "bar" } ,
127+ fields: [ ] ,
128+ limit: 200 ,
129+ total_keys_examined: 25 ,
130+ total_docs_examined: 25 ,
131+ results_returned: 0 ,
132+ } ,
133+ % {
134+ title: "no matches, indexed column (no keys examined)" ,
135+ selector: % { "name.first" => "Lee" , "name.last" => "Jackson" } ,
136+ fields: [ "email" , "twitter" ] ,
137+ limit: 200 ,
138+ total_keys_examined: 0 ,
139+ total_docs_examined: 0 ,
140+ results_returned: 0 ,
141+ } ,
142+ % {
143+ title: "no matches, indexed column" ,
144+ selector: % { "favorites" => % { "$elemMatch" => % { "$eq" => "Haskell" } } } ,
145+ fields: [ "name" , "email" , "twitter" ] ,
146+ limit: 200 ,
147+ total_keys_examined: 15 ,
148+ total_docs_examined: 15 ,
149+ results_returned: 0 ,
150+ } ]
151+
152+ for case <- @ cases do
153+ test "scenario #{ case . title } " do
154+ case = unquote ( Macro . escape ( case ) )
155+ { :ok , resp } = MangoDatabase . find (
156+ @ db_name ,
157+ case . selector ,
158+ fields: case . fields ,
159+ limit: case . limit ,
160+ return_raw: true ,
161+ executionStats: true
162+ )
163+
164+ execution_stats = resp [ "execution_stats" ]
165+ assert execution_stats [ "total_keys_examined" ] == case . total_keys_examined
166+ assert execution_stats [ "total_docs_examined" ] == case . total_docs_examined
167+ assert execution_stats [ "results_returned" ] == case . results_returned
168+ end
169+ end
170+ end
171+
172+ defmodule ExecutionStatsTestsText do
173+ use CouchTestCase
174+ use ExUnit.Case
175+
176+ @ db_name "execution-stats-text-docs"
177+
178+ setup do
179+ UserDocs . setup ( @ db_name , "text" )
180+ end
181+
182+ test "simple text index" do
183+ selector = % { "$text" => "Stephanie" }
184+ { :ok , raw } = MangoDatabase . find ( @ db_name , selector , return_raw: true , executionStats: true )
185+
186+ assert length ( raw [ "docs" ] ) == 1
187+ assert raw [ "execution_stats" ] [ "total_keys_examined" ] == 1
188+ assert raw [ "execution_stats" ] [ "total_docs_examined" ] == 1
189+ assert raw [ "execution_stats" ] [ "total_quorum_docs_examined" ] == 0
190+ assert raw [ "execution_stats" ] [ "results_returned" ] == 1
191+ assert raw [ "execution_stats" ] [ "execution_time_ms" ] > 0
192+ end
193+
194+ test "no execution stats" do
195+ selector = % { "$text" => "Stephanie" }
196+ { :ok , raw } = MangoDatabase . find ( @ db_name , selector , return_raw: true )
197+ refute Map . has_key? ( raw , "execution_stats" )
198+ end
199+ end
0 commit comments