/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ /* vim: set ts=8 sts=2 et sw=2 tw=80: */ /* This Source Code Form is subject to the terms of the Mozilla Public *License,v.2.0.IfacopyoftheMPLwasnotdistributedwiththis
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/* Moving average window size. */ const int32_t AVG_WINDOW = 20; /* Calculate the moving average for the new value aValue for the current window *aWindowandthealreadyexistingaverageaAverage.Whenthemethodreturns *aAveragewillcontainthenewaverageandaWindowwillcontainthenew
* window.*/ void BenchmarkStorageParent::MovingAverage(int32_t& aAverage, int32_t& aWindow, const int32_t aValue) { if (aWindow < AVG_WINDOW) {
aAverage = (aAverage * aWindow + aValue) / (aWindow + 1);
aWindow++; return;
}
MOZ_ASSERT(aWindow == AVG_WINDOW);
aAverage = (aAverage - aAverage / aWindow) + (aValue / aWindow);
}
/* In order to decrease the number of times the database is accessed when the *movingaverageisstoredorretrievedweusethesamevaluetostore_both_ *thewindowandtheaverage.Therangeoftheaverageislimitedsinceitis *apercentage(0-100),andtherangeofthewindowislimited *(1-20).Thusthenumberthatisstoredinthedatabaseisintheform *(digits):WWAAA.Forexample,thevaluestoredwhenanaverage(A)of88 *correspondstoawindow(W)7is7088.Theaverageof100thatcorrespondsto *awindowof20is20100.Thefollowingmethodsarehelperstoextractor
* construct the stored value according to the above. */
/* Stored value will be in the form WWAAA(19098). We need to extract the window *(19)andtheaveragescore(98).TheaValuewill *beparsed,theaWindowwillcontainthewindow(ofthemovingaverage)and
* the return value will contain the average itself. */
int32_t BenchmarkStorageParent::ParseStoredValue(int32_t aValue,
int32_t& aWindow) {
MOZ_ASSERT(aValue > 999);
MOZ_ASSERT(aValue < 100000);
IPCResult BenchmarkStorageParent::RecvPut(const nsCString& aDbName, const nsCString& aKey, const int32_t& aValue) { // In order to calculate and store the new moving average, we need to get the // stored value and window first, to calculate the new score and window, and // then to store the new aggregated value.
mStorage->Get(aDbName, aKey)
->Then(
GetCurrentSerialEventTarget(), __func__,
[storage = mStorage, aDbName, aKey, aValue](int32_t aResult) {
int32_t window = 0;
int32_t average = 0; if (aResult >= 0) { // The key found.
average = ParseStoredValue(aResult, window);
}
MovingAverage(average, window, aValue);
int32_t newValue = PrepareStoredValue(average, window); // Avoid storing if the values are the same. This is an optimization // to minimize the disk usage. if (aResult != newValue) {
storage->Put(aDbName, aKey, newValue);
}
},
[](nsresult rv) { /*do nothing*/ });
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.