Files
react-native/Libraries/WebPerformance/PerformanceEntryReporter.h
T
Ruslan Shestopalyuk 14ab76ac30 Hoist responsibility for clearMarks/Measures to NativePerformanceObserver (#36312)
Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/36312

## Changelog:

[Internal] -

`clearMarks` and `clearMeasures` methods are incidental to the `NativePerformance` TurboModule functionality, as in reality this responsibility belongs more on the `NativePerformanceObserver` and `PerformanceEntryReporter` side.

This is something that [the standard indirectly suggests](https://www.w3.org/TR/user-timing/#clearmarks-method) as well (referencing [performance entry buffer](https://www.w3.org/TR/performance-timeline/#dfn-performance-entry-buffer)).

The new implementation should be also a little bit more efficient, as it avoids calling the predicate for each entry.

Finally (and frankly, the main reason for this change, from my perspective), it will simplify mocking/testing the JS part of the PerfAPI code.

Reviewed By: rubennorte

Differential Revision: D43621174

fbshipit-source-id: c4217a0da1d8ecbce797240627f7b4f057d85b97
2023-02-28 04:10:32 -08:00

157 lines
4.5 KiB
C++

/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#pragma once
#include <react/bridging/Function.h>
#include <react/renderer/core/EventLogger.h>
#include <array>
#include <functional>
#include <mutex>
#include <optional>
#include <unordered_map>
#include <unordered_set>
#include "NativePerformanceObserver.h"
namespace facebook::react {
struct PerformanceMark {
std::string name;
double timeStamp;
};
struct PerformanceMarkHash {
size_t operator()(const PerformanceMark *mark) const {
return std::hash<std::string>()(mark->name);
}
};
struct PerformanceMarkEqual {
bool operator()(const PerformanceMark *lhs, const PerformanceMark *rhs)
const {
return lhs->name == rhs->name;
}
};
using PerformanceMarkRegistryType = std::
unordered_set<PerformanceMark *, PerformanceMarkHash, PerformanceMarkEqual>;
// Only the MARKS_BUFFER_SIZE amount of the latest marks will be kept in
// memory for the sake of the "Performance.measure" mark name lookup
constexpr size_t MARKS_BUFFER_SIZE = 1024;
constexpr double DEFAULT_DURATION_THRESHOLD = 0.0;
enum class PerformanceEntryType {
UNDEFINED = 0,
MARK = 1,
MEASURE = 2,
EVENT = 3,
_COUNT = 4,
};
class PerformanceEntryReporter : public EventLogger {
public:
PerformanceEntryReporter(PerformanceEntryReporter const &) = delete;
void operator=(PerformanceEntryReporter const &) = delete;
// NOTE: This class is not thread safe, make sure that the calls are made from
// the same thread.
// TODO: Consider passing it as a parameter to the corresponding modules at
// creation time instead of having the singleton.
static PerformanceEntryReporter &getInstance();
void setReportingCallback(std::optional<AsyncCallback<>> callback);
void startReporting(PerformanceEntryType entryType);
void stopReporting(PerformanceEntryType entryType);
void setDurationThreshold(
PerformanceEntryType entryType,
double durationThreshold);
GetPendingEntriesResult popPendingEntries();
void logEntry(const RawPerformanceEntry &entry);
bool isReportingType(PerformanceEntryType entryType) const {
return reportingType_[static_cast<int>(entryType)];
}
uint32_t getDroppedEntryCount() const {
return droppedEntryCount_;
}
void mark(const std::string &name, double startTime, double duration);
void measure(
const std::string &name,
double startTime,
double endTime,
const std::optional<double> &duration,
const std::optional<std::string> &startMark,
const std::optional<std::string> &endMark);
void clearEntries(
PerformanceEntryType entryType,
const char *entryName = nullptr);
void event(
std::string name,
double startTime,
double duration,
double processingStart,
double processingEnd,
uint32_t interactionId);
EventTag onEventStart(const char *name) override;
void onEventDispatch(EventTag tag) override;
void onEventEnd(EventTag tag) override;
const std::unordered_map<std::string, uint32_t> &getEventCounts() const {
return eventCounts_;
}
private:
PerformanceEntryReporter() {}
double getMarkTime(const std::string &markName) const;
void scheduleFlushBuffer();
bool isReportingEvents() const {
return isReportingType(PerformanceEntryType::EVENT);
}
std::optional<AsyncCallback<>> callback_;
std::vector<RawPerformanceEntry> entries_;
std::mutex entriesMutex_;
std::array<bool, (size_t)PerformanceEntryType::_COUNT> reportingType_{false};
std::unordered_map<std::string, uint32_t> eventCounts_;
std::array<double, (size_t)PerformanceEntryType::_COUNT> durationThreshold_{
DEFAULT_DURATION_THRESHOLD};
// Mark registry for "measure" lookup
PerformanceMarkRegistryType marksRegistry_;
std::array<PerformanceMark, MARKS_BUFFER_SIZE> marksBuffer_;
size_t marksBufferPosition_{0};
uint32_t droppedEntryCount_{0};
struct EventEntry {
const char *name;
double startTime{0.0};
double dispatchTime{0.0};
};
// Registry to store the events that are currently ongoing.
// Note that we could probably use a more efficient container for that,
// but since we only report discrete events, the volume is normally low,
// so a hash map should be just fine.
std::unordered_map<EventTag, EventEntry> eventsInFlight_;
std::mutex eventsInFlightMutex_;
static EventTag sCurrentEventTag_;
};
} // namespace facebook::react