Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ include(FetchContent)
# Declarations
# --------------------------------------------------
FetchContent_Declare(arcana.cpp
GIT_REPOSITORY https://github.com/microsoft/arcana.cpp.git
GIT_TAG c726dbe58713eda65bfb139c257093c43479b894)
GIT_REPOSITORY https://github.com/ryantrem/arcana.cpp.git
GIT_TAG c117bfda6fc855e8e63f6bfb8cf1b66d51026dcb)
FetchContent_Declare(AndroidExtensions
GIT_REPOSITORY https://github.com/BabylonJS/AndroidExtensions.git
GIT_TAG f7ed149b5360cc8a4908fece66607c5ce1e6095b)
Expand Down
13 changes: 11 additions & 2 deletions Core/Foundation/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
set(SOURCES
"Include/Babylon/Api.h"
"Include/Babylon/DebugTrace.h"
"Source/DebugTrace.cpp")
"Include/Babylon/PerfTrace.h"
"Source/DebugTrace.cpp"
"Source/PerfTrace.cpp")

add_library(Foundation ${SOURCES})

target_include_directories(Foundation INTERFACE "Include")
target_include_directories(Foundation
PRIVATE "Include/Babylon"
INTERFACE "Include")

target_link_libraries(Foundation
PUBLIC napi
PRIVATE napi-extensions
PRIVATE arcana)

set_property(TARGET Foundation PROPERTY FOLDER Core)
source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR} FILES ${SOURCES})
39 changes: 39 additions & 0 deletions Core/Foundation/Include/Babylon/PerfTrace.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#pragma once
#include <memory>
#include <napi/env.h>


namespace Babylon
{
namespace PerfTrace
{
enum class Level
{
None,
Mark,
Log,
};

class Handle
{
public:
~Handle();
Handle(const Handle&) = delete;
Handle& operator=(const Handle&) = delete;
Handle(Handle&&) noexcept;
Handle& operator=(Handle&&) noexcept;

static Napi::Value ToNapi(Napi::Env env, Handle handle);
static Handle FromNapi(Napi::Value napiValue);

private:
friend Handle Trace(const char* name);
Handle(const char* name);
struct Impl;
std::unique_ptr<Impl> m_impl;
};

void SetLevel(Level level);
Handle Trace(const char* name);
}
}
74 changes: 74 additions & 0 deletions Core/Foundation/Source/PerfTrace.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#include "PerfTrace.h"
#include <arcana/tracing/trace_region.h>
#include <unordered_map>

namespace
{
std::unordered_map<std::uint32_t, Babylon::PerfTrace::Handle> g_traceHandles;
std::uint32_t g_nextTraceId = 0;
}

namespace Babylon
{
namespace PerfTrace
{
void SetLevel(Level level)
{
switch(level)
{
case Level::None:
arcana::trace_region::disable();
break;
case Level::Mark:
arcana::trace_region::enable(arcana::trace_level::mark);
break;
case Level::Log:
arcana::trace_region::enable(arcana::trace_level::log);
break;
}
}

// Private implementation to hide arcana::trace_region
struct Handle::Impl
{
arcana::trace_region region;

Impl(const char* name) : region(name) {}
};

Handle::Handle(const char* name)
: m_impl(std::make_unique<Impl>(name))
{
}

Handle::~Handle() = default;
Handle::Handle(Handle&&) noexcept = default;
Handle& Handle::operator=(Handle&&) noexcept = default;

Handle Trace(const char* name)
{
return Handle(name);
}

Napi::Value Handle::ToNapi(Napi::Env env, Handle traceHandle)
{
g_nextTraceId++;
g_traceHandles.emplace(g_nextTraceId, std::move(traceHandle));
return Napi::Value::From(env, g_nextTraceId);
}

Handle Handle::FromNapi(Napi::Value napiValue)
{
const std::uint32_t traceId = napiValue.As<Napi::Number>().Uint32Value();
auto it = g_traceHandles.find(traceId);
if (it == g_traceHandles.end())
{
throw std::runtime_error("Invalid TraceHandle ID");
}

Handle traceHandle = std::move(it->second);
g_traceHandles.erase(it);
return traceHandle;
}
}
}
14 changes: 11 additions & 3 deletions Polyfills/XMLHttpRequest/Source/XMLHttpRequest.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "XMLHttpRequest.h"
#include <Babylon/JsRuntime.h>
#include <Babylon/Polyfills/XMLHttpRequest.h>
#include <arcana/tracing/trace_region.h>
#include <sstream>

namespace Babylon::Polyfills::Internal
{
Expand Down Expand Up @@ -216,11 +218,11 @@ namespace Babylon::Polyfills::Internal

void XMLHttpRequest::Open(const Napi::CallbackInfo& info)
{
const auto inputURL = info[1].As<Napi::String>();
m_url = info[1].As<Napi::String>();

try
{
m_request.Open(MethodType::StringToEnum(info[0].As<Napi::String>().Utf8Value()), inputURL);
m_request.Open(MethodType::StringToEnum(info[0].As<Napi::String>().Utf8Value()), m_url);
}
catch (const std::exception& e)
{
Expand Down Expand Up @@ -254,7 +256,11 @@ namespace Babylon::Polyfills::Internal
}
}

m_request.SendAsync().then(m_runtimeScheduler, arcana::cancellation::none(), [this]() {
std::string traceName = (std::ostringstream{} << "XMLHttpRequest::Send [" << m_url << "]").str();
arcana::trace_region sendRegion{traceName.c_str()};
m_request.SendAsync().then(arcana::inline_scheduler, arcana::cancellation::none(), [sendRegion{std::make_optional(std::move(sendRegion))}]() mutable {
sendRegion.reset();
}).then(m_runtimeScheduler, arcana::cancellation::none(), [this]() {
SetReadyState(ReadyState::Done);
RaiseEvent(EventType::LoadEnd);

Expand All @@ -277,6 +283,8 @@ namespace Babylon::Polyfills::Internal

void XMLHttpRequest::RaiseEvent(const char* eventType)
{
std::string traceName = (std::ostringstream{} << "XMLHttpRequest::RaiseEvent [" << eventType << "] [" << m_url << "]").str();
arcana::trace_region raiseEventRegion{traceName.c_str()};
const auto it = m_eventHandlerRefs.find(eventType);
if (it != m_eventHandlerRefs.end())
{
Expand Down
1 change: 1 addition & 0 deletions Polyfills/XMLHttpRequest/Source/XMLHttpRequest.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ namespace Babylon::Polyfills::Internal
void SetReadyState(ReadyState readyState);
void RaiseEvent(const char* eventType);

std::string m_url{};
UrlLib::UrlRequest m_request{};
JsRuntimeScheduler m_runtimeScheduler;
ReadyState m_readyState{ReadyState::Unsent};
Expand Down
Loading