Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(724)

Side by Side Diff: components/cronet/android/test/javatests/src/org/chromium/net/MetricsTestUtil.java

Issue 2351793003: Implement timing metrics for UrlRequest (Closed)
Patch Set: Small cleanups Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 package org.chromium.net; 5 package org.chromium.net;
6 6
7 import static junit.framework.Assert.assertNotNull; 7 import static junit.framework.Assert.assertNotNull;
8 import static junit.framework.Assert.assertNull; 8 import static junit.framework.Assert.assertNull;
9 import static junit.framework.Assert.assertTrue;
9 10
10 import android.os.ConditionVariable; 11 import android.os.ConditionVariable;
11 12
13 import java.util.Date;
12 import java.util.LinkedList; 14 import java.util.LinkedList;
13 import java.util.NoSuchElementException; 15 import java.util.NoSuchElementException;
14 import java.util.concurrent.Executor; 16 import java.util.concurrent.Executor;
15 import java.util.concurrent.Executors; 17 import java.util.concurrent.Executors;
16 18
17 /** 19 /**
18 * Classes which are useful for testing Cronet's metrics implementation and are needed in more than 20 * Classes which are useful for testing Cronet's metrics implementation and are needed in more than
19 * one test file. 21 * one test file.
20 */ 22 */
21 public class MetricsTestUtil { 23 public class MetricsTestUtil {
22 /** 24 /**
23 * Executor which runs tasks only when told to with runAllTasks(). 25 * Executor which runs tasks only when told to with runAllTasks().
24 */ 26 */
25 public static class TestExecutor implements Executor { 27 public static class TestExecutor implements Executor {
26 private final LinkedList<Runnable> mTaskQueue = new LinkedList<Runnable> (); 28 private final LinkedList<Runnable> mTaskQueue = new LinkedList<Runnable> ();
27 29
28 @Override 30 @Override
29 public void execute(Runnable task) { 31 public void execute(Runnable task) {
30 mTaskQueue.add(task); 32 mTaskQueue.add(task);
31 } 33 }
32 34
33 public void runAllTasks() { 35 public void runAllTasks() {
34 try { 36 try {
35 while (mTaskQueue.size() > 0) { 37 while (mTaskQueue.size() > 0) {
36 mTaskQueue.remove().run(); 38 mTaskQueue.remove().run();
37 } 39 }
38 } catch (NoSuchElementException e) { 40 } catch (NoSuchElementException e) {
39 throw new RuntimeException("Task was removed during iteration", e); 41 throw new RuntimeException("Task was removed during iteration", e);
40 } 42 }
41 } 43 }
42 } 44 }
43 45
44 /** 46 /**
45 * RequestFinishedInfo.Listener for testing, which saves the RequestFinished Info 47 * RequestFinishedInfo.Listener for testing, which saves the RequestFinished Info
46 */ 48 */
47 public static class TestRequestFinishedListener extends RequestFinishedInfo. Listener { 49 public static class TestRequestFinishedListener extends RequestFinishedInfo. Listener {
48 private RequestFinishedInfo mRequestInfo; 50 private RequestFinishedInfo mRequestInfo;
49 private ConditionVariable mBlock; 51 private ConditionVariable mBlock;
50 private int mNumExpectedRequests = -1; 52 private int mNumExpectedRequests = -1;
51 53
54 // TODO(mgersh): it's weird that you can use either this constructor or blockUntilDone() but
55 // not both. Either clean it up or document why it has to work this way.
52 public TestRequestFinishedListener(Executor executor) { 56 public TestRequestFinishedListener(Executor executor) {
53 super(executor); 57 super(executor);
54 } 58 }
55 59
56 public TestRequestFinishedListener(int numExpectedRequests) { 60 public TestRequestFinishedListener(int numExpectedRequests) {
57 super(Executors.newSingleThreadExecutor()); 61 super(Executors.newSingleThreadExecutor());
58 mNumExpectedRequests = numExpectedRequests; 62 mNumExpectedRequests = numExpectedRequests;
59 mBlock = new ConditionVariable(); 63 mBlock = new ConditionVariable();
60 } 64 }
61 65
(...skipping 14 matching lines...) Expand all
76 mRequestInfo = requestInfo; 80 mRequestInfo = requestInfo;
77 mNumExpectedRequests--; 81 mNumExpectedRequests--;
78 if (mNumExpectedRequests == 0) { 82 if (mNumExpectedRequests == 0) {
79 mBlock.open(); 83 mBlock.open();
80 } 84 }
81 } 85 }
82 86
83 public void blockUntilDone() { 87 public void blockUntilDone() {
84 mBlock.block(); 88 mBlock.block();
85 } 89 }
90
91 public void reset() {
92 mBlock.close();
93 mNumExpectedRequests = 1;
94 mRequestInfo = null;
95 }
96 }
97
98 /**
99 * Check existence of all the timing metrics that apply to most test request s,
100 * except those that come from net::LoadTimingInfo::ConnectTiming.
101 * Also check some timing differences, focusing on things we can't check wit h asserts in the
102 * CronetMetrics constructor.
103 * Don't check push times here.
104 */
105 public static void checkTimingMetrics(
106 RequestFinishedInfo.Metrics metrics, Date startTime, Date endTime) {
107 assertNotNull(metrics.getRequestStart());
108 assertTrue(metrics.getRequestStart().after(startTime)
109 || metrics.getRequestStart().equals(startTime));
110 assertNotNull(metrics.getSendingStart());
111 assertTrue(metrics.getSendingStart().after(startTime));
112 assertNotNull(metrics.getSendingEnd());
113 assertTrue(metrics.getSendingEnd().before(endTime));
114 assertNotNull(metrics.getResponseStart());
115 assertTrue(metrics.getResponseStart().after(startTime));
116 assertNotNull(metrics.getResponseEnd());
117 assertTrue(metrics.getResponseEnd().before(endTime)
118 || metrics.getResponseEnd().equals(endTime));
119 // Entire request should take more than 0 ms
120 assertTrue(metrics.getResponseEnd().getTime() - metrics.getRequestStart( ).getTime() > 0);
121 }
122
123 /**
124 * Check that the timing metrics which come from net::LoadTimingInfo::Connec tTiming exist,
125 * except SSL times in the case of non-https requests.
126 */
127 public static void checkHasConnectTiming(
128 RequestFinishedInfo.Metrics metrics, Date startTime, Date endTime, b oolean isSsl) {
129 assertNotNull(metrics.getDnsStart());
130 assertTrue(metrics.getDnsStart().after(startTime));
131 assertNotNull(metrics.getDnsEnd());
132 assertTrue(metrics.getDnsEnd().before(endTime));
133 assertNotNull(metrics.getConnectStart());
134 assertTrue(metrics.getConnectStart().after(startTime));
135 assertNotNull(metrics.getConnectEnd());
136 assertTrue(metrics.getConnectEnd().before(endTime));
137 if (isSsl) {
138 assertNotNull(metrics.getSslStart());
139 assertTrue(metrics.getSslStart().after(startTime));
140 assertNotNull(metrics.getSslEnd());
141 assertTrue(metrics.getSslEnd().before(endTime));
142 } else {
143 assertNull(metrics.getSslStart());
144 assertNull(metrics.getSslEnd());
145 }
146 }
147
148 /**
149 * Check that the timing metrics from net::LoadTimingInfo::ConnectTiming don 't exist.
150 */
151 public static void checkNoConnectTiming(RequestFinishedInfo.Metrics metrics) {
152 assertNull(metrics.getDnsStart());
153 assertNull(metrics.getDnsEnd());
154 assertNull(metrics.getSslStart());
155 assertNull(metrics.getSslEnd());
156 assertNull(metrics.getConnectStart());
157 assertNull(metrics.getConnectEnd());
86 } 158 }
87 } 159 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698