PageRenderTime 5573ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/swift-load-generator/src/main/java/com/facebook/swift/perf/loadgenerator/LoadGenerator.java

https://gitlab.com/mayakarya/swift
Java | 171 lines | 137 code | 18 blank | 16 comment | 11 complexity | 9c3ca40f2eb0c64f475c11e49316499e MD5 | raw file
  1. /*
  2. * Copyright (C) 2012 Facebook, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  5. * not use this file except in compliance with the License. You may obtain
  6. * a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations
  14. * under the License.
  15. */
  16. package com.facebook.swift.perf.loadgenerator;
  17. import com.beust.jcommander.JCommander;
  18. import com.facebook.nifty.client.FramedClientConnector;
  19. import com.facebook.nifty.client.NiftyClientChannel;
  20. import com.facebook.nifty.client.NiftyClientConnector;
  21. import com.facebook.nifty.client.UnframedClientConnector;
  22. import com.facebook.swift.codec.guice.ThriftCodecModule;
  23. import com.facebook.swift.service.ThriftClientManager;
  24. import com.facebook.swift.service.guice.ThriftClientModule;
  25. import com.facebook.swift.service.guice.ThriftClientStatsModule;
  26. import com.google.common.collect.ImmutableMap;
  27. import com.google.common.net.HostAndPort;
  28. import com.google.inject.*;
  29. import io.airlift.bootstrap.LifeCycleManager;
  30. import io.airlift.bootstrap.LifeCycleModule;
  31. import io.airlift.configuration.ConfigurationFactory;
  32. import io.airlift.configuration.ConfigurationModule;
  33. import io.airlift.jmx.JmxModule;
  34. import io.airlift.node.NodeModule;
  35. import org.weakref.jmx.guice.MBeanModule;
  36. import javax.annotation.PostConstruct;
  37. import javax.annotation.PreDestroy;
  38. import java.util.Map;
  39. import static com.facebook.swift.service.guice.ThriftClientBinder.thriftClientBinder;
  40. public class LoadGenerator
  41. {
  42. private final Provider<AbstractClientWorker> clientWorkerProvider;
  43. private final LoadGeneratorCommandLineConfig config;
  44. private static Injector injector;
  45. private final ThriftClientManager clientManager;
  46. private LoadStatsThread loadStatsThread;
  47. private AbstractClientWorker[] clientWorkers;
  48. public static void main(final String[] args)
  49. throws Exception
  50. {
  51. final LoadGeneratorCommandLineConfig config = new LoadGeneratorCommandLineConfig();
  52. JCommander jCommander = new JCommander(config, args);
  53. if (config.displayUsage) {
  54. jCommander.usage();
  55. } else {
  56. injector = Guice.createInjector(
  57. Stage.PRODUCTION,
  58. new ConfigurationModule(new ConfigurationFactory(buildConfigMap(config))),
  59. new LifeCycleModule(),
  60. new ThriftCodecModule(),
  61. new ThriftClientModule(),
  62. new ThriftClientStatsModule(),
  63. new NodeModule(),
  64. new JmxModule(),
  65. new MBeanModule(),
  66. new Module()
  67. {
  68. @Override
  69. public void configure(Binder binder)
  70. {
  71. thriftClientBinder(binder).bindThriftClient(AsyncLoadTest.class);
  72. thriftClientBinder(binder).bindThriftClient(SyncLoadTest.class);
  73. binder.bind(LoadGeneratorCommandLineConfig.class).toInstance(config);
  74. binder.bind(LoadGenerator.class).in(Singleton.class);
  75. if (!config.asyncMode) {
  76. binder.bind(AbstractClientWorker.class).to(SyncClientWorker.class);
  77. } else {
  78. binder.bind(AbstractClientWorker.class).to(AsyncClientWorker.class);
  79. }
  80. TypeLiteral<NiftyClientConnector<? extends NiftyClientChannel>> channelConnectorType =
  81. new TypeLiteral<NiftyClientConnector<? extends NiftyClientChannel>>() {};
  82. NiftyClientConnector<? extends NiftyClientChannel> connector;
  83. switch (config.transport) {
  84. case FRAMED:
  85. connector = new FramedClientConnector(HostAndPort.fromParts(config.serverAddress, config.serverPort));
  86. break;
  87. case UNFRAMED:
  88. connector = new UnframedClientConnector(HostAndPort.fromParts(config.serverAddress, config.serverPort));
  89. break;
  90. default:
  91. throw new IllegalStateException("Unknown transport");
  92. }
  93. binder.bind(channelConnectorType).toInstance(connector);
  94. }
  95. }
  96. );
  97. injector.getInstance(LifeCycleManager.class).start();
  98. }
  99. }
  100. @Inject
  101. public LoadGenerator(
  102. LoadGeneratorCommandLineConfig config,
  103. Provider<AbstractClientWorker> clientWorkerProvider,
  104. ThriftClientManager clientManager)
  105. {
  106. this.config = config;
  107. this.clientWorkerProvider = clientWorkerProvider;
  108. this.clientManager = clientManager;
  109. }
  110. private static Map<String, String> buildConfigMap(LoadGeneratorCommandLineConfig config)
  111. {
  112. ImmutableMap.Builder<String, String> builder = ImmutableMap.builder();
  113. if (config.connectTimeoutMilliseconds > 0) {
  114. addParam(builder, "connect-timeout", config.connectTimeoutMilliseconds + "ms");
  115. }
  116. if (config.receiveTimeoutMilliseconds > 0) {
  117. addParam(builder, "read-timeout", config.receiveTimeoutMilliseconds + "ms");
  118. }
  119. if (config.sendTimeoutMilliseconds > 0) {
  120. addParam(builder, "write-timeout", config.sendTimeoutMilliseconds + "ms");
  121. }
  122. return builder.build();
  123. }
  124. private static void addParam(ImmutableMap.Builder<String, String> builder, String param, String value)
  125. {
  126. builder.put("SyncLoadTest.thrift.client." + param, value);
  127. builder.put("AsyncLoadTest.thrift.client." + param, value);
  128. }
  129. @PostConstruct
  130. public void start()
  131. throws Exception
  132. {
  133. clientWorkers = new AbstractClientWorker[config.numThreads];
  134. for (int i = 0; i < config.numThreads; i++) {
  135. clientWorkers[i] = clientWorkerProvider.get();
  136. }
  137. loadStatsThread = new LoadStatsThread(clientWorkers);
  138. loadStatsThread.start();
  139. // For fair measurement, start the client workers *after* the monitor has already started
  140. for (AbstractClientWorker worker : clientWorkers) {
  141. worker.run();
  142. }
  143. }
  144. @PreDestroy
  145. public void stop()
  146. {
  147. for (AbstractClientWorker worker : clientWorkers) {
  148. worker.shutdown();
  149. }
  150. loadStatsThread.shutdown();
  151. clientManager.close();
  152. }
  153. }