1
0
Fork 0
mirror of https://github.com/zigzap/zap.git synced 2025-10-20 23:24:09 +00:00

simplify rust native threads version, remove useless mutexes from there

This commit is contained in:
Alex Pyattaev 2023-08-22 00:24:39 +03:00
parent 4ad6cd9a4e
commit 53eed55e4c
4 changed files with 70 additions and 59 deletions

View file

@ -6,6 +6,9 @@ DURATION_SECONDS=10
SUBJECT=$1 SUBJECT=$1
TSK_SRV="taskset -c 0,1,2,3"
TSK_LOAD="taskset -c 4,5,6,7"
if [ "$SUBJECT" = "" ] ; then if [ "$SUBJECT" = "" ] ; then
echo "usage: $0 subject # subject: zig or go" echo "usage: $0 subject # subject: zig or go"
exit 1 exit 1
@ -13,54 +16,54 @@ fi
if [ "$SUBJECT" = "zig" ] ; then if [ "$SUBJECT" = "zig" ] ; then
zig build -Doptimize=ReleaseFast wrk > /dev/null zig build -Doptimize=ReleaseFast wrk > /dev/null
./zig-out/bin/wrk & $TSK_SRV ./zig-out/bin/wrk &
PID=$! PID=$!
URL=http://127.0.0.1:3000 URL=http://127.0.0.1:3000
fi fi
if [ "$SUBJECT" = "zigstd" ] ; then if [ "$SUBJECT" = "zigstd" ] ; then
zig build -Doptimize=ReleaseFast wrk_zigstd > /dev/null zig build -Doptimize=ReleaseFast wrk_zigstd > /dev/null
./zig-out/bin/wrk_zigstd & $TSK_SRV ./zig-out/bin/wrk_zigstd &
PID=$! PID=$!
URL=http://127.0.0.1:3000 URL=http://127.0.0.1:3000
fi fi
if [ "$SUBJECT" = "go" ] ; then if [ "$SUBJECT" = "go" ] ; then
cd wrk/go && go build main.go cd wrk/go && go build main.go
./main & $TSK_SRV ./main &
PID=$! PID=$!
URL=http://127.0.0.1:8090/hello URL=http://127.0.0.1:8090/hello
fi fi
if [ "$SUBJECT" = "python" ] ; then if [ "$SUBJECT" = "python" ] ; then
python wrk/python/main.py & $TSK_SRV python wrk/python/main.py &
PID=$! PID=$!
URL=http://127.0.0.1:8080 URL=http://127.0.0.1:8080
fi fi
if [ "$SUBJECT" = "sanic" ] ; then if [ "$SUBJECT" = "sanic" ] ; then
python wrk/sanic/sanic-app.py & $TSK_SRV python wrk/sanic/sanic-app.py &
PID=$! PID=$!
URL=http://127.0.0.1:8000 URL=http://127.0.0.1:8000
fi fi
if [ "$SUBJECT" = "rust" ] ; then if [ "$SUBJECT" = "rust" ] ; then
cd wrk/rust/hello && cargo build --release cd wrk/rust/hello && cargo build --release
./target/release/hello & $TSK_SRV ./target/release/hello &
PID=$! PID=$!
URL=http://127.0.0.1:7878 URL=http://127.0.0.1:7878
fi fi
if [ "$SUBJECT" = "axum" ] ; then if [ "$SUBJECT" = "axum" ] ; then
cd wrk/axum/hello-axum && cargo build --release cd wrk/axum/hello-axum && cargo build --release
./target/release/hello-axum & $TSK_SRV ./target/release/hello-axum &
PID=$! PID=$!
URL=http://127.0.0.1:3000 URL=http://127.0.0.1:3000
fi fi
if [ "$SUBJECT" = "csharp" ] ; then if [ "$SUBJECT" = "csharp" ] ; then
cd wrk/csharp && dotnet publish csharp.csproj -o ./out cd wrk/csharp && dotnet publish csharp.csproj -o ./out
./out/csharp --urls "http://127.0.0.1:5026" & $TSK_SRV ./out/csharp --urls "http://127.0.0.1:5026" &
PID=$! PID=$!
URL=http://127.0.0.1:5026 URL=http://127.0.0.1:5026
fi fi
@ -69,7 +72,7 @@ sleep 1
echo "========================================================================" echo "========================================================================"
echo " $SUBJECT" echo " $SUBJECT"
echo "========================================================================" echo "========================================================================"
wrk -c $CONNECTIONS -t $THREADS -d $DURATION_SECONDS --latency $URL $TSK_LOAD wrk -c $CONNECTIONS -t $THREADS -d $DURATION_SECONDS --latency $URL
kill $PID kill $PID

View file

@ -6,3 +6,4 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
# crossbeam = { version = "0.8.2", features = ["crossbeam-channel"] }

View file

@ -1,14 +1,20 @@
use std::{ //Crossbeam should, but does not make this faster.
sync::{mpsc, Arc, Mutex}, //use crossbeam::channel::bounded;
thread, use std::{net::TcpStream, sync::mpsc, thread};
}; type Job = (fn(TcpStream), TcpStream);
type Sender = mpsc::Sender<Job>;
//type Sender = crossbeam::channel::Sender<Job>;
type Receiver = mpsc::Receiver<Job>;
//type Receiver = crossbeam::channel::Receiver<Job>;
pub struct ThreadPool { pub struct ThreadPool {
workers: Vec<Worker>, workers: Vec<Worker>,
sender: Option<mpsc::Sender<Job>>, senders: Vec<Sender>,
}
type Job = Box<dyn FnOnce() + Send + 'static>; next_sender: usize,
}
impl ThreadPool { impl ThreadPool {
/// Create a new ThreadPool. /// Create a new ThreadPool.
@ -21,39 +27,40 @@ impl ThreadPool {
pub fn new(size: usize) -> ThreadPool { pub fn new(size: usize) -> ThreadPool {
assert!(size > 0); assert!(size > 0);
let (sender, receiver) = mpsc::channel();
let receiver = Arc::new(Mutex::new(receiver));
let mut workers = Vec::with_capacity(size); let mut workers = Vec::with_capacity(size);
let mut senders = Vec::with_capacity(size);
for id in 0..size { for id in 0..size {
workers.push(Worker::new(id, Arc::clone(&receiver))); //let (sender, receiver) = bounded(2);
let (sender, receiver) = mpsc::channel();
senders.push(sender);
workers.push(Worker::new(id, receiver));
} }
ThreadPool { ThreadPool {
workers, workers,
sender: Some(sender), senders,
next_sender: 0,
} }
} }
/// round robin over available workers to ensure we never have to buffer requests
pub fn execute<F>(&self, f: F) pub fn execute(&mut self, handler: fn(TcpStream), stream: TcpStream) {
where let job = (handler, stream);
F: FnOnce() + Send + 'static, self.senders[self.next_sender].send(job).unwrap();
{ //self.senders[self.next_sender].try_send(job).unwrap();
let job = Box::new(f); self.next_sender += 1;
if self.next_sender == self.senders.len() {
self.sender.as_ref().unwrap().send(job).unwrap(); self.next_sender = 0;
}
} }
} }
impl Drop for ThreadPool { impl Drop for ThreadPool {
fn drop(&mut self) { fn drop(&mut self) {
drop(self.sender.take()); self.senders.clear();
for worker in &mut self.workers { for worker in &mut self.workers {
println!("Shutting down worker {}", worker.id); println!("Shutting down worker {}", worker.id);
if let Some(thread) = worker.thread.take() { if let Some(thread) = worker.thread.take() {
thread.join().unwrap(); thread.join().unwrap();
} }
@ -67,26 +74,28 @@ struct Worker {
} }
impl Worker { impl Worker {
fn new(id: usize, receiver: Arc<Mutex<mpsc::Receiver<Job>>>) -> Worker { fn new(id: usize, receiver: Receiver) -> Worker {
let thread = thread::spawn(move || loop { let thread = thread::spawn(move || Self::work(receiver));
let message = receiver.lock().unwrap().recv();
match message {
Ok(job) => {
// println!("Worker got a job; executing.");
job();
}
Err(_) => {
// println!("Worker disconnected; shutting down.");
break;
}
}
});
Worker { Worker {
id, id,
thread: Some(thread), thread: Some(thread),
} }
} }
fn work(receiver: Receiver) {
loop {
let message = receiver.recv();
match message {
Ok((handler, stream)) => {
// println!("Worker got a job; executing.");
handler(stream);
}
Err(_) => {
// println!("Worker disconnected; shutting down.");
break;
}
}
}
}
} }

View file

@ -1,31 +1,30 @@
use hello::ThreadPool; use hello::ThreadPool;
// use std::fs;
use std::io::prelude::*; use std::io::prelude::*;
use std::net::TcpListener; use std::net::TcpListener;
use std::net::TcpStream; use std::net::TcpStream;
// use std::thread;
// use std::time::Duration;
fn main() { fn main() {
let listener = TcpListener::bind("127.0.0.1:7878").unwrap(); let listener = TcpListener::bind("127.0.0.1:7878").unwrap();
let pool = ThreadPool::new(4); //Creating a massive amount of threads so we can always have one ready to go.
let mut pool = ThreadPool::new(128);
// for stream in listener.incoming().take(2) { // for stream in listener.incoming().take(2) {
for stream in listener.incoming() { for stream in listener.incoming() {
let stream = stream.unwrap(); let stream = stream.unwrap();
//handle_connection(stream);
pool.execute(|| { pool.execute(handle_connection, stream);
handle_connection(stream);
});
} }
println!("Shutting down."); println!("Shutting down.");
} }
fn handle_connection(mut stream: TcpStream) { fn handle_connection(mut stream: TcpStream) {
stream.set_nodelay(true).expect("set_nodelay call failed");
let mut buffer = [0; 1024]; let mut buffer = [0; 1024];
stream.read(&mut buffer).unwrap(); let nbytes = stream.read(&mut buffer).unwrap();
if nbytes == 0 {
return;
}
let status_line = "HTTP/1.1 200 OK"; let status_line = "HTTP/1.1 200 OK";
@ -39,5 +38,4 @@ fn handle_connection(mut stream: TcpStream) {
); );
stream.write_all(response.as_bytes()).unwrap(); stream.write_all(response.as_bytes()).unwrap();
stream.flush().unwrap();
} }