|
| 1 | +/* |
| 2 | + * Copyright (C) 2025 Apple Inc. All rights reserved. |
| 3 | + * |
| 4 | + * Redistribution and use in source and binary forms, with or without |
| 5 | + * modification, are permitted provided that the following conditions |
| 6 | + * are met: |
| 7 | + * 1. Redistributions of source code must retain the above copyright |
| 8 | + * notice, this list of conditions and the following disclaimer. |
| 9 | + * 2. Redistributions in binary form must reproduce the above copyright |
| 10 | + * notice, this list of conditions and the following disclaimer in the |
| 11 | + * documentation and/or other materials provided with the distribution. |
| 12 | + * |
| 13 | + * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY |
| 14 | + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 15 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 | + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
| 17 | + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 18 | + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 19 | + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 20 | + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 21 | + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 22 | + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 23 | + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | + */ |
| 25 | + |
| 26 | +const EXPECTED_ASSERTION_COUNT = 1213680; |
| 27 | + |
| 28 | +class Benchmark extends StartupBenchmark { |
| 29 | + lastResult; |
| 30 | + totalHash = 0xdeadbeef; |
| 31 | + samples = []; |
| 32 | + |
| 33 | + constructor({iterationCount}) { |
| 34 | + super({ |
| 35 | + iterationCount, |
| 36 | + expectedCacheCommentCount: 71, |
| 37 | + sourceCodeReuseCount: 1, |
| 38 | + }); |
| 39 | + } |
| 40 | + |
| 41 | + async init() { |
| 42 | + await Promise.all([ |
| 43 | + super.init(), |
| 44 | + this.loadData("cpp", JetStream.preload.SAMPLE_CPP, -1086372285), |
| 45 | + this.loadData("css", JetStream.preload.SAMPLE_CSS, 1173668337), |
| 46 | + this.loadData("markup", JetStream.preload.SAMPLE_HTML, -270772291), |
| 47 | + this.loadData("js", JetStream.preload.SAMPLE_JS, -838545229), |
| 48 | + this.loadData("markdown", JetStream.preload.SAMPLE_MD, 5859883), |
| 49 | + this.loadData("sql", JetStream.preload.SAMPLE_SQL, 5859941), |
| 50 | + this.loadData("json", JetStream.preload.SAMPLE_JSON, 5859883), |
| 51 | + this.loadData("typescript", JetStream.preload.SAMPLE_TS, 133251625), |
| 52 | + ]); |
| 53 | + } |
| 54 | + |
| 55 | + async loadData(lang, file, hash) { |
| 56 | + const sample = { lang, hash }; |
| 57 | + // Push eagerly to have deterministic order. |
| 58 | + this.samples.push(sample); |
| 59 | + sample.content = await JetStream.getString(file); |
| 60 | + // Warm up quickHash and force good string representation. |
| 61 | + this.quickHash(sample.content); |
| 62 | + console.assert(sample.content.length > 0); |
| 63 | + } |
| 64 | + |
| 65 | + runIteration(iteration) { |
| 66 | + // Module is loaded into PrismJSBenchmark |
| 67 | + let PrismJSBenchmark; |
| 68 | + eval(this.iterationSourceCodes[iteration]); |
| 69 | + this.lastResult = PrismJSBenchmark.runTest(this.samples); |
| 70 | + |
| 71 | + for (const result of this.lastResult) { |
| 72 | + result.hash = this.quickHash(result.html); |
| 73 | + this.totalHash ^= result.hash; |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + validate() { |
| 78 | + console.assert(this.lastResult.length == this.samples.length); |
| 79 | + for (let i = 0; i < this.samples.length; i++) { |
| 80 | + const sample = this.samples[i]; |
| 81 | + const result = this.lastResult[i]; |
| 82 | + console.assert(result.html.length > 0); |
| 83 | + console.assert( |
| 84 | + result.hash == sample.hash, |
| 85 | + `Invalid result.hash = ${result.hash}, expected ${sample.hash}` |
| 86 | + ); |
| 87 | + } |
| 88 | + } |
| 89 | +} |
0 commit comments