| 1 |
import { Server, Model, Factory } from "miragejs"; |
| 2 |
|
| 3 |
describe("Internal | Integration | Performance | Simple factory test", () => { |
| 4 |
let server; |
| 5 |
|
| 6 |
beforeEach(() => { |
| 7 |
server = new Server({ |
| 8 |
environment: "test", |
| 9 |
models: { |
| 10 |
car: Model, |
| 11 |
}, |
| 12 |
factories: { |
| 13 |
car: Factory.extend({ |
| 14 |
make: "Fjord", |
| 15 |
model: "Wagon", |
| 16 |
year: "1886", |
| 17 |
}), |
| 18 |
}, |
| 19 |
}); |
| 20 |
server.timing = 0; |
| 21 |
server.logging = false; |
| 22 |
}); |
| 23 |
|
| 24 |
afterEach(() => { |
| 25 |
server.shutdown(); |
| 26 |
}); |
| 27 |
|
| 28 |
const carMaker = (count) => { |
| 29 |
server.createList("car", count); |
| 30 |
}; |
| 31 |
|
| 32 |
perfTest(50, "models", carMaker); |
| 33 |
perfTest(500, "models", carMaker); |
| 34 |
perfTest(5000, "models", carMaker); |
| 35 |
}); |
| 36 |
|
| 37 |
function perfTest(count, message, testFn, timeout = 0) { |
| 38 |
test(`(${count}) ${message}`, () => { |
| 39 |
let duration = time(() => { |
| 40 |
testFn(count); |
| 41 |
}); |
| 42 |
|
| 43 |
if (timeout) { |
| 44 |
expect(duration).toBeLessThan(timeout); |
| 45 |
} else { |
| 46 |
expect(`${duration}ms`).toBeTruthy(); |
| 47 |
} |
| 48 |
}); |
| 49 |
} |
| 50 |
|
| 51 |
function time(fn) { |
| 52 |
let start = now(); |
| 53 |
fn(); |
| 54 |
|
| 55 |
return now() - start; |
| 56 |
} |
| 57 |
|
| 58 |
function now() { |
| 59 |
return Date.now ? Date.now() : +new Date(); |
| 60 |
} |