English 中文(简体)
如何写像阿帕奇克这样的东西,是Schala(多面读到网页上)。
原标题:How to write something like Apache bench is Scala (multi-threaded way to hit a web page)
  • 时间:2011-11-02 15:24:05
  •  标签:
  • java
  • scala

鉴于投入,我怎么能写一个小页说明如下:

url
total requests
total connections (threads)

So it would request x web pages from a given url, using x number of threads.

它应当研究该请求的答复,以确保该请求是 的(不是失败的请求)。

我对这一点的猜测比 Java更容易,因为我听到在Schala撰写了多读的申请书。

因此:

>scalac -url localhost:8080/ -requests 1000 -connections 10

将提出1 000项请求,但同时用10条深线打。

最佳回答

我写了一张 app子,只是这样做,尽管它是在压力测试的APICI正在开发之中。 利用行为者同时提出请求非常容易。 总体想法是,你想建立一个行为者,在发出信息时,履行单一要求。 然后,你会给行为者制造多个事例,并用负荷平衡器向他们分发申请。

这里非常简单的执行,是为了给你一个想法(使用http://akka.io” rel=“nofollow”>Akka行为体):

case class MakeRequest(url: String)
class RequestActor extends Actor {

  //force the actor to run in it s own thread
  self.dispatcher = akka.dispatch.Dispatchers.newThreadBasedDispatcher(self)

  def receive = {
    case MakeRequest(url) => //perform the request
  }
}

val totalRequests = 1000
val url = "http://..."
val totalConections = 4

//create one actor per connection and wrap them in a load balancer
val actors = (0 to totalConnections).map{i => actorOf[RequestActor].start}
val requestBalancer = loadBalancerActor(new CyclicIterator(actors))

//start sending requests
for (i <- 0 to totalRequests) {
  requestBalancer ! MakeRequest(url)
}

//send a poison pill to stop the actors after they ve finished all the requests
requestBalancer ! Broadcast(PoisonPill)

//wait for the actors to finish
while (actors.foldLeft(false){ (b, a) => b || !a.isShutdown}) {
  Thread.sleep(300)
}

//stop the load balancer
requestBalancer ! PoisonPill

如你所知,我向负荷平衡者发送了<代码>MakeRequest的电文,这些电文遍布各行为体。 每个行为者都有一个信息点,因此一个单一的行为者将及时提出一项请求,但各行为体将同时提出请求。

这一例子没有为采取对策提供一种途径,而你冒着过度流入行为者的风险,但很容易确定。 我利用这一总的想法来进行广泛的压力检测,并以极大的成功作为基准。

问题回答

在Schala 2.9+中,这是一种轻而易举的方式。

val url: String
val totalRequests: Int
def makeRequest(url: String): Boolean

val requests = (1 to totalRequests).par.map(i => makeRequest(url))
val successes = requests.count(r => r)
val failures = requests.count(r => !r)




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签