forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
node-api: add string creation benchmark
- Loading branch information
1 parent
bd8045b
commit 50a2109
Showing
5 changed files
with
85 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
build/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
'targets': [ | ||
{ | ||
'target_name': 'binding', | ||
'sources': [ 'binding.c' ] | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |