Skip to content

Commit

Permalink
Revert "Benchmarks and Improvements (#77)"
Browse files Browse the repository at this point in the history
This reverts commit 853723b.
  • Loading branch information
Eyal-Shalev authored May 16, 2022
1 parent 853723b commit 01599da
Show file tree
Hide file tree
Showing 15 changed files with 176 additions and 333 deletions.
47 changes: 6 additions & 41 deletions .github/workflows/test-and-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ on:
tags:
- v*

env:
env:
BUNDLE_NAME: async_channels

jobs:
Expand Down Expand Up @@ -40,34 +40,6 @@ jobs:
fail_ci_if_error: true
verbose: true

benchmark:
runs-on: ubuntu-latest

steps:
- name: Git Checkout
uses: actions/checkout@v2

- name: Setup Deno
uses: denoland/setup-deno@v1
with:
deno-version: v1.x

- name: install colorized-logs
run: sudo apt install colorized-logs

- name: Run Benchmarks
run: |
make benchmark-txt
make benchmark-html
- uses: actions/upload-artifact@v2
with:
name: benchmarks
path: |
benchmark.txt
benchmark.html
build:
runs-on: ubuntu-latest

Expand All @@ -90,7 +62,7 @@ jobs:

- name: Build
run: make all

- name: Post Build Test
run: make post-build-test

Expand All @@ -102,7 +74,7 @@ jobs:
version:
runs-on: ubuntu-latest
if: startsWith(github.ref, 'refs/tags/v')
steps:
steps:
- name: Get version
id: get_version
run: echo ::set-output name=version::${GITHUB_REF/refs\/tags\/v/}
Expand All @@ -116,7 +88,7 @@ jobs:
package-json:
runs-on: ubuntu-latest
needs: [version]
steps:
steps:
- name: Git Checkout
uses: actions/checkout@v2

Expand Down Expand Up @@ -212,7 +184,7 @@ jobs:
release:
name: Release
runs-on: ubuntu-latest
needs: [test, benchmark, build, version]
needs: [test, build, version]

steps:
- name: Git Checkout
Expand All @@ -223,11 +195,6 @@ jobs:
name: dist
path: dist

- uses: actions/download-artifact@v2
with:
name: benchmarks
path: benchmarks

- uses: marvinpinto/action-automatic-releases@latest
if: ${{ needs.version.outputs.is_pre }}
with:
Expand All @@ -240,7 +207,6 @@ jobs:
README.md
dist/*.js
dist/*.js
benchmarks/*
- uses: marvinpinto/action-automatic-releases@latest
if: ${{ ! needs.version.outputs.is_pre }}
Expand All @@ -252,5 +218,4 @@ jobs:
LICENSE
README.md
dist/*.js
dist/*.js
benchmarks/*
dist/*.js
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,3 @@
/coverage
/node_modules
/package-lock.json
/benchmark.txt
/benchmark.html
25 changes: 8 additions & 17 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,8 @@ coverage-html: coverage check-genhtml
coverage-serve: coverage-html check-deno
deno run --allow-net="0.0.0.0:4507" --allow-read="." https://deno.land/std/http/file_server.ts coverage/html

benchmark-write: check-deno
deno bench --unstable --no-check --quiet --import-map scripts/import_map.json > benchmark.out

benchmark: benchmark-write
cat benchmark.out

benchmark-txt: benchmark-write
cat benchmark.out | ansi2txt > benchmark.txt

benchmark-html: benchmark-write
cat benchmark.out | ansi2html > benchmark.html
benchmark: check-deno
@for f in $(shell ls src/*_bench.ts); do deno run --lock=scripts/bench-lock.json --import-map scripts/import_map.json $${f}; done

package-json: check-deno
deno run --lock scripts/package-json-lock.json --import-map scripts/import_map.json --allow-env="SCOPE,VERSION" --allow-write="./package.json" scripts/package-json.ts
Expand All @@ -73,21 +64,21 @@ build: build-esm build-cjs build-iife

build-esm: check-esbuild
esbuild --banner:js="$$LICENSE_BANNER" --bundle --outfile=dist/async_channels.esm.js --format="esm" src/mod.ts

build-cjs: check-esbuild
esbuild --banner:js="$$LICENSE_BANNER" --bundle --outfile=dist/async_channels.cjs.js --format=cjs src/mod.ts

build-iife: check-esbuild
esbuild --banner:js="$$LICENSE_BANNER" --bundle --outfile=dist/async_channels.iife.js --format=iife --global-name=async_channels src/mod.ts

build-min: build-esm-min build-cjs-min build-iife-min

build-esm-min: check-esbuild
esbuild --banner:js="$$LICENSE_BANNER" --bundle --minify --outfile=dist/async_channels.esm.min.js --format="esm" src/mod.ts

build-cjs-min: check-esbuild
esbuild --banner:js="$$LICENSE_BANNER" --bundle --minify --outfile=dist/async_channels.cjs.min.js --format=cjs src/mod.ts

build-iife-min: check-esbuild
esbuild --banner:js="$$LICENSE_BANNER" --bundle --minify --outfile=dist/async_channels.iife.min.js --format=iife --global-name=async_channels src/mod.ts

Expand All @@ -104,7 +95,7 @@ install-esbuild: check-npm
npm i -g esbuild

clean:
rm -rf dist coverage package.json benchmark.txt benchmark.html
rm -rf dist coverage package.json

clear:
clearscr:
clear
5 changes: 5 additions & 0 deletions scripts/bench-lock.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"https://deno.land/[email protected]/_util/assert.ts": "2f868145a042a11d5ad0a3c748dcf580add8a0dbc0e876eaa0026303a5488f58",
"https://deno.land/[email protected]/_util/deep_assign.ts": "2f394c1835c5a3a86a54b929efcd7645db77ace8bf60d7bd19311f01110f1269",
"https://deno.land/[email protected]/testing/bench.ts": "bd1f9aae8fc3569e01fcb09aa9470c6517d85408759c1336699ddb851e721cd1"
}
2 changes: 1 addition & 1 deletion scripts/import_map.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"async_channels": "../src/mod.ts",
"async_channels/": "../src/",
"esbuild": "https://deno.land/x/[email protected]/mod.js",
"deno/": "https://deno.land/std@0.139.0/",
"deno/": "https://deno.land/std@0.108.0/",
"semver": "https://deno.land/x/[email protected]/mod.ts"
}
}
15 changes: 7 additions & 8 deletions scripts/test-lock.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
{
"https://deno.land/[email protected]/_util/assert.ts": "e94f2eb37cebd7f199952e242c77654e43333c1ac4c5c700e929ea3aa5489f74",
"https://deno.land/[email protected]/_util/deep_assign.ts": "52d4ed44314c5c22e9346264d1ef6c204debf3289be9f5c3c8cf3e8668595113",
"https://deno.land/[email protected]/fmt/colors.ts": "30455035d6d728394781c10755351742dd731e3db6771b1843f9b9e490104d37",
"https://deno.land/[email protected]/testing/_diff.ts": "029a00560b0d534bc0046f1bce4bd36b3b41ada3f2a3178c85686eb2ff5f1413",
"https://deno.land/[email protected]/testing/_format.ts": "0d8dc79eab15b67cdc532826213bbe05bccfd276ca473a50a3fc7bbfb7260642",
"https://deno.land/[email protected]/testing/asserts.ts": "dc7ab67b635063989b4aec8620dbcc6fa7c2465f2d9c856bddf8c0e7b45b4481",
"https://deno.land/[email protected]/testing/bench.ts": "636b601ed9a56277ea7eba86a71065d233db11a650706d07e80276d59d696f8c"
}
"https://deno.land/[email protected]/_util/assert.ts": "2f868145a042a11d5ad0a3c748dcf580add8a0dbc0e876eaa0026303a5488f58",
"https://deno.land/[email protected]/_util/deep_assign.ts": "2f394c1835c5a3a86a54b929efcd7645db77ace8bf60d7bd19311f01110f1269",
"https://deno.land/[email protected]/fmt/colors.ts": "8368ddf2d48dfe413ffd04cdbb7ae6a1009cf0dccc9c7ff1d76259d9c61a0621",
"https://deno.land/[email protected]/testing/_diff.ts": "ccd6c3af6e44c74bf1591acb1361995f5f50df64323a6e7fb3f16c8ea792c940",
"https://deno.land/[email protected]/testing/asserts.ts": "6b0d6ba564bdff807bd0f0e93e02c48aa3177acf19416bf84a7f420191ef74cd",
"https://deno.land/[email protected]/testing/bench.ts": "bd1f9aae8fc3569e01fcb09aa9470c6517d85408759c1336699ddb851e721cd1"
}
41 changes: 4 additions & 37 deletions src/channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,6 @@ export interface ChannelOptions {
* When `debug` is `true`, this struct will be added to the debug messages.
*/
debugExtra?: Record<string, unknown>;

/**
* Provide a custom logger - defaults to `console`
*/
logger?: Console;
}

export type Closer = Pick<Channel<unknown>, "close">;
Expand All @@ -76,11 +71,9 @@ export interface ClosedReceiver extends Receiver<unknown> {
/**
* @template T The type of value that can be sent to or received by this channel.
*/
export class Channel<T>
implements AsyncIterable<T>, AsyncIterator<T, void, void> {
export class Channel<T> implements AsyncIterable<T> {
#queue: Queue<T>;
#state: State<T>;
#logger: Console;

/**
* Constructs a new Channel with an optional buffer.
Expand All @@ -90,15 +83,14 @@ export class Channel<T>
* @param {ChannelOptions} [options]
*/
constructor(
readonly bufferSize: number = 0,
readonly bufferSize = 0,
protected readonly options?: ChannelOptions,
) {
if (!isNonNegativeSafeInteger(bufferSize)) {
throw new RangeError("bufferSize must be a safe non-negative integer.");
}
this.#state = Idle(this.debug.bind(this));
this.#queue = new Queue<T>(bufferSize);
this.#logger = options?.logger ?? console;
}

/**
Expand Down Expand Up @@ -266,31 +258,6 @@ export class Channel<T>
}
}

/**
* Blocks until a value is available on the channel, or returns immedietly if the channel is closed.
*/
public async next(): Promise<IteratorResult<T, void>> {
const [value, ok] = await this.get();
if (!ok) return { done: true, value: void 0 };
return { value, done: false };
}

/**
* Closes the channel, and returns an empty result.
*/
public return() {
this.close();
return this.next();
}

/**
* Logs the error, closes the channel, and returns an empty result.
*/
public throw(e?: unknown) {
this.error(e);
return this.return();
}

/**
* Applies `fn` on `this` and returns the result.
*
Expand Down Expand Up @@ -466,7 +433,7 @@ export class Channel<T>
* @internal
*/
error(...args: unknown[]): void {
this.#logger.error(...args, {
console.error(...args, {
[Symbol.for("time")]: new Date(),
[Symbol.for("state")]: this.#state.name,
...this.options?.debugExtra,
Expand All @@ -478,7 +445,7 @@ export class Channel<T>
*/
debug(...args: unknown[]): void {
if (this.options?.debug) {
this.#logger.debug(...args, {
console.debug(...args, {
[Symbol.for("time")]: new Date(),
[Symbol.for("state")]: this.#state.name,
...this.options?.debugExtra,
Expand Down
66 changes: 16 additions & 50 deletions src/channel_bench.ts
Original file line number Diff line number Diff line change
@@ -1,55 +1,21 @@
import { bench, runBenchmarks } from "deno/testing/bench.ts";
import { Channel } from "./channel.ts";

Deno.bench("Channel.get", async () => {
const c = new Channel<void>();
queueMicrotask(() => c.send());
await c.get();
c.close();
});
bench({
runs: 100,
name: `Timer`,
func: async (b) => {
b.start();

Deno.bench("Channel.send", async () => {
const c = new Channel<void>();
queueMicrotask(() => c.get());
await c.send();
c.close();
});
const c = new Channel();
setTimeout(() => {
c.send(1);
});
await c.get();
c.close();

[
10_000,
100_000,
1_000_000,
].forEach((n) => {
const data = Array(n);
Deno.bench({
group: `Consume Array(${n})`,
name: `Consume Array(${n})`,
fn: async () => {
const c = Channel.from(data, { bufferSize: data.length });
await c.forEach(() => {}).get();
},
});
Deno.bench({
group: `Consume Array(${n})`,
name: `Consume Array(${n}) with Buffer(${n} / 2)`,
fn: async () => {
const c = Channel.from(data, { bufferSize: data.length / 2 });
await c.forEach(() => {}).get();
},
});
Deno.bench({
group: `Consume Array(${n})`,
name: `Consume Array(${n}) with Buffer(${n} / 4)`,
fn: async () => {
const c = Channel.from(data, { bufferSize: data.length / 2 });
await c.forEach(() => {}).get();
},
});
Deno.bench({
group: `Consume Array(${n})`,
name: `Consume Array(${n}) with Buffer(${n})`,
fn: async () => {
const c = Channel.from(data, { bufferSize: data.length });
await c.forEach(() => {}).get();
},
});
b.stop();
},
});

await runBenchmarks();
Loading

0 comments on commit 01599da

Please sign in to comment.