#!/usr/bin/perl

use strict;
use Net::SMTP;
use Time::HiRes qw(sleep gettimeofday tv_interval);

$|++;

die <<"ENDUSAGE" if ($#ARGV != 4);

Usage: $0 ENDPOINT FROM-ADDRESS TO-ADDRESS THREADS DURATION
Will start THREADS to send dummy e-mails to TO-ADDRESS. The test will run
for DURATION seconds. ENDPOINT is the IP address to connect to.

ENDUSAGE

my ($endpoint, $from, $address, $threads, $duration) = @ARGV;
for my $i (1..$threads) {
    next if (fork());

    my $t_start = [gettimeofday()];
    my $runs = 0;
    while (tv_interval($t_start) < $duration) {
        $runs++;
        my $t_run = [gettimeofday()];
        my $smtp = Net::SMTP->new($endpoint, Timeout => 5)
          or die ("Cannot start SMTP\n");
        $smtp->mail($from);
        $smtp->to($address);
        $smtp->data();
        $smtp->datasend("To: $address\n");
        $smtp->datasend("Subject: Testing mail\n");
        $smtp->datasend("\n");
        $smtp->datasend("This is just a test message.\n");
        $smtp->dataend();
        $smtp->quit();
        print(tv_interval($t_run), "\n");
    }
    exit(0);
}

while(wait() != -1) {
}
