Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rust #66

Draft
wants to merge 15 commits into
base: master
Choose a base branch
from
Draft

Rust #66

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
33 changes: 33 additions & 0 deletions benchmarks/Rust/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Only allow specificly allowed things
# Can see what's ignored by this with: git status --ignored

# Deny everything by default
*

# Allow directories (directories with no tracked files don't get committed regardless)
!**/

# General git related root files
!/.gitignore
!/.gitattributes
!/.gitmodules

# Editor config files
!/.editorconfig

# License files
!LICENSE*
!COPYING*

# Documentation files
!*.md

# Rust Build files
!/Cargo.toml
!/Cargo.lock
!/rustfmt.toml

# Rust Source files
!/src/**/*.rs
!/test/**/*.rs
!/benches/**/*.rs
7 changes: 7 additions & 0 deletions benchmarks/Rust/Cargo.lock

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

8 changes: 8 additions & 0 deletions benchmarks/Rust/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "awfy"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
1 change: 1 addition & 0 deletions benchmarks/Rust/rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Use only defaults by leaving this blank
15 changes: 15 additions & 0 deletions benchmarks/Rust/src/benchmark.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use std::any::Any;

pub trait Benchmark {
fn benchmark(&self) -> Box<dyn Any>;
fn verify_result(&self, result: Box<dyn Any>) -> bool;

fn inner_benchmark_loop(&self, inner_iterations: usize) -> bool {
for _ in 0..inner_iterations {
if !self.verify_result(self.benchmark()) {
return false;
}
}
true
}
}
35 changes: 35 additions & 0 deletions benchmarks/Rust/src/bin/harness.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use awfy::run::Run;
use std::env::args;

fn process_arguments() -> Run {
let mut args = args();
args.next(); // Skip binary name
let mut run = Run::new(args.next().unwrap());
if let Some(arg) = args.next() {
run.set_num_iterations(arg.parse().unwrap());
if let Some(arg) = args.next() {
run.set_inner_iterations(arg.parse().unwrap());
}
}
run
}

fn print_usage() {
println!("Harness [benchmark] [num-iterations [inner-iter]]");
println!();
println!(" benchmark - benchmark class name ");
println!(" num-iterations - number of times to execute benchmark, default: 1");
println!(" inner-iter - number of times the benchmark is executed in an inner loop, ");
println!(" which is measured in total, default: 1");
}

fn main() {
if args().len() < 2 {
print_usage();
return;
}

let mut run = process_arguments();
run.run_benchmark();
run.print_total();
}
85 changes: 85 additions & 0 deletions benchmarks/Rust/src/bounce.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
use crate::benchmark::Benchmark;
use crate::som::random::Random;
use std::any::Any;

#[derive(Default)]
pub struct Bounce;

impl Benchmark for Bounce {
#[allow(clippy::items_after_statements)]
fn benchmark(&self) -> Box<dyn Any> {
let mut random = Random::default();

const BALL_COUNT: usize = 100;
let mut bounces = 0usize;
let mut balls = [Ball::default(); BALL_COUNT];
for ball in &mut balls {
*ball = Ball::new(&mut random);
}

for _i in 0..50 {
for ball in &mut balls {
if ball.bounce() {
bounces += 1;
}
}
}
Box::new(bounces)
}

fn verify_result(&self, result: Box<dyn Any>) -> bool {
if let Ok(result) = result.downcast::<usize>() {
1331 == *result
} else {
false
}
}
}

#[derive(Default, Debug, Clone, Copy)]
struct Ball {
x: i32,
y: i32,
x_vel: i32,
y_vel: i32,
}

impl Ball {
fn new(random: &mut Random) -> Self {
let x = random.next() % 500;
let y = random.next() % 500;
let x_vel = (random.next() % 300) - 150;
let y_vel = (random.next() % 300) - 150;
Ball { x, y, x_vel, y_vel }
}

fn bounce(&mut self) -> bool {
let x_limit = 500;
let y_limit = 500;
let mut bounced = false;

self.x += self.x_vel;
self.y += self.y_vel;
if self.x > x_limit {
self.x = x_limit;
self.x_vel = 0 - self.x_vel.abs();
bounced = true;
}
if self.x < 0 {
self.x = 0;
self.x_vel = self.x_vel.abs();
bounced = true;
}
if self.y > y_limit {
self.y = y_limit;
self.y_vel = 0 - self.y_vel.abs();
bounced = true;
}
if self.y < 0 {
self.y = 0;
self.y_vel = self.y_vel.abs();
bounced = true;
}
bounced
}
}
Loading