Skip to content

Commit

Permalink
~70k posts/sec
Browse files Browse the repository at this point in the history
  • Loading branch information
noboomu committed Mar 31, 2017
1 parent 2d41345 commit 6c0ae6c
Show file tree
Hide file tree
Showing 9 changed files with 512 additions and 158 deletions.
4 changes: 2 additions & 2 deletions src/com/wurrly/BaseServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
import com.wurrly.controllers.Users;
import com.wurrly.models.User;
import com.wurrly.modules.DIModule;
import com.wurrly.server.ServerRequest;
import com.wurrly.utilities.HandleGenerator;
import com.wurrly.utilities.ServerRequest;

import io.undertow.Undertow;
import io.undertow.UndertowOptions;
Expand Down Expand Up @@ -206,7 +206,7 @@ public void handleRequest(HttpServerExchange exchange) throws Exception
.setSocketOption(org.xnio.Options.BACKLOG, 10000)
.setServerOption(UndertowOptions.RECORD_REQUEST_START_TIME, false)

.setWorkerThreads(Runtime.getRuntime().availableProcessors() * 2)
.setWorkerThreads(Runtime.getRuntime().availableProcessors() * 8)
.setHandler(new HttpHandler()
{
@Override
Expand Down
42 changes: 38 additions & 4 deletions src/com/wurrly/controllers/Users.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/
package com.wurrly.controllers;

import java.nio.ByteBuffer;
import java.util.Collections;
import java.util.Map;
import java.util.Optional;
Expand All @@ -16,7 +17,7 @@
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;

import javax.ws.rs.FormParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -28,7 +29,7 @@
import com.jsoniter.output.JsonStream;
import com.typesafe.config.Config;
import com.wurrly.models.User;
import com.wurrly.utilities.ServerRequest;
import com.wurrly.server.ServerRequest;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
Expand All @@ -42,6 +43,7 @@
@Singleton
public class Users
{


@Inject
@Named("es.index.name")
Expand All @@ -58,6 +60,38 @@ public Users()

}

@GET
@Path("/{userId}/type")
@ApiOperation(value = "Find users by id with type", nickname = "user", httpMethod = "GET", response = User.class)
public Any userType(final ServerRequest serverRequest, @PathParam("userId") final Long userId, @QueryParam("context") Optional<String> context, @QueryParam("type") User.UserType type)
{
//
// log.debug("esIndexName: " + esIndexName);
// log.debug("configuration: " + configuration);

// log.debug("context: " + context);
//
//
return Any.wrap(new User(userId,type));

}

@POST
@Path("/form")
@ApiOperation(value = "Find users by id with type", nickname = "user", httpMethod = "GET", response = User.class)
public Any userForm(final ServerRequest serverRequest, @PathParam("userId") final Long userId, @QueryParam("context") Optional<String> context, @FormParam("type") User.UserType type, ByteBuffer testFile)
{
//
// log.debug("esIndexName: " + esIndexName);
// log.debug("configuration: " + configuration);

log.debug("testFile: " + testFile);
//
//
return Any.wrap(new User(userId,type));

}


@GET
@Path("/{userId}")
Expand All @@ -80,15 +114,15 @@ public Any user(final ServerRequest serverRequest, @PathParam("userId") final Lo
@Path("/")
// @ApiImplicitParams({ @ApiImplicitParam(dataType = "com.wurrly.models.User", name = "user", paramType = "body", required = false, allowMultiple = false) })
@ApiOperation(value = "Find users by id", nickname = "user", httpMethod = "POST", response = JsonNode.class)
public User createUser(final ServerRequest serverRequest, @QueryParam("context") Optional<String> context, final User user )
public Any createUser(final ServerRequest serverRequest, @QueryParam("context") Optional<String> context, final User user )
{
//

// log.debug("context: " + context);
// log.debug("request: " + serverRequest);
// log.debug("file: " + user);

return user;
return Any.wrap(user);



Expand Down
33 changes: 33 additions & 0 deletions src/com/wurrly/models/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,20 @@
*/
package com.wurrly.models;

import com.jsoniter.annotation.JsonIgnore;

public class User
{
public static enum UserType
{
GUEST,MEMBER,ADMIN
}

private Long id = 0l;

@JsonIgnore
private UserType type = UserType.GUEST;

public User()
{

Expand All @@ -16,6 +26,13 @@ public User(Long id)
{
this.id = id;
}

public User(Long id, UserType type)
{
this.id = id;
this.type = type;
}


/**
* @return the id
Expand All @@ -32,6 +49,22 @@ public void setId(Long id)
{
this.id = id;
}

// /**
// * @return the type
// */
// public UserType getType()
// {
// return type;
// }

/**
* @param type the type to set
*/
public void setType(UserType type)
{
this.type = type;
}



Expand Down
156 changes: 156 additions & 0 deletions src/com/wurrly/server/Extractors.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
/**
*
*/
package com.wurrly.server;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.Deque;
import java.util.Optional;

import com.jsoniter.JsonIterator;
import com.jsoniter.any.Any;
import com.jsoniter.spi.TypeLiteral;

import io.undertow.server.HttpServerExchange;
import io.undertow.server.handlers.form.FormDataParser;
import io.undertow.server.handlers.form.FormData.FormValue;

/**
* @author jbauer
*/
public class Extractors
{
public static class Optional
{

public static final java.util.Optional<JsonIterator> jsonIterator(final HttpServerExchange exchange)
{
return java.util.Optional.ofNullable( JsonIterator.parse(exchange.getAttachment(ServerRequest.JSON_DATA).array()));
}

public static final <T> java.util.Optional<T> typed(final HttpServerExchange exchange, final TypeLiteral<T> type )
{
return jsonIterator(exchange).map(i -> {
try
{
return i.read(type);
} catch (Exception e)
{
return null;
}
});
}

public static final java.util.Optional<Any> any(final HttpServerExchange exchange, final String name)
{
return java.util.Optional.ofNullable(exchange.getAttachment(ServerRequest.JSON_DATA)).map(t -> {

return JsonIterator.deserialize(t.array());

});
}

public static final java.util.Optional<Integer> integerValue(final HttpServerExchange exchange, final String name)
{
return string(exchange, name).map(Integer::parseInt);
}

public static final java.util.Optional<Long> longValue(final HttpServerExchange exchange, final String name)
{
return string(exchange, name).map(Long::parseLong);
}

public static final java.util.Optional<Boolean> booleanValue(final HttpServerExchange exchange, final String name)
{
return string(exchange, name).map(Boolean::parseBoolean);
}

public static final <E extends Enum<E>> java.util.Optional<E> enumValue(final HttpServerExchange exchange, final Class<E> clazz, final String name)
{
return string(exchange, name).map(e -> Enum.valueOf(clazz, name));
}

public static final java.util.Optional<String> string(final HttpServerExchange exchange, final String name)
{
return java.util.Optional.ofNullable(exchange.getQueryParameters().get(name)).map(Deque::getFirst);
}

public static final java.util.Optional<Path> filePath(final HttpServerExchange exchange, final String name)
{
return java.util.Optional.ofNullable(exchange.getAttachment(FormDataParser.FORM_DATA).get(name)).map(Deque::getFirst).map(FormValue::getPath);
}
}

public static final <T> T typed(final HttpServerExchange exchange, final TypeLiteral<T> type ) throws Exception
{
return jsonIterator(exchange).read(type);
}

public static final Any any(final HttpServerExchange exchange, final String name)
{
try
{
return JsonIterator.parse( exchange.getAttachment(ServerRequest.JSON_DATA).array() ).readAny();
} catch (IOException e)
{
return Any.wrapNull();
}
}

public static final JsonIterator jsonIterator(final HttpServerExchange exchange )
{
return JsonIterator.parse(exchange.getAttachment(ServerRequest.JSON_DATA).array());
}

public static final Path filePath(final HttpServerExchange exchange, final String name)
{
return exchange.getAttachment(FormDataParser.FORM_DATA).get(name).getFirst().getPath();
}

public static final ByteBuffer fileBytes(final HttpServerExchange exchange, final String name) throws IOException
{
final Path filePath = filePath(exchange,name);

try(final FileChannel fileChannel = FileChannel.open(filePath, StandardOpenOption.READ))
{
final ByteBuffer buffer = ByteBuffer.allocate((int)fileChannel.size());

fileChannel.read(buffer);

buffer.flip();

return buffer;
}

}

public static final String string(final HttpServerExchange exchange, final String name)
{
return exchange.getQueryParameters().get(name).getFirst();
}

public static final Long longValue(final HttpServerExchange exchange, final String name)
{
return Long.parseLong(string(exchange, name));
}

public static final Integer integerValue(final HttpServerExchange exchange, final String name)
{
return Integer.parseInt(string(exchange, name));
}

public static final Boolean booleanValue(final HttpServerExchange exchange, final String name)
{
return Boolean.parseBoolean(string(exchange, name));
}

public static final <E extends Enum<E>> E enumValue(final HttpServerExchange exchange, final String name, Class<E> clazz)
{
return Enum.valueOf(clazz, string(exchange, name));
}

}
Loading

0 comments on commit 6c0ae6c

Please sign in to comment.