Skip to content
Merged
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
85 changes: 39 additions & 46 deletions benches/benchmarks.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
use criterion::{black_box, criterion_group, criterion_main, Criterion, BenchmarkId};
use key_cycle_proxy::{
config::{ApiKeyInfo, UpstreamConfig},
proxy::{KeyPool, ProxyEngine, ProxyHandler, UpstreamClient},
routes::create_router,
types::OpenAIRequest,
};
use criterion::{black_box, criterion_group, criterion_main, BenchmarkId, Criterion};
use key_cycle_proxy::{config::ApiKeyInfo, proxy::KeyPool, types::OpenAIRequest};
use secrecy::SecretString;
use serde_json::json;
use std::{sync::Arc, time::Duration};
use tokio::runtime::Runtime;
use wiremock::{matchers::method, Mock, MockServer, ResponseTemplate};

fn create_test_keys(count: usize) -> Vec<ApiKeyInfo> {
(0..count)
Expand All @@ -25,48 +19,41 @@ fn create_test_keys(count: usize) -> Vec<ApiKeyInfo> {

fn bench_key_selection(c: &mut Criterion) {
let mut group = c.benchmark_group("key_selection");

for key_count in [5, 10, 25, 50, 100].iter() {
let keys = create_test_keys(*key_count);
let pool = KeyPool::new(keys, "round_robin");

group.bench_with_input(
BenchmarkId::new("round_robin", key_count),
key_count,
|b, _| {
b.iter(|| {
black_box(pool.get_key_for_model("gpt-3.5-turbo"))
})
},
|b, _| b.iter(|| black_box(pool.get_key_for_model("gpt-3.5-turbo"))),
);

let keys = create_test_keys(*key_count);
let pool = KeyPool::new(keys, "least_latency");

group.bench_with_input(
BenchmarkId::new("least_latency", key_count),
key_count,
|b, _| {
b.iter(|| {
black_box(pool.get_key_for_model("gpt-3.5-turbo"))
})
},
|b, _| b.iter(|| black_box(pool.get_key_for_model("gpt-3.5-turbo"))),
);
}

group.finish();
}

fn bench_json_parsing(c: &mut Criterion) {
let mut group = c.benchmark_group("json_parsing");

let simple_request = json!({
"model": "gpt-3.5-turbo",
"messages": [
{"role": "user", "content": "Hello!"}
]
}).to_string();

})
.to_string();

let complex_request = json!({
"model": "gpt-4-turbo",
"messages": [
Expand All @@ -84,58 +71,64 @@ fn bench_json_parsing(c: &mut Criterion) {
"stream": false,
"user": "benchmark-user-123"
}).to_string();

group.bench_function("simple_request", |b| {
b.iter(|| {
let request: OpenAIRequest = serde_json::from_str(black_box(&simple_request)).unwrap();
black_box(request)
})
});

group.bench_function("complex_request", |b| {
b.iter(|| {
let request: OpenAIRequest = serde_json::from_str(black_box(&complex_request)).unwrap();
black_box(request)
})
});

group.finish();
}

fn bench_concurrent_key_access(c: &mut Criterion) {
let rt = Runtime::new().unwrap();
let mut group = c.benchmark_group("concurrent_access");
group.sample_size(50); // Reduce sample size for concurrent tests

let keys = create_test_keys(10);
let pool = Arc::new(KeyPool::new(keys, "round_robin"));

group.bench_function("sequential_access", |b| {
b.iter(|| {
for _ in 0..100 {
black_box(pool.get_key_for_model("gpt-3.5-turbo"));
}
})
});

group.bench_function("concurrent_access", |b| {
b.to_async(&rt).iter(|| async {
let mut handles = vec![];

for _ in 0..10 {
let pool_clone = pool.clone();
let handle = tokio::spawn(async move {
b.iter_custom(|iters| {
let start = std::time::Instant::now();
rt.block_on(async {
for _ in 0..iters {
let mut handles = vec![];

for _ in 0..10 {
black_box(pool_clone.get_key_for_model("gpt-3.5-turbo"));
let pool_clone = pool.clone();
let handle = tokio::spawn(async move {
for _ in 0..10 {
black_box(pool_clone.get_key_for_model("gpt-3.5-turbo"));
}
});
handles.push(handle);
}
});
handles.push(handle);
}

futures::future::join_all(handles).await

futures::future::join_all(handles).await;
}
});
start.elapsed()
})
});

group.finish();
}

Expand All @@ -145,4 +138,4 @@ criterion_group!(
bench_json_parsing,
bench_concurrent_key_access
);
criterion_main!(benches);
criterion_main!(benches);
151 changes: 92 additions & 59 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
use serde::{Deserialize, Serialize};
use anyhow::{Context, Result};
use secrecy::SecretString;
use serde::{Deserialize, Serialize};
use std::time::Duration;
use anyhow::{Context, Result};

#[derive(Debug, Clone, Deserialize, Serialize)]
#[derive(Debug, Clone, Deserialize, Serialize, Default)]
pub struct Config {
#[serde(default)]
pub server: ServerConfig,
#[serde(default)]
pub upstream: UpstreamConfig,
#[serde(default)]
pub keys: KeysConfig,
#[serde(default)]
pub rate_limit: RateLimitConfig,
#[serde(default)]
pub observability: ObservabilityConfig,
}

Expand Down Expand Up @@ -65,6 +70,7 @@ pub struct ObservabilityConfig {
}

#[derive(Debug, Clone, Deserialize)]
#[allow(dead_code)]
pub struct ApiKeyInfo {
#[serde(skip_serializing)]
pub key: SecretString,
Expand Down Expand Up @@ -95,18 +101,6 @@ pub struct LegacyApiKeyInfo {
pub models: Vec<String>,
}

impl Default for Config {
fn default() -> Self {
Self {
server: ServerConfig::default(),
upstream: UpstreamConfig::default(),
keys: KeysConfig::default(),
rate_limit: RateLimitConfig::default(),
observability: ObservabilityConfig::default(),
}
}
}

impl Default for ServerConfig {
fn default() -> Self {
Self {
Expand Down Expand Up @@ -159,82 +153,121 @@ impl Default for ObservabilityConfig {
}

// Default value functions
fn default_bind_addr() -> String { "0.0.0.0:8080".to_string() }
fn default_request_body_limit() -> usize { 262_144 }
fn default_graceful_shutdown_seconds() -> u64 { 10 }
fn default_base_url() -> String { "https://api.openai.com/v1".to_string() }
fn default_connect_timeout() -> u64 { 800 }
fn default_request_timeout() -> u64 { 60_000 }
fn default_retry_initial_backoff() -> u64 { 50 }
fn default_retry_max_backoff() -> u64 { 2000 }
fn default_max_retries() -> u32 { 3 }
fn default_rotation_strategy() -> String { "round_robin_health_weighted".to_string() }
fn default_unhealthy_penalty() -> u32 { 5 }
fn default_per_key_rps() -> u32 { 3 }
fn default_global_rps() -> u32 { 50 }
fn default_burst() -> u32 { 10 }
fn default_metrics_bind() -> String { "0.0.0.0:9090".to_string() }
fn default_tracing_level() -> String { "info".to_string() }
fn default_bind_addr() -> String {
"0.0.0.0:8080".to_string()
}
fn default_request_body_limit() -> usize {
262_144
}
fn default_graceful_shutdown_seconds() -> u64 {
10
}
fn default_base_url() -> String {
"https://api.openai.com/v1".to_string()
}
fn default_connect_timeout() -> u64 {
800
}
fn default_request_timeout() -> u64 {
60_000
}
fn default_retry_initial_backoff() -> u64 {
50
}
fn default_retry_max_backoff() -> u64 {
2000
}
fn default_max_retries() -> u32 {
3
}
fn default_rotation_strategy() -> String {
"round_robin_health_weighted".to_string()
}
fn default_unhealthy_penalty() -> u32 {
5
}
fn default_per_key_rps() -> u32 {
3
}
fn default_global_rps() -> u32 {
50
}
fn default_burst() -> u32 {
10
}
fn default_metrics_bind() -> String {
"0.0.0.0:9090".to_string()
}
fn default_tracing_level() -> String {
"info".to_string()
}

pub fn load_config() -> Result<(Config, Vec<ApiKeyInfo>)> {
// Load main config
let mut config = Config::default();

// Try to load from config file if it exists
if let Ok(config_str) = std::fs::read_to_string("config.toml") {
config = toml::from_str(&config_str)
.context("Failed to parse config.toml")?;
config = toml::from_str(&config_str).context("Failed to parse config.toml")?;
}

// Load API keys from environment or legacy config.json
let api_keys = load_api_keys()?;

Ok((config, api_keys))
}

fn load_api_keys() -> Result<Vec<ApiKeyInfo>> {
// First try environment variable
if let Ok(keys_env) = std::env::var("OPENAI_KEYS") {
let keys: Vec<&str> = keys_env.split(',').collect();
return Ok(keys.into_iter().map(|key| ApiKeyInfo {
key: SecretString::new(key.trim().to_string()),
url: default_base_url(),
models: vec!["others".to_string()],
latency: None,
health_score: 1.0,
}).collect());
return Ok(keys
.into_iter()
.map(|key| ApiKeyInfo {
key: SecretString::new(key.trim().to_string()),
url: default_base_url(),
models: vec!["others".to_string()],
latency: None,
health_score: 1.0,
})
.collect());
}

// Fallback to legacy config.json
if let Ok(config_content) = std::fs::read_to_string("config.json") {
let legacy_config: LegacyConfig = serde_json::from_str(&config_content)
.context("Failed to parse config.json")?;

return Ok(legacy_config.api_keys.into_iter().map(|key_info| ApiKeyInfo {
key: SecretString::new(key_info.key),
url: key_info.url,
models: key_info.models,
latency: None,
health_score: 1.0,
}).collect());
let legacy_config: LegacyConfig =
serde_json::from_str(&config_content).context("Failed to parse config.json")?;

return Ok(legacy_config
.api_keys
.into_iter()
.map(|key_info| ApiKeyInfo {
key: SecretString::new(key_info.key),
url: key_info.url,
models: key_info.models,
latency: None,
health_score: 1.0,
})
.collect());
}

anyhow::bail!("No API keys found. Set OPENAI_KEYS environment variable or create config.json");
}

impl UpstreamConfig {
pub fn connect_timeout(&self) -> Duration {
Duration::from_millis(self.connect_timeout_ms)
}

pub fn request_timeout(&self) -> Duration {
Duration::from_millis(self.request_timeout_ms)
}


#[allow(dead_code)]
pub fn retry_initial_backoff(&self) -> Duration {
Duration::from_millis(self.retry_initial_backoff_ms)
}

pub fn retry_max_backoff(&self) -> Duration {
Duration::from_millis(self.retry_max_backoff_ms)
}
Expand All @@ -244,4 +277,4 @@ impl ServerConfig {
pub fn graceful_shutdown_duration(&self) -> Duration {
Duration::from_secs(self.graceful_shutdown_seconds)
}
}
}
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ pub mod config;
pub mod proxy;
pub mod routes;
pub mod types;
pub mod util;
pub mod util;
Loading
Loading