-
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.
- Loading branch information
1 parent
8bafc86
commit e6617e8
Showing
6 changed files
with
272 additions
and
47 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
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,17 +1,15 @@ | ||
import { | ||
assert, | ||
bench, | ||
BenchmarkResult, | ||
BenchmarkTimer, | ||
runBenchmarks, | ||
} from "../../deps/std/testing.ts"; | ||
import { INDENT, Indent } from "./indent.ts"; | ||
import { getCachedIndent } from "./utils.ts"; | ||
|
||
const C = 0x10; // 16, cache size | ||
const CACHE_BENCH_ID = "CACHE_TEST"; | ||
const COMPUTED_BENCH_ID = "COMPUTED_TEST"; | ||
const BENCH_RUNS = 1e3; // the higher the number, the more accurate the benchmark results | ||
const BENCH_RUNS = 1e6; // the higher the number, the more accurate the benchmark results | ||
|
||
/** | ||
* @see https://deno.land/[email protected]/testing#benching | ||
|
@@ -22,12 +20,12 @@ bench({ | |
func: (timer: BenchmarkTimer): void => { | ||
const store: string[] = []; | ||
timer.start(); | ||
for (let i = 1; i <= C; i++) store.push(getCachedIndent(Indent.Tab1, i)); | ||
for (let i = 1; i <= C; i++) store.push(getCachedIndent(Indent.Tab2, i)); | ||
for (let i = 1; i <= C; i++) store.push(getCachedIndent(Indent.Space1, i)); | ||
for (let i = 1; i <= C; i++) store.push(getCachedIndent(Indent.Space2, i)); | ||
for (let i = 1; i <= C; i++) store.push(getCachedIndent(Indent.Space3, i)); | ||
for (let i = 1; i <= C; i++) store.push(getCachedIndent(Indent.Space4, i)); | ||
for (let i = 1; i <= 16; i++) store.push(getCachedIndent(Indent.Tab1, i)); | ||
for (let i = 1; i <= 16; i++) store.push(getCachedIndent(Indent.Tab2, i)); | ||
for (let i = 1; i <= 16; i++) store.push(getCachedIndent(Indent.Space1, i)); | ||
for (let i = 1; i <= 16; i++) store.push(getCachedIndent(Indent.Space2, i)); | ||
for (let i = 1; i <= 16; i++) store.push(getCachedIndent(Indent.Space3, i)); | ||
for (let i = 1; i <= 16; i++) store.push(getCachedIndent(Indent.Space4, i)); | ||
timer.stop(); | ||
}, | ||
}); | ||
|
@@ -38,30 +36,23 @@ bench({ | |
func: (timer: BenchmarkTimer): void => { | ||
const store: string[] = []; | ||
timer.start(); | ||
for (let i = 1; i <= C; i++) store.push(INDENT[Indent.Tab1].repeat(i)); | ||
for (let i = 1; i <= C; i++) store.push(INDENT[Indent.Tab2].repeat(i)); | ||
for (let i = 1; i <= C; i++) store.push(INDENT[Indent.Space1].repeat(i)); | ||
for (let i = 1; i <= C; i++) store.push(INDENT[Indent.Space2].repeat(i)); | ||
for (let i = 1; i <= C; i++) store.push(INDENT[Indent.Space3].repeat(i)); | ||
for (let i = 1; i <= C; i++) store.push(INDENT[Indent.Space4].repeat(i)); | ||
for (let i = 1; i <= 16; i++) store.push(INDENT[Indent.Tab1].repeat(i)); | ||
for (let i = 1; i <= 16; i++) store.push(INDENT[Indent.Tab2].repeat(i)); | ||
for (let i = 1; i <= 16; i++) store.push(INDENT[Indent.Space1].repeat(i)); | ||
for (let i = 1; i <= 16; i++) store.push(INDENT[Indent.Space2].repeat(i)); | ||
for (let i = 1; i <= 16; i++) store.push(INDENT[Indent.Space3].repeat(i)); | ||
for (let i = 1; i <= 16; i++) store.push(INDENT[Indent.Space4].repeat(i)); | ||
timer.stop(); | ||
}, | ||
}); | ||
|
||
Deno.test("benchmarking the cache algorithm vs the `repeat` method", async () => { | ||
const { results } = await runBenchmarks({ silent: true }); | ||
const cacheResults = results.find(({ name }: BenchmarkResult) => | ||
name === CACHE_BENCH_ID | ||
) as BenchmarkResult; | ||
const computedResults = results.find(({ name }: BenchmarkResult) => | ||
name === COMPUTED_BENCH_ID | ||
) as BenchmarkResult; | ||
const speedBoost = computedResults.measuredRunsAvgMs / | ||
cacheResults.measuredRunsAvgMs; | ||
const speedBoostPercentage = (speedBoost - 1) * 100; | ||
const { results: [cache, computed] } = await runBenchmarks({ silent: true }); | ||
const speedBoostPercentage = 100 * computed.measuredRunsAvgMs / | ||
cache.measuredRunsAvgMs; | ||
const finalMessage = `the cache algorithm is ${ | ||
speedBoostPercentage.toFixed(2) | ||
}% the speed of the \`repeat\` algorithm`; | ||
console.log(finalMessage); | ||
assert(speedBoost > 1); | ||
assert(speedBoostPercentage > 100); | ||
}); |
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.
e6617e8
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Failed to deploy: