Skip to content

Commit

Permalink
run some tests only in production mode (test extensively)
Browse files Browse the repository at this point in the history
  • Loading branch information
dmonad committed Mar 27, 2019
1 parent eb8808f commit 5d5b7ad
Show file tree
Hide file tree
Showing 10 changed files with 83 additions and 22 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

### Performance resources

* https://youtu.be/IFWulQnM5E0 Good intro video
* https://github.com/thlorenz/v8-perf
* https://github.com/thlorenz/deoptigate - A great tool for investigating deoptimizations
* https://github.com/vhf/v8-bailout-reasons - Description of some deopt messages
10 changes: 8 additions & 2 deletions encoding.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
/**
* @module encoding
* Encodes numbers in little-endian order (least to most significant byte order)
* This should be compatible with Golang's binary encoding (https://golang.org/pkg/encoding/binary/)
* which is also used in Protocol Buffers.
*/
import * as buffer from './buffer.js'

Expand All @@ -12,7 +15,10 @@ const bits8 = 0b11111111
export class Encoder {
constructor () {
this.cpos = 0
this.cbuf = buffer.createUint8ArrayFromLen(100)
this.cbuf = new Uint8Array(100)
/**
* @type {Array<Uint8Array>}
*/
this.bufs = []
}
}
Expand Down Expand Up @@ -67,7 +73,7 @@ export const toBuffer = encoder => {
export const write = (encoder, num) => {
if (encoder.cpos === encoder.cbuf.length) {
encoder.bufs.push(encoder.cbuf)
encoder.cbuf = buffer.createUint8ArrayFromLen(encoder.cbuf.length * 2)
encoder.cbuf = new Uint8Array(encoder.cbuf.length * 2)
encoder.cpos = 0
}
encoder.cbuf[encoder.cpos++] = num
Expand Down
2 changes: 2 additions & 0 deletions environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,5 @@ export const hasParam = name => computeParams().has(name)
/* istanbul ignore next */
export const getParam = (name, defaultVal = null) => computeParams().get(name) || defaultVal
// export const getArgs = name => computeParams() && args

export const production = getParam('production', false)
15 changes: 7 additions & 8 deletions indexeddb.test.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
import * as t from './testing.js'
import * as idb from './indexeddb.js'
import * as environment from './environment.js'

if (environment.isNode) {
// @ts-ignore
global.indexedDB = require('fake-indexeddb')
// @ts-ignore
global.IDBKeyRange = require('fake-indexeddb/lib/FDBKeyRange')
}
import { isBrowser } from './environment.js'

/* istanbul ignore next */
const initTestDB = db => idb.createStores(db, [['test', { autoIncrement: true }]])
const testDBName = 'idb-test'

/* istanbul ignore next */
const createTransaction = db => db.transaction(['test'], 'readwrite')

/* istanbul ignore next */
/**
* @param {IDBTransaction} t
* @return {IDBObjectStore}
*/
const getStore = t => idb.getStore(t, 'test')

/* istanbul ignore next */
export const testRetrieveElements = async () => {
t.skip(!isBrowser)
t.describe('create, then iterate some keys')
await idb.deleteDB(testDBName)
const db = await idb.openDB(testDBName, initTestDB)
Expand Down
2 changes: 1 addition & 1 deletion map.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export const create = () => new Map()
* @param {function():T} createT
* @return {T}
*/
export const setTfUndefined = (map, key, createT) => {
export const setIfUndefined = (map, key, createT) => {
let set = map.get(key)
if (set === undefined) {
map.set(key, set = createT())
Expand Down
6 changes: 3 additions & 3 deletions prng.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ export const uint32 = (gen, min, max) => int32(gen, min, max) >>> 0
* @return {Number} A random integer on [min, max]
*/
export const int31 = (gen, min, max) => {
const _min = min & binary.BITS32
const _max = max & binary.BITS32
return math.floor((gen.next() * (_max + 1 - _min)) + _min)
const _min = min & binary.BITS31
const _max = max & binary.BITS31
return math.floor(gen.next() * ((_max - _min + 1) & binary.BITS31) + _min)
}

/**
Expand Down
12 changes: 9 additions & 3 deletions prng.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import * as t from './testing.js'
import { Xorshift32 } from './prng/Xorshift32.js'
import { Mt19937 } from './prng/Mt19937.js'
import * as dom from './dom.js'
import { isBrowser } from './environment.js'
import { isBrowser, production } from './environment.js'

const genTestData = 5000

Expand Down Expand Up @@ -173,8 +173,14 @@ const runGenTest = (tc, gen) => {
* @param {t.TestCase} tc
*/
export const testGeneratorXoroshiro128plus = tc => runGenTest(tc, new Xoroshiro128plus(tc.seed))
export const testGeneratorXorshift32 = tc => runGenTest(tc, new Xorshift32(tc.seed))
export const testGeneratorMt19937 = tc => runGenTest(tc, new Mt19937(tc.seed))
export const testGeneratorXorshift32 = tc => {
t.skip(!production)
runGenTest(tc, new Xorshift32(tc.seed))
}
export const testGeneratorMt19937 = tc => {
t.skip(!production)
runGenTest(tc, new Mt19937(tc.seed))
}

/* istanbul ignore next */
/**
Expand Down
9 changes: 8 additions & 1 deletion rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import fs from 'fs'

const files = fs.readdirSync('./').filter(file => /(?<!\.(test|config))\.js$/.test(file))
const files = fs.readdirSync('./').filter(file => /(?<!(test|config))\.js$/.test(file))

export default [{
input: './test.js',
output: {
dir: './dist',
format: 'iife',
sourcemap: true
}
}, {
input: files,
output: {
dir: './dist',
Expand Down
40 changes: 36 additions & 4 deletions sort.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,19 @@ const createSortTest = (tc, createArray, compare, getVal) => {
runSortTest(tc, createArray(10000), compare, getVal)
t.describe('sort 100k elements')
runSortTest(tc, createArray(100000), compare, getVal)
t.describe('sort 1M elements')
runSortTest(tc, createArray(1000000), compare, getVal)
t.describe('sort 10M elements')
runSortTest(tc, createArray(10000000), compare, getVal)
if (t.production) {
t.describe('sort 1M elements')
runSortTest(tc, createArray(1000000), compare, getVal)
t.describe('sort 10M elements')
runSortTest(tc, createArray(10000000), compare, getVal)
}
}

/**
* @param {t.TestCase} tc
*/
export const testSortUint16 = tc => {
t.skip(!t.production)
const getVal = i => i
const compare = (a, b) => a - b
const createArray = len => Array.from(new Uint16Array(prng.arrayBuffer(tc.prng, len * 2)))
Expand All @@ -74,6 +77,7 @@ export const testSortUint16 = tc => {
* @param {t.TestCase} tc
*/
export const testSortUint32 = tc => {
t.skip(!t.production)
const getVal = i => i
const compare = (a, b) => a - b
const createArray = len => Array.from(new Uint32Array(prng.arrayBuffer(tc.prng, len * 4)))
Expand All @@ -89,3 +93,31 @@ export const testSortObjectUint32 = tc => {
const createArray = len => Array.from(new Uint32Array(prng.arrayBuffer(tc.prng, len * 4))).map(index => ({ index }))
createSortTest(tc, createArray, compare, getVal)
}

export const testListVsArrayPerformance = tc => {
/**
* @typedef {{ val: number }} Val
* @typedef {{ val: Val, next: item }|null} item
*/
const len = 100000
t.measureTime('array creation', () => {
/**
* @type {Array<Val>}
*/
const array = new Array(len)
for (let i = 0; i < len; i++) {
array[i] = { val: i }
}
})
t.measureTime('list creation', () => {
/**
* @type {item}
*/
const listStart = { val: { val: 0 }, next: null }
for (let i = 1, n = listStart; i < len; i++) {
const next = { val: { val: i }, next: null }
n.next = next
n = next
}
})
}
2 changes: 2 additions & 0 deletions testing.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import * as array from './array.js'
import * as env from './environment.js'
import * as json from './json.js'

export { production } from './environment.js'

/* istanbul ignore next */
export const envSeed = env.hasParam('--seed') ? Number.parseInt(env.getParam('--seed')) : null

Expand Down

0 comments on commit 5d5b7ad

Please sign in to comment.