| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 library server.performance.scenarios; |
| 6 |
| 7 import 'dart:async'; |
| 8 import 'dart:io'; |
| 9 |
| 10 import 'package:analysis_server/plugin/protocol/protocol.dart'; |
| 11 import 'package:unittest/unittest.dart'; |
| 12 |
| 13 import 'performance_tests.dart'; |
| 14 |
| 15 void printBenchmarkResults(String id, String description, List<int> times) { |
| 16 String now = new DateTime.now().toUtc().toIso8601String(); |
| 17 print('$now ========== $id'); |
| 18 print('times: $times'); |
| 19 print(description.trim()); |
| 20 print('--------------------'); |
| 21 print(''); |
| 22 print(''); |
| 23 } |
| 24 |
| 25 class BenchmarkScenario extends AbstractTimingTest { |
| 26 /** |
| 27 * Init. |
| 28 * - Start Analysis Server. |
| 29 * - Set the analysis [roots]. |
| 30 * - Wait for analysis to complete. |
| 31 * - Make [file] the priority file. |
| 32 * |
| 33 * Measurement. |
| 34 * - Change the [file] according to the [fileChange]. |
| 35 * - Record the time to finish analysis. |
| 36 * |
| 37 * Repeat. |
| 38 * - Undo changes to the [file]. |
| 39 * - Repeat measurement [numOfRepeats] times. |
| 40 */ |
| 41 Future<List<int>> waitAnalyze_change_analyze( |
| 42 {List<String> roots, |
| 43 String file, |
| 44 FileChange fileChange, |
| 45 int numOfRepeats}) async { |
| 46 expect(roots, isNotNull, reason: 'roots'); |
| 47 expect(file, isNotNull, reason: 'file'); |
| 48 expect(fileChange, isNotNull, reason: 'fileChange'); |
| 49 expect(numOfRepeats, isNotNull, reason: 'numOfRepeats'); |
| 50 // Initialize Analysis Server. |
| 51 await super.setUp(); |
| 52 await subscribeToStatusNotifications(); |
| 53 // Set roots and analyze. |
| 54 await sendAnalysisSetAnalysisRoots(roots, []); |
| 55 await analysisFinished; |
| 56 // Make the file priority. |
| 57 await sendAnalysisSetPriorityFiles([file]); |
| 58 // Repeat. |
| 59 List<int> times = <int>[]; |
| 60 for (int i = 0; i < numOfRepeats; i++) { |
| 61 // Update and wait for analysis. |
| 62 Stopwatch stopwatch = new Stopwatch()..start(); |
| 63 await _applyFileChange(file, fileChange); |
| 64 await analysisFinished; |
| 65 times.add(stopwatch.elapsed.inMilliseconds); |
| 66 // Remove the overlay and analyze. |
| 67 await sendAnalysisUpdateContent({file: new RemoveContentOverlay()}); |
| 68 await analysisFinished; |
| 69 } |
| 70 // Done. |
| 71 await shutdown(); |
| 72 return times; |
| 73 } |
| 74 |
| 75 /** |
| 76 * Init. |
| 77 * 1. Start Analysis Server. |
| 78 * 2. Set the analysis [roots]. |
| 79 * 3. Wait for analysis to complete. |
| 80 * 4. Make [file] the priority file. |
| 81 * |
| 82 * Measurement. |
| 83 * 5. Change the [file] according to the [fileChange]. |
| 84 * 6. Request [completeAfterStr] in the updated file content. |
| 85 * 7. Record the time to get completion results. |
| 86 * 8. Undo changes to the [file] and analyze. |
| 87 * 9. Go to (5). |
| 88 */ |
| 89 Future<List<int>> waitAnalyze_change_getCompletion( |
| 90 {List<String> roots, |
| 91 String file, |
| 92 FileChange fileChange, |
| 93 String completeAfterStr, |
| 94 int numOfRepeats}) async { |
| 95 expect(roots, isNotNull, reason: 'roots'); |
| 96 expect(file, isNotNull, reason: 'file'); |
| 97 expect(fileChange, isNotNull, reason: 'fileChange'); |
| 98 expect(completeAfterStr, isNotNull, reason: 'completeAfterStr'); |
| 99 expect(numOfRepeats, isNotNull, reason: 'numOfRepeats'); |
| 100 // Initialize Analysis Server. |
| 101 await super.setUp(); |
| 102 await subscribeToStatusNotifications(); |
| 103 // Set roots and analyze. |
| 104 await sendAnalysisSetAnalysisRoots(roots, []); |
| 105 await analysisFinished; |
| 106 // Make the file priority. |
| 107 await sendAnalysisSetPriorityFiles([file]); |
| 108 // Repeat. |
| 109 List<int> times = <int>[]; |
| 110 for (int i = 0; i < numOfRepeats; i++) { |
| 111 String updatedContent = await _applyFileChange(file, fileChange); |
| 112 // Measure completion time. |
| 113 int completionOffset = |
| 114 _indexOfEnd(file, updatedContent, completeAfterStr); |
| 115 Duration completionDuration = |
| 116 await _measureCompletionTime(file, completionOffset); |
| 117 times.add(completionDuration.inMilliseconds); |
| 118 // Remove the overlay and analyze. |
| 119 await sendAnalysisUpdateContent({file: new RemoveContentOverlay()}); |
| 120 await analysisFinished; |
| 121 } |
| 122 // Done. |
| 123 await shutdown(); |
| 124 return times; |
| 125 } |
| 126 |
| 127 /** |
| 128 * Compute updated content of the [file] as described by [desc], add overlay |
| 129 * for the [file], and return the updated content. |
| 130 */ |
| 131 Future<String> _applyFileChange(String file, FileChange desc) async { |
| 132 String originalContent = _getFileContent(file); |
| 133 int offset = _indexOfEnd(file, originalContent, desc.afterStr); |
| 134 offset -= desc.afterStrBack; |
| 135 String updatedContent = originalContent.substring(0, offset) + |
| 136 desc.insertStr + |
| 137 originalContent.substring(offset); |
| 138 await sendAnalysisUpdateContent( |
| 139 {file: new AddContentOverlay(updatedContent)}); |
| 140 return updatedContent; |
| 141 } |
| 142 |
| 143 Future<Duration> _measureCompletionTime(String file, int offset) async { |
| 144 Stopwatch stopwatch = new Stopwatch(); |
| 145 stopwatch.start(); |
| 146 Completer<Duration> completer = new Completer<Duration>(); |
| 147 var completionSubscription = onCompletionResults.listen((_) { |
| 148 completer.complete(stopwatch.elapsed); |
| 149 }); |
| 150 try { |
| 151 await sendCompletionGetSuggestions(file, offset); |
| 152 return await completer.future; |
| 153 } finally { |
| 154 completionSubscription.cancel(); |
| 155 } |
| 156 } |
| 157 |
| 158 /** |
| 159 * 1. Start Analysis Server. |
| 160 * 2. Set the analysis [roots]. |
| 161 * 3. Wait for analysis to complete. |
| 162 * 4. Record the time to finish analysis. |
| 163 * 5. Shutdown. |
| 164 * 6. Go to (1). |
| 165 */ |
| 166 static Future<List<int>> start_waitInitialAnalysis_shutdown( |
| 167 {List<String> roots, int numOfRepeats}) async { |
| 168 expect(roots, isNotNull, reason: 'roots'); |
| 169 expect(numOfRepeats, isNotNull, reason: 'numOfRepeats'); |
| 170 // Repeat. |
| 171 List<int> times = <int>[]; |
| 172 for (int i = 0; i < numOfRepeats; i++) { |
| 173 BenchmarkScenario instance = new BenchmarkScenario(); |
| 174 // Initialize Analysis Server. |
| 175 await instance.setUp(); |
| 176 await instance.subscribeToStatusNotifications(); |
| 177 // Set roots and analyze. |
| 178 Stopwatch stopwatch = new Stopwatch()..start(); |
| 179 await instance.sendAnalysisSetAnalysisRoots(roots, []); |
| 180 await instance.analysisFinished; |
| 181 times.add(stopwatch.elapsed.inMilliseconds); |
| 182 // Stop the server. |
| 183 await instance.shutdown(); |
| 184 } |
| 185 return times; |
| 186 } |
| 187 |
| 188 static String _getFileContent(String path) { |
| 189 File file = new File(path); |
| 190 expect(file.existsSync(), isTrue, reason: 'File $path does not exist.'); |
| 191 return file.readAsStringSync(); |
| 192 } |
| 193 |
| 194 /** |
| 195 * Return the index of [what] in [where] in the [file], fail if not found. |
| 196 */ |
| 197 static int _indexOf(String file, String where, String what) { |
| 198 int index = where.indexOf(what); |
| 199 expect(index, isNot(-1), reason: 'Cannot find |$what| in $file.'); |
| 200 return index; |
| 201 } |
| 202 |
| 203 /** |
| 204 * Return the end index if [what] in [where] in the [file], fail if not found. |
| 205 */ |
| 206 static int _indexOfEnd(String file, String where, String what) { |
| 207 return _indexOf(file, where, what) + what.length; |
| 208 } |
| 209 } |
| 210 |
| 211 class FileChange { |
| 212 final String afterStr; |
| 213 final int afterStrBack; |
| 214 final String insertStr; |
| 215 |
| 216 FileChange({this.afterStr, this.afterStrBack: 0, this.insertStr}) { |
| 217 expect(afterStr, isNotNull, reason: 'afterStr'); |
| 218 expect(insertStr, isNotNull, reason: 'insertStr'); |
| 219 } |
| 220 } |
| OLD | NEW |