简体   繁体   English

如何使用 javascript 中的异步函数衡量性能

[英]How to measure performance with async functions in javascript

I've created a small app that compares the speeds of WebAssembly and Javascript.我创建了一个小应用程序来比较 WebAssembly 和 Javascript 的速度。 To my surprise, JavaScript is way faster at calculating factorials of a large number.令我惊讶的是,JavaScript 在计算大数的阶乘时要快得多。 Or at least it seems like it is.或者至少看起来是这样。 I am pretty sure this is not right and is caused by the await syntax I'm using.我很确定这是不对的,是由我使用的 await 语法引起的。 (The functions are the same.) Also, what are some really time heavy tasks I can use to compare those two? (功能是相同的。)另外,我可以使用哪些非常耗时的任务来比较这两者? This doesn't really seem that time consuming, since it takes less than a 0.1 millisecond.这看起来并不那么耗时,因为它只需要不到 0.1 毫秒。

AssemblyScript (this gets compiled into wasm) AssemblyScript(这被编译成 wasm)

// The entry file of your WebAssembly module.
export function fib(num: i32): i32 {
  var a: i32 = 1,
    b: i32 = 0,
    temp: i32;

  while (num > 0) {
    temp = a;
    a = a + b;
    b = temp;
    num--;
  }

  return b;
}

App.js应用程序.js

import waApi from "./api";
...
  <button
       onClick={async () => {
       const t0 = performance.now();
       (await waApi).fib(200);
       const t1 = performance.now();
       this.updateGraph(t1 - t0, "wa");
       const t2 = performance.now();
       this.fib(200);
       const t3 = performance.now();
       this.updateGraph(t3 - t2, "js");
       }}>
Calculate with both 
  </button>

api.js api.js

import { instantiateStreaming } from "assemblyscript/lib/loader";

export default instantiateStreaming(fetch("./assembly.wasm"));

The problem with micro-benchmarks (ie benchmarks that analyse the performance of small, very fast function) is that you almost always end up measuring the wrong thing.微基准测试(即分析小而非常快的函数的性能的基准测试)的问题在于,您几乎总是最终会测量错误的东西。 Your measurements are going to be heavily skewed by:您的测量将因以下因素严重倾斜:

  • The overhead involved in invoking WebAssembly functions from JavaScript从 JavaScript 调用 WebAssembly 函数所涉及的开销
  • The ability of the AssemblyScript compiler (which is very new) to optimise your function AssemblyScript 编译器(这是非常新的)优化函数的能力
  • The accuracy of your timer计时器的准确性
  • The time for your JavaScript engine to reach peak optimisation您的 JavaScript 引擎达到优化峰值的时间

Just to name a few!仅举几个!

A more realistic benchmark would perform calculations that execute a far greater range of WebAssembly operations, be more 'realistic' in nature (ie measure the types of load you really would offload to WebAssembly), and take longer to execute.更现实的基准将执行计算,以执行更大范围的 WebAssembly 操作,本质上更“现实”(即测量您真正将卸载到 WebAssembly 的负载类型),并且执行时间更长。

Here's a much better benchmark based on a GameBoy emulator:这是一个基于 GameBoy 模拟器的更好的基准测试:

https://medium.com/@torch2424/webassembly-is-fast-a-real-world-benchmark-of-webassembly-vs-es6-d85a23f8e193 https://medium.com/@torch2424/webassembly-is-fast-a-real-world-benchmark-of-webassembly-vs-es6-d85a23f8e193

It should be它应该是

import waApi from "./api";

class Test extends React.Component { 
  async componentDidMount() {
    this.wasmModule = await waApi;
  }

  render() {
    return (
      <button
        onClick={() => {
          const module = this.wasmModule;
          if (!module) {
            console.warn("module not yet loaded. Try later");
            return;
          }
          const t0 = performance.now();
          module.fib(200);
          const t1 = performance.now();
          this.updateGraph(t1 - t0, "wa");
          const t2 = performance.now();
          this.fib(200);
          const t3 = performance.now();
          this.updateGraph(t3 - t2, "js");
        }}>
    );
  }
}

Because in your example for wasm part you also measured loading and instantiation of module.因为在您的 wasm 部分示例中,您还测量了模块的加载和实例化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM