Skip to content

Commit

Permalink
Performance tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ParfenovIgor committed Jan 4, 2025
1 parent c31a0ae commit 922d072
Show file tree
Hide file tree
Showing 8 changed files with 150 additions and 10 deletions.
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
BUILD_DIR=$(abspath build)

.PHONY: compiler arch stdlib altlib test clean
.PHONY: compiler arch stdlib altlib test perftest clean

compiler: arch stdlib
$(MAKE) -C compiler BUILD_DIR=$(BUILD_DIR)
Expand All @@ -17,5 +17,8 @@ altlib: compiler
test: compiler altlib
$(MAKE) -C test BUILD_DIR=$(BUILD_DIR)

perftest: compiler altlib
$(MAKE) -C test perf BUILD_DIR=$(BUILD_DIR)

clean:
rm -r build
6 changes: 5 additions & 1 deletion altlib/stdlib.al
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,9 @@ func ^.atoi_(str #1C) -> #I {
//* Gets a seed `seed` and return the next pseudorandom integer.
func ^.rand_(seed #I) -> #I {
seed := seed * 1103515245 + 12345
return (seed / 65536) % 32768
seed := (seed / 65536) % 32768
eval if (seed < 0) {
seed := seed + 32768
}
return seed
}
6 changes: 5 additions & 1 deletion docs/altlibref.html
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,11 @@ <h3 id="6.3">rand_</h3>
Gets a seed <code>seed</code> and return the next pseudorandom integer.
<pre><code>func ^.rand_(seed #I) -> #I {
seed := seed * 1103515245 + 12345
return (seed / 65536) % 32768
seed := (seed / 65536) % 32768
eval if (seed &lt 0) {
seed := seed + 32768
}
return seed
}
</code></pre>
<h2 id="7">string</h2>
Expand Down
8 changes: 6 additions & 2 deletions stdlib/src/stdlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ char *_itoa(int n) {
heap_t *heap;

void _init_malloc() {
int heap_size = 0x100000;
int heap_size = 0x10000000;
int heap_index_size = 0x1000;
heap = create_heap(heap_size, heap_index_size);
}
Expand All @@ -62,5 +62,9 @@ void _free(void *ptr) {

int _rand(int seed) {
seed = seed * 1103515245 + 12345;
return (seed / 65536) % 32768;
seed = (seed / 65536) % 32768;
if (seed < 0) {
seed += 32768;
}
return seed;
}
13 changes: 8 additions & 5 deletions test/Makefile
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
BUILD_DIR=$(abspath build)

.PHONY: algo unit integration
.PHONY: algo integration perf unit

all: algo unit integration
all: algo integration unit

algo:
$(MAKE) -C algo BUILD_DIR=$(BUILD_DIR)

unit:
$(MAKE) -C unit BUILD_DIR=$(BUILD_DIR)

integration:
$(MAKE) -C integration BUILD_DIR=$(BUILD_DIR)

perf:
$(MAKE) -C perf BUILD_DIR=$(BUILD_DIR)

unit:
$(MAKE) -C unit BUILD_DIR=$(BUILD_DIR)
26 changes: 26 additions & 0 deletions test/perf/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
ALFLAGS=-i altlib ../../altlib/
CFLAGS=-g -fno-stack-protector -I include -I ../../stdlib/include
LDFLAGS=-z noexecstack

SRCS_AL := $(wildcard *.al)
EXES_AL := $(patsubst %.al, $(BUILD_DIR)/test/perf/%, $(SRCS_AL))

OBJS_ARCH := $(wildcard $(BUILD_DIR)/arch/*.o)
OBJS_ALTLIB := $(wildcard $(BUILD_DIR)/altlib/*.o)
OBJS_STDLIB := $(wildcard $(BUILD_DIR)/stdlib/*.o)

all: $(EXES_AL)

$(BUILD_DIR)/test/perf/%: %.al make_dir
echo $(patsubst %.al, %, $<) > $(patsubst %, %_report, $@)

$(BUILD_DIR)/calias -a $(ALFLAGS) $< -o $(patsubst %, %1.o, $@)
ld $(LDFLAGS) -o $(patsubst %, %1, $@) $(patsubst %, %1.o, $@) $(OBJS_ARCH) $(OBJS_ALTLIB)
{ time -p $(patsubst %, %1, $@) ; } 2>&1 | grep real | awk '{print $$2}' >> $(patsubst %, %_report, $@)

gcc $(CFLAGS) -c $(patsubst %.al, %.c, $<) -o $(patsubst %, %2.o, $@)
gcc $(LDFLAGS) -o $(patsubst %, %2, $@) $(patsubst %, %2.o, $@) $(OBJS_ARCH) $(OBJS_STDLIB)
{ time -p $(patsubst %, %2, $@) ; } 2>&1 | grep real | awk '{print $$2}' >> $(patsubst %, %_report, $@)

make_dir:
mkdir -p $(BUILD_DIR)/test/perf
52 changes: 52 additions & 0 deletions test/perf/merge_sort.al
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
include altlib."posix.al"
include altlib."stdio.al"
include altlib."test_allocator.al"

func .merge_sort(a #1I, lx #I, rx #I, allocator #1TestAllocator) -> #V {
eval if (rx - lx > 1) {
def m := (lx + rx) / 2
eval merge_sort(a, lx, m, allocator)
eval merge_sort(a, m, rx, allocator)
def b := allocator.alloc((rx - lx) * $#I)
def ptr1 := lx
def ptr2 := m
def ptr3 := 0
eval while (ptr3 < rx - lx) {
eval if (ptr1 < m and (ptr2 = rx or a[ptr1] < a[ptr2])) {
b[ptr3]& <- a[ptr1]
ptr1 := ptr1 + 1
}
else {
b[ptr3]& <- a[ptr2]
ptr2 := ptr2 + 1
}
ptr3 := ptr3 + 1
}
ptr3 := 0
eval while (ptr3 < rx - lx) {
a[lx + ptr3]& <- b[ptr3]
ptr3 := ptr3 + 1
}
}
}

func ^._start() -> #V {
def allocator #TestAllocator
eval allocator&.init(1024 * 1024 * 1024)
defer eval allocator&.deinit()

def n := 1000000
def a := allocator&.alloc(n * $#I) as #1I
def i := 0
def seed := 123
eval while (i < n) {
a[i]& <- rand_(seed)
seed := a[i]
i := i + 1
}

eval merge_sort(a, 0, n, allocator&)

eval posix_exit(0)
}
44 changes: 44 additions & 0 deletions test/perf/merge_sort.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include <stdio.h>
#include <stdlib.h>

void merge_sort(int *a, int lx, int rx) {
if (rx - lx > 1) {
int m = (lx + rx) / 2;
merge_sort(a, lx, m);
merge_sort(a, m, rx);

int *b = _malloc((rx - lx) * sizeof(int));
int ptr1 = lx, ptr2 = m, ptr3 = 0;
while (ptr3 < rx - lx) {
if (ptr1 < m && (ptr2 == rx || a[ptr1] < a[ptr2])) {
b[ptr3] = a[ptr1];
ptr1 += 1;
}
else {
b[ptr3] = a[ptr2];
ptr2++;
}
ptr3++;
}
ptr3 = 0;
while (ptr3 < rx - lx) {
a[lx + ptr3] = b[ptr3];
ptr3++;
}
}
}

int main() {
int n = 1000000;
int *a = _malloc(n * sizeof(int));
int seed = 123;
for (int i = 0; i < n; i++) {
a[i] = _rand(seed);
seed = a[i];
}

merge_sort(a, 0, n);
for(int i=0;i<n;i++) _puti(a[i]);

return 0;
}

0 comments on commit 922d072

Please sign in to comment.