Skip to content

Instantly share code, notes, and snippets.

@ralight
Created May 14, 2013 22:07
Show Gist options
  • Save ralight/5580046 to your computer and use it in GitHub Desktop.
Save ralight/5580046 to your computer and use it in GitHub Desktop.
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <mosquitto.h>
#define MAX 100000
int main(int argc, char *argv[])
{
int i;
char buf[1024];
struct mosquitto *mosq = NULL;
char err[1024];
int len;
struct timeval start, stop;
double elapsed;
long e;
int rc;
mosquitto_lib_init();
mosq = mosquitto_new(NULL, true, NULL);
if(!mosq){
fprintf(stderr, "Error: Out of memory.\n");
mosquitto_lib_cleanup();
return 1;
}
rc = mosquitto_connect(mosq, "localhost", 1884, 60);
if(rc){
if(rc == MOSQ_ERR_ERRNO){
strerror_r(errno, err, 1024);
fprintf(stderr, "Error: %s\n", err);
}else{
fprintf(stderr, "Unable to connect (%d).\n", rc);
}
mosquitto_lib_cleanup();
return rc;
}
mosquitto_loop_start(mosq);
gettimeofday(&start, NULL);
for(i=0; i<MAX; i++){
snprintf(buf, 1000, "Hello MQTT world .. This is message %010d", i);
mosquitto_publish(mosq, NULL, "pcap/scapy/BOOM", strlen(buf), buf, 0, false);
}
mosquitto_disconnect(mosq);
mosquitto_loop_stop(mosq, false);
gettimeofday(&stop, NULL);
e = stop.tv_sec*1000000 + stop.tv_usec - (start.tv_sec*1000000 + start.tv_usec);
elapsed = (double)e/1000000.0;
printf("Elapsed: %.2f. %.2f qps\n", elapsed, ((double)MAX)/elapsed);
mosquitto_destroy(mosq);
mosquitto_lib_cleanup();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment