Bug 748914 - Part 2: add a clear() method to JS histograms; r=taras

This commit is contained in:
Nathan Froyd 2012-05-08 15:39:24 -04:00
Родитель ae7166da99
Коммит bbcef6a2d6
3 изменённых файлов: 31 добавлений и 2 удалений

Просмотреть файл

@ -461,6 +461,19 @@ JSHistogram_Snapshot(JSContext *cx, unsigned argc, jsval *vp)
}
}
JSBool
JSHistogram_Clear(JSContext *cx, unsigned argc, jsval *vp)
{
JSObject *obj = JS_THIS_OBJECT(cx, vp);
if (!obj) {
return JS_FALSE;
}
Histogram *h = static_cast<Histogram*>(JS_GetPrivate(obj));
h->Clear();
return JS_TRUE;
}
nsresult
WrapAndReturnHistogram(Histogram *h, JSContext *cx, jsval *ret)
{
@ -475,8 +488,9 @@ WrapAndReturnHistogram(Histogram *h, JSContext *cx, jsval *ret)
if (!obj)
return NS_ERROR_FAILURE;
JS::AutoObjectRooter root(cx, obj);
if (!(JS_DefineFunction (cx, obj, "add", JSHistogram_Add, 1, 0)
&& JS_DefineFunction (cx, obj, "snapshot", JSHistogram_Snapshot, 1, 0))) {
if (!(JS_DefineFunction(cx, obj, "add", JSHistogram_Add, 1, 0)
&& JS_DefineFunction(cx, obj, "snapshot", JSHistogram_Snapshot, 0, 0)
&& JS_DefineFunction(cx, obj, "clear", JSHistogram_Clear, 0, 0))) {
return NS_ERROR_FAILURE;
}
*ret = OBJECT_TO_JSVAL(obj);

Просмотреть файл

@ -158,6 +158,7 @@ interface nsITelemetry : nsISupports
* The returned object has the following functions:
* add(int) - Adds an int value to the appropriate bucket
* snapshot() - Returns a snapshot of the histogram with the same data fields as in histogramSnapshots()
* clear() - Zeros out the histogram's buckets and sum
*/
[implicit_jscontext]
jsval newHistogram(in ACString name, in PRUint32 min, in PRUint32 max, in PRUint32 bucket_count, in unsigned long histogram_type);

Просмотреть файл

@ -39,6 +39,20 @@ function test_histogram(histogram_type, name, min, max, bucket_count) {
var s = h.snapshot().counts;
do_check_eq(s[0], 2)
do_check_eq(s[1], 2)
// Check that clearing works.
h.clear();
var s = h.snapshot();
for each(var i in s.counts) {
do_check_eq(i, 0);
}
do_check_eq(s.sum, 0);
h.add(0);
h.add(1);
var c = h.snapshot().counts;
do_check_eq(c[0], 1);
do_check_eq(c[1], 1);
}
function expect_fail(f) {