11/// Demo showing the async token counter improvement
2- ///
2+ ///
33/// This example demonstrates the key improvement: no blocking runtime creation
4- ///
5- /// BEFORE (blocking):
4+ ///
5+ /// BEFORE (blocking):
66/// ```rust
77/// let content = tokio::runtime::Runtime::new()?.block_on(async {
88/// let response = reqwest::get(&file_url).await?;
99/// // ... download logic
1010/// })?;
1111/// ```
12- ///
12+ ///
1313/// AFTER (async):
1414/// ```rust
1515/// let client = reqwest::Client::new();
1616/// let response = client.get(&file_url).send().await?;
1717/// let bytes = response.bytes().await?;
1818/// tokio::fs::write(&file_path, bytes).await?;
1919/// ```
20-
21- use goose:: token_counter:: { TokenCounter , AsyncTokenCounter , create_async_token_counter} ;
20+ use goose:: token_counter:: { create_async_token_counter, TokenCounter } ;
2221use std:: time:: Instant ;
2322
2423#[ tokio:: main]
2524async fn main ( ) -> Result < ( ) , Box < dyn std:: error:: Error > > {
2625 println ! ( "🚀 Async Token Counter Demo" ) ;
2726 println ! ( "===========================" ) ;
28-
27+
2928 // Test text samples
3029 let samples = vec ! [
3130 "Hello, world!" ,
@@ -34,70 +33,72 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
3433 "Lorem ipsum dolor sit amet, consectetur adipiscing elit." ,
3534 "async/await patterns eliminate blocking operations" ,
3635 ] ;
37-
36+
3837 println ! ( "\n 📊 Performance Comparison" ) ;
3938 println ! ( "-------------------------" ) ;
40-
39+
4140 // Test original TokenCounter
4241 let start = Instant :: now ( ) ;
4342 let sync_counter = TokenCounter :: new ( "Xenova--gpt-4o" ) ;
4443 let sync_init_time = start. elapsed ( ) ;
45-
44+
4645 let start = Instant :: now ( ) ;
4746 let mut sync_total = 0 ;
4847 for sample in & samples {
4948 sync_total += sync_counter. count_tokens ( sample) ;
5049 }
5150 let sync_count_time = start. elapsed ( ) ;
52-
51+
5352 println ! ( "🔴 Synchronous TokenCounter:" ) ;
5453 println ! ( " Init time: {:?}" , sync_init_time) ;
5554 println ! ( " Count time: {:?}" , sync_count_time) ;
5655 println ! ( " Total tokens: {}" , sync_total) ;
57-
58- // Test AsyncTokenCounter
56+
57+ // Test AsyncTokenCounter
5958 let start = Instant :: now ( ) ;
6059 let async_counter = create_async_token_counter ( "Xenova--gpt-4o" ) . await ?;
6160 let async_init_time = start. elapsed ( ) ;
62-
61+
6362 let start = Instant :: now ( ) ;
6463 let mut async_total = 0 ;
6564 for sample in & samples {
6665 async_total += async_counter. count_tokens ( sample) ;
6766 }
6867 let async_count_time = start. elapsed ( ) ;
69-
68+
7069 println ! ( "\n 🟢 Async TokenCounter:" ) ;
7170 println ! ( " Init time: {:?}" , async_init_time) ;
7271 println ! ( " Count time: {:?}" , async_count_time) ;
7372 println ! ( " Total tokens: {}" , async_total) ;
7473 println ! ( " Cache size: {}" , async_counter. cache_size( ) ) ;
75-
74+
7675 // Test caching benefit
7776 let start = Instant :: now ( ) ;
7877 let mut cached_total = 0 ;
7978 for sample in & samples {
8079 cached_total += async_counter. count_tokens ( sample) ; // Should hit cache
8180 }
8281 let cached_time = start. elapsed ( ) ;
83-
82+
8483 println ! ( "\n ⚡ Cached TokenCounter (2nd run):" ) ;
8584 println ! ( " Count time: {:?}" , cached_time) ;
8685 println ! ( " Total tokens: {}" , cached_total) ;
8786 println ! ( " Cache size: {}" , async_counter. cache_size( ) ) ;
88-
87+
8988 // Verify same results
9089 assert_eq ! ( sync_total, async_total) ;
9190 assert_eq ! ( async_total, cached_total) ;
92-
91+
9392 println ! ( "\n ✅ Key Improvements:" ) ;
9493 println ! ( " • No blocking runtime creation (eliminates deadlock risk)" ) ;
9594 println ! ( " • Global tokenizer caching with DashMap (lock-free concurrent access)" ) ;
9695 println ! ( " • Fast AHash for better cache performance" ) ;
9796 println ! ( " • Cache size management (prevents unbounded growth)" ) ;
98- println ! ( " • Token result caching ({}x faster on repeated text)" ,
99- async_count_time. as_nanos( ) / cached_time. as_nanos( ) . max( 1 ) ) ;
97+ println ! (
98+ " • Token result caching ({}x faster on repeated text)" ,
99+ async_count_time. as_nanos( ) / cached_time. as_nanos( ) . max( 1 )
100+ ) ;
100101 println ! ( " • Proper async patterns throughout" ) ;
101-
102+
102103 Ok ( ( ) )
103- }
104+ }
0 commit comments