chore: add thread safe function bench suite

This commit is contained in:
LongYinan 2020-12-28 14:02:28 +08:00
parent a17999024b
commit ff5c103020
No known key found for this signature in database
GPG key ID: A3FFE134A3E20881
3 changed files with 44 additions and 6 deletions

View file

@ -8,7 +8,7 @@ version = "0.1.0"
crate-type = ["cdylib"]
[dependencies]
napi = {path = "../napi"}
napi = {path = "../napi", features = ["napi4"]}
napi-derive = {path = "../napi-derive"}
[target.'cfg(all(unix, not(target_env = "musl"), not(target_arch = "aarch64")))'.dependencies]

View file

@ -1,15 +1,26 @@
import b from 'benny'
const { benchAsyncTask } = require('./index.node')
const { benchAsyncTask, benchThreadsafeFunction } = require('./index.node')
const buffer = Buffer.from('hello 🚀 rust!')
export const benchAsync = () =>
b.suite(
'Async task',
b.add('napi-rs', async () => {
b.add('spawn task', async () => {
await benchAsyncTask(buffer)
}),
b.add('thread safe function', async () => {
await new Promise<number | undefined>((resolve, reject) => {
benchThreadsafeFunction(buffer, (err?: Error, value?: number) => {
if (err) {
reject(err)
} else {
resolve(value)
}
})
})
}),
b.cycle(),
b.complete(),
)

View file

@ -1,5 +1,5 @@
use napi::{CallContext, Env, JsBuffer, JsBufferValue, JsNumber, JsObject, Ref, Result, Task};
use napi::threadsafe_function::*;
use napi::*;
struct BufferLength(Ref<JsBufferValue>);
@ -8,7 +8,7 @@ impl Task for BufferLength {
type JsValue = JsNumber;
fn compute(&mut self) -> Result<Self::Output> {
Ok((&self.0).len())
Ok(self.0.len() + 1)
}
fn resolve(self, env: Env, output: Self::Output) -> Result<Self::JsValue> {
@ -25,7 +25,34 @@ fn bench_async_task(ctx: CallContext) -> Result<JsObject> {
Ok(async_promise.promise_object())
}
#[js_function(2)]
fn bench_threadsafe_function(ctx: CallContext) -> Result<JsUndefined> {
let buffer_ref = ctx.get::<JsBuffer>(0)?.into_ref()?;
let callback = ctx.get::<JsFunction>(1)?;
let tsfn = ctx.env.create_threadsafe_function(
&callback,
0,
|ctx: ThreadSafeCallContext<(usize, Ref<JsBufferValue>)>| {
ctx
.env
.create_uint32(ctx.value.0 as u32)
.and_then(|v| ctx.value.1.unref(ctx.env).map(|_| vec![v]))
},
)?;
std::thread::spawn(move || {
tsfn.call(
Ok((buffer_ref.len() + 1, buffer_ref)),
ThreadsafeFunctionCallMode::NonBlocking,
);
});
ctx.env.get_undefined()
}
pub fn register_js(exports: &mut JsObject) -> Result<()> {
exports.create_named_method("benchAsyncTask", bench_async_task)?;
exports.create_named_method("benchThreadsafeFunction", bench_threadsafe_function)?;
Ok(())
}