org.polyfillservice.perf.services.StdOutPerfReportService Maven / Gradle / Ivy
package org.polyfillservice.perf.services;
import com.inamik.text.tables.GridTable;
import com.inamik.text.tables.SimpleTable;
import com.inamik.text.tables.grid.Border;
import com.inamik.text.tables.grid.Util;
import org.polyfillservice.api.components.Polyfill;
import org.polyfillservice.api.components.Query;
import org.polyfillservice.api.components.ServiceConfig;
import org.polyfillservice.api.interfaces.PolyfillService;
import org.polyfillservice.api.interfaces.UserAgentParserService;
import org.polyfillservice.perf.interfaces.PerfReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
/**
* Created by smo on 6/9/17.
* PerfReportService to print measurements to standard output.
*/
@Service("stdout")
public class StdOutPerfReportService implements PerfReportService {
@Resource(name = "uaList")
private List uaList;
@Autowired
private ServiceConfig serviceConfig;
@Autowired
private PolyfillApiPerfMeasureService apiPerfMeasureService;
@Autowired
private SourceSizeMeasureService sizeMeasureService;
@Autowired
private PolyfillService polyfillService;
@Autowired
private UserAgentParserService uaParserService;
@Autowired
private Query defaultQuery;
private List headers = Arrays.asList(
"User Agent",
"Average Query",
"Raw Source",
"Min Source",
"Gzip Min Source",
"# of Polyfills",
"Polyfills");
private Query rawSourceQuery;
private Query minSourceQuery;
@PostConstruct
private void init() {
rawSourceQuery = new Query.Builder(defaultQuery.getFeatures()).setMinify(false).build();
minSourceQuery = new Query.Builder(defaultQuery.getFeatures()).setMinify(true).build();
}
@Override
public void showReport() {
printConfigurations();
printMeasurements();
}
private void printConfigurations() {
Set polyfillNames = polyfillService.getAllPolyfills().keySet();
// output starts here
print("# Configurations"
+ "\n" + serviceConfig.toString()
+ "\nNumber of polyfills loaded:" + polyfillNames.size()
+ "\nPolyfills loaded:" + polyfillNames
+ "\n"
);
}
private void printMeasurements() {
GridTable table = buildMeasurementsTable();
// output starts here
print("# Measurements");
print(table);
}
private GridTable buildMeasurementsTable() {
SimpleTable table = SimpleTable.of();
// add headers
table.nextRow();
this.headers.forEach(table::nextCell);
for (String uaString : uaList) {
// measure performance
double avgElapsedTime = apiPerfMeasureService.getAvgElapsedTime(uaString);
// measure source sizes
String rawSource = polyfillService.getPolyfillsSource(uaString, rawSourceQuery);
String minSource = polyfillService.getPolyfillsSource(uaString, minSourceQuery);
int rawSourceByteSize = sizeMeasureService.getByteSize(rawSource);
int minSourceByteSize = sizeMeasureService.getByteSize(minSource);
int gzipMinSourceByteSize = sizeMeasureService.getGzipByteSize(minSource);
// get names of loaded polyfills for this user agent
List polyfillNames = polyfillService.getPolyfills(uaString).stream()
.map(Polyfill::getName)
.collect(Collectors.toList());
// add row to table
table.nextRow()
.nextCell(normalizeUA(uaString))
.nextCell(formatMs(avgElapsedTime))
.nextCell(formatBytes(rawSourceByteSize))
.nextCell(formatBytes(minSourceByteSize))
.nextCell(formatBytes(gzipMinSourceByteSize))
.nextCell(String.valueOf(polyfillNames.size()))
.nextCell(polyfillNames);
}
GridTable gridTable = table.toGrid();
gridTable = Border.of(Border.Chars.of('+', '-', '|')).apply(gridTable);
return gridTable;
}
private String formatMs(double ms) {
return String.format("%.3f ms", ms);
}
private String formatBytes(int bytes) {
return String.format("%.3f kb", (bytes+0.0)/1000);
}
private String normalizeUA(String uaString) {
return uaParserService.parse(uaString).toString();
}
private void print(Object input) {
if (input instanceof GridTable) {
Util.print((GridTable)input);
} else {
System.out.println(input);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy