Skip to content

Instantly share code, notes, and snippets.

@pbrumblay
Created June 20, 2022 23:22
Show Gist options
  • Save pbrumblay/27b9ce22fc596ba38af73bd26b7bb39c to your computer and use it in GitHub Desktop.
Save pbrumblay/27b9ce22fc596ba38af73bd26b7bb39c to your computer and use it in GitHub Desktop.
Publish test data
import {Command} from 'commander';
import 'dotenv/config.js'; // eslint-disable-line import/no-unassigned-import
import {PubSub} from '@google-cloud/pubsub';
const program = new Command();
program
.requiredOption('-t, --targettopic <topic name>', 'Target topic to move messages to.')
.requiredOption('-tp, --topicproject <project>', 'Name of project where topic is defined.')
.option('-n, --nummessages <number>', 'Number of messages to move at a time.', 100)
program.parse(process.argv);
const options = program.opts();
const publishClient = new PubSub({projectId: options.topicproject});
const publisher = publishClient.topic(options.targettopic, {
batching: {
maxMessages: 100,
maxMilliseconds: 1000,
},
});
const promises = [];
for (let i = 0; i < options.nummessages; i++) {
promises.push(publisher.publishMessage({ data: Buffer.from(`Lots of lots of messages ${i}`)}));
console.log('i: ' + i);
}
await Promise.all(promises);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment