Skip to content

Instantly share code, notes, and snippets.

@scriby
Created July 14, 2017 19:10
Show Gist options
  • Save scriby/a9da97fc3bfbc3df44037938a036742e to your computer and use it in GitHub Desktop.
Save scriby/a9da97fc3bfbc3df44037938a036742e to your computer and use it in GitHub Desktop.
Protractor memory leak test
import * as ab from 'asyncblock';
import * as fs from 'fs';
import { by } from 'protractor';
import * as profiler from 'v8-profiler';
import { browserSync, elementSync } from '../../app/vars';
new Array(5000).join().split(',').forEach((num, i) => {
describe('1', () => {
beforeAll(() => {
ab(() => {
browserSync.get('data:,');
return browserSync.executeScript(() => {
document.body.innerHTML = '<div class="testArea"><span></span></div>';
});
});
});
it('does a thing', () => {
ab(() => {
const element = elementSync.findElement(by.css('.testArea'));
const span = element.findElement(by.css('span'));
expect(element.getTagName()).toBe('div' as any);
expect(span.getTagName()).toBe('span' as any);
});
});
it('', () => {
//console.log(process.memoryUsage());
if (i % 100 === 0) {
setImmediate(() => {
global.gc();
const snap = profiler.takeSnapshot('profile');
saveHeapSnapshot(snap, '/Users/scriby/heaps');
});
}
});
});
});
function saveHeapSnapshot(snapshot: any, datadir: any) {
let buffer = '';
const stamp = Date.now();
snapshot.serialize(
function iterator(data: any, length: any) {
buffer += data;
}, function complete() {
const name = stamp + '.heapsnapshot';
fs.writeFile(datadir + '/' + name , buffer, () => {
console.log('Heap snapshot written to ' + name);
});
}
);
}
import * as fs from 'fs';
import { browser, by } from 'protractor';
import * as profiler from 'v8-profiler';
new Array(5000).join().split(',').forEach((num, i) => {
describe('1', () => {
beforeAll(() => {
browser.get('data:,');
return browser.executeScript(() => {
document.body.innerHTML = '<div class="testArea"><span></span></div>';
});
});
it('does a thing', () => {
const element = browser.findElement(by.css('.testArea'));
const span = element.findElement(by.css('span'));
expect(element.getTagName()).toBe('div' as any);
expect(span.getTagName()).toBe('span' as any);
});
it('', () => {
//console.log(process.memoryUsage());
if (i % 100 === 0) {
setImmediate(() => {
global.gc();
const snap = profiler.takeSnapshot('profile');
saveHeapSnapshot(snap, '/Users/scriby/heaps');
});
}
});
});
});
function saveHeapSnapshot(snapshot: any, datadir: any) {
let buffer = '';
const stamp = Date.now();
snapshot.serialize(
function iterator(data: any, length: any) {
buffer += data;
}, function complete() {
const name = stamp + '.heapsnapshot';
fs.writeFile(datadir + '/' + name , buffer, () => {
console.log('Heap snapshot written to ' + name);
});
}
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment