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
52 changes: 52 additions & 0 deletions benchmarks/file_io/file_io.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main(int argc, char *argv[]) {
int n = argc > 1 ? atoi(argv[1]) : 1000000;

// Create temp file
char filename[] = "/tmp/barbell_file_io_XXXXXX";
int fd = mkstemp(filename);
if (fd < 0) {
perror("mkstemp");
return 1;
}
close(fd);

// Write n bytes
FILE *f = fopen(filename, "wb");
if (!f) {
perror("fopen write");
return 1;
}

unsigned char *buf = malloc(n);
for (int i = 0; i < n; i++) {
buf[i] = (unsigned char)(i % 256);
}
fwrite(buf, 1, n, f);
fclose(f);

// Read back and sum
f = fopen(filename, "rb");
if (!f) {
perror("fopen read");
return 1;
}

size_t bytes_read = fread(buf, 1, n, f);
fclose(f);

long sum = 0;
for (size_t i = 0; i < bytes_read; i++) {
sum += buf[i];
}

// Cleanup
free(buf);
unlink(filename);

printf("%ld\n", sum);
return 0;
}
49 changes: 49 additions & 0 deletions benchmarks/file_io/file_io.hml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// File I/O benchmark - Hemlock

fn parse_int(s) {
let result = 0;
let idx = 0;
while (idx < s.length) {
let ch = s[idx];
let code: i32 = ch;
let zero: i32 = '0';
result = result * 10 + (code - zero);
idx = idx + 1;
}
return result;
}

let n = 1000000;
if (args.length > 1) {
n = parse_int(args[1]);
}

// Create temp filename
let filename = "/tmp/barbell_file_io_hemlock.tmp";

// Build data to write (bytes 0-255 repeating)
let data = [];
let i = 0;
while (i < n) {
data.push(i % 256);
i = i + 1;
}

// Write to file
write_file(filename, data);

// Read back from file
let read_data = read_file(filename);

// Sum all bytes
let sum = 0;
i = 0;
while (i < read_data.length) {
sum = sum + read_data[i];
i = i + 1;
}

// Delete file
delete_file(filename);

print(sum);
27 changes: 27 additions & 0 deletions benchmarks/file_io/file_io.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const fs = require('fs');
const os = require('os');
const path = require('path');

const n = parseInt(process.argv[2]) || 1000000;

// Create temp file
const filename = path.join(os.tmpdir(), `barbell_file_io_${process.pid}_${Date.now()}`);

// Write n bytes
const buf = Buffer.alloc(n);
for (let i = 0; i < n; i++) {
buf[i] = i % 256;
}
fs.writeFileSync(filename, buf);

// Read back and sum
const readBuf = fs.readFileSync(filename);
let sum = 0;
for (let i = 0; i < readBuf.length; i++) {
sum += readBuf[i];
}

// Cleanup
fs.unlinkSync(filename);

console.log(sum);
24 changes: 24 additions & 0 deletions benchmarks/file_io/file_io.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import sys
import os
import tempfile

n = int(sys.argv[1]) if len(sys.argv) > 1 else 1000000

# Create temp file
fd, filename = tempfile.mkstemp(prefix='barbell_file_io_')
os.close(fd)

try:
# Write n bytes
data = bytes(i % 256 for i in range(n))
with open(filename, 'wb') as f:
f.write(data)

# Read back and sum
with open(filename, 'rb') as f:
read_data = f.read()

total = sum(read_data)
print(total)
finally:
os.unlink(filename)
22 changes: 22 additions & 0 deletions benchmarks/file_io/file_io.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
require 'tempfile'

n = ARGV[0] ? ARGV[0].to_i : 1000000

# Create temp file
file = Tempfile.new('barbell_file_io_')
filename = file.path
file.close

begin
# Write n bytes
data = (0...n).map { |i| (i % 256).chr }.join.b
File.binwrite(filename, data)

# Read back and sum
read_data = File.binread(filename)
total = read_data.bytes.sum

puts total
ensure
File.unlink(filename) if File.exist?(filename)
end
7 changes: 5 additions & 2 deletions run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ usage() {
echo " --iter N Number of iterations (default: 3)"
echo " --help, -h Show this help"
echo ""
echo "Benchmarks: fib, array_sum, string_concat, primes_sieve"
echo "Benchmarks: fib, array_sum, string_concat, primes_sieve, file_io"
echo " (leave empty to run all)"
}

Expand Down Expand Up @@ -72,6 +72,9 @@ get_input() {
primes_sieve)
[[ $QUICK_MODE -eq 1 ]] && echo 100000 || echo 1000000
;;
file_io)
[[ $QUICK_MODE -eq 1 ]] && echo 100000 || echo 1000000
;;
esac
}

Expand Down Expand Up @@ -195,7 +198,7 @@ run_all() {
if [[ -n "$BENCHMARK" ]]; then
benchmarks="$BENCHMARK"
else
benchmarks="fib array_sum string_concat primes_sieve"
benchmarks="fib array_sum string_concat primes_sieve file_io"
fi

local languages="c hemlockc hemlock python javascript ruby"
Expand Down