|
| 1 | +/** |
| 2 | + * @license Copyright 2017 Google Inc. All Rights Reserved. |
| 3 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 |
| 4 | + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. |
| 5 | + */ |
| 6 | +'use strict'; |
| 7 | + |
| 8 | +const TraceParser = require('../../../lib/traces/trace-parser'); |
| 9 | +const fs = require('fs'); |
| 10 | +const assert = require('assert'); |
| 11 | + |
| 12 | + |
| 13 | +/* eslint-env mocha */ |
| 14 | +describe('traceParser parser', () => { |
| 15 | + it('returns preact trace data the same as JSON.parse', (done) => { |
| 16 | + const filename = '../../fixtures/traces/progressive-app-m60.json'; |
| 17 | + const readStream = fs.createReadStream(__dirname + '/' + filename, { |
| 18 | + encoding: 'utf-8', |
| 19 | + // devtools sends traces in 10mb chunks, but this trace is 12MB so we'll do a few chunks |
| 20 | + highWaterMark: 4 * 1024 * 1024 |
| 21 | + }); |
| 22 | + const parser = new TraceParser(); |
| 23 | + |
| 24 | + readStream.on('data', (chunk) => { |
| 25 | + parser.parseChunk(chunk); |
| 26 | + }); |
| 27 | + readStream.on('end', () => { |
| 28 | + const streamedTrace = parser.getTrace(); |
| 29 | + const readTrace = require(filename); |
| 30 | + |
| 31 | + assert.equal(streamedTrace.traceEvents.length, readTrace.traceEvents.length); |
| 32 | + assert.deepStrictEqual(streamedTrace.traceEvents, readTrace.traceEvents); |
| 33 | + |
| 34 | + done(); |
| 35 | + }); |
| 36 | + }); |
| 37 | + |
| 38 | + |
| 39 | + it('parses a trace > 256mb (slow)', () => { |
| 40 | + const parser = new TraceParser(); |
| 41 | + let bytesRead = 0; |
| 42 | + // FYI: this trace doesn't have a traceEvents property ;) |
| 43 | + const events = require('../../fixtures/traces/devtools-homepage-w-screenshots-trace.json'); |
| 44 | + |
| 45 | + /** |
| 46 | + * This function will synthesize a trace that's over 256 MB. To do that, we'll take an existing |
| 47 | + * trace and repeat the same events again and again until we've gone over 256 MB. |
| 48 | + * Note: We repeat all but the last event, as it's the CpuProfile event, and it triggers |
| 49 | + * specific handling in the devtools streaming parser. |
| 50 | + * Once we reach > 256 MB, we add in the CpuProfile event. |
| 51 | + */ |
| 52 | + function buildAndParse256mbTrace() { |
| 53 | + const stripOuterBrackets = str => str.replace(/^\[/, '').replace(/\]$/, ''); |
| 54 | + const partialEventsStr = events => stripOuterBrackets(JSON.stringify(events)); |
| 55 | + const traceEventsStr = partialEventsStr(events.slice(0, events.length-2)) + ','; |
| 56 | + |
| 57 | + // read the trace intro |
| 58 | + parser.parseChunk(`{"traceEvents": [${traceEventsStr}`); |
| 59 | + bytesRead += traceEventsStr.length; |
| 60 | + |
| 61 | + // just keep reading until we've gone over 256 MB |
| 62 | + // 256 MB is hard limit of a string in v8 |
| 63 | + // https://mobile.twitter.com/bmeurer/status/879276976523157505 |
| 64 | + while (bytesRead <= (Math.pow(2, 28)) - 16) { |
| 65 | + parser.parseChunk(traceEventsStr); |
| 66 | + bytesRead += traceEventsStr.length; |
| 67 | + } |
| 68 | + |
| 69 | + // the CPU Profiler event is last (and big), inject it just once |
| 70 | + const lastEventStr = partialEventsStr(events.slice(-1)); |
| 71 | + parser.parseChunk(lastEventStr + ']}'); |
| 72 | + bytesRead += lastEventStr.length; |
| 73 | + } |
| 74 | + |
| 75 | + buildAndParse256mbTrace(); |
| 76 | + const streamedTrace = parser.getTrace(); |
| 77 | + |
| 78 | + assert.ok(bytesRead > 256 * 1024 * 1024, `${bytesRead} bytes read`); |
| 79 | + assert.strictEqual(bytesRead, 270179102, `${bytesRead} bytes read`); |
| 80 | + |
| 81 | + // if > 256 MB are read we should have ~480,000 trace events |
| 82 | + assert.ok(streamedTrace.traceEvents.length > 400 * 1000, 'not >400,000 trace events'); |
| 83 | + assert.ok(streamedTrace.traceEvents.length > events.length * 5, 'not way more trace events'); |
| 84 | + assert.strictEqual(streamedTrace.traceEvents.length, 480151); |
| 85 | + |
| 86 | + assert.deepStrictEqual( |
| 87 | + streamedTrace.traceEvents[events.length - 2], |
| 88 | + events[0]); |
| 89 | + }); |
| 90 | +}); |
0 commit comments