Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,7 @@ deps/luajit/src/luajit

.idea
CMakeLists.txt

# Nix
result
result-*
40 changes: 34 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@ else ifeq ($(TARGET), darwin)
# is not set then it's forced to 10.4, which breaks compile on Mojave.
export MACOSX_DEPLOYMENT_TARGET = $(shell sw_vers -productVersion)
LDFLAGS += -pagezero_size 10000 -image_base 100000000
LIBS += -L/usr/local/opt/openssl/lib
CFLAGS += -I/usr/local/include -I/usr/local/opt/openssl/include
# OpenSSL: try pkg-config, fall back to user-specified OPENSSL_INC/OPENSSL_LIB
OPENSSL_CFLAGS := $(or $(shell pkg-config --cflags openssl 2>/dev/null), -I$(OPENSSL_INC))
OPENSSL_LIBS := $(or $(shell pkg-config --libs openssl 2>/dev/null), -L$(OPENSSL_LIB) -lcrypto -lssl)
CFLAGS += $(OPENSSL_CFLAGS)
LIBS += $(OPENSSL_LIBS)
else ifeq ($(TARGET), linux)
CFLAGS += -D_POSIX_C_SOURCE=200809L -D_BSD_SOURCE
LIBS += -ldl
Expand All @@ -29,37 +32,62 @@ BIN := wrk
ODIR := obj
OBJ := $(patsubst %.c,$(ODIR)/%.o,$(SRC)) $(ODIR)/bytecode.o

LDIR = deps/luajit/src
LIBS := -lluajit $(LIBS)
CFLAGS += -I$(LDIR)
LDFLAGS += -L$(LDIR)
# LuaJIT configuration
# Use system LuaJIT instead of bundled: make USE_SYSTEM_LUAJIT=1
# With pkg-config (auto-detects paths): make USE_SYSTEM_LUAJIT=1
# With manual paths: make USE_SYSTEM_LUAJIT=1 LUAJIT_INC=/path/to/include LUAJIT_LIB=/path/to/lib
ifdef USE_SYSTEM_LUAJIT
# Try pkg-config first, fall back to user-specified paths
LUAJIT_CFLAGS := $(or $(shell pkg-config --cflags luajit 2>/dev/null), -I$(LUAJIT_INC))
LUAJIT_LIBS := $(or $(shell pkg-config --libs luajit 2>/dev/null), -L$(LUAJIT_LIB) -lluajit-5.1)
LUAJIT_BIN ?= luajit
CFLAGS += $(LUAJIT_CFLAGS)
LIBS := $(LUAJIT_LIBS) $(LIBS)
else
LDIR = deps/luajit/src
LIBS := -lluajit $(LIBS)
CFLAGS += -I$(LDIR)
LDFLAGS += -L$(LDIR)
endif

all: $(BIN)

clean:
$(RM) $(BIN) obj/*
ifndef USE_SYSTEM_LUAJIT
@$(MAKE) -C deps/luajit clean
endif

$(BIN): $(OBJ)
@echo LINK $(BIN)
@$(CC) $(LDFLAGS) -o $@ $^ $(LIBS)

ifdef USE_SYSTEM_LUAJIT
$(OBJ): config.h Makefile | $(ODIR)
else
$(OBJ): config.h Makefile $(LDIR)/libluajit.a | $(ODIR)
endif

$(ODIR):
@mkdir -p $@

$(ODIR)/bytecode.o: src/wrk.lua
@echo LUAJIT $<
ifdef USE_SYSTEM_LUAJIT
@$(LUAJIT_BIN) -b $< $@
else
@$(SHELL) -c 'cd $(LDIR) && ./luajit -b $(CURDIR)/$< $(CURDIR)/$@'
endif

$(ODIR)/%.o : %.c
@echo CC $<
@$(CC) $(CFLAGS) -c -o $@ $<

ifndef USE_SYSTEM_LUAJIT
$(LDIR)/libluajit.a:
@echo Building LuaJIT...
@$(MAKE) -C $(LDIR) BUILDMODE=static
endif

.PHONY: all clean
.SUFFIXES:
Expand Down
61 changes: 61 additions & 0 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

86 changes: 86 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
{
description = "wrk2 - a HTTP benchmarking tool based mostly on wrk";

inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-25.11";
flake-utils.url = "github:numtide/flake-utils";
};

outputs = { self, nixpkgs, flake-utils }:
flake-utils.lib.eachDefaultSystem (system:
let
pkgs = nixpkgs.legacyPackages.${system};

# Extract version from src/wrk.h
version = builtins.head (builtins.match ''.*#define VERSION[[:space:]]+"([^"]+)".*'' (builtins.readFile ./src/wrk.h));

sources = [
"wrk" "net" "ssl" "aprintf" "stats" "script"
"units" "ae" "zmalloc" "http_parser" "tinymt64" "hdr_histogram"
];

compileSource = name: ''
echo "CC ${name}.c"
$CC -std=c99 -Wall -O2 -D_REENTRANT \
-I ${pkgs.luajit}/include/luajit-2.1 \
-I ${pkgs.openssl.dev}/include \
-I ${pkgs.zlib.dev}/include \
-I src \
-c -o obj/${name}.o src/${name}.c
'';
in
{
packages.default = pkgs.stdenv.mkDerivation {
pname = "wrk2";
inherit version;

src = ./.;

nativeBuildInputs = [ pkgs.luajit ];

buildInputs = [
pkgs.openssl
pkgs.luajit
pkgs.zlib
];

buildPhase = ''
mkdir -p obj

echo "LUAJIT src/wrk.lua"
${pkgs.luajit}/bin/luajit -b src/wrk.lua obj/bytecode.o

${pkgs.lib.concatMapStringsSep "\n" compileSource sources}

echo "LINK wrk"
$CC -o wrk obj/*.o \
-L ${pkgs.luajit}/lib \
-L ${pkgs.openssl.out}/lib \
-L ${pkgs.zlib}/lib \
-lluajit-5.1 -lpthread -lm -lcrypto -lssl -lz
'';

installPhase = ''
mkdir -p $out/bin
cp wrk $out/bin/wrk2
'';

meta = with pkgs.lib; {
description = "wrk2 - a HTTP benchmarking tool based mostly on wrk";
homepage = "https://github.com/giltene/wrk2";
license = licenses.bsd3;
platforms = platforms.unix;
mainProgram = "wrk2";
};
};

devShells.default = pkgs.mkShell {
buildInputs = [
pkgs.luajit
pkgs.openssl
pkgs.gnumake
];
};
}
);
}
1 change: 0 additions & 1 deletion src/hdr_histogram.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
#include <stdlib.h>
#include <stdbool.h>
#include <math.h>
#include <x86intrin.h>
#include <assert.h>
#include <stdio.h>
#include <string.h>
Expand Down
5 changes: 5 additions & 0 deletions src/script.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
#include "stats.h"
#include "zmalloc.h"

// Compatibility with newer LuaJIT that uses luaL_Reg instead of luaL_reg
#ifndef luaL_reg
#define luaL_reg luaL_Reg
#endif

typedef struct {
char *name;
int type;
Expand Down
Loading