generated from napi-rs/package-template
-
-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: async/sync get png data from Canvas
- Loading branch information
1 parent
1b2e5d1
commit f6d8cd6
Showing
15 changed files
with
789 additions
and
125 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
skia | ||
skia-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
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,102 @@ | ||
name: Benchmark | ||
|
||
env: | ||
DEBUG: 'napi:*' | ||
|
||
on: | ||
push: | ||
branches: | ||
- main | ||
pull_request: | ||
|
||
jobs: | ||
bench: | ||
name: Bench | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: actions/checkout@v2 | ||
with: | ||
submodules: true | ||
|
||
- name: Setup node | ||
uses: actions/setup-node@v2-beta | ||
with: | ||
node-version: 14 | ||
check-latest: true | ||
|
||
- name: Set env | ||
run: | | ||
echo "${PWD}/depot_tools" >> $GITHUB_PATH | ||
shell: bash | ||
|
||
- name: Install tools on Linux | ||
run: | | ||
sudo apt-get install -y ninja-build | ||
- name: Install | ||
uses: actions-rs/toolchain@v1 | ||
with: | ||
toolchain: stable | ||
profile: default | ||
override: true | ||
|
||
- name: Generate Cargo.lock | ||
uses: actions-rs/cargo@v1 | ||
with: | ||
command: generate-lockfile | ||
|
||
- name: Cache cargo registry | ||
uses: actions/cache@v1 | ||
with: | ||
path: ~/.cargo/registry | ||
key: bench-cargo-registry-trimmed-${{ hashFiles('**/Cargo.lock') }} | ||
|
||
- name: Cache cargo index | ||
uses: actions/cache@v1 | ||
with: | ||
path: ~/.cargo/git | ||
key: bench-cargo-index-trimmed-${{ hashFiles('**/Cargo.lock') }} | ||
|
||
- name: Cache NPM dependencies | ||
uses: actions/cache@v1 | ||
with: | ||
path: node_modules | ||
key: bench-${{ hashFiles('yarn.lock') }} | ||
|
||
- name: 'Install dependencies' | ||
run: yarn install --frozen-lockfile --registry https://registry.npmjs.org --network-timeout 300000 | ||
|
||
- name: Compile skia | ||
env: | ||
PYTHONHTTPSVERIFY: 0 | ||
run: node ./scripts/build-skia.js | ||
|
||
- name: 'Build' | ||
run: yarn build | ||
|
||
- name: 'Run benchmark' | ||
run: yarn bench | ||
|
||
- name: Store benchmark result | ||
uses: rhysd/github-action-benchmark@v1 | ||
if: github.ref == 'refs/heads/main' | ||
with: | ||
tool: 'benchmarkjs' | ||
output-file-path: bench.txt | ||
github-token: ${{ secrets.GH_TOKEN }} | ||
auto-push: true | ||
|
||
- name: Store benchmark result | ||
uses: rhysd/github-action-benchmark@v1 | ||
if: github.ref != 'refs/heads/main' | ||
with: | ||
tool: 'benchmarkjs' | ||
output-file-path: bench.txt | ||
github-token: ${{ secrets.GITHUB_TOKEN }} | ||
comment-always: true | ||
|
||
- name: Clear the cargo caches | ||
run: | | ||
cargo install cargo-cache --no-default-features --features ci-autoclean | ||
cargo-cache |
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 |
---|---|---|
|
@@ -119,4 +119,5 @@ dist | |
/target | ||
Cargo.lock | ||
|
||
*.node | ||
*.node | ||
bench.txt |
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 |
---|---|---|
@@ -1,28 +1,70 @@ | ||
import { promises as fs } from 'fs' | ||
import { join } from 'path' | ||
|
||
import b from 'benny' | ||
import { Summary } from 'benny/lib/internal/common-types' | ||
import { createCanvas, Canvas } from 'canvas' | ||
|
||
import { createCanvas as skiaCreateCanvas } from '../index' | ||
|
||
function draw(factory: (width: number, height: number) => Canvas) { | ||
const canvas = factory(1024, 768) | ||
|
||
const ctx = canvas.getContext('2d')! | ||
|
||
ctx.lineWidth = 10 | ||
ctx.strokeStyle = '#03a9f4' | ||
ctx.fillStyle = '#03a9f4' | ||
|
||
// Wall | ||
ctx.strokeRect(75, 140, 150, 110) | ||
|
||
// Door | ||
ctx.fillRect(130, 190, 40, 60) | ||
|
||
import { sync } from '../index' | ||
// Roof | ||
ctx.beginPath() | ||
ctx.moveTo(50, 140) | ||
ctx.lineTo(150, 60) | ||
ctx.lineTo(250, 140) | ||
ctx.closePath() | ||
ctx.stroke() | ||
|
||
function add(a: number) { | ||
return a + 100 | ||
canvas.toBuffer('image/png') | ||
} | ||
|
||
async function run() { | ||
await b.suite( | ||
'Add 100', | ||
function house() { | ||
return b.suite( | ||
'Draw house', | ||
|
||
b.add('Native a + 100', () => { | ||
sync(10) | ||
b.add('@napi-rs/skia', () => { | ||
// @ts-expect-error | ||
draw(skiaCreateCanvas) | ||
}), | ||
|
||
b.add('JavaScript a + 100', () => { | ||
add(10) | ||
b.add('node-canvas', () => { | ||
draw(createCanvas) | ||
}), | ||
|
||
b.cycle(), | ||
b.complete(), | ||
) | ||
} | ||
|
||
async function run() { | ||
const output = [await house()].map(formatSummary).join('\n') | ||
await fs.writeFile(join(process.cwd(), 'bench.txt'), output, 'utf8') | ||
} | ||
|
||
run().catch((e) => { | ||
console.error(e) | ||
}) | ||
|
||
function formatSummary(summary: Summary): string { | ||
return summary.results | ||
.map( | ||
(result) => | ||
`${summary.name}#${result.name} x ${result.ops} ops/sec ±${result.margin}% (${result.samples} runs sampled)`, | ||
) | ||
.join('\n') | ||
} |
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 |
---|---|---|
@@ -1 +1,6 @@ | ||
export function createCanvas(width: number, height: number): HTMLCanvasElement | ||
export function createCanvas( | ||
width: number, | ||
height: number, | ||
): HTMLCanvasElement & { | ||
png(): Promise<Buffer> | ||
} |
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
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
Oops, something went wrong.