2014-10-27 4 views
7

मैं निम्नलिखित POC कोशिश कर रहा था कि कैसे उच्च संगामिति प्राप्त करने के लिए जाँच करने के लिएइस भविष्य और थ्रेड में spray.io के साथ उच्च समेकन कैसे प्राप्त करें। उदाहरण उदाहरण?

implicit def executionContext = context.system.dispatchers.lookup("async-futures-dispatcher") 
    implicit val timeout = 10 seconds 


    val contestroute = "/contestroute" { 
     get { 
      respondWithMediaType(`application/json`) { 
      dynamic { 
       onSuccess(
        Future { 
        val start = System.currentTimeMillis() 
        // np here should be dealt by 200 threads defined below, so why 
        // overall time takes so long? why doesn't it really utilize all 
        // threads I have given to it? how to update the code so it 
        // utilizes the 200 threads? 
        Thread.sleep(5000) 
        val status = s"timediff ${System.currentTimeMillis() - start}ms ${Thread.currentThread().getName}" 
        status 
        }) { time => 
        complete(s"status: $time") 
       } 
      } 
      } 
     } 
    } 

मेरे config:

async-futures-dispatcher { 
    # Dispatcher is the name of the event-based dispatcher 
    type = Dispatcher 
    # What kind of ExecutionService to use 
    executor = "thread-pool-executor" 
    # Configuration for the thread pool 
    thread-pool-executor { 
    # minimum number of threads to cap factor-based core number to 
    core-pool-size-min = 200 
    # No of core threads ... ceil(available processors * factor) 
    core-pool-size-factor = 20.0 
    # maximum number of threads to cap factor-based number to 
    core-pool-size-max = 200 
    } 
    # Throughput defines the maximum number of messages to be 
    # processed per actor before the thread jumps to the next actor. 
    # Set to 1 for as fair as possible. 
    throughput = 100 
} 

लेकिन जब मैं इस तरह अपाचे बेंच चलाएँ:

ab -n 200 -c 50 http://LAP:8080/contestroute 

परिणाम मैं प्राप्त करें:

Server Software:  Apache-Coyote/1.1 
Server Port:erred:  37500 bytes 
HTML transferred:  10350 bytes 
Requests per second: 4.31 [#/sec] (mean) 
Time per request:  34776.278 [ms] (mean) 
Time per request:  231.842 [ms] (mean, across all concurrent requests) 
Transfer rate:   1.05 [Kbytes/sec] received 

Connection Times (ms) 
       min mean[+/-sd] median max 
Connect:  5 406 1021.3  7 3001 
Processing: 30132 30466 390.8 30308 31231 
Waiting: 30131 30464 391.8 30306 31231 
Total:  30140 30872 998.9 30353 33228   8080 

Document Path:   /contestroute 
Document Length:  69 bytes 

Concurrency Level:  150 
Time taken for tests: 34.776 seconds 
Complete requests:  150 
Failed requests:  0 
Write errors:   0 
Non-2xx responses:  150 
Total transferred:  37500 bytes 
HTML transferred:  10350 bytes 
Requests per second: 4.31 [#/sec] (mean) 
Time per request:  34776.278 [ms] (mean) 
Time per request:  231.842 [ms] (mean, across all concurrent requests) 
Transfer rate:   1.05 [Kbytes/sec] received 

Connection Times (ms) 
       min mean[+/-sd] median max 
Connect:  5 406 1021.3  7 3001 
Processing: 30132 30466 390.8 30308 31231 
Waiting: 30131 30464 391.8 30306 31231 
Total:  30140 30872 998.9 30353 33228 

क्या मुझे कुछ बड़ा याद आ रहा है? मुझे अपने spray और futures को दिए गए सभी थ्रेड का उपयोग करने के लिए मुझे क्या बदलने की आवश्यकता है?

+2

मुझे पता है यह उदाहरण कोड है, लेकिन 'भविष्य' के रूप में निष्पादित कोड के अंदर 'नींद' क्यों है। ऐसा करके आप मूल रूप से उस थ्रेड को ले रहे हैं जो भविष्य में 5 सेकंड के लिए कमीशन से बाहर चल रहा है और इसे 5 सेकंड के दौरान किसी अन्य अभिनेता/भविष्य को सौंपा नहीं जा सकता है। – cmbaxter

+0

मैं एक लंबे समय तक चलने वाला ऑपरेशन अनुकरण कर रहा हूं, इसलिए मैं देख सकता हूं कि मैं इस लंबे समय तक चलने वाले ऑपरेशन को चलाने के लिए 200 धागे का उपयोग कर सकता हूं या नहीं। – Jas

+1

क्या आप सुनिश्चित हैं कि स्प्रे के लिए अंतर्निहित अभिनेता प्रणाली भी 'async-futures-dispatcher' निष्पादन संदर्भ को उठा नहीं रही है। यदि यह अनजाने में इसे उठा रहा है (क्योंकि इसे अंतर्निहित के रूप में परिभाषित किया गया है) तो यह समस्या का हिस्सा हो सकता है। जैसा कि आपने यहां किया है, एक्क्का मुख्य प्रेषक से अलग निष्पादन संदर्भ में अवरुद्ध कोड को बंद करने के लिए फ़ायरवॉल करना एक अच्छा विचार है, लेकिन अगर यह इस मार्ग का प्रतिनिधित्व करने वाले कलाकारों को चलाने के लिए अक्का इस निष्पादन संदर्भ का भी उपयोग कर रहा है तो यह इसके उद्देश्य को पूरा नहीं करेगा। – cmbaxter

उत्तर

13

अपने उदाहरण में सभी स्प्रे संचालन और अवरुद्ध संचालन एक ही संदर्भ में होता है (मैं बिल्ला सर्वलेट 3.0 की चोटी पर चल रहा हूँ जोड़ने के लिए)। आपको 2 संदर्भों को विभाजित करने की आवश्यकता है:

मुझे गतिशीलता का उपयोग करने का कारण भी नहीं दिख रहा है, मुझे लगता है कि केवल 'पूर्ण' अच्छा होना चाहिए।

implicit val timeout = 10.seconds 

    // Execution Context for blocking ops 
    val blockingExecutionContext = { 
    ExecutionContext.fromExecutor(Executors.newFixedThreadPool(2000)) 
    } 

    // Execution Context for Spray 
    import context.dispatcher 

    override def receive: Receive = runRoute(contestroute) 

    val contestroute = path("contestroute") { 
    get { 

     complete { 
      Future.apply { 
      val start = System.currentTimeMillis() 
      // np here should be dealt by 200 threads defined below, so why 
      // overall time takes so long? why doesn't it really utilize all 
      // threads I have given to it? how to update the code so it 
      // utilizes the 200 threads? 
      Thread.sleep(5000) 
      val status = s"timediff ${System.currentTimeMillis() - start}ms ${Thread.currentThread().getName}" 
      status 
      }(blockingExecutionContext) 
     } 

    } 
    } 

उसके बाद आप

ab -n 200 -c 200 http://LAP:8080/contestroute 

साथ यह परीक्षण कर सकते हैं और आप देखेंगे कि स्प्रे अवरुद्ध संचालन

परिणाम के लिए सभी 200 धागे पैदा करेगा:

Concurrency Level:  200 
Time taken for tests: 5.096 seconds 
+1

छोटे सुधार: यह स्प्रे नहीं है कि "सभी 200 धागे बनाएं ["] लेकिन यह स्कैला/अक्का की एक विशेषता है। – jrudolph

+2

यह मेरी टिप्पणी में मैंने जो कुछ कहा था, वह स्प्रे भी उस संदर्भ का उपयोग कर रहा था जहां अवरोध हो रहा था इस प्रकार सिस्टम के थ्रूपुट को गंभीर रूप से सीमित कर रहा था। – cmbaxter

+0

क्या इस दृष्टिकोण का मतलब यह नहीं है कि एकल धागा अभी भी सभी रूटिंग को संसाधित कर रहा है (= बाधा + सभी अनुरोधों को अवरुद्ध करने का निरंतर खतरा)? उच्च समेकन प्राप्त करने के लिए, आने वाले अनुरोध के सर्वर हाथ प्रबंधन को जल्द से जल्द एक नए धागे को नहीं करना चाहिए? – user48956

संबंधित मुद्दे