Skip to content

Instantly share code, notes, and snippets.

@bloodeagle40234
Last active June 29, 2021 07:53
Show Gist options
  • Save bloodeagle40234/5fd694af2171fbedb025013839bf0516 to your computer and use it in GitHub Desktop.
Save bloodeagle40234/5fd694af2171fbedb025013839bf0516 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"os"
"math/rand"
"strconv"
"time"
)
var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
var DISK_PATH = "/mnt/sdb"
var TIMES = 100 * 1024
func RandString(n int) string {
b := make([]rune, n)
for i := range b {
b[i] = letters[rand.Intn(len(letters))]
}
return string(b)
}
func Benchmark(disknum int, body string, times int, c chan int) {
write_dir := DISK_PATH + strconv.Itoa(disknum)
output_path := write_dir + "/" + "output" + "/" + "golang" + strconv.Itoa(disknum)
fmt.Println(output_path)
f, err := os.Create(output_path);
defer f.Close()
if err != nil {
return
}
for i := 0; i < times; i++ {
_, err = f.Write([]byte(body))
if err != nil {
return
}
err = f.Sync()
if err != nil {
return
}
}
close(c)
}
func main(){
num := 1
c := make(chan int, num)
body := RandString(1024)
start := time.Now()
for i := 1; i < num + 1; i++ {
go Benchmark(i, body, TIMES, c)
}
for i := range c {
_ = i
}
end := time.Now()
fmt.Printf("total time: %f\n", (end.Sub(start)).Seconds())
}
import eventlet
import eventlet.tpool
eventlet.monkey_patch()
import os.path
import random
import string
import time
import os
DISK_PATH = "/mnt/sdb"
TIMES = 100 * 1024
def strgen(size):
randlist = [random.choice(string.ascii_letters + string.digits) for x in range(size)]
return "".join(randlist)
def benchmark(disk_num, body, times):
write_dir = "%s%s" % (DISK_PATH, str(disk_num))
output_path = os.path.join(write_dir, "output", "python%s" % disk_num)
print(output_path)
with open(output_path, "w") as f:
for x in range(times):
f.write(body)
f.flush()
os.fsync(f.fileno())
# eventlet.sleep()
if __name__ == "__main__":
body = strgen(1024)
start = time.time()
parallel = 1
pool = eventlet.greenpool.GreenPool(parallel)
for x in range(1, parallel + 1):
pool.spawn_n(eventlet.tpool.execute, benchmark, x, body, TIMES)
pool.waitall()
print("total time: %s" % (time.time() - start))
use threadpool::ThreadPool;
use std::fs::{File, create_dir_all};
use std::io;
use std::io::prelude::*;
use rand::{thread_rng, Rng};
use rand::distributions::Alphanumeric;
use std:: time::Instant;
use once_cell::sync::Lazy;
static DISK_PATH: &str = "/mnt/sdb";
static TIMES: usize = 100 * 1024;
static BODY: Lazy<String> = Lazy::new(|| {
thread_rng()
.sample_iter(&Alphanumeric)
.take(1024)
.map(char::from)
.collect()
});
fn benchmark(disk_num: String, times: usize) -> std::io::Result<()> {
let dir = format!("{}{}", DISK_PATH, disk_num);
let output_path = format!("{}/output/rust{}", dir, disk_num);
create_dir_all(dir).unwrap();
println!("{}", output_path);
let mut output = File::create(&output_path)?;
for _x in 0..times {
output.write_all(BODY.as_bytes())?;
output.sync_data()?;
}
Ok(())
}
fn main() {
let start = Instant::now();
let parallel = 1;
let tpool = ThreadPool::new(parallel);
for x in 1..parallel+1 {
let disknum = x.to_string();
tpool.execute(|| { benchmark(disknum, TIMES).unwrap() });
}
tpool.join();
let end = start.elapsed();
println!("total time: {}", end.as_secs_f32());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment