<commit_before>#!/usr/bin/perl
# Simple regression script

use strict;
use IO::Socket;
use LWP::Simple;
use Test::More tests => 92;
use File::Temp qw/tempfile/;
use Data::Dumper;

use constant {
TYPE_THRESHOLD_v1 => 0,
TYPE_REMOVE       => 1,
TYPE_INJECT       => 2,
TYPE_THRESHOLD_v2 => 3,
TYPE_THRESHOLD_v3 => 4
};

sub max ($$) { $_[$_[0] < $_[1]] }
sub trim {
    my $string = shift;
    $string =~ s/^\s+//;
    $string =~ s/\s+$//;
    return $string;
}

sub thrasher_query_v1($$$) {
my ($address, $host, $uri) = @_;

    my $addr = inet_aton($address);
    my $data = pack("Ca*nna*a*", TYPE_THRESHOLD_v1, $addr, length($uri), length($host), $uri, $host);
    $main::thrasher->syswrite($data);
    $main::thrasher->read(my $buf, 1, 0);
    return unpack("C", $buf);
}

sub thrasher_add($) {
my ($address) = @_;

    my $addr = inet_aton($address);
    my $data = pack("Ca*", TYPE_INJECT, $addr);
    $main::thrasher->syswrite($data);
}

sub thrasher_remove($) {
my ($address) = @_;

    my $addr = inet_aton($address);
    my $data = pack("Ca*", TYPE_REMOVE, $addr);
    $main::thrasher->syswrite($data);
}

sub thrasher_query_v2($) {
my ($address) = @_;

    my $addr = inet_aton($address);
    my $data = pack("Ca*", TYPE_THRESHOLD_v2, $addr);
    $main::thrasher->syswrite($data);
    $main::thrasher->read(my $buf, 1, 0);
    return unpack("C", $buf);
}

sub thrasher_query_v3($$$$) {
my ($identifier, $address, $host, $uri) = @_;

    my $addr = inet_aton($address);
    my $data = pack("CNa*nna*a*", TYPE_THRESHOLD_v3, $identifier, $addr, length($uri), length($host), $uri, $host);
    $main::thrasher->syswrite($data);
    $main::thrasher->read(my $buf, 5, 0);
    my ($id, $permit) = unpack("NC", $buf);
    die "$id != $identifier" if ($id != $identifier);
    return $permit;
}

sub thrasherd_http_holddowns {
    my %holddowns;
    my $content = get("http://localhost:54321/holddowns");
    #print $content;
    foreach my $line (split(/\n/, $content)) {
        next if ($line =~ /Blocked/);
        chomp $line;
        my ($ip, $trigger, $count, $velocity, $timeout, $hardto, $recentto) = split(/ +/, $line);
        $holddowns{$ip} = {trigger => $trigger, count => $count, velocity=> $velocity, timeout => $timeout, hardTimeout => $hardto, recentTimeout => $recentto};
    }
    return %holddowns;
}

sub thrasherd_http_config {
    my %config;
    my $content = get("http://localhost:54321/config");
    #print $content;
    foreach my $line (split(/\n/, $content)) {
        next if ($line !~ /:/);
        chomp $line;
        my ($key, $value) = split(/:/, $line);
        $config{trim($key)} = trim($value);
    }
    return %config;
}

sub thrasherd_http_connections {
    my %connections;
    my $content = get("http://localhost:54321/connections");
    #print $content;
    foreach my $line (split(/\n/, $content)) {
        next if ($line !~ /^\d/);
        chomp $line;
        my ($ip, $port, $requests, $conn, $last) = ($line =~ /^([\d\.]+) +(\d+) +(\d+) +(\w+ +\d+ [\d:]+) +(N.A|\w+ +\d+ [\d:]+)/);
        $connections{"$ip:$port"} = {requests => $requests, connDate => $conn, lastDate => $last};
    }
    #print Dumper(\%connections);
    return %connections;
}

sub thrasherd_http_addrs {
    my %addrs;
    my $content = get("http://localhost:54321/addrs");
    #print $content;
    foreach my $line (split(/\n/, $content)) {
        next if ($line !~ /^\d/);
        chomp $line;
        my ($ip, $connections, $timeout) = split(/ +/, $line);
        $addrs{$ip} = {connections => $connections, timeout => $timeout};
    }
    return %addrs;
}

sub thrasherd_http_uris {
    my %uris;
    my $content = get("http://localhost:54321/uris");
    #print $content;
    foreach my $line (split(/\n/, $content)) {
        next if ($line !~ /^\d/);
        chomp $line;
        my ($ip, $connections, $timeout, $uri) = split(/ +/, $line);
        $uris{"$ip:$uri"} = {connections => $connections, timeout => $timeout};
    }
    return %uris;
}

sub thrasherd_http_hosts {
    my %hosts;
    my $content = get("http://localhost:54321/hosts");
    #print $content;
    foreach my $line (split(/\n/, $content)) {
        next if ($line !~ /^\d/);
        chomp $line;
        my ($ip, $connections, $timeout, $host) = split(/ +/, $line);
        $hosts{"$ip:$host"} = {connections => $connections, timeout => $timeout};
    }
    return %hosts;
}

######################################################################

$main::thrasher = IO::Socket::INET->new('localhost:54320');
die "Couldn't connect to thrashd on 'localhost:54320'" if (!$main::thrasher);
$main::sockport = $main::thrasher->sockport();

if ($ARGV[0] eq "--singletest") {
    for (my $x = 0; $x < 300000; $x++) {
                thrasher_query_v1("10.11.10.10", "host", "/");
    }
    exit 0;
} elsif ($ARGV[0] eq "--spreadtest") {
    for (my $x = 0; $x < 50; $x++) {
        for (my $y = 0; $y < 50; $y++) {
            for (my $z = 0; $z < 500; $z++) {
                thrasher_query_v1("10.10.$x.$y", "host$x.$y", "/$x/$z");
            }
        }
    }
    exit 0;
}

# Check if things have cleared out, don't want failed tests
my %addrs = thrasherd_http_addrs();
my %uris = thrasherd_http_uris();
if (exists $addrs{"4.3.2.1"} || exists $uris{"4.3.2.1:/"}) {
    die "Some tests are going to fail, restart thrashd OR wait " . 
        max($addrs{"4.3.2.1"}->{timeout}, $uris{"4.3.2.1:/"}->{timeout}) . 
        " seconds and try again";<commit_msg>Added simple --conntest option for connection/load test<commit_after>#!/usr/bin/perl
# Simple regression script

use strict;
use IO::Socket;
use LWP::Simple;
use Test::More tests => 92;
use File::Temp qw/tempfile/;
use Data::Dumper;

use constant {
TYPE_THRESHOLD_v1 => 0,
TYPE_REMOVE       => 1,
TYPE_INJECT       => 2,
TYPE_THRESHOLD_v2 => 3,
TYPE_THRESHOLD_v3 => 4
};

sub max ($$) { $_[$_[0] < $_[1]] }
sub trim {
    my $string = shift;
    $string =~ s/^\s+//;
    $string =~ s/\s+$//;
    return $string;
}

sub thrasher_query_v1($$$) {
my ($address, $host, $uri) = @_;

    my $addr = inet_aton($address);
    my $data = pack("Ca*nna*a*", TYPE_THRESHOLD_v1, $addr, length($uri), length($host), $uri, $host);
    $main::thrasher->syswrite($data);
    $main::thrasher->read(my $buf, 1, 0);
    return unpack("C", $buf);
}

sub thrasher_add($) {
my ($address) = @_;

    my $addr = inet_aton($address);
    my $data = pack("Ca*", TYPE_INJECT, $addr);
    $main::thrasher->syswrite($data);
}

sub thrasher_remove($) {
my ($address) = @_;

    my $addr = inet_aton($address);
    my $data = pack("Ca*", TYPE_REMOVE, $addr);
    $main::thrasher->syswrite($data);
}

sub thrasher_query_v2($) {
my ($address) = @_;

    my $addr = inet_aton($address);
    my $data = pack("Ca*", TYPE_THRESHOLD_v2, $addr);
    $main::thrasher->syswrite($data);
    $main::thrasher->read(my $buf, 1, 0);
    return unpack("C", $buf);
}

sub thrasher_query_v3($$$$) {
my ($identifier, $address, $host, $uri) = @_;

    my $addr = inet_aton($address);
    my $data = pack("CNa*nna*a*", TYPE_THRESHOLD_v3, $identifier, $addr, length($uri), length($host), $uri, $host);
    $main::thrasher->syswrite($data);
    $main::thrasher->read(my $buf, 5, 0);
    my ($id, $permit) = unpack("NC", $buf);
    die "$id != $identifier" if ($id != $identifier);
    return $permit;
}

sub thrasherd_http_holddowns {
    my %holddowns;
    my $content = get("http://localhost:54321/holddowns");
    #print $content;
    foreach my $line (split(/\n/, $content)) {
        next if ($line =~ /Blocked/);
        chomp $line;
        my ($ip, $trigger, $count, $velocity, $timeout, $hardto, $recentto) = split(/ +/, $line);
        $holddowns{$ip} = {trigger => $trigger, count => $count, velocity=> $velocity, timeout => $timeout, hardTimeout => $hardto, recentTimeout => $recentto};
    }
    return %holddowns;
}

sub thrasherd_http_config {
    my %config;
    my $content = get("http://localhost:54321/config");
    #print $content;
    foreach my $line (split(/\n/, $content)) {
        next if ($line !~ /:/);
        chomp $line;
        my ($key, $value) = split(/:/, $line);
        $config{trim($key)} = trim($value);
    }
    return %config;
}

sub thrasherd_http_connections {
    my %connections;
    my $content = get("http://localhost:54321/connections");
    #print $content;
    foreach my $line (split(/\n/, $content)) {
        next if ($line !~ /^\d/);
        chomp $line;
        my ($ip, $port, $requests, $conn, $last) = ($line =~ /^([\d\.]+) +(\d+) +(\d+) +(\w+ +\d+ [\d:]+) +(N.A|\w+ +\d+ [\d:]+)/);
        $connections{"$ip:$port"} = {requests => $requests, connDate => $conn, lastDate => $last};
    }
    #print Dumper(\%connections);
    return %connections;
}

sub thrasherd_http_addrs {
    my %addrs;
    my $content = get("http://localhost:54321/addrs");
    #print $content;
    foreach my $line (split(/\n/, $content)) {
        next if ($line !~ /^\d/);
        chomp $line;
        my ($ip, $connections, $timeout) = split(/ +/, $line);
        $addrs{$ip} = {connections => $connections, timeout => $timeout};
    }
    return %addrs;
}

sub thrasherd_http_uris {
    my %uris;
    my $content = get("http://localhost:54321/uris");
    #print $content;
    foreach my $line (split(/\n/, $content)) {
        next if ($line !~ /^\d/);
        chomp $line;
        my ($ip, $connections, $timeout, $uri) = split(/ +/, $line);
        $uris{"$ip:$uri"} = {connections => $connections, timeout => $timeout};
    }
    return %uris;
}

sub thrasherd_http_hosts {
    my %hosts;
    my $content = get("http://localhost:54321/hosts");
    #print $content;
    foreach my $line (split(/\n/, $content)) {
        next if ($line !~ /^\d/);
        chomp $line;
        my ($ip, $connections, $timeout, $host) = split(/ +/, $line);
        $hosts{"$ip:$host"} = {connections => $connections, timeout => $timeout};
    }
    return %hosts;
}

######################################################################

$main::thrasher = IO::Socket::INET->new('localhost:54320');
die "Couldn't connect to thrashd on 'localhost:54320'" if (!$main::thrasher);
$main::sockport = $main::thrasher->sockport();

if ($ARGV[0] eq "--singletest") {
    for (my $x = 0; $x < 300000; $x++) {
                thrasher_query_v1("10.11.10.10", "host", "/");
    }
    exit 0;
} elsif ($ARGV[0] eq "--spreadtest") {
    for (my $x = 0; $x < 50; $x++) {
        for (my $y = 0; $y < 50; $y++) {
            for (my $z = 0; $z < 500; $z++) {
                thrasher_query_v1("10.10.$x.$y", "host$x.$y", "/$x/$z");
            }
        }
    }
    exit 0;
} elsif ($ARGV[0] eq "--conntest") {
    my $num = 2000;
    print "Connecting $num\n";
    my @thrashers;
    for (my $x = 0; $x < $num; $x++) {
        $thrashers[$x] = IO::Socket::INET->new('localhost:54320');
        die "Couldn't connect to thrashd on 'localhost:54320' for $x" if (!$thrashers[$x]);
    }

    my $addr = inet_aton("10.10.10.10");
    my $uri = "/test.uri";
    my $host = "/test.host";
    my $data = pack("Ca*nna*a*", TYPE_THRESHOLD_v1, $addr, length($uri), length($host), $uri, $host);
    for (my $y = 0; $y < $num; $y++) {
        print "Writing\n";
        for (my $x = 0; $x < $num; $x++) {
            $thrashers[$x]->syswrite($data);
        }
        print "Reading\n";
        for (my $x = 0; $x < $num; $x++) {
            $thrashers[$x]->read(my $buf, 1, 0);
        }
        print "Done $y\n";
    }
    print "Done\n";
    exit 0;
}

# Check if things have cleared out, don't want failed tests
my %addrs = thrasherd_http_addrs();
my %uris = thrasherd_http_uris();
if (exists $addrs{"4.3.2.1"} || exists $uris{"4.3.2.1:/"}) {
    die "Some tests are going to fail, restart thrashd OR wait " . 
        max($addrs{"4.3.2.1"}->{timeout}, $uris{"4.3.2.1:/"}->{timeout}) . 
        " seconds and try again";
{'fraction_non_alphanumeric': 0.2268765338072269, 'fraction_numerical': 0.022086824067022087, 'mean_word_length': 2.938020659780073, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 72, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 11, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7619453', 'n_tokens_mistral': 4684, 'n_tokens_neox': 4418, 'n_words': 1038}