Skip to content
Open
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
5,812 changes: 4,531 additions & 1,281 deletions Cargo.lock

Large diffs are not rendered by default.

39 changes: 36 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
cp_macros = { path = "crates/cp_macros" }

anyhow = "1.0"
tracing = "0.1"
tracing-subscriber = { version = "0.3", features = ["smallvec", "parking_lot", "ansi", "local-time", "json"] }
Expand All @@ -21,7 +23,7 @@ hyper = { version = "0.14", features = ["full"] }
figment = { version = "0.10", features = ["env", "toml", "json"] }
byte-unit = { version = "4.0", default-features = false, features = ["std", "serde"] }
async-trait = "0.1"
sea-orm = { version = "0.10", features = ["sqlx-sqlite", "runtime-tokio-rustls", "macros"] }
sea-orm = { version = "0.11", features = ["sqlx-sqlite", "runtime-tokio-rustls", "macros"] }
sea-orm-migration = "0.10"
rand = { version = "0.8", features = ["std", "std_rng", "getrandom", "min_const_gen", "simd_support"] }
argon2 = { version = "0.4", features = ["std"] }
Expand All @@ -37,23 +39,54 @@ parking_lot = "0.12"
arc-swap = "1.5"
once_cell = "1.9"
tokio-graceful-shutdown = "0.12"
rocksdb = { version = "0.19", default-features = false, features = ["zstd"] }
# 与libv8冲突(重复定义符号), 考虑改用persy,lmdb,redis...
#rocksdb = { version = "0.20", default-features = false, features = ["zstd"] }
futures = "0.3"
async-graphql = { version = "5.0", features = ["tracing", "chrono", "smol_str", "tokio-sync"] }
sysinfo = "0.27"
fnv = "1.0"
crossbeam-utils = "0.8"
humantime-serde = "1"
dirs = "5.0"
reqwest = { version = "0.11", features = ["blocking", "json"] }

deno_core = { version = "0.179.0" }
deno_ast = { version = "0.25.0", features = ["transpiling"] }
deno_runtime = { version = "0.105.0" }

# 因为axum-sessions需要sha-1 = "^0.10", 但deno_ast需要sha-1 = "=0.10.0", 所以必须手动锁定版本
sha-1 = "=0.10.0"
url = "2.3.1"
rust-embed = { version = "6.6.1", features = [] }
persy = { version = "1.4.4", optional = true }
redb = "0.15.0"
heed = "0.11.0"

[patch.crates-io]
# 强行更改libsqlite3-sys版本为0.25
sqlx = { git = "https://github.com/BoxCatTeam/sqlx", branch = "libsqlite3-sys0.25" }

[dev-dependencies]
graphql_client = "0.11"
criterion = { version = "0.4.0", features = ["html_reports"] }

[features]
io_uring = ["rocksdb/io-uring"]
default = []
io_uring = []
persy = ["dep:persy"]

[profile.release]
lto = true
strip = true
codegen-units = 1
opt-level = 3
panic = "abort"

[[bench]]
name = "bench_kv_store"
harness = false

[workspace]
members = [
"crates/cp_macros"
]
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ todo
`cargo install cargo-outdated`

## 命令
- 运行测试

`cargo test -- --test-threads=1`

***因为部分测试依赖不同环境变量并且共用全局的设置/日志器,
所以并行测试可能会引起奇怪的错误***

- 调试运行

`cargo run`
Expand Down
68 changes: 68 additions & 0 deletions benches/bench_kv_store.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
use std::env::temp_dir;
use std::fs::{remove_dir_all, remove_file};

use criterion::{criterion_group, criterion_main, Criterion, black_box};
use rand::{Rng, thread_rng};

use cat_panel_backend::kv::{KVStore, LmdbStore, PersyStore, RedbStore};

fn bench_persy(c: &mut Criterion) {
let path = temp_dir().join("cp_bench_persy");
let mut store = PersyStore::new(&path).unwrap();
let mut rng = thread_rng();

let mut data = Vec::with_capacity(128);

for _ in 0..128 {
let i = rng.gen::<isize>().to_string();
store.insert(&i, i.as_bytes().repeat(1000)).unwrap();
data.push(i);
}

c.bench_function("kv-persy-read", |b| b.iter(|| {
let _ = black_box(store.get(black_box(data.get(rng.gen_range(0..128)).unwrap())));
}));
remove_file(path.with_extension("persy")).unwrap();
}

fn bench_lmdb(c: &mut Criterion) {
let path = temp_dir().join("cp_bench_lmdb");
let mut store = LmdbStore::new(&path).unwrap();
let mut rng = thread_rng();

let mut data = Vec::with_capacity(128);

for _ in 0..128 {
let i = rng.gen::<isize>().to_string();
store.insert(&i, i.as_bytes().repeat(1000)).unwrap();
data.push(i);
}

c.bench_function("kv-lmdb-read", |b| b.iter(|| {
let _ = black_box(store.get(black_box(data.get(rng.gen_range(0..128)).unwrap())));
}));
//drop(store);
//remove_dir_all(path.with_extension("mdb")).unwrap();
}

fn bench_redb(c: &mut Criterion) {
let path = temp_dir().join("cp_bench_redb");
let mut store = RedbStore::new(&path).unwrap();
let mut rng = thread_rng();

let mut data = Vec::with_capacity(128);

for _ in 0..128 {
let i = rng.gen::<isize>().to_string();
store.insert(&i, i.as_bytes().repeat(1000)).unwrap();
data.push(i);
}

c.bench_function("kv-redb-read", |b| b.iter(|| {
let _ = black_box(store.get(black_box(data.get(rng.gen_range(0..128)).unwrap())));
}));
remove_file(path.with_extension("redb")).unwrap();
}

criterion_group!(benches, bench_persy, bench_lmdb, bench_redb);
criterion_main!(benches);
51 changes: 51 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
use std::env;

fn git_commit_hash() -> String {
if let Ok(output) = std::process::Command::new("git")
.arg("rev-list")
.arg("-1")
.arg("HEAD")
.output()
{
if output.status.success() {
std::str::from_utf8(&output.stdout[..40])
.unwrap()
.to_string()
} else {
// When not in git repository
// (e.g. when the user install by `cargo install xx`)
"UNKNOWN".to_string()
}
} else {
// When there is no git command for some reason
"UNKNOWN".to_string()
}
}

fn main() {
println!("cargo:rustc-env=GIT_COMMIT_HASH={}", git_commit_hash());
println!("cargo:rerun-if-env-changed=GIT_COMMIT_HASH");
println!(
"cargo:rustc-env=GIT_COMMIT_HASH_SHORT={}",
&git_commit_hash()[..7]
);
println!("cargo:rustc-env=TARGET={}", env::var("TARGET").unwrap());
println!("cargo:rustc-env=PROFILE={}", env::var("PROFILE").unwrap());
// https://doc.rust-lang.org/reference/conditional-compilation.html#set-configuration-options
println!(
"cargo:rustc-env=TARGET_OS={}",
env::var("CARGO_CFG_TARGET_OS").unwrap()
);
println!(
"cargo:rustc-env=TARGET_ARCH={}",
env::var("CARGO_CFG_TARGET_ARCH").unwrap()
);
println!(
"cargo:rustc-env=TARGET_FAMILY={}",
env::var("CARGO_CFG_TARGET_FAMILY").unwrap()
);
println!(
"cargo:rustc-env=TARGET_ENV={}",
env::var("CARGO_CFG_TARGET_ENV").unwrap()
);
}
20 changes: 20 additions & 0 deletions components/catpanel.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/// <reference types="./lib.deno.d.ts" />

declare const cp_env: {
cp_version: string;
cp_git_hash: string;
target_os: string;
target_arch: string;
target_family: string;
target_env: string;
profile: string;
};

declare class Info {
name: string | (() => string | Promise<string>);
available_version: string[] | (() => string[] | Promise<string[]>);
}

declare function info(_: Info): void;

declare function installer(_: (version: string, target_dir: string) => Promise<boolean>): void;
27 changes: 27 additions & 0 deletions components/helper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/// <reference path="./catpanel.d.ts" />

import { ensureFile } from "https://deno.land/std/fs/ensure_file.ts";

async function downloadFile(src: string, dest: string) {
if (!(src.startsWith("http://") || src.startsWith("https://"))) {
throw new TypeError("URL must start with be http:// or https://");
}
const resp = await fetch(src);
if (!resp.ok) {
throw new Deno.errors.BadResource(
`Request failed with status ${resp.status}`,
);
} else if (!resp.body) {
throw new Deno.errors.UnexpectedEof(
`The download url ${src} doesn't contain a file to download`,
);
} else if (resp.status === 404) {
throw new Deno.errors.NotFound(
`The requested url "${src}" could not be found`,
);
}

await ensureFile(dest);
const file = await Deno.open(dest, { truncate: true, write: true });
await resp.body.pipeTo(file.writable);
}
Loading