加特林负载测试给出 400 错误请求错误

Gatling load test gives 400 bad request error

我正在尝试通过使用 post 对服务器的请求为该项目 https://github.com/theiterators/akka-http-microservice 编写负载测试。但所有的回应都失败了。我该如何解决

后模拟

    import io.gatling.core.Predef._
    import io.gatling.http.Predef._
    import scala.concurrent.duration._
    
    class PostSimulation extends Simulation {
    
    
      val httpProtocol = http
        .baseUrl("http://localhost:9000") // Here is the root for all relative URLs
        .inferHtmlResources()
        .acceptHeader("*/*")
        .acceptEncodingHeader("gzip, deflate")
        .contentTypeHeader("application/json")
        .userAgentHeader("PostmanRuntime/7.24.0")
    
    
    
      val headers_0 = Map("Content-Type" -> "application/json")
    
    
      val data = StringBody(
      """{"distance":662.3054387220024,
      "ip1Info":{"query":"8.8.8.8",
      "country":"United States",
      "city":"Ashburn",
      "lat":39.03,
      "lon":-77.5},
      "ip2Info":{"query":"93.184.216.34"
      ,"country":"United States",
      "city":"Norwell",
      "lat":42.1596,
      "lon":-70.8217}}""")
    
    
      val scn = scenario("PostSimulation")
        .exec(http("request_1")
          .post("/ip/8.8.8.8")
          .headers(headers_0)
          .body(data).asJson
          .check(status.is(200)))
        .exec(http("request_2")
          .post("/ip/8.8.8.8")
          .headers(headers_0)
          .body(data).asJson
          .check(status.is(200)))
        .exec(http("request_3")
          .post("/ip/8.8.8.8")
          .headers(headers_0)
          .body(data).asJson
          .check(status.is(200)))
    
    
    
    
    
    
    
      setUp(scn.inject(atOnceUsers(1))).protocols(httpProtocol)
    
    }

错误:

    sbt:akka-http-microservice> gatling:testOnly PostSimulation
    [info] compiling 1 Scala source to /home/mehveen/akka-http-microservice/target/scala-2.13/test-classes ...
    io.netty.util.internal.NativeLibraryLoader.loadFirstAvailable(NativeLibraryLoader.java:96)
        at io.netty.handler.ssl.OpenSsl.loadTcNative(OpenSsl.java:592)
        at io.netty.handler.ssl.OpenSsl.<clinit>(OpenSsl.java:136)
        at io.gatling.http.util.SslContextsFactory.<init>(SslContexts.scala:52)
        at io.gatling.http.engine.HttpEngine$.apply(HttpEngine.scala:49)
        at io.gatling.http.protocol.HttpProtocol$$anon.newComponents(HttpProtocol.scala:52)
        at io.gatling.core.protocol.ProtocolComponentsRegistry.$anonfun$components(Protocol.scala:68)
        at scala.collection.mutable.HashMap.getOrElseUpdate(HashMap.scala:454)
        at io.gatling.core.protocol.ProtocolComponentsRegistry.componentsFactory(Protocol.scala:68)
        at io.gatling.core.protocol.ProtocolComponentsRegistry.comps(Protocol.scala:71)
        at io.gatling.core.protocol.ProtocolComponentsRegistry.$anonfun$components(Protocol.scala:73)
        at scala.collection.mutable.HashMap.getOrElseUpdate(HashMap.scala:454)
        at io.gatling.core.protocol.ProtocolComponentsRegistry.components(Protocol.scala:73)
        at io.gatling.http.action.HttpActionBuilder.lookUpHttpComponents(HttpActionBuilder.scala:26)
        at io.gatling.http.action.HttpRequestActionBuilder.build(HttpRequestActionBuilder.scala:33)
        at io.gatling.core.structure.BuildAction.$anonfun$build(BuildAction.scala:28)
        at scala.collection.LinearSeqOps.foldLeft(LinearSeq.scala:169)
        at scala.collection.LinearSeqOps.foldLeft$(LinearSeq.scala:165)
        at scala.collection.immutable.List.foldLeft(List.scala:79)
        at io.gatling.core.structure.BuildAction.build(BuildAction.scala:27)
        at io.gatling.core.structure.BuildAction.build$(BuildAction.scala:26)
        at io.gatling.core.structure.ScenarioBuilder.build(StructureBuilder.scala:51)
        at io.gatling.core.structure.PopulationBuilder.build(PopulationBuilder.scala:100)
        at io.gatling.core.scenario.SimulationParams.buildScenario(Simulation.scala:170)
        at io.gatling.core.scenario.SimulationParams.$anonfun$scenarios(Simulation.scala:174)
        at scala.collection.immutable.List.map(List.scala:246)
        at io.gatling.core.scenario.SimulationParams.scenarios(Simulation.scala:174)
        at io.gatling.app.Runner.run0(Runner.scala:82)
        at io.gatling.app.Runner.run(Runner.scala:52)
        at io.gatling.app.Gatling$.start(Gatling.scala:80)
        at io.gatling.app.Gatling$.fromArgs(Gatling.scala:45)
        at io.gatling.sbt.GatlingTask.liftedTree1(GatlingTask.scala:52)
        at io.gatling.sbt.GatlingTask.execute(GatlingTask.scala:51)
        at sbt.ForkMain$Run.lambda$runTest(ForkMain.java:413)
        at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
        at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
        at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
        at java.base/java.lang.Thread.run(Thread.java:834)
    17:06:20.933 [pool-1-thread-1] DEBUG io.netty.util.internal.NativeLibraryLoader - netty_tcnative_linux_x86_64 cannot be loaded from java.library.path, now trying export to -Dio.netty.native.workdir: /tmp
    java.lang.UnsatisfiedLinkError: no netty_tcnative_linux_x86_64 in java.library.path: [/usr/java/packages/lib, /usr/lib64, /lib64, /lib, /usr/lib]
        at java.base/java.lang.ClassLoader.loadLibrary(ClassLoader.java:2660)
        at java.base/java.lang.Runtime.loadLibrary0(Runtime.java:827)
        at java.base/java.lang.System.loadLibrary(System.java:1871)
        at io.netty.util.internal.NativeLibraryUtil.loadLibrary(NativeLibraryUtil.java:38)
        at io.netty.util.internal.NativeLibraryLoader.loadLibrary(NativeLibraryLoader.java:351)
        at io.netty.util.internal.NativeLibraryLoader.load(NativeLibraryLoader.java:136)
        at io.netty.util.internal.NativeLibraryLoader.loadFirstAvailable(NativeLibraryLoader.java:96)
        at io.netty.handler.ssl.OpenSsl.loadTcNative(OpenSsl.java:592)
        at io.netty.handler.ssl.OpenSsl.<clinit>(OpenSsl.java:136)
        at io.gatling.http.util.SslContextsFactory.<init>(SslContexts.scala:52)
        at io.gatling.http.engine.HttpEngine$.apply(HttpEngine.scala:49)
        at io.gatling.http.protocol.HttpProtocol$$anon.newComponents(HttpProtocol.scala:52)
        at io.gatling.core.protocol.ProtocolComponentsRegistry.$anonfun$components(Protocol.scala:68)
        at scala.collection.mutable.HashMap.getOrElseUpdate(HashMap.scala:454)
        at io.gatling.core.protocol.ProtocolComponentsRegistry.componentsFactory(Protocol.scala:68)
        at io.gatling.core.protocol.ProtocolComponentsRegistry.comps(Protocol.scala:71)
        at io.gatling.core.protocol.ProtocolComponentsRegistry.$anonfun$components(Protocol.scala:73)
        at scala.collection.mutable.HashMap.getOrElseUpdate(HashMap.scala:454)
        at io.gatling.core.protocol.ProtocolComponentsRegistry.components(Protocol.scala:73)
   content: UnpooledHeapByteBuf(ridx: 0, widx: 265, cap: 265/265))
    POST /ip/8.8.8.8 HTTP/1.1
    accept: application/json
    user-agent: PostmanRuntime/7.24.0
    accept-encoding: gzip, deflate
    host: localhost:9000
    content-type: application/json
    content-length: 265, content=null}
    17:06:23.538 [gatling-1-2] DEBUG io.gatling.http.client.impl.HttpAppHandler - Read msg='DefaultHttpResponse(decodeResult: success, version: HTTP/1.1)
    HTTP/1.1 400 Bad Request
    Server: akka-http/10.2.4
    Date: Fri, 23 Apr 2021 12:06:23 GMT
    Content-Type: text/plain; charset=UTF-8
    Content-Length: 166'
    17:06:23.543 [gatling-1-2] DEBUG io.gatling.http.client.impl.HttpAppHandler - Read msg='DefaultLastHttpContent(data: PooledSlicedByteBuf(ridx: 0, widx: 166, cap: 166/166, unwrapped: PooledUnsafeDirectByteBuf(ridx: 319, widx: 319, cap: 2048)), decoderResult: success)'
    17:06:23.563 [gatling-1-2] DEBUG io.gatling.http.engine.response.DefaultStatsProcessor - Request 'request_1' failed for user 1: status.find.is(200), but actually found 400
    17:06:23.583 [gatling-1-2] DEBUG io.gatling.http.engine.response.DefaultStatsProcessor - 
    >>>>>>>>>>>>>>>>>>>>>>>>>>
    Request:
    request_1: KO status.find.is(200), but actually found 400
    =========================
    Session:
    Session(PostSimulation,1,Map(gatling.http.ssl.sslContexts -> io.gatling.http.util.SslContexts@6bb54726, gatling.http.cache.dns -> io.gatling.http.resolver.ShufflingNameResolver@4971b2c, gatling.http.cache.baseUrl -> http://localhost:9000),KO,List(),io.gatling.core.protocol.ProtocolComponentsRegistry$$Lambda4/0x00000001004fb040@7b4036c5,io.netty.channel.epoll.EpollEventLoop@4d79eb73)
    =========================
    HTTP request:
    POST http://localhost:9000/ip/8.8.8.8
    headers:
        accept: application/json
        user-agent: PostmanRuntime/7.24.0
        accept-encoding: gzip, deflate
        host: localhost:9000
        content-type: application/json
        content-length: 265
    body:StringChunksRequestBody{contentType='application/json', charset=UTF-8, content={"distance":662.3054387220024,
      "ip1Info":{"query":"8.8.8.8",
      "country":"United States",
      "city":"Ashburn",
      "lat":39.03,
      "lon":-77.5},
      "ip2Info":{"query":"93.184.216.34"
      ,"country":"United States",
      "city":"Norwell",
      "lat":42.1596,
      "lon":-70.8217}}}
    =========================
    HTTP response:
    status:
        400 Bad Request
    headers:
        Server: akka-http/10.2.4
        Date: Fri, 23 Apr 2021 12:06:23 GMT
        Content-Type: text/plain; charset=UTF-8
        Content-Length: 166
    body:
    The request content was malformed:
    DecodingFailure at .ip1: Attempt to decode value on failed cursor
    DecodingFailure at .ip2: Attempt to decode value on failed cursor
    <<<<<<<<<<<<<<<<<<<<<<<<<
    17:06:23.585 [gatling-1-2] DEBUG io.gatling.http.engine.tx.HttpTxExecutor - Sending request=request_2 uri=http://localhost:9000/ip/8.8.8.8: scenario=PostSimulation, userId=1
    17:06:23.586 [gatling-1-2] DEBUG io.gatling.http.client.impl.HttpAppHandler - Write request WritableRequest{request=DefaultFullHttpRequest(decodeResult: success, version: HTTP/1.1, content: UnpooledHeapByteBuf(ridx: 0, widx: 265, cap: 265/265))
    POST /ip/8.8.8.8 HTTP/1.1
    accept: application/json
    user-agent: PostmanRuntime/7.24.0
    accept-encoding: gzip, deflate
    host: localhost:9000
    content-type: application/json
    content-length: 265, content=null}
    17:06:23.592 [gatling-1-2] DEBUG io.gatling.http.client.impl.HttpAppHandler - Read msg='DefaultHttpResponse(decodeResult: success, version: HTTP/1.1)
    HTTP/1.1 400 Bad Request
    Server: akka-http/10.2.4
    Date: Fri, 23 Apr 2021 12:06:23 GMT
    Content-Type: text/plain; charset=UTF-8
    Content-Length: 166'
    17:06:23.593 [gatling-1-2] DEBUG io.gatling.http.client.impl.HttpAppHandler - Read msg='DefaultLastHttpContent(data: PooledSlicedByteBuf(ridx: 0, widx: 166, cap: 166/166, unwrapped: PooledUnsafeDirectByteBuf(ridx: 319, widx: 319, cap: 2048)), decoderResult: success)'
    17:06:23.593 [gatling-1-2] DEBUG io.gatling.http.engine.response.DefaultStatsProcessor - Request 'request_2' failed for user 1: status.find.is(200), but actually found 400
    17:06:23.595 [gatling-1-2] DEBUG io.gatling.http.engine.response.DefaultStatsProcessor - 
    >>>>>>>>>>>>>>>>>>>>>>>>>>
    Request:
    request_2: KO status.find.is(200), but actually found 400
    =========================
    Session:
    Session(PostSimulation,1,Map(gatling.http.ssl.sslContexts -> io.gatling.http.util.SslContexts@6bb54726, gatling.http.cache.dns -> io.gatling.http.resolver.ShufflingNameResolver@4971b2c, gatling.http.cache.baseUrl -> http://localhost:9000),KO,List(),io.gatling.core.protocol.ProtocolComponentsRegistry$$Lambda4/0x00000001004fb040@7b4036c5,io.netty.channel.epoll.EpollEventLoop@4d79eb73)
    =========================
    HTTP request:
    POST http://localhost:9000/ip/8.8.8.8
    headers:
        accept: application/json
        user-agent: PostmanRuntime/7.24.0
        accept-encoding: gzip, deflate
        host: localhost:9000
        content-type: application/json
        content-length: 265
    body:StringChunksRequestBody{contentType='application/json', charset=UTF-8, content={"distance":662.3054387220024,
      "ip1Info":{"query":"8.8.8.8",
      "country":"United States",
      "city":"Ashburn",
      "lat":39.03,
      "lon":-77.5},
      "ip2Info":{"query":"93.184.216.34"
      ,"country":"United States",
      "city":"Norwell",
      "lat":42.1596,
      "lon":-70.8217}}}
    =========================
    HTTP response:
    status:
        400 Bad Request
    headers:
        Server: akka-http/10.2.4
        Date: Fri, 23 Apr 2021 12:06:23 GMT
        Content-Type: text/plain; charset=UTF-8
        Content-Length: 166
    body:
    The request content was malformed:
    DecodingFailure at .ip1: Attempt to decode value on failed cursor
    DecodingFailure at .ip2: Attempt to decode value on failed cursor
    <<<<<<<<<<<<<<<<<<<<<<<<<
    17:06:23.597 [gatling-1-2] DEBUG io.gatling.http.engine.tx.HttpTxExecutor - Sending request=request_3 uri=http://localhost:9000/ip/8.8.8.8: scenario=PostSimulation, userId=1
    17:06:23.598 [gatling-1-2] DEBUG io.gatling.http.client.impl.HttpAppHandler - Write request WritableRequest{request=DefaultFullHttpRequest(decodeResult: success, version: HTTP/1.1, content: UnpooledHeapByteBuf(ridx: 0, widx: 265, cap: 265/265))
    POST /ip/8.8.8.8 HTTP/1.1
    accept: application/json
    user-agent: PostmanRuntime/7.24.0
    accept-encoding: gzip, deflate
    host: localhost:9000
    content-type: application/json
    content-length: 265, content=null}
    17:06:23.610 [gatling-1-2] DEBUG io.gatling.http.client.impl.HttpAppHandler - Read msg='DefaultHttpResponse(decodeResult: success, version: HTTP/1.1)
    HTTP/1.1 400 Bad Request
    Server: akka-http/10.2.4
    Date: Fri, 23 Apr 2021 12:06:23 GMT
    Content-Type: text/plain; charset=UTF-8
    Content-Length: 166'
    17:06:23.611 [gatling-1-2] DEBUG io.gatling.http.client.impl.HttpAppHandler - Read msg='DefaultLastHttpContent(data: PooledSlicedByteBuf(ridx: 0, widx: 166, cap: 166/166, unwrapped: PooledUnsafeDirectByteBuf(ridx: 319, widx: 319, cap: 1024)), decoderResult: success)'
    17:06:23.612 [gatling-1-2] DEBUG io.gatling.http.engine.response.DefaultStatsProcessor - Request 'request_3' failed for user 1: status.find.is(200), but actually found 400
    17:06:23.613 [gatling-1-2] DEBUG io.gatling.http.engine.response.DefaultStatsProcessor - 
    >>>>>>>>>>>>>>>>>>>>>>>>>>
    Request:
    request_3: KO status.find.is(200), but actually found 400
    =========================
    Session:
    Session(PostSimulation,1,Map(gatling.http.ssl.sslContexts -> io.gatling.http.util.SslContexts@6bb54726, gatling.http.cache.dns -> io.gatling.http.resolver.ShufflingNameResolver@4971b2c, gatling.http.cache.baseUrl -> http://localhost:9000),KO,List(),io.gatling.core.protocol.ProtocolComponentsRegistry$$Lambda4/0x00000001004fb040@7b4036c5,io.netty.channel.epoll.EpollEventLoop@4d79eb73)
    =========================
    HTTP request:
    POST http://localhost:9000/ip/8.8.8.8
    headers:
        accept: application/json
        user-agent: PostmanRuntime/7.24.0
        accept-encoding: gzip, deflate
        host: localhost:9000
        content-type: application/json
        content-length: 265
    body:StringChunksRequestBody{contentType='application/json', charset=UTF-8, content={"distance":662.3054387220024,
      "ip1Info":{"query":"8.8.8.8",
      "country":"United States",
      "city":"Ashburn",
      "lat":39.03,
      "lon":-77.5},
      "ip2Info":{"query":"93.184.216.34"
      ,"country":"United States",
      "city":"Norwell",
      "lat":42.1596,
      "lon":-70.8217}}}
    =========================
    HTTP response:
    status:
        400 Bad Request
    headers:
        Server: akka-http/10.2.4
        Date: Fri, 23 Apr 2021 12:06:23 GMT
        Content-Type: text/plain; charset=UTF-8
        Content-Length: 166
    body:
    The request content was malformed:
    DecodingFailure at .ip1: Attempt to decode value on failed cursor
    DecodingFailure at .ip2: Attempt to decode value on failed cursor
    <<<<<<<<<<<<<<<<<<<<<<<<<
    17:06:23.613 [gatling-1-2] DEBUG io.gatling.core.action.Exit - End user #1
    17:06:23.622 [GatlingSystem-akka.actor.default-dispatcher-6] DEBUG io.gatling.core.controller.inject.Injector - End user #PostSimulation
    17:06:23.623 [GatlingSystem-akka.actor.default-dispatcher-6] INFO io.gatling.core.controller.inject.Injector - All users of scenario PostSimulation are stopped
    17:06:23.625 [GatlingSystem-akka.actor.default-dispatcher-6] INFO io.gatling.core.controller.inject.Injector - Stopping
    17:06:23.626 [GatlingSystem-akka.actor.default-dispatcher-7] INFO io.gatling.core.controller.Controller - Injector has stopped, initiating graceful stop
    ================================================================================
    2021-04-23 17:06:23                                           0s elapsed
    ---- Requests ------------------------------------------------------------------
    > Global                                                   (OK=0      KO=3     )
    > request_1                                                (OK=0      KO=1     )
    > request_2                                                (OK=0      KO=1     )
    > request_3                                                (OK=0      KO=1     )
    ---- Errors --------------------------------------------------------------------
    > status.find.is(200), but actually found 400                         3 (100.0%)
    ---- PostSimulation ------------------------------------------------------------
    [##########################################################################]100%
              waiting: 0      / active: 0      / done: 1     
    ================================================================================
    17:06:23.776 [GatlingSystem-akka.actor.default-dispatcher-4] DEBUG io.gatling.core.controller.Controller - StatsEngine was stopped
    Simulation PostSimulation completed in 0 seconds
    17:06:23.782 [gatling-1-2] DEBUG io.netty.buffer.PoolThreadCache - Freed 5 thread-local buffer(s) from thread: gatling-1-2
    Parsing log file(s)...
    17:06:23.855 [pool-1-thread-1] INFO io.gatling.charts.stats.LogFileReader - Collected List(/home/mehveen/akka-http-microservice/target/gatling/postsimulation-20210423120620892/simulation.log) from postsimulation-20210423120620892
    17:06:23.866 [pool-1-thread-1] INFO io.gatling.charts.stats.LogFileReader - First pass
    17:06:23.871 [pool-1-thread-1] INFO io.gatling.charts.stats.LogFileReader - First pass done: read 6 lines
    17:06:23.882 [pool-1-thread-1] INFO io.gatling.charts.stats.LogFileReader - Second pass
    17:06:23.917 [pool-1-thread-1] INFO io.gatling.charts.stats.LogFileReader - Second pass: read 6 lines
    Parsing log file(s) done
    Generating reports...
    ================================================================================
    ---- Global Information --------------------------------------------------------
    > request count                                          3 (OK=0      KO=3     )
    > min response time                                      7 (OK=-      KO=7     )
    > max response time                                    175 (OK=-      KO=175   )
    > mean response time                                    65 (OK=-      KO=65    )
    > std deviation                                         78 (OK=-      KO=78    )
    > response time 50th percentile                         13 (OK=-      KO=13    )
    > response time 75th percentile                         94 (OK=-      KO=94    )
    > response time 95th percentile                        159 (OK=-      KO=159   )
    > response time 99th percentile                        172 (OK=-      KO=172   )
    > mean requests/sec                                      3 (OK=-      KO=3     )
    ---- Response Time Distribution ------------------------------------------------
    > t < 800 ms                                             0 (  0%)
    > 800 ms < t < 1200 ms                                   0 (  0%)
    > t > 1200 ms                                            0 (  0%)
    > failed                                                 3 (100%)
    ---- Errors --------------------------------------------------------------------
    > status.find.is(200), but actually found 400                         3 (100.0%)
    ================================================================================
    Reports generated in 0s.
    Please open the following file: /home/mehveen/akka-http-microservice/target/gatling/postsimulation-20210423120620892/index.html
    [info] Simulation PostSimulation successful.
    [info] Simulation(s) execution ended.
    [success] Total time: 8 s, completed 23 Apr. 2021, 5:06:24 pm**

您的请求正文与服务器对此 API 的期望不匹配。

    The request content was malformed:
    DecodingFailure at .ip1: Attempt to decode value on failed cursor
    DecodingFailure at .ip2: Attempt to decode value on failed cursor

似乎表明您应该在您的负载中包含 ip1ip2 对象。

===编辑

您使用 Gatling 发送的内容:

{
   "distance":662.3054387220024,
   "ip1Info":{
      "query":"8.8.8.8",
      "country":"United States",
      "city":"Ashburn",
      "lat":39.03,
      "lon":-77.5
   },
   "ip2Info":{
      "query":"93.184.216.34",
      "country":"United States",
      "city":"Norwell",
      "lat":42.1596,
      "lon":-70.8217
   }
}

你用 curl 发送的内容(-d 选项,数据用单引号引起来,我想后面的内容会被忽略):

{
   "ip1":"8.8.8.8",
   "ip2":"93.184.216.34"
}