Bug 705845 - Add telemetry for size of values stored in localStorage; r=honzab

This commit is contained in:
Nathan Froyd 2011-12-01 19:19:56 -05:00
parent 0ee9916f7b
commit 45b521a800
3 changed files with 74 additions and 0 deletions

View File

@ -73,6 +73,7 @@ using mozilla::dom::StorageChild;
#include "nsNetCID.h"
#include "mozilla/Preferences.h"
#include "nsThreadUtils.h"
#include "mozilla/Telemetry.h"
// calls FlushAndDeleteTemporaryTables(false)
#define NS_DOMSTORAGE_FLUSH_TIMER_TOPIC "domstorage-flush-timer"
@ -1601,6 +1602,40 @@ nsDOMStorage::GetItem(const nsAString& aKey, nsAString &aData)
return NS_OK;
}
static Telemetry::ID
TelemetryIDForKey(nsPIDOMStorage::nsDOMStorageType type)
{
switch (type) {
default:
MOZ_ASSERT(false);
// We need to return something to satisfy the compiler.
// Fallthrough.
case nsPIDOMStorage::GlobalStorage:
return Telemetry::GLOBALDOMSTORAGE_KEY_SIZE_BYTES;
case nsPIDOMStorage::LocalStorage:
return Telemetry::LOCALDOMSTORAGE_KEY_SIZE_BYTES;
case nsPIDOMStorage::SessionStorage:
return Telemetry::SESSIONDOMSTORAGE_KEY_SIZE_BYTES;
}
}
static Telemetry::ID
TelemetryIDForValue(nsPIDOMStorage::nsDOMStorageType type)
{
switch (type) {
default:
MOZ_ASSERT(false);
// We need to return something to satisfy the compiler.
// Fallthrough.
case nsPIDOMStorage::GlobalStorage:
return Telemetry::GLOBALDOMSTORAGE_VALUE_SIZE_BYTES;
case nsPIDOMStorage::LocalStorage:
return Telemetry::LOCALDOMSTORAGE_VALUE_SIZE_BYTES;
case nsPIDOMStorage::SessionStorage:
return Telemetry::SESSIONDOMSTORAGE_VALUE_SIZE_BYTES;
}
}
NS_IMETHODIMP
nsDOMStorage::GetItem(const nsAString& aKey, nsIDOMStorageItem **aItem)
{
@ -1617,6 +1652,9 @@ nsDOMStorage::SetItem(const nsAString& aKey, const nsAString& aData)
if (!CacheStoragePermissions())
return NS_ERROR_DOM_SECURITY_ERR;
Telemetry::Accumulate(TelemetryIDForKey(mStorageType), aKey.Length());
Telemetry::Accumulate(TelemetryIDForValue(mStorageType), aData.Length());
nsString oldValue;
nsresult rv = mStorageImpl->SetValue(IsCallerSecure(), aKey, aData, oldValue);
if (NS_FAILED(rv))

View File

@ -13,15 +13,34 @@ function run_test()
testURI(Services.io.newURI("moz-safe-about:rights", null, null));
}
function sum(a)
{
return a.reduce(function(prev, current, index, array) {
return prev + current;
});
}
function testURI(aURI)
{
print("Testing: " + aURI.spec);
let storage = getStorageForURI(aURI);
let Telemetry = Components.classes["@mozilla.org/base/telemetry;1"].
getService(Components.interfaces.nsITelemetry);
let key_histogram = Telemetry.getHistogramById("LOCALDOMSTORAGE_KEY_SIZE_BYTES");
let value_histogram = Telemetry.getHistogramById("LOCALDOMSTORAGE_VALUE_SIZE_BYTES");
let before_key_snapshot = key_histogram.snapshot();
let before_value_snapshot = value_histogram.snapshot();
storage.setItem("test-item", "test-value");
print("Check that our value has been correctly stored.");
let after_key_snapshot = key_histogram.snapshot();
let after_value_snapshot = value_histogram.snapshot();
do_check_eq(storage.length, 1);
do_check_eq(storage.key(0), "test-item");
do_check_eq(storage.getItem("test-item"), "test-value");
do_check_eq(sum(after_key_snapshot.counts),
sum(before_key_snapshot.counts)+1);
do_check_eq(sum(after_value_snapshot.counts),
sum(before_value_snapshot.counts)+1);
print("Check that our value is correctly removed.");
storage.removeItem("test-item");

View File

@ -259,5 +259,22 @@ HISTOGRAM(XUL_REFLOW_MS, 1, 3000, 10, EXPONENTIAL, "xul reflows")
HISTOGRAM(XUL_INITIAL_FRAME_CONSTRUCTION, 1, 3000, 10, EXPONENTIAL, "initial xul frame construction")
HISTOGRAM_BOOLEAN(XMLHTTPREQUEST_ASYNC_OR_SYNC, "Type of XMLHttpRequest, async or sync")
/**
* DOM Storage telemetry.
*/
#define DOMSTORAGE_HISTOGRAM(PREFIX, TYPE, TYPESTRING, DESCRIPTION) \
HISTOGRAM(PREFIX ## DOMSTORAGE_ ## TYPE ## _SIZE_BYTES, \
1024, 32768, 10, EXPONENTIAL, "DOM storage: size of " TYPESTRING "s stored in " DESCRIPTION "Storage")
#define DOMSTORAGE_KEY_VAL_SIZE(PREFIX, DESCRIPTION) \
DOMSTORAGE_HISTOGRAM(PREFIX, KEY, "key", DESCRIPTION) \
DOMSTORAGE_HISTOGRAM(PREFIX, VALUE, "value", DESCRIPTION)
DOMSTORAGE_KEY_VAL_SIZE(GLOBAL, "global")
DOMSTORAGE_KEY_VAL_SIZE(LOCAL, "local")
DOMSTORAGE_KEY_VAL_SIZE(SESSION, "session")
#undef DOMSTORAGE_KEY_VAL_SIZE
#undef DOMSTORAGE_HISTOGRAM
#undef HISTOGRAM_BOOLEAN