File: benchmark.pp

package info (click to toggle)
fpc 3.2.0%2Bdfsg-12
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, bullseye-backports
  • size: 338,552 kB
  • sloc: pascal: 3,794,737; xml: 191,997; ansic: 9,637; asm: 8,482; java: 5,346; sh: 4,664; yacc: 3,751; makefile: 2,688; lex: 2,538; javascript: 2,375; sql: 929; php: 473; cpp: 145; perl: 134; sed: 132; csh: 34; tcl: 7
file content (131 lines) | stat: -rw-r--r-- 3,893 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
(*
     This file is part of libmicrohttpd
     Copyright (C) 2007, 2013 Christian Grothoff (and other contributing authors)

     This library is free software; you can redistribute it and/or
     modify it under the terms of the GNU Lesser General Public
     License as published by the Free Software Foundation; either
     version 2.1 of the License, or (at your option) any later version.

     This library is distributed in the hope that it will be useful,
     but WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     Lesser General Public License for more details.

     You should have received a copy of the GNU Lesser General Public
     License along with this library; if not, write to the Free Software
     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*)
(**
 * @file benchmark.pp (Original: benchmark.c)
 * @brief minimal code to benchmark MHD GET performance
 * @author Christian Grothoff / Silvio Clécio
 *)

program benchmark;

{$mode objfpc}{$H+}
{$MACRO ON}
{$IF DEFINED(CPU_COUNT) and (CPU_COUNT + 0) < 2}
  {$UNDEF CPU_COUNT}
{$ENDIF}
{$IF NOT DEFINED(CPU_COUNT)}
  {$DEFINE CPU_COUNT := 2}
{$ENDIF}

uses
{$IFDEF MSWINDOWS}
  WinSock2,
{$ELSE}
  BaseUnix, Unix,
{$ENDIF}
  cmem, sysutils, cutils, libmicrohttpd;

const
  PAGE: Pcchar = '<html><head><title>libmicrohttpd demo</title></head><body>libmicrohttpd demo</body></html>';
  SMALL = 1024 * 128;
  NUMBER_OF_THREADS = CPU_COUNT;

var
  small_deltas: array[0..SMALL] of cuint;
  response: PMHD_Response;

  procedure completed_callback(cls: Pointer; connection: PMHD_Connection;
    con_cls: PPointer; toe: MHD_RequestTerminationCode); cdecl;
  var
    tv: ptimeval;
    tve: timeval;
    delta: cuint64;
  begin
    tv := con_cls^;
    if nil = tv then
      Exit;
    fpgettimeofday(@tve, nil);
    delta := 0;
    if tve.tv_usec >= tv^.tv_usec then
      delta += (tve.tv_sec - tv^.tv_sec) * 1000000 +
        (tve.tv_usec - tv^.tv_usec)
    else
      delta += (tve.tv_sec - tv^.tv_sec) * 1000000 -
        tv^.tv_usec + tve.tv_usec;
    if delta < SMALL then
      Inc(small_deltas[delta])
    else
      WriteLn(stdout, Format('D: %u 1', [delta]));
    Free(tv);
  end;

  function uri_logger_cb(cls: Pointer; uri: Pcchar): Pointer; cdecl;
  var
    tv: ptimeval;
  begin
    tv := Malloc(SizeOf(timeval));
    if nil <> tv then
      fpgettimeofday(tv, nil);
    Result := tv;
  end;

  function ahc_echo(cls: Pointer; connection: PMHD_Connection; url: Pcchar;
    method: Pcchar; version: Pcchar; upload_data: Pcchar;
    upload_data_size: Psize_t; ptr: PPointer): cint; cdecl;
  begin
    if 0 <> strcomp(method, 'GET') then
      Exit(MHD_NO);
    Result := MHD_queue_response(connection, MHD_HTTP_OK, response);
  end;

var
  d: PMHD_Daemon;
  i: cuint;
begin
  if argc <> 2 then
  begin
    WriteLn(argv[0] + ' PORT');
    Halt(1);
  end;
  response := MHD_create_response_from_buffer(Length(PAGE), Pointer(PAGE),
    MHD_RESPMEM_PERSISTENT);
{$IF 0}
  MHD_add_response_header (response, MHD_HTTP_HEADER_CONNECTION, 'close');
{$ENDIF}
  d := MHD_start_daemon(MHD_USE_SELECT_INTERNALLY or MHD_SUPPRESS_DATE_NO_CLOCK
{$IFDEF EPOLL_SUPPORT}
         or MHD_USE_EPOLL_LINUX_ONLY or MHD_USE_EPOLL_TURBO
{$ENDIF},
         StrToInt(argv[1]), nil, nil, @ahc_echo, nil,
         MHD_OPTION_CONNECTION_TIMEOUT, 120,
         MHD_OPTION_THREAD_POOL_SIZE, NUMBER_OF_THREADS,
         MHD_OPTION_URI_LOG_CALLBACK, @uri_logger_cb, nil,
         MHD_OPTION_NOTIFY_COMPLETED, @completed_callback, nil,
         MHD_OPTION_CONNECTION_LIMIT, 1000,
         MHD_OPTION_END);
  if d = nil then
    Halt(1);
  ReadLn;
  MHD_stop_daemon(d);
  MHD_destroy_response(response);
  for i := 0 to SMALL do
    if 0 <> small_deltas[i] then
      WriteLn(stdout, Format('D: %d %u', [i, small_deltas[i]]));
end.