Helper.java 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package hello;
  2. import io.undertow.server.HttpServerExchange;
  3. import org.apache.commons.dbcp.ConnectionFactory;
  4. import org.apache.commons.dbcp.DriverManagerConnectionFactory;
  5. import org.apache.commons.dbcp.PoolableConnectionFactory;
  6. import org.apache.commons.dbcp.PoolingDataSource;
  7. import org.apache.commons.pool.impl.GenericObjectPool;
  8. import javax.sql.DataSource;
  9. import java.util.Deque;
  10. import java.util.concurrent.ThreadLocalRandom;
  11. /**
  12. * Provides utility methods for the benchmark tests.
  13. */
  14. final class Helper {
  15. private Helper() {
  16. throw new AssertionError();
  17. }
  18. /**
  19. * Constructs a new SQL data source with the given parameters. Connections
  20. * to this data source are pooled.
  21. *
  22. * @param uri the URI for database connections
  23. * @param user the username for the database
  24. * @param password the password for the database
  25. * @return a new SQL data source
  26. */
  27. static DataSource newDataSource(String uri,
  28. String user,
  29. String password) {
  30. GenericObjectPool connectionPool = new GenericObjectPool();
  31. connectionPool.setMaxActive(256);
  32. connectionPool.setMaxIdle(256);
  33. ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(
  34. uri, user, password);
  35. //
  36. // This constructor modifies the connection pool, setting its connection
  37. // factory to this. (So despite how it may appear, all of the objects
  38. // declared in this method are incorporated into the returned result.)
  39. //
  40. new PoolableConnectionFactory(
  41. connectionFactory, connectionPool, null, null, false, true);
  42. return new PoolingDataSource(connectionPool);
  43. }
  44. /**
  45. * Returns the value of the "queries" request parameter, which is an integer
  46. * bound between 1 and 500 with a default value of 1.
  47. *
  48. * @param exchange the current HTTP exchange
  49. * @return the value of the "queries" request parameter
  50. */
  51. static int getQueries(HttpServerExchange exchange) {
  52. Deque<String> values = exchange.getQueryParameters().get("queries");
  53. if (values == null) {
  54. return 1;
  55. }
  56. String textValue = values.peekFirst();
  57. if (textValue == null) {
  58. return 1;
  59. }
  60. try {
  61. int parsedValue = Integer.parseInt(textValue);
  62. return Math.min(500, Math.max(1, parsedValue));
  63. } catch (NumberFormatException e) {
  64. return 1;
  65. }
  66. }
  67. /**
  68. * Returns a random integer that is a suitable value for both the {@code id}
  69. * and {@code randomNumber} properties of a world object.
  70. *
  71. * @return a random world number
  72. */
  73. static int randomWorld() {
  74. return 1 + ThreadLocalRandom.current().nextInt(10000);
  75. }
  76. }