summaryrefslogtreecommitdiff
path: root/rel/overlay/var/share/www/script/test/stats.js
blob: d2fd6eac55850bd1273d6e7a3933b800f848e322 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
// 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.

couchTests.stats = function(debug) {

  function newDb(name, doSetup) {
    var db = new CouchDB(name, {"X-Couch-Full-Commit": "false"});
    if(doSetup) {
      db.deleteDb();
      db.createDb();
    }
    return db;
  };

  function getStat(mod, key) {
    return CouchDB.requestStats(mod, key, true);
  };

  function doView(db) {
    var designDoc = {
      _id:"_design/test", // turn off couch.js id escaping?
      language: "javascript",
      views: {
        all_docs: {map: "function(doc) {emit(doc.integer, null);}"},
      }
    };
    db.save(designDoc);
    db.view("test/all_docs");
  };

  function runTest(mod, key, funcs) {
    var db = newDb("test_suite_db", true);
    if(funcs.setup) funcs.setup(db);
    var before = getStat(mod, key).current;
    if(funcs.run) funcs.run(db);
    var after = getStat(mod, key).current;
    if(funcs.test) funcs.test(before, after);
  }

  if (debug) debugger;

  (function() {
    var db = newDb("test_suite_db");
    db.deleteDb();
  
    var before = getStat("couchdb", "open_databases").current;
    db.createDb();
    var after = getStat("couchdb", "open_databases").current;
    TEquals(before+1, after, "Creating a db increments open db count.");
  })();
  
  runTest("couchdb", "open_databases", {
    setup: function() {restartServer();},
    run: function(db) {db.open("123");},
    test: function(before, after) {
      TEquals(before+1, after, "Opening a db increments open db count.");
    }
  });
  
  runTest("couchdb", "open_databases", {
    run: function(db) {db.deleteDb();},
    test: function(before, after) {
      TEquals(before-1, after, "Deleting a db decrements open db count.");
    }
  });
  
  (function() {
    restartServer();
    var max = 5;
    
    var testFun = function() {
      var pre_dbs = getStat("couchdb", "open_databases").current || 0;
      var pre_files = getStat("couchdb", "open_os_files").current || 0;
     
      var triggered = false;
      var db = null;
      for(var i = 0; i < max*2; i++) {
        while (true) {
            try {
              db = newDb("test_suite_db_" + i, true);
              break;
            } catch(e) {
                // all_dbs_active error!
              triggered = true;
            }
        }

        // Trigger a delayed commit
        db.save({_id: "" + i, "lang": "Awesome!"});
      }
      T(triggered, "We managed to force a all_dbs_active error.");
      
      var open_dbs = getStat("couchdb", "open_databases").current;
      TEquals(open_dbs > 0, true, "We actually opened some dbs.");
      TEquals(open_dbs, max, "We only have max db's open.");
      
      for(var i = 0; i < max * 2; i++) {
        newDb("test_suite_db_" + i).deleteDb();
      }
      
      var post_dbs = getStat("couchdb", "open_databases").current;
      var post_files = getStat("couchdb", "open_os_files").current;
      TEquals(pre_dbs, post_dbs, "We have the same number of open dbs.");
      TEquals(pre_files, post_files, "We have the same number of open files.");
    };
    
    run_on_modified_server(
      [{section: "couchdb", key: "max_dbs_open", value: "5"}],
      testFun
    );
  })();
  
  // Just fetching the before value is the extra +1 in test
  runTest("httpd", "requests", {
    run: function() {CouchDB.request("GET", "/");},
    test: function(before, after) {
      TEquals(before+2, after, "Request counts are incremented properly.");
    }
  });
  
  runTest("couchdb", "database_reads", {
    setup: function(db) {db.save({"_id": "test"});},
    run: function(db) {db.open("test");},
    test: function(before, after) {
      TEquals(before+1, after, "Reading a doc increments docs reads.");
    }
  });
  
  runTest("couchdb", "database_reads", {
    setup: function(db) {db.save({"_id": "test"});},
    run: function(db) {db.request("GET", "/");},
    test: function(before, after) {
      TEquals(before, after, "Only doc reads increment doc reads.");
    }
  });
  
  runTest("couchdb", "database_reads", {
    setup: function(db) {db.save({"_id": "test"});},
    run: function(db) {db.open("test", {"open_revs": "all"});},
    test: function(before, after) {
      TEquals(before+1, after, "Reading doc revs increments docs reads.");
    }
  });
  
  runTest("couchdb", "database_writes", {
    run: function(db) {db.save({"a": "1"});},
    test: function(before, after) {
      TEquals(before+1, after, "Saving docs incrememnts doc writes.");
    }
  });
  
  runTest("couchdb", "database_writes", {
    run: function(db) {
      CouchDB.request("POST", "/test_suite_db", {
        headers: {"Content-Type": "application/json"},
        body: '{"a": "1"}'
      })
    },
    test: function(before, after) {
      TEquals(before+1, after, "POST'ing new docs increments doc writes.");
    }
  })
  
  runTest("couchdb", "database_writes", {
    setup: function(db) {db.save({"_id": "test"});},
    run: function(db) {var doc = db.open("test"); db.save(doc);},
    test: function(before, after) {
      TEquals(before+1, after, "Updating docs incrememnts doc writes.");
    }
  });
  
  runTest("couchdb", "database_writes", {
    setup: function(db) {db.save({"_id": "test"});},
    run: function(db) {var doc = db.open("test"); db.deleteDoc(doc);},
    test: function(before, after) {
      TEquals(before+1, after, "Deleting docs increments doc writes.");
    }
  });
  
  runTest("couchdb", "database_writes", {
    setup: function(db) {db.save({"_id": "test"});},
    run: function(db) {
      CouchDB.request("COPY", "/test_suite_db/test", {
        headers: {"Destination": "copy_of_test"}
      });
    },
    test: function(before, after) {
      TEquals(before+1, after, "Copying docs increments doc writes.");
    }
  });
  
  runTest("couchdb", "database_writes", {
    run: function() {
      CouchDB.request("PUT", "/test_suite_db/bin_doc2/foo2.txt", {
        body: "This is no base64 encoded test",
        headers: {"Content-Type": "text/plain;charset=utf-8"}
      });
    },
    test: function(before, after) {
      TEquals(before+1, after, "Create with attachment increments doc writes.");
    }
  });
  
  runTest("couchdb", "database_writes", {
    setup: function(db) {db.save({"_id": "test"});},
    run: function(db) {
      var doc = db.open("test");
      CouchDB.request("PUT", "/test_suite_db/test/foo2.txt?rev=" + doc._rev, {
        body: "This is no base64 encoded text",
        headers: {"Content-Type": "text/plainn;charset=utf-8"}
      });
    },
    test: function(before, after) {
      TEquals(before+1, after, "Adding attachment increments doc writes.");
    }
  });
  
  runTest("httpd", "bulk_requests", {
    run: function(db) {db.bulkSave(makeDocs(5));},
    test: function(before, after) {
      TEquals(before+1, after, "The bulk_requests counter is incremented.");
    }
  });
  
  runTest("httpd", "view_reads", {
    run: function(db) {doView(db);},
    test: function(before, after) {
      TEquals(before+1, after, "Reading a view increments view reads.");
    }
  });
  
  runTest("httpd", "view_reads", {
    setup: function(db) {db.save({"_id": "test"});},
    run: function(db) {db.open("test");},
    test: function(before, after) {
      TEquals(before, after, "Reading a doc doesn't increment view reads.");
    }
  });
  
  runTest("httpd", "temporary_view_reads", {
    run: function(db) {db.query(function(doc) {emit(doc._id)})},
    test: function(before, after) {
      TEquals(before+1, after, "Temporary views have their own counter.");
    }
  });
  
  runTest("httpd", "temporary_view_reads", {
    run: function(db) {doView(db);},
    test: function(before, after) {
      TEquals(before, after, "Permanent views don't affect temporary views.");
    }
  });
  
  runTest("httpd", "view_reads", {
    run: function(db) {db.query(function(doc) {emit(doc._id)});},
    test: function(before, after) {
      TEquals(before, after, "Temporary views don't affect permanent views.");
    }
  });
  
  // Relies on getting the stats values being GET requests.
  runTest("httpd_request_methods", "GET", {
    test: function(before, after) {
      TEquals(before+1, after, "Get requests are incremented properly.");
    }
  });
  
  runTest("httpd_request_methods", "GET", {
    run: function() {CouchDB.request("POST", "/");},
    test: function(before, after) {
      TEquals(before+1, after, "POST requests don't affect GET counter.");
    }
  });
  
  runTest("httpd_request_methods", "POST", {
    run: function() {CouchDB.request("POST", "/");},
    test: function(before, after) {
      TEquals(before+1, after, "POST requests are incremented properly.");
    }
  });
  
  runTest("httpd_status_codes", "404", {
    run: function() {CouchDB.request("GET", "/nonexistant_db");},
    test: function(before, after) {
      TEquals(before+1, after, "Increments 404 counter on db not found.");
    }
  });
  
  runTest("httpd_status_codes", "404", {
    run: function() {CouchDB.request("GET", "/");},
    test: function(before, after) {
      TEquals(before, after, "Getting DB info doesn't increment 404's");
    }
  });

  (function() {
    var aggregates = [
      "current",
      "description",
      "mean",
      "min",
      "max",
      "stddev",
      "sum"
    ];
    var summary = JSON.parse(CouchDB.request("GET", "/_stats", {
      headers: {"Accept": "application/json"}
    }).responseText);
    for(var i in summary) {
      for(var j in summary[i]) {
        for(var k in summary[i][j]) {
          T(aggregates.indexOf(k) >= 0, "Unknown property name: " + j);
        }
        for(var k in aggregates) {
          var mesg = "Missing required property: " + aggregates[k];
          T(summary[i][j][aggregates[k]] !== undefined, mesg);
        }
      }
    }
  })();
};