Http Server Test

AWK

GNU Awk 4.1.4, API: 1.1 (GNU MPFR 4.0.1, GNU MP 6.1.2)

#!/usr/bin/gawk -f
BEGIN {
    HttpService = "/inet/tcp/8848/0/0"
    while(1){
        print "HTTP/1.1 200 OK\r\nContent-length:56\r\nContent-Type: text/html; charset=UTF-8\r\n\r\n<!DOCTYPE html><html><body>Goodbye, world!</body></html>" |& HttpService
        while((HttpService |& getline) > 0) 1
        close(HttpService)
    }
}
httperf --client=0/1 --server=localhost --port=8848 --uri=/ --send-buffer=4096 --recv-buffer=16384 --num-conns=100000 --num-calls=1
httperf: warning: open file limit > FD_SETSIZE; limiting max. # of open files to FD_SETSIZE
Maximum connect burst length: 1

Total: connections 100000 requests 39360 replies 39360 test-duration 27.433 s

Connection rate: 3645.3 conn/s (0.3 ms/conn, <=1 concurrent connections)
Connection time [ms]: min 0.0 avg 0.7 max 10.1 median 0.5 stddev 0.5
Connection time [ms]: connect 0.3
Connection length [replies/conn]: 1.000

Request rate: 1434.8 req/s (0.7 ms/req)
Request size [B]: 62.0

Reply rate [replies/s]: min 927.8 avg 1472.7 max 3534.0 stddev 1152.7 (5 samples)
Reply time [ms]: response 0.1 transfer 0.0
Reply size [B]: header 78.0 content 56.0 footer 0.0 (total 134.0)
Reply status: 1xx=0 2xx=39360 3xx=0 4xx=0 5xx=0

CPU time [s]: user 0.12 system 27.28 (user 0.5% system 99.5% total 99.9%)
Net I/O: 274.6 KB/s (2.2*10^6 bps)

Errors: total 60640 client-timo 0 socket-timo 0 connrefused 60640 connreset 0
Errors: fd-unavail 0 addrunavail 0 ftab-full 0 other 0

Bash

while true; do { echo 'HTTP/1.1 200 OK\r\nContent-length:56\r\nContent-Type: text/html; charset=UTF-8\r\n\r\n<!DOCTYPE html><html><body>Goodbye, world!</body></html>'; } | nc -l 18855 -q 1 > /dev/null; done

C

gcc version 7.4.0 (Ubuntu 7.4.0-1ubuntu1~18.04)

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <err.h>

char response[] = "HTTP/1.1 200 OK\r\nContent-length:56\r\nContent-Type: text/html; charset=UTF-8\r\n\r\n<!DOCTYPE html><html><body>Goodbye, world!</body></html>";

int main()
{
    int one = 1, client_fd;
    struct sockaddr_in svr_addr, cli_addr;
    socklen_t sin_len = sizeof(cli_addr);
    int sock = socket(AF_INET, SOCK_STREAM, 0);
    setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(int));
    int port = 8849;
    svr_addr.sin_family = AF_INET;
    svr_addr.sin_addr.s_addr = INADDR_ANY;
    svr_addr.sin_port = htons(port);
    bind(sock, (struct sockaddr *) &svr_addr, sizeof(svr_addr));
    listen(sock, 5);
    while (1)
    {
        client_fd = accept(sock, (struct sockaddr *) &cli_addr, &sin_len);
        write(client_fd, response, sizeof(response) - 1);
        close(client_fd);
    }
}
httperf --client=0/1 --server=localhost --port=8849 --uri=/ --send-buffer=4096 --recv-buffer=16384 --num-conns=100000 --num-calls=1
httperf: warning: open file limit > FD_SETSIZE; limiting max. # of open files to FD_SETSIZE
Maximum connect burst length: 1

Total: connections 100000 requests 100000 replies 100000 test-duration 5.021 s

Connection rate: 19916.2 conn/s (0.1 ms/conn, <=1 concurrent connections)
Connection time [ms]: min 0.0 avg 0.1 max 2.4 median 0.5 stddev 0.0
Connection time [ms]: connect 0.0
Connection length [replies/conn]: 1.000

Request rate: 19916.2 req/s (0.1 ms/req)
Request size [B]: 62.0

Reply rate [replies/s]: min 19913.0 avg 19913.0 max 19913.0 stddev 0.0 (1 samples)
Reply time [ms]: response 0.0 transfer 0.0
Reply size [B]: header 78.0 content 56.0 footer 0.0 (total 134.0)
Reply status: 1xx=0 2xx=100000 3xx=0 4xx=0 5xx=0

CPU time [s]: user 0.06 system 4.95 (user 1.1% system 98.5% total 99.6%)
Net I/O: 3812.1 KB/s (31.2*10^6 bps)

Errors: total 0 client-timo 0 socket-timo 0 connrefused 0 connreset 0
Errors: fd-unavail 0 addrunavail 0 ftab-full 0 other 0

C#

.NET Core 2.2.203

using System;
using System.Net;
using System.Net.Sockets;
using System.Text;

namespace Temp
{
    class TEMP
    {
        static void Main()
        {
            while (true)
            {
                try
                {
                    const string msg = "HTTP/1.1 200 OK\r\nContent-length:56\r\nContent-Type: text/html; charset=UTF-8\r\n\r\n<!DOCTYPE html><html><body>Goodbye, world!</body></html>";
                    TcpListener tcpListener = new TcpListener(IPAddress.Any, 8850);
                    tcpListener.Start();
                    while (true)
                    {
                        Socket socketConnection = tcpListener.AcceptSocket();
                        socketConnection.Send(Encoding.ASCII.GetBytes(msg.ToCharArray(), 0, msg.Length));
                        socketConnection.Disconnect(true);
                    }
                }
                catch()
                {
                    // ignored
                }
            }
        }
    }
}
httperf --client=0/1 --server=localhost --port=8850 --uri=/ --send-buffer=4096 --recv-buffer=16384 --num-conns=100000 --num-calls=1
httperf: warning: open file limit > FD_SETSIZE; limiting max. # of open files to FD_SETSIZE
Maximum connect burst length: 1

Total: connections 100000 requests 95476 replies 95453 test-duration 34.832 s

Connection rate: 2870.9 conn/s (0.3 ms/conn, <=1 concurrent connections)
Connection time [ms]: min 0.0 avg 0.0 max 34.6 median 0.5 stddev 0.2
Connection time [ms]: connect 0.0
Connection length [replies/conn]: 1.000

Request rate: 2741.1 req/s (0.4 ms/req)
Request size [B]: 62.0

Reply rate [replies/s]: min 1624.1 avg 2977.8 max 6496.4 stddev 1827.6 (6 samples)
Reply time [ms]: response 0.0 transfer 0.0
Reply size [B]: header 78.0 content 56.0 footer 0.0 (total 134.0)
Reply status: 1xx=0 2xx=95453 3xx=0 4xx=0 5xx=0

CPU time [s]: user 8.25 system 26.56 (user 23.7% system 76.2% total 99.9%)
Net I/O: 524.6 KB/s (4.3*10^6 bps)

Errors: total 4547 client-timo 0 socket-timo 0 connrefused 4393 connreset 154
Errors: fd-unavail 0 addrunavail 0 ftab-full 0 other 0

Erlang

-module(main).
-export([main/0]).

main() ->
    {ok, Listen} = gen_tcp:listen(8852,[binary, {packet, 0}, {reuseaddr, true}, {active, true}]),
    do(Listen, gen_tcp:accept(Listen)),
    gen_tcp:close(Listen).

do(Listen, {ok, Socket}) -> 
    receive
        {tcp, Socket, _} ->
            gen_tcp:send(Socket, <<"HTTP/1.1 200 OK\r\nContent-length:56\r\nContent-Type: text/html; charset=UTF-8\r\n\r\n<!DOCTYPE html><html><body>Goodbye, world!</body></html>">>),
            gen_tcp:close(Socket)
    end,
    do(Listen, gen_tcp:accept(Listen)).
httperf --client=0/1 --server=localhost --port=8852 --uri=/ --send-buffer=4096 --recv-buffer=16384 --num-conns=100000 --num-calls=1
httperf: warning: open file limit > FD_SETSIZE; limiting max. # of open files to FD_SETSIZE
Maximum connect burst length: 1

Total: connections 100000 requests 100000 replies 100000 test-duration 58.661 s

Connection rate: 1704.7 conn/s (0.6 ms/conn, <=1 concurrent connections)
Connection time [ms]: min 0.0 avg 0.6 max 7.5 median 0.5 stddev 0.4
Connection time [ms]: connect 0.5
Connection length [replies/conn]: 1.000

Request rate: 1704.7 req/s (0.6 ms/req)
Request size [B]: 62.0

Reply rate [replies/s]: min 1127.7 avg 1289.4 max 2645.9 stddev 450.0 (11 samples)
Reply time [ms]: response 0.1 transfer 0.0
Reply size [B]: header 78.0 content 56.0 footer 0.0 (total 134.0)
Reply status: 1xx=0 2xx=100000 3xx=0 4xx=0 5xx=0

CPU time [s]: user 0.40 system 58.21 (user 0.7% system 99.2% total 99.9%)
Net I/O: 326.3 KB/s (2.7*10^6 bps)

Errors: total 0 client-timo 0 socket-timo 0 connrefused 0 connreset 0
Errors: fd-unavail 0 addrunavail 0 ftab-full 0 other 0

Free Pascal

program fuckfpws;
{$mode objfpc}{$H+}
uses Classes, fphttpserver;

Type TTestHTTPServer = Class(TFPHTTPServer)
  public procedure HandleRequest(Var req:TFPHTTPConnectionRequest; Var res:TFPHTTPConnectionResponse); override;
  end;

Var ws:TTestHTTPServer;

procedure TTestHTTPServer.HandleRequest(var req:TFPHTTPConnectionRequest; var res:TFPHTTPConnectionResponse);
Var F:TStringStream;
begin
  F:=TStringStream.Create('Goodbye,World!');
  res.ContentLength:=F.Size;
  res.ContentStream:=F;
  res.SendContent;
  res.ContentStream:=Nil;
  F.Free;
end;

begin
  ws:=TTestHTTPServer.Create(Nil);
  ws.Threaded:=False;
  ws.Port:=18856;
  ws.AcceptIdleTimeout:=1000;
  ws.Active:=True;
  ws.Free;
end.

Haskell

{-# LANGUAGE OverloadedStrings #-}
 
import Data.ByteString.Char8 ()
import Data.Conduit ( ($), yield )
import Data.Conduit.Network ( ServerSettings(..), runTCPServer )
 
main :: IO ()
main = runTCPServer (ServerSettings 8853 ["127.0.0.1"]) $ const (yield response $)
    where response = "HTTP/1.0 200 OK\nContent-Length: 16\n\nGoodbye, World!\n"

Java

openjdk version “11.0.2” 2019-01-15
OpenJDK Runtime Environment (build 11.0.2+9-Ubuntu-3ubuntu118.04.3)
OpenJDK 64-Bit Server VM (build 11.0.2+9-Ubuntu-3ubuntu118.04.3, mixed mode, sharing)

javac 11.0.2

import java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class Main
{
    public static void main(String[] args) throws IOException
    {
        ServerSocket listener = new ServerSocket(18849);
        while(true)
        {
            Socket sock = listener.accept();
            new PrintWriter(sock.getOutputStream(), true).println("HTTP/1.1 200 OK\r\nContent-length:56\r\nContent-Type: text/html; charset=UTF-8\r\n\r\n<!DOCTYPE html><html><body>Goodbye, world!</body></html>");
            sock.close();
        }
    }
}
httperf --client=0/1 --server=localhost --port=18849 --uri=/ --send-buffer=4096 --recv-buffer=16384 --num-conns=100000 --num-calls=1
httperf: warning: open file limit > FD_SETSIZE; limiting max. # of open files to FD_SETSIZE
Maximum connect burst length: 1

Total: connections 100000 requests 100000 replies 100000 test-duration 10.253 s

Connection rate: 9752.9 conn/s (0.1 ms/conn, <=1 concurrent connections)
Connection time [ms]: min 0.0 avg 0.1 max 28.3 median 0.5 stddev 0.2
Connection time [ms]: connect 0.1
Connection length [replies/conn]: 1.000

Request rate: 9752.9 req/s (0.1 ms/req)
Request size [B]: 62.0

Reply rate [replies/s]: min 9605.8 avg 9739.9 max 9874.1 stddev 189.8 (2 samples)
Reply time [ms]: response 0.0 transfer 0.0
Reply size [B]: header 78.0 content 56.0 footer 0.0 (total 134.0)
Reply status: 1xx=0 2xx=100000 3xx=0 4xx=0 5xx=0

CPU time [s]: user 0.44 system 9.76 (user 4.3% system 95.2% total 99.5%)
Net I/O: 1866.8 KB/s (15.3*10^6 bps)

Errors: total 0 client-timo 0 socket-timo 0 connrefused 0 connreset 0
Errors: fd-unavail 0 addrunavail 0 ftab-full 0 other 0

Node.js

import http = require("http");
http.createServer((req, res) => {
    res.writeHead(200, { 'Content-Type': "text/plain" });
    res.end("Goodbye, World!");
}).listen(18851, "127.0.0.1");

Perl

use Socket;
my $port = 18852;
my $protocol = getprotobyname("tcp");
socket(SOCK, PF_INET, SOCK_STREAM, $protocol);
setsockopt(SOCK, SOL_SOCKET, SO_REUSEADDR, 1);
bind(SOCK, sockaddr_in($port, INADDR_ANY));
listen(SOCK, SOMAXCONN);
while(accept(CLIENT, SOCK))
{
    print CLIENT "HTTP/1.1 200 OK\r\nContent-length:56\r\nContent-Type: text/html; charset=UTF-8\r\n\r\n<!DOCTYPE html><html><body>Goodbye, world!</body></html>";
    close CLIENT;
}

Php

<?php
$socket = socket_create(AF_INET, SOCK_STREAM, 0);
socket_bind($socket, 0, 18853);
socket_listen($socket);
while (true) if ($client = @socket_accept($socket))
{
    socket_write($client, "HTTP/1.1 200 OK\r\nContent-length:56\r\nContent-Type: text/html; charset=UTF-8\r\n\r\n<!DOCTYPE html><html><body>Goodbye, world!</body></html>");
    socket_close($client);
}

Python

import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('127.0.0.1', 18854))
s.listen(5)
while True:
    c,a = s.accept()
    c.send(b"HTTP/1.1 200 OK\r\nContent-length:56\r\nContent-Type: text/html; charset=UTF-8\r\n\r\n<!DOCTYPE html><html><body>Goodbye, world!</body></html>")
    c.close()

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注