Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

ServerTest.java 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2016 SonarSource SA
  4. * mailto:contact AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonarsource.sonarqube.perf.server;
  21. import com.sonar.orchestrator.Orchestrator;
  22. import org.sonarsource.sonarqube.perf.PerfTestCase;
  23. import org.sonarsource.sonarqube.perf.ServerLogs;
  24. import java.io.IOException;
  25. import java.util.Collections;
  26. import java.util.Date;
  27. import java.util.List;
  28. import org.apache.commons.io.FileUtils;
  29. import org.junit.Test;
  30. public class ServerTest extends PerfTestCase {
  31. // ES + TOMCAT
  32. @Test
  33. public void server_startup_and_shutdown() throws Exception {
  34. String defaultWebJavaOptions = "-Xmx768m -XX:MaxPermSize=160m -XX:+HeapDumpOnOutOfMemoryError -Djava.awt.headless=true -Dfile.encoding=UTF-8 -Djruby.management.enabled=false";
  35. Orchestrator orchestrator = Orchestrator.builderEnv()
  36. .setOrchestratorProperty("javaVersion", "LATEST_RELEASE")
  37. .addPlugin("java")
  38. // See http://wiki.apache.org/tomcat/HowTo/FasterStartUp
  39. // Sometimes source of entropy is too small and Tomcat spends ~20 seconds on the step :
  40. // "Creation of SecureRandom instance for session ID generation using [SHA1PRNG]"
  41. // Using /dev/urandom fixes the issue on linux
  42. .setServerProperty("sonar.web.javaOpts", defaultWebJavaOptions + " -Djava.security.egd=file:/dev/./urandom")
  43. .build();
  44. try {
  45. long startupDuration = start(orchestrator);
  46. assertDurationAround(startupDuration, 46000);
  47. long shutdownDuration = stop(orchestrator);
  48. // can't use percent margins because logs are second-grained but not milliseconds
  49. assertDurationLessThan(shutdownDuration, 4000);
  50. } finally {
  51. orchestrator.stop();
  52. }
  53. }
  54. long start(Orchestrator orchestrator) throws IOException {
  55. ServerLogs.clear(orchestrator);
  56. orchestrator.start();
  57. return logsPeriod(orchestrator);
  58. }
  59. long stop(Orchestrator orchestrator) throws Exception {
  60. ServerLogs.clear(orchestrator);
  61. orchestrator.stop();
  62. return logsPeriod(orchestrator);
  63. }
  64. private long logsPeriod(Orchestrator orchestrator) throws IOException {
  65. // compare dates of first and last log
  66. List<String> lines = FileUtils.readLines(orchestrator.getServer().getLogs());
  67. if (lines.size() < 2) {
  68. throw new IllegalStateException("Fail to estimate server shutdown or startup duration. Not enough logs.");
  69. }
  70. Date start = ServerLogs.extractFirstDate(lines);
  71. Collections.reverse(lines);
  72. Date end = ServerLogs.extractFirstDate(lines);
  73. return end.getTime() - start.getTime();
  74. }
  75. }