public
Description: A very fast & simple Ruby web server
Homepage: http://code.macournoyer.com/thin/
Clone URL: git://github.com/macournoyer/thin.git
commit  12c8b5a3b699af419ea210b8496f915956617e01
tree    b025e248a10bd1888e984d4cec10322426bc8b7a
parent  d6d9661620f37b5060631aa592c57374cafd0e25
thin / script / bleak
100755 85 lines (70 sloc) 2.487 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/usr/bin/env ruby-bleak-house
# Script to launch thin with Bleak House
# http://blog.evanweaver.com/files/doc/fauna/bleak_house/files/README.html
#
# Will dump data to log/memlog
# Analyze the dump w/:
#
# bleak log/memlog
#
 
module Kernel
  def alias_method_chain(target, feature)
    # Strip out punctuation on predicates or bang methods since
    # e.g. target?_without_feature is not a valid method name.
    aliased_target, punctuation = target.to_s.sub(/([?!=])$/, ''), $1
    yield(aliased_target, punctuation) if block_given?
    
    with_method, without_method = "#{aliased_target}_with_#{feature}#{punctuation}", "#{aliased_target}_without_#{feature}#{punctuation}"
    
    alias_method without_method, target
    alias_method target, with_method
    
    case
      when public_method_defined?(without_method)
        public target
      when protected_method_defined?(without_method)
        protected target
      when private_method_defined?(without_method)
        private target
    end
  end
end
 
module BleakInstruments
  module Connection
    def self.included(base)
      base.class_eval do
        alias_method_chain :receive_data, :instrument
        alias_method_chain :process, :instrument
      end
    end
    
    def receive_data_with_instrument(data)
      receive_data_without_instrument(data)
      $memlogger.snapshot($logfile, "connection/receive_data", false, 0.1)
    end
    
    def process_with_instrument
      process_without_instrument
      $memlogger.snapshot($logfile, "connection/process", false, 0.1)
    end
  end
  
  module Connector
    def self.included(base)
      base.class_eval do
        alias_method_chain :connect, :instrument
        alias_method_chain :initialize_connection, :instrument
      end
    end
    
    def connect_with_instrument
      connect_without_instrument
      $memlogger.snapshot($logfile, "connector/connect", false, 0.1)
    end
    
    def initialize_connection_with_instrument(connection)
      initialize_connection_without_instrument(connection)
      $memlogger.snapshot($logfile, "connector/initialize_connection", false, 0.1)
    end
  end
end
 
require 'rubygems'
require 'bleak_house'
require File.dirname(__FILE__) + '/../lib/thin'
 
Thin::Connection.send :include, BleakInstruments::Connection
Thin::Connectors::TcpServer.send :include, BleakInstruments::Connector
 
$memlogger = BleakHouse::Logger.new
File.delete($logfile = File.expand_path("log/memlog")) rescue nil
 
load 'bin/thin'