Effect CommunityEC
Effect Community2y ago
7 replies
d-yama

Creating a Tiny HTTP Server with Effect and Test Code

I created a tiny HTTP server using Effect while looking at the test code.

import { HttpServer } from '@effect/platform'  
import { NodeContext, NodeHttpServer } from '@effect/platform-node'  
import { Layer } from 'effect'  
import { createServer } from 'node:http'  
import { runMain } from '@effect/platform-node/NodeRuntime'  
  
const app = HttpServer.router.empty.pipe(  
  HttpServer.router.get('/', HttpServer.response.json({ message: 'Hello World!!' })),  
)  
  
const live = Layer.mergeAll(  
  NodeContext.layer,  
  NodeHttpServer.etag.layer,  
  NodeHttpServer.server.layerServer(createServer, { port: 3000 })  
)  
  
const HttpLive = HttpServer.server  
  .serve(app)  
  .pipe(Layer.provide(live))  
  
runMain(Layer.launch(HttpLive))


I ran a simple performance test using Apache Benche. The result was about 7,000 req/sec.

ab -n 15000 -c 100 http://localhost:3000/


I implemented an HTTP server with the same configuration in Fastify and performed the same performance test, and the result was 21,000 req/sec. How does this difference arise? Is there something wrong with the way I wrote my code?
The code implemented in Fastify is as follows.

import fastify from 'fastify'  
  
const server = fastify()  
  
server.get('/', async (request, reply) => {  
  reply.send({ hello: 'world' })
})  
  
server.listen({ port: 3000 }, (err, address) => {    
  console.log(`Server listening at ${address}`)  
})
Was this page helpful?