Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 8dca82ed authored by tsaichristine's avatar tsaichristine
Browse files

statsd unit test and benchmark refactor

Abstract test utilities into two new functions: writeAttribution and
parseStatsEventToLogEvent

Bug: 149590301
Test: bit statsd_test:*
&& bit statsd_benchmark:*

Change-Id: I5f77646b6d2d828344b8b6de8777a60d98f96d58
parent b6c403b0
Loading
Loading
Loading
Loading
+6 −15
Original line number Original line Diff line number Diff line
@@ -14,12 +14,14 @@
 * limitations under the License.
 * limitations under the License.
 */
 */
#include <vector>
#include <vector>
#include "benchmark/benchmark.h"

#include "FieldValue.h"
#include "FieldValue.h"
#include "HashableDimensionKey.h"
#include "HashableDimensionKey.h"
#include "benchmark/benchmark.h"
#include "logd/LogEvent.h"
#include "logd/LogEvent.h"
#include "stats_log_util.h"
#include "metric_util.h"
#include "stats_event.h"
#include "stats_event.h"
#include "stats_log_util.h"


namespace android {
namespace android {
namespace os {
namespace os {
@@ -34,24 +36,13 @@ static void createLogEventAndMatcher(LogEvent* event, FieldMatcher* field_matche


    std::vector<int> attributionUids = {100, 100};
    std::vector<int> attributionUids = {100, 100};
    std::vector<string> attributionTags = {"LOCATION", "LOCATION"};
    std::vector<string> attributionTags = {"LOCATION", "LOCATION"};
    writeAttribution(statsEvent, attributionUids, attributionTags);


    vector<const char*> cTags(attributionTags.size());
    for (int i = 0; i < cTags.size(); i++) {
        cTags[i] = attributionTags[i].c_str();
    }

    AStatsEvent_writeAttributionChain(statsEvent,
                                      reinterpret_cast<const uint32_t*>(attributionUids.data()),
                                      cTags.data(), attributionUids.size());
    AStatsEvent_writeFloat(statsEvent, 3.2f);
    AStatsEvent_writeFloat(statsEvent, 3.2f);
    AStatsEvent_writeString(statsEvent, "LOCATION");
    AStatsEvent_writeString(statsEvent, "LOCATION");
    AStatsEvent_writeInt64(statsEvent, 990);
    AStatsEvent_writeInt64(statsEvent, 990);
    AStatsEvent_build(statsEvent);


    size_t size;
    parseStatsEventToLogEvent(statsEvent, event);
    uint8_t* buf = AStatsEvent_getBuffer(statsEvent, &size);
    event->parseBuffer(buf, size);
    AStatsEvent_release(statsEvent);


    field_matcher->set_field(1);
    field_matcher->set_field(1);
    auto child = field_matcher->add_child();
    auto child = field_matcher->add_child();
+6 −15
Original line number Original line Diff line number Diff line
@@ -14,12 +14,14 @@
 * limitations under the License.
 * limitations under the License.
 */
 */
#include <vector>
#include <vector>
#include "benchmark/benchmark.h"

#include "FieldValue.h"
#include "FieldValue.h"
#include "HashableDimensionKey.h"
#include "HashableDimensionKey.h"
#include "benchmark/benchmark.h"
#include "logd/LogEvent.h"
#include "logd/LogEvent.h"
#include "stats_log_util.h"
#include "metric_util.h"
#include "stats_event.h"
#include "stats_event.h"
#include "stats_log_util.h"


namespace android {
namespace android {
namespace os {
namespace os {
@@ -34,24 +36,13 @@ static void createLogEventAndLink(LogEvent* event, Metric2Condition *link) {


    std::vector<int> attributionUids = {100, 100};
    std::vector<int> attributionUids = {100, 100};
    std::vector<string> attributionTags = {"LOCATION", "LOCATION"};
    std::vector<string> attributionTags = {"LOCATION", "LOCATION"};
    writeAttribution(statsEvent, attributionUids, attributionTags);


    vector<const char*> cTags(attributionTags.size());
    for (int i = 0; i < cTags.size(); i++) {
        cTags[i] = attributionTags[i].c_str();
    }

    AStatsEvent_writeAttributionChain(statsEvent,
                                      reinterpret_cast<const uint32_t*>(attributionUids.data()),
                                      cTags.data(), attributionUids.size());
    AStatsEvent_writeFloat(statsEvent, 3.2f);
    AStatsEvent_writeFloat(statsEvent, 3.2f);
    AStatsEvent_writeString(statsEvent, "LOCATION");
    AStatsEvent_writeString(statsEvent, "LOCATION");
    AStatsEvent_writeInt64(statsEvent, 990);
    AStatsEvent_writeInt64(statsEvent, 990);
    AStatsEvent_build(statsEvent);


    size_t size;
    parseStatsEventToLogEvent(statsEvent, event);
    uint8_t* buf = AStatsEvent_getBuffer(statsEvent, &size);
    event->parseBuffer(buf, size);
    AStatsEvent_release(statsEvent);


    link->conditionId = 1;
    link->conditionId = 1;


+27 −35
Original line number Original line Diff line number Diff line
@@ -247,21 +247,37 @@ FieldMatcher CreateDimensions(const int atomId, const std::vector<int>& fields)
    return dimensions;
    return dimensions;
}
}


void writeAttribution(AStatsEvent* statsEvent, const vector<int>& attributionUids,
                      const vector<string>& attributionTags) {
    vector<const char*> cTags(attributionTags.size());
    for (int i = 0; i < cTags.size(); i++) {
        cTags[i] = attributionTags[i].c_str();
    }

    AStatsEvent_writeAttributionChain(statsEvent,
                                      reinterpret_cast<const uint32_t*>(attributionUids.data()),
                                      cTags.data(), attributionUids.size());
}

void parseStatsEventToLogEvent(AStatsEvent* statsEvent, LogEvent* logEvent) {
    AStatsEvent_build(statsEvent);

    size_t size;
    uint8_t* buf = AStatsEvent_getBuffer(statsEvent, &size);
    logEvent->parseBuffer(buf, size);

    AStatsEvent_release(statsEvent);
}

std::unique_ptr<LogEvent> CreateScreenStateChangedEvent(
std::unique_ptr<LogEvent> CreateScreenStateChangedEvent(
        uint64_t timestampNs, const android::view::DisplayStateEnum state) {
        uint64_t timestampNs, const android::view::DisplayStateEnum state) {
    AStatsEvent* statsEvent = AStatsEvent_obtain();
    AStatsEvent* statsEvent = AStatsEvent_obtain();
    AStatsEvent_setAtomId(statsEvent, util::SCREEN_STATE_CHANGED);
    AStatsEvent_setAtomId(statsEvent, util::SCREEN_STATE_CHANGED);
    AStatsEvent_overwriteTimestamp(statsEvent, timestampNs);
    AStatsEvent_overwriteTimestamp(statsEvent, timestampNs);

    AStatsEvent_writeInt32(statsEvent, state);
    AStatsEvent_writeInt32(statsEvent, state);
    AStatsEvent_build(statsEvent);

    size_t size;
    uint8_t* buf = AStatsEvent_getBuffer(statsEvent, &size);


    std::unique_ptr<LogEvent> logEvent = std::make_unique<LogEvent>(/*uid=*/0, /*pid=*/0);
    std::unique_ptr<LogEvent> logEvent = std::make_unique<LogEvent>(/*uid=*/0, /*pid=*/0);
    logEvent->parseBuffer(buf, size);
    parseStatsEventToLogEvent(statsEvent, logEvent.get());
    AStatsEvent_release(statsEvent);
    return logEvent;
    return logEvent;
}
}


@@ -272,24 +288,12 @@ std::unique_ptr<LogEvent> CreateScheduledJobStateChangedEvent(
    AStatsEvent_setAtomId(statsEvent, util::SCHEDULED_JOB_STATE_CHANGED);
    AStatsEvent_setAtomId(statsEvent, util::SCHEDULED_JOB_STATE_CHANGED);
    AStatsEvent_overwriteTimestamp(statsEvent, timestampNs);
    AStatsEvent_overwriteTimestamp(statsEvent, timestampNs);


    vector<const char*> cTags(attributionTags.size());
    writeAttribution(statsEvent, attributionUids, attributionTags);
    for (int i = 0; i < cTags.size(); i++) {
        cTags[i] = attributionTags[i].c_str();
    }

    AStatsEvent_writeAttributionChain(statsEvent,
                                      reinterpret_cast<const uint32_t*>(attributionUids.data()),
                                      cTags.data(), attributionUids.size());
    AStatsEvent_writeString(statsEvent, jobName.c_str());
    AStatsEvent_writeString(statsEvent, jobName.c_str());
    AStatsEvent_writeInt32(statsEvent, state);
    AStatsEvent_writeInt32(statsEvent, state);
    AStatsEvent_build(statsEvent);

    size_t size;
    uint8_t* buf = AStatsEvent_getBuffer(statsEvent, &size);


    std::unique_ptr<LogEvent> logEvent = std::make_unique<LogEvent>(/*uid=*/0, /*pid=*/0);
    std::unique_ptr<LogEvent> logEvent = std::make_unique<LogEvent>(/*uid=*/0, /*pid=*/0);
    logEvent->parseBuffer(buf, size);
    parseStatsEventToLogEvent(statsEvent, logEvent.get());
    AStatsEvent_release(statsEvent);
    return logEvent;
    return logEvent;
}
}


@@ -319,24 +323,12 @@ std::unique_ptr<LogEvent> CreateSyncStateChangedEvent(uint64_t timestampNs,
    AStatsEvent_setAtomId(statsEvent, util::SYNC_STATE_CHANGED);
    AStatsEvent_setAtomId(statsEvent, util::SYNC_STATE_CHANGED);
    AStatsEvent_overwriteTimestamp(statsEvent, timestampNs);
    AStatsEvent_overwriteTimestamp(statsEvent, timestampNs);


    vector<const char*> cTags(attributionTags.size());
    writeAttribution(statsEvent, attributionUids, attributionTags);
    for (int i = 0; i < cTags.size(); i++) {
        cTags[i] = attributionTags[i].c_str();
    }

    AStatsEvent_writeAttributionChain(statsEvent,
                                      reinterpret_cast<const uint32_t*>(attributionUids.data()),
                                      cTags.data(), attributionUids.size());
    AStatsEvent_writeString(statsEvent, name.c_str());
    AStatsEvent_writeString(statsEvent, name.c_str());
    AStatsEvent_writeInt32(statsEvent, state);
    AStatsEvent_writeInt32(statsEvent, state);
    AStatsEvent_build(statsEvent);

    size_t size;
    uint8_t* buf = AStatsEvent_getBuffer(statsEvent, &size);


    std::unique_ptr<LogEvent> logEvent = std::make_unique<LogEvent>(/*uid=*/0, /*pid=*/0);
    std::unique_ptr<LogEvent> logEvent = std::make_unique<LogEvent>(/*uid=*/0, /*pid=*/0);
    logEvent->parseBuffer(buf, size);
    parseStatsEventToLogEvent(statsEvent, logEvent.get());
    AStatsEvent_release(statsEvent);
    return logEvent;
    return logEvent;
}
}


+6 −0
Original line number Original line Diff line number Diff line
@@ -18,6 +18,7 @@
#include "frameworks/base/cmds/statsd/src/statsd_config.pb.h"
#include "frameworks/base/cmds/statsd/src/statsd_config.pb.h"
#include "src/StatsLogProcessor.h"
#include "src/StatsLogProcessor.h"
#include "src/logd/LogEvent.h"
#include "src/logd/LogEvent.h"
#include "stats_event.h"
#include "statslog.h"
#include "statslog.h"


namespace android {
namespace android {
@@ -92,6 +93,11 @@ FieldMatcher CreateAttributionUidAndTagDimensions(const int atomId,
FieldMatcher CreateAttributionUidDimensions(const int atomId,
FieldMatcher CreateAttributionUidDimensions(const int atomId,
                                            const std::vector<Position>& positions);
                                            const std::vector<Position>& positions);


void writeAttribution(AStatsEvent* statsEvent, const vector<int>& attributionUids,
                      const vector<string>& attributionTags);

void parseStatsEventToLogEvent(AStatsEvent* statsEvent, LogEvent* logEvent);

// Create log event for screen state changed.
// Create log event for screen state changed.
std::unique_ptr<LogEvent> CreateScreenStateChangedEvent(
std::unique_ptr<LogEvent> CreateScreenStateChangedEvent(
        uint64_t timestampNs, const android::view::DisplayStateEnum state);
        uint64_t timestampNs, const android::view::DisplayStateEnum state);
+4 −28
Original line number Original line Diff line number Diff line
@@ -41,22 +41,10 @@ void makeLogEvent(LogEvent* logEvent, const int32_t atomId, const int64_t timest
    AStatsEvent_setAtomId(statsEvent, atomId);
    AStatsEvent_setAtomId(statsEvent, atomId);
    AStatsEvent_overwriteTimestamp(statsEvent, timestamp);
    AStatsEvent_overwriteTimestamp(statsEvent, timestamp);


    vector<const char*> cTags(attributionTags.size());
    writeAttribution(statsEvent, attributionUids, attributionTags);
    for (int i = 0; i < cTags.size(); i++) {
        cTags[i] = attributionTags[i].c_str();
    }

    AStatsEvent_writeAttributionChain(statsEvent,
                                      reinterpret_cast<const uint32_t*>(attributionUids.data()),
                                      cTags.data(), attributionUids.size());
    AStatsEvent_writeString(statsEvent, name.c_str());
    AStatsEvent_writeString(statsEvent, name.c_str());
    AStatsEvent_build(statsEvent);

    size_t size;
    uint8_t* buf = AStatsEvent_getBuffer(statsEvent, &size);
    logEvent->parseBuffer(buf, size);


    AStatsEvent_release(statsEvent);
    parseStatsEventToLogEvent(statsEvent, logEvent);
}
}


void makeLogEvent(LogEvent* logEvent, const int32_t atomId, const int64_t timestamp,
void makeLogEvent(LogEvent* logEvent, const int32_t atomId, const int64_t timestamp,
@@ -66,22 +54,10 @@ void makeLogEvent(LogEvent* logEvent, const int32_t atomId, const int64_t timest
    AStatsEvent_setAtomId(statsEvent, atomId);
    AStatsEvent_setAtomId(statsEvent, atomId);
    AStatsEvent_overwriteTimestamp(statsEvent, timestamp);
    AStatsEvent_overwriteTimestamp(statsEvent, timestamp);


    vector<const char*> cTags(attributionTags.size());
    writeAttribution(statsEvent, attributionUids, attributionTags);
    for (int i = 0; i < cTags.size(); i++) {
        cTags[i] = attributionTags[i].c_str();
    }

    AStatsEvent_writeAttributionChain(statsEvent,
                                      reinterpret_cast<const uint32_t*>(attributionUids.data()),
                                      cTags.data(), attributionUids.size());
    AStatsEvent_writeInt32(statsEvent, value);
    AStatsEvent_writeInt32(statsEvent, value);
    AStatsEvent_build(statsEvent);

    size_t size;
    uint8_t* buf = AStatsEvent_getBuffer(statsEvent, &size);
    logEvent->parseBuffer(buf, size);


    AStatsEvent_release(statsEvent);
    parseStatsEventToLogEvent(statsEvent, logEvent);
}
}
}  // anonymous namespace
}  // anonymous namespace


Loading