rack-ai 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.rspec +3 -0
- data/.rubocop.yml +55 -0
- data/CHANGELOG.md +65 -0
- data/LICENSE +21 -0
- data/README.md +687 -0
- data/ROADMAP.md +203 -0
- data/Rakefile +40 -0
- data/benchmarks/performance_benchmark.rb +283 -0
- data/examples/rails_integration.rb +301 -0
- data/examples/sinatra_integration.rb +458 -0
- data/lib/rack/ai/configuration.rb +208 -0
- data/lib/rack/ai/features/caching.rb +278 -0
- data/lib/rack/ai/features/classification.rb +67 -0
- data/lib/rack/ai/features/enhancement.rb +219 -0
- data/lib/rack/ai/features/logging.rb +238 -0
- data/lib/rack/ai/features/moderation.rb +104 -0
- data/lib/rack/ai/features/routing.rb +143 -0
- data/lib/rack/ai/features/security.rb +275 -0
- data/lib/rack/ai/middleware.rb +268 -0
- data/lib/rack/ai/providers/base.rb +107 -0
- data/lib/rack/ai/providers/huggingface.rb +259 -0
- data/lib/rack/ai/providers/local.rb +152 -0
- data/lib/rack/ai/providers/openai.rb +246 -0
- data/lib/rack/ai/utils/logger.rb +111 -0
- data/lib/rack/ai/utils/metrics.rb +220 -0
- data/lib/rack/ai/utils/sanitizer.rb +200 -0
- data/lib/rack/ai/version.rb +7 -0
- data/lib/rack/ai.rb +48 -0
- data/rack-ai.gemspec +51 -0
- metadata +290 -0
data/ROADMAP.md
ADDED
@@ -0,0 +1,203 @@
|
|
1
|
+
# Rack::AI Roadmap
|
2
|
+
|
3
|
+
This document outlines the planned development roadmap for Rack::AI, from the current v0.1.0 to future enterprise-grade features.
|
4
|
+
|
5
|
+
## Current Version: v0.1.0 (MVP)
|
6
|
+
|
7
|
+
### ✅ Completed Features
|
8
|
+
- Core Rack middleware architecture
|
9
|
+
- Pluggable AI provider system (OpenAI, HuggingFace, Local)
|
10
|
+
- Basic AI features (classification, moderation, security, caching, logging, enhancement, routing)
|
11
|
+
- Configuration system with Ruby DSL and YAML support
|
12
|
+
- Rails and Sinatra integration examples
|
13
|
+
- Comprehensive test suite and benchmarks
|
14
|
+
- Security-first design with data sanitization
|
15
|
+
- Fail-safe mode and error handling
|
16
|
+
- Metrics and monitoring capabilities
|
17
|
+
|
18
|
+
## v0.2.0 - Enhanced Performance & Reliability (Q1 2024)
|
19
|
+
|
20
|
+
### 🎯 Performance Optimizations
|
21
|
+
- [ ] **Connection Pooling**: Implement HTTP connection pooling for AI providers
|
22
|
+
- [ ] **Request Batching**: Batch multiple requests to AI providers for efficiency
|
23
|
+
- [ ] **Smart Caching**: Enhanced Redis-based caching with TTL optimization
|
24
|
+
- [ ] **Async Processing**: Improved async processing with configurable thread pools
|
25
|
+
- [ ] **Memory Optimization**: Reduce memory footprint and garbage collection pressure
|
26
|
+
|
27
|
+
### 🔧 Reliability Improvements
|
28
|
+
- [ ] **Circuit Breaker**: Implement circuit breaker pattern for AI provider failures
|
29
|
+
- [ ] **Health Checks**: Built-in health check endpoints for AI providers
|
30
|
+
- [ ] **Graceful Degradation**: Better fallback mechanisms when AI services are unavailable
|
31
|
+
- [ ] **Request Queuing**: Queue requests during AI provider outages
|
32
|
+
- [ ] **Retry Logic**: Exponential backoff and jitter for failed requests
|
33
|
+
|
34
|
+
### 📊 Enhanced Monitoring
|
35
|
+
- [ ] **Detailed Metrics**: More granular metrics for performance analysis
|
36
|
+
- [ ] **Distributed Tracing**: OpenTelemetry integration for request tracing
|
37
|
+
- [ ] **Custom Dashboards**: Pre-built Grafana dashboards
|
38
|
+
- [ ] **Alerting**: Built-in alerting for anomalies and failures
|
39
|
+
|
40
|
+
## v0.3.0 - Advanced AI Features (Q2 2024)
|
41
|
+
|
42
|
+
### 🤖 Multi-Model Support
|
43
|
+
- [ ] **Model Orchestration**: Route requests to different AI models based on content type
|
44
|
+
- [ ] **A/B Testing**: Built-in A/B testing for different AI models
|
45
|
+
- [ ] **Model Fallbacks**: Automatic fallback to simpler models when complex ones fail
|
46
|
+
- [ ] **Custom Models**: Support for fine-tuned and custom AI models
|
47
|
+
|
48
|
+
### 🧠 Advanced Analytics
|
49
|
+
- [ ] **Pattern Learning**: Learn from historical data to improve classifications
|
50
|
+
- [ ] **Anomaly Detection**: Advanced statistical anomaly detection
|
51
|
+
- [ ] **Trend Analysis**: Identify and predict traffic trends
|
52
|
+
- [ ] **User Behavior Analysis**: Track and analyze user interaction patterns
|
53
|
+
|
54
|
+
### 🔍 Enhanced Security
|
55
|
+
- [ ] **Advanced Threat Detection**: ML-based threat detection beyond rule-based systems
|
56
|
+
- [ ] **Behavioral Analysis**: Detect suspicious behavior patterns
|
57
|
+
- [ ] **Zero-Day Protection**: Proactive protection against unknown threats
|
58
|
+
- [ ] **Compliance Reporting**: Automated compliance reports for security audits
|
59
|
+
|
60
|
+
## v0.4.0 - Enterprise Features (Q3 2024)
|
61
|
+
|
62
|
+
### 🏢 Enterprise Integration
|
63
|
+
- [ ] **SSO Integration**: Support for SAML, OAuth, and other enterprise SSO systems
|
64
|
+
- [ ] **RBAC**: Role-based access control for AI features
|
65
|
+
- [ ] **Multi-Tenancy**: Support for multi-tenant deployments
|
66
|
+
- [ ] **Enterprise Logging**: Integration with enterprise logging systems (Splunk, ELK)
|
67
|
+
|
68
|
+
### 🔐 Advanced Security & Compliance
|
69
|
+
- [ ] **GDPR Compliance**: Built-in GDPR compliance features
|
70
|
+
- [ ] **SOC 2 Compliance**: SOC 2 Type II compliance features
|
71
|
+
- [ ] **Data Residency**: Control over where AI processing occurs
|
72
|
+
- [ ] **Audit Trails**: Comprehensive audit trails for all AI decisions
|
73
|
+
|
74
|
+
### 📈 Scalability
|
75
|
+
- [ ] **Horizontal Scaling**: Support for distributed deployments
|
76
|
+
- [ ] **Load Balancing**: Intelligent load balancing across AI providers
|
77
|
+
- [ ] **Auto-Scaling**: Automatic scaling based on traffic patterns
|
78
|
+
- [ ] **Edge Computing**: Support for edge AI processing
|
79
|
+
|
80
|
+
## v0.5.0 - AI-Powered Optimization (Q4 2024)
|
81
|
+
|
82
|
+
### 🎯 Intelligent Optimization
|
83
|
+
- [ ] **Auto-Configuration**: AI-powered automatic configuration optimization
|
84
|
+
- [ ] **Performance Tuning**: Automatic performance tuning based on usage patterns
|
85
|
+
- [ ] **Resource Optimization**: Intelligent resource allocation and cost optimization
|
86
|
+
- [ ] **Predictive Scaling**: Predict and prepare for traffic spikes
|
87
|
+
|
88
|
+
### 🔄 Advanced Caching
|
89
|
+
- [ ] **Semantic Caching**: Cache based on semantic similarity, not just exact matches
|
90
|
+
- [ ] **Predictive Prefetching**: AI-powered predictive content prefetching
|
91
|
+
- [ ] **Cache Warming**: Intelligent cache warming strategies
|
92
|
+
- [ ] **Multi-Layer Caching**: Hierarchical caching with different TTLs
|
93
|
+
|
94
|
+
### 🌐 Global Distribution
|
95
|
+
- [ ] **CDN Integration**: Integration with major CDN providers
|
96
|
+
- [ ] **Geographic Optimization**: Optimize AI processing based on user location
|
97
|
+
- [ ] **Multi-Region Deployment**: Support for multi-region AI deployments
|
98
|
+
- [ ] **Latency Optimization**: Minimize latency through intelligent routing
|
99
|
+
|
100
|
+
## v1.0.0 - Production-Ready Enterprise Platform (Q1 2025)
|
101
|
+
|
102
|
+
### 🚀 Production Readiness
|
103
|
+
- [ ] **99.9% Uptime SLA**: Guaranteed uptime with comprehensive monitoring
|
104
|
+
- [ ] **Enterprise Support**: 24/7 enterprise support with dedicated success managers
|
105
|
+
- [ ] **Professional Services**: Implementation and optimization consulting
|
106
|
+
- [ ] **Training Programs**: Comprehensive training for development teams
|
107
|
+
|
108
|
+
### 🔧 Advanced Configuration
|
109
|
+
- [ ] **Visual Configuration**: Web-based configuration interface
|
110
|
+
- [ ] **Configuration Validation**: Advanced validation and testing tools
|
111
|
+
- [ ] **Environment Management**: Manage configurations across environments
|
112
|
+
- [ ] **Configuration Templates**: Pre-built templates for common use cases
|
113
|
+
|
114
|
+
### 📊 Business Intelligence
|
115
|
+
- [ ] **Executive Dashboards**: High-level dashboards for business stakeholders
|
116
|
+
- [ ] **ROI Analytics**: Measure and report on AI ROI
|
117
|
+
- [ ] **Cost Analysis**: Detailed cost analysis and optimization recommendations
|
118
|
+
- [ ] **Business Insights**: AI-generated business insights and recommendations
|
119
|
+
|
120
|
+
## Future Considerations (v2.0+)
|
121
|
+
|
122
|
+
### 🔮 Emerging Technologies
|
123
|
+
- [ ] **Quantum-Safe Security**: Prepare for quantum computing threats
|
124
|
+
- [ ] **Edge AI**: Advanced edge computing capabilities
|
125
|
+
- [ ] **Federated Learning**: Support for federated learning across deployments
|
126
|
+
- [ ] **Neuromorphic Computing**: Support for next-generation AI hardware
|
127
|
+
|
128
|
+
### 🌍 Ecosystem Expansion
|
129
|
+
- [ ] **Plugin Marketplace**: Marketplace for third-party AI plugins
|
130
|
+
- [ ] **API Ecosystem**: Rich API ecosystem for integrations
|
131
|
+
- [ ] **Community Contributions**: Enhanced community contribution framework
|
132
|
+
- [ ] **Open Source Governance**: Formal open source governance model
|
133
|
+
|
134
|
+
### 🎓 AI Democratization
|
135
|
+
- [ ] **No-Code Configuration**: Visual, no-code configuration tools
|
136
|
+
- [ ] **AI Explainability**: Advanced explainable AI features
|
137
|
+
- [ ] **Automated Optimization**: Fully automated AI optimization
|
138
|
+
- [ ] **Self-Healing Systems**: Self-diagnosing and self-healing AI systems
|
139
|
+
|
140
|
+
## Implementation Strategy
|
141
|
+
|
142
|
+
### Development Principles
|
143
|
+
1. **Backward Compatibility**: Maintain backward compatibility across versions
|
144
|
+
2. **Security First**: Security considerations in every feature
|
145
|
+
3. **Performance Focus**: Performance optimization as a core requirement
|
146
|
+
4. **Community Driven**: Community feedback drives feature prioritization
|
147
|
+
5. **Enterprise Ready**: Enterprise requirements considered from the start
|
148
|
+
|
149
|
+
### Release Cycle
|
150
|
+
- **Major Releases**: Quarterly (every 3 months)
|
151
|
+
- **Minor Releases**: Monthly bug fixes and small features
|
152
|
+
- **Patch Releases**: As needed for critical issues
|
153
|
+
- **LTS Versions**: Long-term support for enterprise customers
|
154
|
+
|
155
|
+
### Quality Assurance
|
156
|
+
- **Automated Testing**: Comprehensive test coverage (>95%)
|
157
|
+
- **Performance Testing**: Regular performance regression testing
|
158
|
+
- **Security Audits**: Regular third-party security audits
|
159
|
+
- **Beta Program**: Extensive beta testing with enterprise customers
|
160
|
+
|
161
|
+
## Community & Ecosystem
|
162
|
+
|
163
|
+
### Open Source Community
|
164
|
+
- **Contributor Guidelines**: Clear guidelines for community contributions
|
165
|
+
- **Code of Conduct**: Inclusive and welcoming community standards
|
166
|
+
- **Regular Releases**: Predictable release schedule
|
167
|
+
- **Documentation**: Comprehensive and up-to-date documentation
|
168
|
+
|
169
|
+
### Enterprise Ecosystem
|
170
|
+
- **Partner Program**: Certified partner program for integrators
|
171
|
+
- **Training Certification**: Professional certification programs
|
172
|
+
- **Support Tiers**: Multiple support tiers for different needs
|
173
|
+
- **Professional Services**: Implementation and optimization services
|
174
|
+
|
175
|
+
### Research & Innovation
|
176
|
+
- **Research Partnerships**: Partnerships with academic institutions
|
177
|
+
- **Innovation Labs**: Internal innovation labs for emerging technologies
|
178
|
+
- **Open Research**: Publish research findings and methodologies
|
179
|
+
- **Industry Collaboration**: Collaborate with industry standards bodies
|
180
|
+
|
181
|
+
## Success Metrics
|
182
|
+
|
183
|
+
### Technical Metrics
|
184
|
+
- **Performance**: <10ms median processing time
|
185
|
+
- **Reliability**: 99.9% uptime SLA
|
186
|
+
- **Accuracy**: >95% AI classification accuracy
|
187
|
+
- **Scalability**: Support for 1M+ requests per minute
|
188
|
+
|
189
|
+
### Business Metrics
|
190
|
+
- **Adoption**: 10,000+ active installations
|
191
|
+
- **Community**: 1,000+ contributors
|
192
|
+
- **Enterprise**: 100+ enterprise customers
|
193
|
+
- **Ecosystem**: 50+ certified partners
|
194
|
+
|
195
|
+
### Impact Metrics
|
196
|
+
- **Security**: 90% reduction in security incidents
|
197
|
+
- **Performance**: 50% improvement in application performance
|
198
|
+
- **Cost**: 30% reduction in infrastructure costs
|
199
|
+
- **Developer Experience**: 80% developer satisfaction score
|
200
|
+
|
201
|
+
---
|
202
|
+
|
203
|
+
This roadmap is a living document that will be updated based on community feedback, market needs, and technological advances. We welcome input from the community to help prioritize features and ensure Rack::AI meets the evolving needs of modern web applications.
|
data/Rakefile
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "bundler/gem_tasks"
|
4
|
+
require "rspec/core/rake_task"
|
5
|
+
require "rubocop/rake_task"
|
6
|
+
|
7
|
+
RSpec::Core::RakeTask.new(:spec)
|
8
|
+
RuboCop::RakeTask.new
|
9
|
+
|
10
|
+
task default: %i[spec rubocop]
|
11
|
+
|
12
|
+
desc "Run performance benchmarks"
|
13
|
+
task :benchmark do
|
14
|
+
ruby "benchmarks/performance_benchmark.rb"
|
15
|
+
end
|
16
|
+
|
17
|
+
desc "Generate YARD documentation"
|
18
|
+
task :docs do
|
19
|
+
sh "yard doc"
|
20
|
+
end
|
21
|
+
|
22
|
+
desc "Start local AI server for development"
|
23
|
+
task :local_ai do
|
24
|
+
puts "Starting local AI server on port 8080..."
|
25
|
+
puts "This is a mock server for development purposes"
|
26
|
+
# In a real implementation, this would start a local AI server
|
27
|
+
end
|
28
|
+
|
29
|
+
desc "Run security audit"
|
30
|
+
task :security do
|
31
|
+
sh "bundle audit --update"
|
32
|
+
end
|
33
|
+
|
34
|
+
desc "Clean up generated files"
|
35
|
+
task :clean do
|
36
|
+
rm_rf "coverage"
|
37
|
+
rm_rf "doc"
|
38
|
+
rm_rf ".yardoc"
|
39
|
+
rm_rf "pkg"
|
40
|
+
end
|
@@ -0,0 +1,283 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "benchmark/ips"
|
4
|
+
require "rack/test"
|
5
|
+
require "rack/ai"
|
6
|
+
|
7
|
+
# Performance benchmarks for Rack::AI middleware
|
8
|
+
class PerformanceBenchmark
|
9
|
+
include Rack::Test::Methods
|
10
|
+
|
11
|
+
def initialize
|
12
|
+
setup_apps
|
13
|
+
setup_mock_responses
|
14
|
+
end
|
15
|
+
|
16
|
+
def run_all_benchmarks
|
17
|
+
puts "=== Rack::AI Performance Benchmarks ==="
|
18
|
+
puts
|
19
|
+
|
20
|
+
benchmark_middleware_overhead
|
21
|
+
benchmark_ai_features
|
22
|
+
benchmark_provider_performance
|
23
|
+
benchmark_concurrent_requests
|
24
|
+
benchmark_memory_usage
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
def setup_apps
|
30
|
+
# Baseline app without AI
|
31
|
+
@baseline_app = lambda { |env| [200, {}, ["OK"]] }
|
32
|
+
|
33
|
+
# App with minimal AI features
|
34
|
+
@minimal_ai_app = Rack::Builder.new do
|
35
|
+
use Rack::AI::Middleware,
|
36
|
+
provider: :openai,
|
37
|
+
api_key: "test-key",
|
38
|
+
features: [:classification],
|
39
|
+
fail_safe: true,
|
40
|
+
async_processing: false
|
41
|
+
|
42
|
+
run lambda { |env| [200, {}, ["OK"]] }
|
43
|
+
end
|
44
|
+
|
45
|
+
# App with all AI features
|
46
|
+
@full_ai_app = Rack::Builder.new do
|
47
|
+
use Rack::AI::Middleware,
|
48
|
+
provider: :openai,
|
49
|
+
api_key: "test-key",
|
50
|
+
features: [:classification, :moderation, :security, :caching, :logging],
|
51
|
+
fail_safe: true,
|
52
|
+
async_processing: false
|
53
|
+
|
54
|
+
run lambda { |env| [200, {}, ["OK"]] }
|
55
|
+
end
|
56
|
+
|
57
|
+
# App with async processing
|
58
|
+
@async_ai_app = Rack::Builder.new do
|
59
|
+
use Rack::AI::Middleware,
|
60
|
+
provider: :openai,
|
61
|
+
api_key: "test-key",
|
62
|
+
features: [:classification, :security],
|
63
|
+
fail_safe: true,
|
64
|
+
async_processing: true
|
65
|
+
|
66
|
+
run lambda { |env| [200, {}, ["OK"]] }
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
def setup_mock_responses
|
71
|
+
# Mock successful AI responses for consistent benchmarking
|
72
|
+
require "webmock"
|
73
|
+
WebMock.enable!
|
74
|
+
|
75
|
+
WebMock.stub_request(:post, "https://api.openai.com/v1/chat/completions")
|
76
|
+
.to_return(
|
77
|
+
status: 200,
|
78
|
+
body: {
|
79
|
+
choices: [{
|
80
|
+
message: {
|
81
|
+
content: { classification: "human", confidence: 0.9 }.to_json
|
82
|
+
}
|
83
|
+
}]
|
84
|
+
}.to_json
|
85
|
+
)
|
86
|
+
|
87
|
+
WebMock.stub_request(:post, "https://api.openai.com/v1/moderations")
|
88
|
+
.to_return(
|
89
|
+
status: 200,
|
90
|
+
body: {
|
91
|
+
results: [{
|
92
|
+
flagged: false,
|
93
|
+
categories: {},
|
94
|
+
category_scores: {}
|
95
|
+
}]
|
96
|
+
}.to_json
|
97
|
+
)
|
98
|
+
end
|
99
|
+
|
100
|
+
def benchmark_middleware_overhead
|
101
|
+
puts "## Middleware Overhead Benchmark"
|
102
|
+
puts
|
103
|
+
|
104
|
+
env = build_test_env
|
105
|
+
|
106
|
+
Benchmark.ips do |x|
|
107
|
+
x.config(time: 5, warmup: 2)
|
108
|
+
|
109
|
+
x.report("Baseline (no AI)") do
|
110
|
+
@baseline_app.call(env.dup)
|
111
|
+
end
|
112
|
+
|
113
|
+
x.report("Minimal AI (classification only)") do
|
114
|
+
@minimal_ai_app.call(env.dup)
|
115
|
+
end
|
116
|
+
|
117
|
+
x.report("Full AI (all features)") do
|
118
|
+
@full_ai_app.call(env.dup)
|
119
|
+
end
|
120
|
+
|
121
|
+
x.compare!
|
122
|
+
end
|
123
|
+
|
124
|
+
puts
|
125
|
+
end
|
126
|
+
|
127
|
+
def benchmark_ai_features
|
128
|
+
puts "## Individual AI Features Benchmark"
|
129
|
+
puts
|
130
|
+
|
131
|
+
env = build_test_env
|
132
|
+
provider = Rack::AI::Providers::OpenAI.new(api_key: "test-key")
|
133
|
+
request_data = provider.build_request_data(env)
|
134
|
+
|
135
|
+
Benchmark.ips do |x|
|
136
|
+
x.config(time: 5, warmup: 2)
|
137
|
+
|
138
|
+
x.report("Classification") do
|
139
|
+
provider.classify_request(request_data)
|
140
|
+
end
|
141
|
+
|
142
|
+
x.report("Content Moderation") do
|
143
|
+
provider.moderate_content("Test content for moderation")
|
144
|
+
end
|
145
|
+
|
146
|
+
x.report("Anomaly Detection") do
|
147
|
+
provider.detect_anomalies(request_data)
|
148
|
+
end
|
149
|
+
|
150
|
+
x.compare!
|
151
|
+
end
|
152
|
+
|
153
|
+
puts
|
154
|
+
end
|
155
|
+
|
156
|
+
def benchmark_provider_performance
|
157
|
+
puts "## AI Provider Performance Comparison"
|
158
|
+
puts
|
159
|
+
|
160
|
+
env = build_test_env
|
161
|
+
request_data = { method: "GET", path: "/test", user_agent: "Test" }
|
162
|
+
|
163
|
+
providers = {
|
164
|
+
"OpenAI" => Rack::AI::Providers::OpenAI.new(api_key: "test-key"),
|
165
|
+
"HuggingFace" => Rack::AI::Providers::HuggingFace.new(api_key: "test-key"),
|
166
|
+
"Local" => Rack::AI::Providers::Local.new(api_url: "http://localhost:8080")
|
167
|
+
}
|
168
|
+
|
169
|
+
# Mock responses for all providers
|
170
|
+
WebMock.stub_request(:post, %r{https://api-inference\.huggingface\.co/.*})
|
171
|
+
.to_return(status: 200, body: '[{"generated_text": "human"}]')
|
172
|
+
|
173
|
+
WebMock.stub_request(:post, %r{http://localhost:8080/.*})
|
174
|
+
.to_return(status: 200, body: '{"classification": "human", "confidence": 0.9}')
|
175
|
+
|
176
|
+
Benchmark.ips do |x|
|
177
|
+
x.config(time: 3, warmup: 1)
|
178
|
+
|
179
|
+
providers.each do |name, provider|
|
180
|
+
x.report("#{name} Classification") do
|
181
|
+
begin
|
182
|
+
provider.classify_request(request_data)
|
183
|
+
rescue => e
|
184
|
+
# Handle provider errors gracefully in benchmark
|
185
|
+
nil
|
186
|
+
end
|
187
|
+
end
|
188
|
+
end
|
189
|
+
|
190
|
+
x.compare!
|
191
|
+
end
|
192
|
+
|
193
|
+
puts
|
194
|
+
end
|
195
|
+
|
196
|
+
def benchmark_concurrent_requests
|
197
|
+
puts "## Concurrent Request Processing"
|
198
|
+
puts
|
199
|
+
|
200
|
+
env = build_test_env
|
201
|
+
thread_counts = [1, 2, 4, 8]
|
202
|
+
|
203
|
+
thread_counts.each do |thread_count|
|
204
|
+
puts "### #{thread_count} concurrent threads"
|
205
|
+
|
206
|
+
Benchmark.ips do |x|
|
207
|
+
x.config(time: 3, warmup: 1)
|
208
|
+
|
209
|
+
x.report("Sync Processing") do
|
210
|
+
threads = thread_count.times.map do
|
211
|
+
Thread.new { @minimal_ai_app.call(env.dup) }
|
212
|
+
end
|
213
|
+
threads.each(&:join)
|
214
|
+
end
|
215
|
+
|
216
|
+
x.report("Async Processing") do
|
217
|
+
threads = thread_count.times.map do
|
218
|
+
Thread.new { @async_ai_app.call(env.dup) }
|
219
|
+
end
|
220
|
+
threads.each(&:join)
|
221
|
+
end
|
222
|
+
|
223
|
+
x.compare!
|
224
|
+
end
|
225
|
+
|
226
|
+
puts
|
227
|
+
end
|
228
|
+
end
|
229
|
+
|
230
|
+
def benchmark_memory_usage
|
231
|
+
puts "## Memory Usage Analysis"
|
232
|
+
puts
|
233
|
+
|
234
|
+
require "objspace"
|
235
|
+
|
236
|
+
env = build_test_env
|
237
|
+
|
238
|
+
# Measure baseline memory
|
239
|
+
GC.start
|
240
|
+
baseline_objects = ObjectSpace.count_objects
|
241
|
+
|
242
|
+
# Process requests and measure memory growth
|
243
|
+
1000.times { @full_ai_app.call(env.dup) }
|
244
|
+
|
245
|
+
GC.start
|
246
|
+
after_objects = ObjectSpace.count_objects
|
247
|
+
|
248
|
+
puts "Memory usage (object count changes):"
|
249
|
+
[:TOTAL, :T_OBJECT, :T_CLASS, :T_MODULE, :T_FLOAT, :T_STRING, :T_REGEXP,
|
250
|
+
:T_ARRAY, :T_HASH, :T_STRUCT, :T_BIGNUM, :T_FILE, :T_DATA, :T_MATCH,
|
251
|
+
:T_COMPLEX, :T_RATIONAL, :T_NODE, :T_ICLASS].each do |type|
|
252
|
+
|
253
|
+
baseline = baseline_objects[type] || 0
|
254
|
+
after = after_objects[type] || 0
|
255
|
+
diff = after - baseline
|
256
|
+
|
257
|
+
if diff != 0
|
258
|
+
puts " #{type}: #{diff > 0 ? '+' : ''}#{diff}"
|
259
|
+
end
|
260
|
+
end
|
261
|
+
|
262
|
+
puts
|
263
|
+
end
|
264
|
+
|
265
|
+
def build_test_env
|
266
|
+
{
|
267
|
+
"REQUEST_METHOD" => "GET",
|
268
|
+
"PATH_INFO" => "/api/test",
|
269
|
+
"QUERY_STRING" => "param=value",
|
270
|
+
"HTTP_USER_AGENT" => "Benchmark/1.0",
|
271
|
+
"REMOTE_ADDR" => "127.0.0.1",
|
272
|
+
"HTTP_HOST" => "localhost:3000",
|
273
|
+
"rack.input" => StringIO.new,
|
274
|
+
"rack.errors" => StringIO.new
|
275
|
+
}
|
276
|
+
end
|
277
|
+
end
|
278
|
+
|
279
|
+
# Run benchmarks if this file is executed directly
|
280
|
+
if __FILE__ == $0
|
281
|
+
benchmark = PerformanceBenchmark.new
|
282
|
+
benchmark.run_all_benchmarks
|
283
|
+
end
|