Skip to content

Commit

Permalink
node-api: add string creation benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielschulhof committed Jun 7, 2023
1 parent bd8045b commit 50a2109
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 0 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -1394,6 +1394,7 @@ LINT_CPP_EXCLUDE += src/tracing/trace_event.h src/tracing/trace_event_common.h

LINT_CPP_FILES = $(filter-out $(LINT_CPP_EXCLUDE), $(wildcard \
benchmark/napi/*/*.cc \
benchmark/napi/*/*.c \
src/*.c \
src/*.cc \
src/*.h \
Expand Down
1 change: 1 addition & 0 deletions benchmark/napi/string/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
build/
56 changes: 56 additions & 0 deletions benchmark/napi/string/binding.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#include <assert.h>
#define NAPI_EXPERIMENTAL
#include <node_api.h>

#define NAPI_CALL(call) \
do { \
napi_status status = call; \
assert(status == napi_ok && #call " failed"); \
} while (0);

#define EXPORT_FUNC(env, exports, name, func) \
do { \
napi_value js_func; \
NAPI_CALL(napi_create_function( \
(env), (name), NAPI_AUTO_LENGTH, (func), NULL, &js_func)); \
NAPI_CALL(napi_set_named_property((env), (exports), (name), js_func)); \
} while (0);

const char* one_byte_string = "The Quick Brown Fox Jumped Over The Lazy Dog.";
const char16_t* two_byte_string =
u"The Quick Brown Fox Jumped Over The Lazy Dog.";

#define DECLARE_BINDING(CapName, lowercase_name, var_name) \
static napi_value CreateString##CapName(napi_env env, \
napi_callback_info info) { \
size_t argc = 4; \
napi_value argv[4]; \
uint32_t n; \
uint32_t index; \
napi_handle_scope scope; \
napi_value js_string; \
\
NAPI_CALL(napi_get_cb_info(env, info, &argc, argv, NULL, NULL)); \
NAPI_CALL(napi_get_value_uint32(env, argv[0], &n)); \
NAPI_CALL(napi_open_handle_scope(env, &scope)); \
NAPI_CALL(napi_call_function(env, argv[1], argv[2], 0, NULL, NULL)); \
for (index = 0; index < n; index++) { \
NAPI_CALL(napi_create_string_##lowercase_name( \
env, (var_name), NAPI_AUTO_LENGTH, &js_string)); \
} \
NAPI_CALL(napi_call_function(env, argv[1], argv[3], 1, &argv[0], NULL)); \
NAPI_CALL(napi_close_handle_scope(env, scope)); \
\
return NULL; \
}

DECLARE_BINDING(Latin1, latin1, one_byte_string)
DECLARE_BINDING(Utf8, utf8, one_byte_string)
DECLARE_BINDING(Utf16, utf16, two_byte_string)

NAPI_MODULE_INIT() {
EXPORT_FUNC(env, exports, "createStringLatin1", CreateStringLatin1);
EXPORT_FUNC(env, exports, "createStringUtf8", CreateStringUtf8);
EXPORT_FUNC(env, exports, "createStringUtf16", CreateStringUtf16);
return exports;
}
8 changes: 8 additions & 0 deletions benchmark/napi/string/binding.gyp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
'targets': [
{
'target_name': 'binding',
'sources': [ 'binding.c' ]
}
]
}
19 changes: 19 additions & 0 deletions benchmark/napi/string/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use strict';
const common = require('../../common.js');

let binding;
try {
binding = require(`./build/${common.buildType}/binding`);
} catch {
console.error(`${__filename}: Binding failed to load`);
process.exit(0);
}

const bench = common.createBenchmark(main, {
n: [1e5, 1e6, 1e7],
stringType: ['Latin1', 'Utf8', 'Utf16']
});

function main({ n, stringType }) {
binding[`createString${stringType}`](n, bench, bench.start, bench.end);
}

0 comments on commit 50a2109

Please sign in to comment.