Generate attachments from local files

This ensures our benchmarking is closer to real-world usage, e.g. images, video,
etc. that are not compressible.
This commit is contained in:
Daniel Gasienica 2018-04-02 17:57:00 -04:00
parent 4a664bdab8
commit b07c66eaa7

View file

@ -1,3 +1,8 @@
/* eslint-env node */
const fs = require('fs-extra');
const path = require('path');
const { const {
isFunction, isFunction,
isNumber, isNumber,
@ -8,6 +13,7 @@ const {
sample, sample,
} = require('lodash'); } = require('lodash');
const Attachments = require('../../app/attachments');
const Message = require('./types/message'); const Message = require('./types/message');
const { deferredToPromise } = require('./deferred_to_promise'); const { deferredToPromise } = require('./deferred_to_promise');
const { sleep } = require('./sleep'); const { sleep } = require('./sleep');
@ -47,7 +53,8 @@ exports.createConversation = async ({
await Promise.all(range(0, numMessages).map(async (index) => { await Promise.all(range(0, numMessages).map(async (index) => {
await sleep(index * 100); await sleep(index * 100);
console.log(`Create message ${index + 1}`); console.log(`Create message ${index + 1}`);
const message = new WhisperMessage(createRandomMessage({ conversationId })); const messageAttributes = await createRandomMessage({ conversationId });
const message = new WhisperMessage(messageAttributes);
return deferredToPromise(message.save()); return deferredToPromise(message.save());
})); }));
}; };
@ -71,7 +78,7 @@ const SAMPLE_MESSAGES = [
]; ];
const ATTACHMENT_SAMPLE_RATE = 0.33; const ATTACHMENT_SAMPLE_RATE = 0.33;
const createRandomMessage = ({ conversationId } = {}) => { const createRandomMessage = async ({ conversationId } = {}) => {
if (!isString(conversationId)) { if (!isString(conversationId)) {
throw new TypeError('"conversationId" must be a string'); throw new TypeError('"conversationId" must be a string');
} }
@ -81,7 +88,7 @@ const createRandomMessage = ({ conversationId } = {}) => {
const hasAttachment = Math.random() <= ATTACHMENT_SAMPLE_RATE; const hasAttachment = Math.random() <= ATTACHMENT_SAMPLE_RATE;
const attachments = hasAttachment const attachments = hasAttachment
? [createRandomInMemoryAttachment()] : []; ? [await createRandomInMemoryAttachment()] : [];
const type = sample(['incoming', 'outgoing']); const type = sample(['incoming', 'outgoing']);
const commonProperties = { const commonProperties = {
attachments, attachments,
@ -119,17 +126,40 @@ const _createMessage = ({ commonProperties, conversationId, type } = {}) => {
} }
}; };
const MEGA_BYTE = 1e6; const FIXTURES_PATH = path.join(__dirname, '..', '..', 'fixtures');
const createRandomInMemoryAttachment = () => { const readData = Attachments.readData(FIXTURES_PATH);
const numBytes = (1 + Math.ceil((Math.random() * 50))) * MEGA_BYTE; const createRandomInMemoryAttachment = async () => {
const array = new Uint32Array(numBytes).fill(1); const files = (await fs.readdir(FIXTURES_PATH)).map(createFileEntry);
const data = array.buffer; const { contentType, fileName } = sample(files);
const fileName = Math.random().toString().slice(2); const data = await readData(fileName);
return { return {
contentType: 'application/octet-stream', contentType,
data, data,
fileName, fileName,
size: numBytes, size: data.byteLength,
}; };
}; };
const createFileEntry = fileName => ({
fileName,
contentType: fileNameToContentType(fileName),
});
const fileNameToContentType = (fileName) => {
const fileExtension = path.extname(fileName).toLowerCase();
switch (fileExtension) {
case '.gif':
return 'image/gif';
case '.png':
return 'image/png';
case '.jpg':
case '.jpeg':
return 'image/jpeg';
case '.mp4':
return 'video/mp4';
case '.txt':
return 'text/plain';
default:
return 'application/octet-stream';
}
};