Skip to content

Commit

Permalink
add bench/twitter
Browse files Browse the repository at this point in the history
in an attempt to verify benchmarks, the new cpp and zig files now loop
over statuses in twitter.json and check that there are exactly 100.

running bench/twitter/run.sh
  * builds zig-out/bin/twitterbench
  * fetches simdjson.cpp/.h and twitter.json
  * builds bench/twitter/simdjzon-twitterbench
  * benchmarks them w/ poop
  • Loading branch information
travisstaloch committed Feb 3, 2024
1 parent 174b7f1 commit aeab8cd
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 0 deletions.
4 changes: 4 additions & 0 deletions bench/twitter/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
simdjson-bench
simdjson.cpp
simdjson.h
twitter.json
28 changes: 28 additions & 0 deletions bench/twitter/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include "simdjson.h"
using namespace simdjson;
int main(int argc, char** argv) {
if(argc != 2) {
std::cout << "USAGE: ./simdjson <file.json>" << std::endl;
exit(1);
}
dom::parser parser;
try
{
const dom::element doc = parser.load(argv[1]);
int i = 0;
for(auto status: doc["statuses"]) {
// std::cout << status["id"] << '\n';
i += 1;
}
if(i != 100) {
std::cout << "error. expected i=100. found i=" << i << '\n';
return 1;
}
}
catch(const std::exception& e)
{
std::cerr << e.what() << '\n';
return 1;
}
return 0;
}
33 changes: 33 additions & 0 deletions bench/twitter/main.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const std = @import("std");
const dom = @import("simdjzon").dom;

pub fn main() !u8 {
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer arena.deinit();
const allocator = arena.allocator();
const args = try std.process.argsAlloc(allocator);
if(args.len != 2) {
std.debug.print("USAGE: ./simdjson <file.json>\n", .{});
}
var parser = try dom.Parser.initFile(allocator, args[1], .{});
defer parser.deinit();
parser.parse() catch |err| {
std.log.err("parse failed. {s}", .{@errorName(err)});
return 1;
};
const statuses = try parser.element().at_pointer("/statuses");
const array = try statuses.get_array();
var i: usize = 0;
while(array.at(i)) |status| : (i += 1) {
_ = status; // autofix
// const id = try status.at_pointer("/id");
// std.debug.print("{}\n", .{try id.get_int64()});
}
// std.debug.print("i={}\n", .{i});
if (i != 100) {
std.debug.print("error. expected i=100. found i={}\n", .{i});
return 1;
}

return 0;
}
19 changes: 19 additions & 0 deletions bench/twitter/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/bash
set -xe

rm -f simdjson.cpp simdjson.h twitter.json simdjson-twitterbench

# build simdjzon
zig version
zig build -Doptimize=ReleaseFast

# build simdjson
g++ --version
cat bench/twitter/main.cpp
wget https://github.com/simdjson/simdjson/raw/master/singleheader/simdjson.cpp -O bench/twitter/simdjson.cpp
wget https://github.com/simdjson/simdjson/raw/master/singleheader/simdjson.h -O bench/twitter/simdjson.h
wget https://github.com/simdjson/simdjson/raw/master/jsonexamples/twitter.json -O bench/twitter/twitter.json
g++ bench/twitter/main.cpp bench/twitter/simdjson.cpp -Ibench/twitter -o bench/twitter/simdjson-twitterbench -O3 -march=native

# bench
poop "./bench/twitter/simdjson-twitterbench bench/twitter/twitter.json" "zig-out/bin/twitterbench bench/twitter/twitter.json"
14 changes: 14 additions & 0 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,18 @@ pub fn build(b: *std.Build) void {

const run_step = b.step("run", "Run the app");
run_step.dependOn(&run_cmd.step);

const twitterbench = b.addExecutable(.{
.name = "twitterbench",
.root_source_file = .{ .path = "bench/twitter/main.zig" },
.target = target,
.optimize = optimize,
});
twitterbench.root_module.addImport("simdjzon", mod);
b.installArtifact(twitterbench);
const run_twitter_bench = b.addRunArtifact(twitterbench);
if (b.args) |args| run_twitter_bench.addArgs(args);
const run_twitter_bench_step = b.step("twitter-bench", "Run the twitter-bench");
run_twitter_bench_step.dependOn(&run_twitter_bench.step);

}

0 comments on commit aeab8cd

Please sign in to comment.