/prj/test/performance/psr/src/main/java/com/tangosol/coherence/performance/psr/TestResult.java

https://github.com/oracle/coherence · Java · 524 lines · 277 code · 63 blank · 184 comment · 22 complexity · 9015a30996c8a8e209a0b72b9201121c MD5 · raw file

  1. /*
  2. * Copyright (c) 2000, 2020, Oracle and/or its affiliates.
  3. *
  4. * Licensed under the Universal Permissive License v 1.0 as shown at
  5. * http://oss.oracle.com/licenses/upl.
  6. */
  7. package com.tangosol.coherence.performance.psr;
  8. import com.tangosol.io.pof.PofReader;
  9. import com.tangosol.io.pof.PofWriter;
  10. import com.tangosol.io.pof.PortableObject;
  11. import com.tangosol.util.Base;
  12. import java.io.IOException;
  13. import java.io.PrintStream;
  14. import java.io.Serializable;
  15. import java.util.Collection;
  16. import java.util.Iterator;
  17. /**
  18. * Class that encapsulates test result data.
  19. *
  20. * @author jh 2007.02.15
  21. */
  22. public class TestResult
  23. extends Base
  24. implements PortableObject, Cloneable, Serializable
  25. {
  26. // ----- constructors ---------------------------------------------------
  27. /**
  28. * Default constructor.
  29. */
  30. public TestResult()
  31. {
  32. super();
  33. }
  34. /**
  35. * Create a new TestResult with the specified data.
  36. *
  37. * @param cMillis the test duration in milliseconds
  38. * @param cSuccess the number of successful operations performed by
  39. * the test
  40. * @param cFailure the number of failed operations performed by the
  41. * test
  42. * @param cb the number of bytes transfered by the test
  43. * @param histLatency the histogram of latency samples
  44. */
  45. public TestResult(long cMillis, long cSuccess, long cFailure, long cb,
  46. Histogram histLatency)
  47. {
  48. setDuration (cMillis);
  49. setSuccessCount(cSuccess);
  50. setFailureCount(cFailure);
  51. setByteCount (cb);
  52. setLatency (histLatency);
  53. }
  54. // ----- PortableObject -------------------------------------------------
  55. /**
  56. * {@inheritDoc}
  57. */
  58. public void readExternal(PofReader in)
  59. throws IOException
  60. {
  61. setDuration (in.readLong(0));
  62. setSuccessCount(in.readLong(1));
  63. setFailureCount(in.readLong(2));
  64. setByteCount (in.readLong(3));
  65. setLatency ((Histogram) in.readObject(4));
  66. }
  67. /**
  68. * {@inheritDoc}
  69. */
  70. public void writeExternal(PofWriter out)
  71. throws IOException
  72. {
  73. out.writeLong (0, getDuration());
  74. out.writeLong (1, getSuccessCount());
  75. out.writeLong (2, getFailureCount());
  76. out.writeLong (3, getByteCount());
  77. out.writeObject(4, getLatency());
  78. }
  79. // ----- Object methods -------------------------------------------------
  80. /**
  81. * {@inheritDoc}
  82. */
  83. public String toString()
  84. {
  85. return "TestResult(" +
  86. "Start=" + m_ldtStart +
  87. ", End=" + m_ldtStop +
  88. ", Duration=" + getDuration() + "ms" +
  89. ", Successes=" + getSuccessCount() +
  90. ", Failures=" + getFailureCount() +
  91. ", Bytes=" + getByteCount() +
  92. ", Rate=" + getRate() + "ops" +
  93. ", Throughput=" + toBandwidthString(getThroughput(), false) +
  94. ", Latency=" + getLatency() + ")";
  95. }
  96. // ----- helper methods -------------------------------------------------
  97. /**
  98. * Start timing the test.
  99. */
  100. public void start()
  101. {
  102. if (m_ldtStart == 0L)
  103. {
  104. m_ldtStart = System.currentTimeMillis();
  105. }
  106. }
  107. /**
  108. * Stop timing the test.
  109. */
  110. public void stop()
  111. {
  112. if (m_ldtStart == 0L)
  113. {
  114. return;
  115. }
  116. if (m_ldtStop == 0L)
  117. {
  118. m_cMillis = (m_ldtStop = System.currentTimeMillis()) - m_ldtStart;
  119. }
  120. }
  121. /**
  122. * Incorporate the data of the given TestResult into this TestResult.
  123. *
  124. * @param result the TestResult to incorporate
  125. */
  126. public void add(TestResult result)
  127. {
  128. if (result == null)
  129. {
  130. return;
  131. }
  132. incSuccessCount(result.getSuccessCount());
  133. incFailureCount(result.getFailureCount());
  134. incByteCount (result.getByteCount());
  135. getLatency().addSamples(result.getLatency());
  136. }
  137. /**
  138. * Encorporate the data of the given collection of TestResult objects into
  139. * this TestResult.
  140. *
  141. * @param colResult the collection of TestResult objects to encorporate
  142. */
  143. public void add(Collection colResult)
  144. {
  145. if (colResult == null)
  146. {
  147. return;
  148. }
  149. for (Iterator iter = colResult.iterator(); iter.hasNext(); )
  150. {
  151. add((TestResult) iter.next());
  152. }
  153. }
  154. /**
  155. * Return a new TestResult that represents the delta between this
  156. * TestResult and the specified TestResult (i.e. this - that).
  157. *
  158. * @param resultThat the TestResult to subtract from this TestResult
  159. *
  160. * @return the delta TestResult
  161. */
  162. public TestResult computeDelta(TestResult resultThat)
  163. {
  164. TestResult resultDiff;
  165. try
  166. {
  167. resultDiff = (TestResult) clone();
  168. if (resultThat == null)
  169. {
  170. resultDiff.setLatency((Histogram) getLatency().clone());
  171. }
  172. else
  173. {
  174. resultDiff.setSuccessCount(getSuccessCount() - resultThat.getSuccessCount());
  175. resultDiff.setFailureCount(getFailureCount() - resultThat.getFailureCount());
  176. resultDiff.setByteCount (getByteCount() - resultThat.getByteCount());
  177. resultDiff.setLatency (getLatency().computeDelta(resultThat.getLatency()));
  178. }
  179. }
  180. catch (CloneNotSupportedException e)
  181. {
  182. throw new RuntimeException(e);
  183. }
  184. return resultDiff;
  185. }
  186. /**
  187. * Write results in spreadsheet loadable format.
  188. *
  189. * @param out the stream to write the report to
  190. */
  191. public void writeReport(PrintStream out)
  192. {
  193. out.print("Duration (ms):");
  194. out.print('\t');
  195. out.print("Successful Operations:");
  196. out.print('\t');
  197. out.print("Failed Operations:");
  198. out.print('\t');
  199. out.print("Byte Count:");
  200. out.print('\t');
  201. out.print("Rate (ops):");
  202. out.print('\t');
  203. out.print("Throughput (Bps):");
  204. out.print('\t');
  205. out.print("Latency:");
  206. out.println();
  207. out.print(getDuration());
  208. out.print('\t');
  209. out.print(getSuccessCount());
  210. out.print('\t');
  211. out.print(getFailureCount());
  212. out.print('\t');
  213. out.print(getByteCount());
  214. out.print('\t');
  215. out.print(getRate());
  216. out.print('\t');
  217. out.print(getThroughput());
  218. out.print('\t');
  219. out.print(getLatency());
  220. out.println();
  221. out.println();
  222. out.println("Latency Report:");
  223. getLatency().writeReport(out, true);
  224. }
  225. // ----- accessors ------------------------------------------------------
  226. /**
  227. * Return the test duration in milliseconds.
  228. *
  229. * @return the number of milliseconds that the test took
  230. */
  231. public long getDuration()
  232. {
  233. return m_cMillis;
  234. }
  235. /**
  236. * Set the test duration in milliseconds.
  237. *
  238. * @param cMillis the number of milliseconds that the test took
  239. */
  240. public void setDuration(long cMillis)
  241. {
  242. assert cMillis >= 0L;
  243. m_cMillis = cMillis;
  244. }
  245. /**
  246. * Increment the test duration by the specified number of milliseconds.
  247. *
  248. * @param cMillis the number of milliseconds to increment the duration by
  249. */
  250. public void incDuration(long cMillis)
  251. {
  252. assert cMillis >= 0L;
  253. m_cMillis += cMillis;
  254. }
  255. /**
  256. * Return the total number of operations (successful and unsuccessful)
  257. * performed by the test.
  258. *
  259. * @return the total number of operations
  260. */
  261. public long getOperationCount()
  262. {
  263. return m_cSuccess + m_cFailure;
  264. }
  265. /**
  266. * Return the number of failed operations performed by the test.
  267. *
  268. * @return the number of failed operations
  269. */
  270. public long getFailureCount()
  271. {
  272. return m_cFailure;
  273. }
  274. /**
  275. * Set the number of failed operations performed by the test.
  276. *
  277. * @param cFailure the number of failed operations
  278. */
  279. public void setFailureCount(long cFailure)
  280. {
  281. assert cFailure >= 0L;
  282. m_cFailure = cFailure;
  283. }
  284. /**
  285. * Increment the number of failed operations performed by the test by
  286. * the specified amount.
  287. *
  288. * @param cFailure the number of operations to increment the total by
  289. */
  290. public void incFailureCount(long cFailure)
  291. {
  292. assert cFailure >= 0L;
  293. m_cFailure += cFailure;
  294. }
  295. /**
  296. * Return the number of successful operations performed by the test.
  297. *
  298. * @return the number of successful operations
  299. */
  300. public long getSuccessCount()
  301. {
  302. return m_cSuccess;
  303. }
  304. /**
  305. * Set the number of successful operations performed by the test.
  306. *
  307. * @param cSuccess the number of successful operations
  308. */
  309. public void setSuccessCount(long cSuccess)
  310. {
  311. assert cSuccess >= 0L;
  312. m_cSuccess = cSuccess;
  313. }
  314. /**
  315. * Increment the number of failed operations performed by the test by
  316. * the specified amount.
  317. *
  318. * @param cSuccess the number of operations to increment the total by
  319. */
  320. public void incSuccessCount(long cSuccess)
  321. {
  322. assert cSuccess >= 0L;
  323. m_cSuccess += cSuccess;
  324. }
  325. /**
  326. * Return the total number of bytes transfered by the test.
  327. *
  328. * @return the total number of bytes transfered
  329. */
  330. public long getByteCount()
  331. {
  332. return m_cb;
  333. }
  334. /**
  335. * Set the total number of bytes transfered by the test.
  336. *
  337. * @param cb the total number of bytes transfered
  338. */
  339. public void setByteCount(long cb)
  340. {
  341. assert cb >= 0L;
  342. m_cb = cb;
  343. }
  344. /**
  345. * Increment the total number of bytes transfered by the test by the
  346. * specified amount.
  347. *
  348. * @param cb the number of bytes to increase the total by
  349. */
  350. public void incByteCount(long cb)
  351. {
  352. assert cb >= 0L;
  353. m_cb += cb;
  354. }
  355. /**
  356. * Return the histogram of latency samples gathered during the test.
  357. *
  358. * @return the latency histogram
  359. */
  360. public Histogram getLatency()
  361. {
  362. Histogram histLatency = m_histLatency;
  363. if (histLatency == null)
  364. {
  365. setLatency(histLatency = new ScaledHistogram(50, "ms"));
  366. }
  367. return histLatency;
  368. }
  369. /**
  370. * Configure the histogram of latency samples.
  371. *
  372. * @param histLatency the latency histogram
  373. */
  374. public void setLatency(Histogram histLatency)
  375. {
  376. assert histLatency != null;
  377. m_histLatency = histLatency;
  378. }
  379. /**
  380. * Calculate the throughput of the test in bytes per second.
  381. *
  382. * @return the calculated throughput
  383. */
  384. public long getThroughput()
  385. {
  386. long cMillis = getDuration();
  387. if (cMillis == 0L)
  388. {
  389. return 0L;
  390. }
  391. long cb = getByteCount();
  392. if (cb > Long.MAX_VALUE / 1000L) // avoid overflow
  393. {
  394. return (cb / cMillis) * 1000L;
  395. }
  396. else
  397. {
  398. return (cb * 1000L) / cMillis;
  399. }
  400. }
  401. /**
  402. * Calcluate the rate of the test in operations per second.
  403. *
  404. * @return the calculated rate
  405. */
  406. public long getRate()
  407. {
  408. long cMillis = getDuration();
  409. if (cMillis == 0L)
  410. {
  411. return 0L;
  412. }
  413. long cOps = getOperationCount();
  414. if (cOps > Long.MAX_VALUE / 1000L) // avoid overflow
  415. {
  416. return (cOps / cMillis) * 1000L;
  417. }
  418. else
  419. {
  420. return (cOps * 1000L) / cMillis;
  421. }
  422. }
  423. // ----- Object methods -------------------------------------------------
  424. /**
  425. * {@inheritDoc}
  426. */
  427. protected Object clone()
  428. throws CloneNotSupportedException
  429. {
  430. return super.clone();
  431. }
  432. // ----- data members ---------------------------------------------------
  433. /**
  434. * The total test time in milliseconds.
  435. */
  436. private long m_cMillis;
  437. /**
  438. * The number of successful operations.
  439. */
  440. private long m_cSuccess;
  441. /**
  442. * The number of failed operations.
  443. */
  444. private long m_cFailure;
  445. /**
  446. * The total number of bytes transfered.
  447. */
  448. private long m_cb;
  449. /**
  450. * The histogram of latency samples.
  451. */
  452. private transient Histogram m_histLatency;
  453. /**
  454. * The start time of the test.
  455. */
  456. private transient long m_ldtStart;
  457. /**
  458. * The stop time of the test.
  459. */
  460. private transient long m_ldtStop;
  461. }