Skip to content

Commit

Permalink
fix: tests
Browse files Browse the repository at this point in the history
  • Loading branch information
robik committed Sep 20, 2024
1 parent cad9c9c commit e57a510
Show file tree
Hide file tree
Showing 22 changed files with 471 additions and 625 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,18 @@ void NativePerformance::measure(
#endif
}

void NativePerformance::logEvent(
jsi::Runtime& rt,
std::string name,
double startTime,
double duration,
double processingStart,
double processingEnd,
double interactionId) {
PerformanceEntryReporter::getInstance()->logEventEntry(
name, startTime, duration, processingStart, processingEnd, interactionId);
}

std::unordered_map<std::string, double> NativePerformance::getSimpleMemoryInfo(
jsi::Runtime& rt) {
auto heapInfo = rt.instrumentation().getHeapInfo(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ class NativePerformance : public NativePerformanceCxxSpec<NativePerformance> {
std::optional<std::string> startMark,
std::optional<std::string> endMark);

void logEvent(
jsi::Runtime& rt,
std::string name,
double startTime,
double duration,
double processingStart,
double processingEnd,
double interactionId);

// To align with web API, we will make sure to return three properties
// (jsHeapSizeLimit, totalJSHeapSize, usedJSHeapSize) + anything needed from
// the VM side.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,14 @@ jsi::Object NativePerformanceObserver::createObserver(
// the spec. The specification requires us to queue a single task that
// dispatches observer callbacks. Instead, we are queuing all callbacks as
// separate tasks in the scheduler.
PerformanceObserverCallback cb = [this, callback = std::move(callback)](
PerformanceObserver& currentObserver) {
jsInvoker_->invokeAsync(
SchedulerPriority::IdlePriority,
[currentObserver, callback](jsi::Runtime& /*rt*/) mutable {
callback.call();
currentObserver.flush();
});
PerformanceObserverCallback cb = [callback = std::move(callback)]() {
callback.callWithPriority(SchedulerPriority::IdlePriority);
};

auto observer = std::make_shared<PerformanceObserver>(std::move(cb));
auto& registry =
PerformanceEntryReporter::getInstance()->getObserverRegistry();

auto observer = PerformanceObserver::create(registry, std::move(cb));
jsi::Object observerObj{rt};
observerObj.setNativeState(rt, observer);
return observerObj;
Expand Down Expand Up @@ -97,10 +94,6 @@ void NativePerformanceObserver::observe(
{.buffered = buffered, .durationThreshold = durationThreshold});
}
}

auto& registry =
PerformanceEntryReporter::getInstance()->getObserverRegistry();
registry.addObserver(observer);
}

void NativePerformanceObserver::disconnect(
Expand All @@ -114,10 +107,7 @@ void NativePerformanceObserver::disconnect(
}

observerObj.setNativeState(rt, nullptr);

auto& registry =
PerformanceEntryReporter::getInstance()->getObserverRegistry();
registry.removeObserver(observer);
observer->disconnect();
}

std::vector<PerformanceEntry> NativePerformanceObserver::takeRecords(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#endif

#include <react/performance/timeline/PerformanceEntryReporter.h>
#include <react/performance/timeline/PerformanceObserver.h>
#include <optional>
#include <string>
#include <vector>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace facebook::react {
template <class T>
class CircularBuffer {
public:
explicit CircularBuffer(size_t maxSize): maxSize_(maxSize) {
explicit CircularBuffer(size_t maxSize) : maxSize_(maxSize) {
entries_.reserve(maxSize_);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ void PerformanceObserver::handleEntry(const PerformanceEntry& entry) {
std::vector<PerformanceEntry> PerformanceObserver::takeRecords() {
std::vector<PerformanceEntry> result;
buffer_.swap(result);
flush();
return result;
}

Expand All @@ -51,6 +52,7 @@ void PerformanceObserver::observe(
scheduleFlushBuffer();
}
}
registry_.addObserver(shared_from_this());
}

void PerformanceObserver::observe(
Expand All @@ -59,6 +61,7 @@ void PerformanceObserver::observe(
observedTypes_ = std::move(types);
requiresDroppedEntries_ = false;
durationThreshold_ = options.durationThreshold;
registry_.addObserver(shared_from_this());
}

double PerformanceObserver::getDroppedEntriesCount() noexcept {
Expand All @@ -77,6 +80,10 @@ double PerformanceObserver::getDroppedEntriesCount() noexcept {
return droppedEntriesCount;
}

void PerformanceObserver::disconnect() noexcept {
registry_.removeObserver(shared_from_this());
}

void PerformanceObserver::flush() noexcept {
didScheduleFlushBuffer = false;
}
Expand All @@ -89,7 +96,7 @@ void PerformanceObserver::scheduleFlushBuffer() {
if (!didScheduleFlushBuffer) {
didScheduleFlushBuffer = true;

callback_(*this);
callback_();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@
#include <unordered_set>
#include <vector>
#include "PerformanceEntryBuffer.h"
#include "PerformanceObserverRegistry.h"

namespace facebook::react {

class PerformanceObserver;

using PerformanceObserverEntryTypeFilter =
std::unordered_set<PerformanceEntryType>;
using PerformanceObserverCallback =
std::function<void(PerformanceObserver& observer)>;
using PerformanceObserverCallback = std::function<void()>;

/**
* Represents subset of spec's `PerformanceObserverInit` that is allowed for
Expand Down Expand Up @@ -52,10 +52,27 @@ struct PerformanceObserverObserveSingleOptions {
* Entries are pushed to the observer by the `PerformanceEntryReporter` class,
* through the `PerformanceObserverRegistry` class which acts as a central hub.
*/
class PerformanceObserver : public jsi::NativeState {
class PerformanceObserver
: public jsi::NativeState,
public std::enable_shared_from_this<PerformanceObserver> {
private:
struct PrivateUseCreateMethod {
explicit PrivateUseCreateMethod() = default;
};

public:
explicit PerformanceObserver(PerformanceObserverCallback&& callback)
: callback_(std::move(callback)) {}
explicit PerformanceObserver(
PrivateUseCreateMethod,
PerformanceObserverRegistry& registry,
PerformanceObserverCallback&& callback)
: registry_(registry), callback_(std::move(callback)) {}

static std::shared_ptr<PerformanceObserver> create(
PerformanceObserverRegistry& registry,
PerformanceObserverCallback&& callback) {
return std::make_shared<PerformanceObserver>(
PrivateUseCreateMethod(), registry, std::move(callback));
}

~PerformanceObserver() = default;

Expand Down Expand Up @@ -95,19 +112,21 @@ class PerformanceObserver : public jsi::NativeState {
PerformanceObserverObserveMultipleOptions options = {});

/**
* Internal function called by JS bridge to get number of dropped entries
* count counted at call time.
* Disconnects observer from the registry
*/
double getDroppedEntriesCount() noexcept;
void disconnect() noexcept;

/**
* Called when the callback was dispatched
* Internal function called by JS bridge to get number of dropped entries
* count counted at call time.
*/
void flush() noexcept;
double getDroppedEntriesCount() noexcept;

private:
void scheduleFlushBuffer();
void flush() noexcept;

PerformanceObserverRegistry& registry_;
PerformanceObserverCallback callback_;
PerformanceObserverEntryTypeFilter observedTypes_;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

#include "PerformanceObserverRegistry.h"
#include "PerformanceObserver.h"

namespace facebook::react {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@
#include <memory>
#include <mutex>
#include <set>
#include "PerformanceObserver.h"
#include "PerformanceEntry.h"

namespace facebook::react {

class PerformanceObserver;

/**
* PerformanceObserverRegistry acts as a container for known performance
* observer instances.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ constexpr auto OK = false;
constexpr auto OVERWRITE = true;

TEST(CircularBuffer, CanAddAndRetrieveElements) {
CircularBuffer<int> buffer;
CircularBuffer<int> buffer{5};

ASSERT_EQ(OK, buffer.add(1));
ASSERT_EQ(OK, buffer.add(2));
Expand Down
Loading

0 comments on commit e57a510

Please sign in to comment.