Skip to content

Commit

Permalink
remove scratch files
Browse files Browse the repository at this point in the history
  • Loading branch information
mrattle committed Aug 28, 2024
1 parent df84c73 commit 29b2adb
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 1,509 deletions.
53 changes: 44 additions & 9 deletions benches/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ fn bench_get(c: &mut Criterion) {
});
}

fn bench_set_with_string(c: &mut Criterion) {
fn bench_parsed_set_with_string(c: &mut Criterion) {
let rt = Runtime::new().unwrap();

c.bench_function("set_small_string_with_parse_input", |b| {
c.bench_function("new_set_small_with_string", |b| {
b.to_async(&rt).iter_custom(|iters| async move {
let mut client = setup_client().await;
let start = std::time::Instant::now();
Expand All @@ -48,10 +48,10 @@ fn bench_set_with_string(c: &mut Criterion) {
});
}

fn bench_set_with_u64(c: &mut Criterion) {
fn bench_parsed_set_with_u64(c: &mut Criterion) {
let rt = Runtime::new().unwrap();

c.bench_function("set_small_int", |b| {
c.bench_function("new_set_small_with_int", |b| {
b.to_async(&rt).iter_custom(|iters| async move {
let mut client = setup_client().await;
let start = std::time::Instant::now();
Expand All @@ -63,6 +63,21 @@ fn bench_set_with_u64(c: &mut Criterion) {
});
}

fn bench_original_set_with_string(c: &mut Criterion) {
let rt = Runtime::new().unwrap();

c.bench_function("original_set_small_string", |b| {
b.to_async(&rt).iter_custom(|iters| async move {
let mut client = setup_client().await;
let start = std::time::Instant::now();
for _ in 0..iters {
let _ = client.original_set("foo", "bar", None, None).await;
}
start.elapsed()
});
});
}

fn bench_get_many(c: &mut Criterion) {
let rt = Runtime::new().unwrap();
let keys = &["foo", "bar", "baz"];
Expand All @@ -89,10 +104,10 @@ fn bench_get_many(c: &mut Criterion) {
});
}

fn bench_set_large(c: &mut Criterion) {
fn bench_new_set_large_with_string(c: &mut Criterion) {
let rt = Runtime::new().unwrap();

c.bench_function("set_large", |b| {
c.bench_function("new_set_large_with_string", |b| {
b.to_async(&rt).iter_custom(|iters| async move {
let mut client = setup_client().await;
let large_payload = "a".repeat(LARGE_PAYLOAD_SIZE);
Expand All @@ -107,6 +122,24 @@ fn bench_set_large(c: &mut Criterion) {
});
}

fn bench_original_set_large_with_string(c: &mut Criterion) {
let rt = Runtime::new().unwrap();

c.bench_function("original_set_large_with_string", |b| {
b.to_async(&rt).iter_custom(|iters| async move {
let mut client = setup_client().await;
let large_payload = "a".repeat(LARGE_PAYLOAD_SIZE);
let start = std::time::Instant::now();
for _ in 0..iters {
let _ = client
.original_set("large_foo", large_payload.as_str(), None, None)
.await;
}
start.elapsed()
});
});
}

fn bench_get_large(c: &mut Criterion) {
let rt = Runtime::new().unwrap();
let large_payload = "a".repeat(LARGE_PAYLOAD_SIZE);
Expand Down Expand Up @@ -181,10 +214,12 @@ fn bench_increment(c: &mut Criterion) {
criterion_group!(
benches,
bench_get,
bench_set_with_string,
bench_set_with_u64,
bench_parsed_set_with_string,
bench_parsed_set_with_u64,
bench_original_set_with_string,
bench_get_many,
bench_set_large,
bench_new_set_large_with_string,
bench_original_set_large_with_string,
bench_get_large,
bench_get_many_large,
bench_increment,
Expand Down
45 changes: 45 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,51 @@ impl Client {
}
}

/// Sets the given key.
///
/// If `ttl` or `flags` are not specified, they will default to 0. If the value is set
/// successfully, `()` is returned, otherwise [`Error`] is returned.
pub async fn original_set<K, V>(
&mut self,
key: K,
value: V,
ttl: Option<i64>,
flags: Option<u32>,
) -> Result<(), Error>
where
K: AsRef<[u8]>,
V: AsRef<[u8]>,
{
let kr = key.as_ref();
let vr = value.as_ref();

self.conn.write_all(b"set ").await?;
self.conn.write_all(kr).await?;

let flags = flags.unwrap_or(0).to_string();
self.conn.write_all(b" ").await?;
self.conn.write_all(flags.as_ref()).await?;

let ttl = ttl.unwrap_or(0).to_string();
self.conn.write_all(b" ").await?;
self.conn.write_all(ttl.as_ref()).await?;

self.conn.write_all(b" ").await?;
let vlen = vr.len().to_string();
self.conn.write_all(vlen.as_ref()).await?;
self.conn.write_all(b"\r\n").await?;

self.conn.write_all(vr).await?;
self.conn.write_all(b"\r\n").await?;
self.conn.flush().await?;

match self.get_read_write_response().await? {
Response::Status(Status::Stored) => Ok(()),
Response::Status(s) => Err(s.into()),
_ => Err(Status::Error(ErrorKind::Protocol(None)).into()),
}
}

/// Add a key. If the value exists, Err(Protocol(NotStored)) is returned.
pub async fn add<K, V>(
&mut self,
Expand Down
Loading

0 comments on commit 29b2adb

Please sign in to comment.