Java 给定一个包含API名称、开始和结束时间戳的日志文件,编写一个程序,打印每个API调用的平均延迟
问:给定一个包含API名称、开始和结束时间戳的日志文件,编写一个程序,打印每个API调用的平均延迟Java 给定一个包含API名称、开始和结束时间戳的日志文件,编写一个程序,打印每个API调用的平均延迟,java,algorithm,data-structures,Java,Algorithm,Data Structures,问:给定一个包含API名称、开始和结束时间戳的日志文件,编写一个程序,打印每个API调用的平均延迟 谷歌采访中有人问我,我的解决方案被拒绝了。所以,我想知道如何以优化的方式解决这个问题 $cat日志 得到_foo开始22222100 吉福端22222150 获得酒吧开始222200 得到_foo开始2222220 接杆端22230 吉福端2250 解决方案: $cat日志| myprog get_foo:平均值=40 get_bar:average=30我建议您将流程细分,以简化问题的复杂性 关
谷歌采访中有人问我,我的解决方案被拒绝了。所以,我想知道如何以优化的方式解决这个问题 $cat日志 得到_foo开始22222100 吉福端22222150 获得酒吧开始222200 得到_foo开始2222220 接杆端22230 吉福端2250 解决方案: $cat日志| myprog get_foo:平均值=40
get_bar:average=30我建议您将流程细分,以简化问题的复杂性 关于如何定义流程,这里可能是一个很好的起点: 在Java中完成,可能会导致以下情况:
public class Program {
// The HashMap which contains api keys and object wrappers associated to it.
private Map<String, ApiCall> hashMap = new HashMap<>();
/**
* getHashMap
* Setter of the hashMap which contains api keys and object wrappers
* associated to it.
* @return
*/
public Map<String, ApiCall> getHashMap() {
return hashMap;
}
/**
* setHashMap
* Getter of the hashMap which contains api keys and object wrappers
* associated to it.
* @param hashMap
*/
public void setHashMap(Map<String, ApiCall> hashMap) {
this.hashMap = hashMap;
}
/**
* getNewApiCall
* @return a new ApiCall wrapper object
*/
public ApiCall getNewApiCall() {
return new ApiCall();
}
/**
* main
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
Program app = new Program();
// Arbitrary path to test with my environment
String path = args.length > 1 ? args[0] : "/home/user/test";
// Read all lines from your log file
List<Object> lines = Arrays.asList(
Files.lines(
Paths.get(new File(path).getPath())
).toArray()
);
for (Object o:lines) {
String s = o.toString().trim();
// If it's not and empty line
if (!s.isEmpty()) {
/*
* Split the lines into array with
* [0] = api_name, [1] start or end, [2] timestamp
*/
String[] values = s.split(" ");
// Add the API call to a hashmap without any value
app.getHashMap().put(values[0], null);
// Here you have all name of calls of your API
/*
* Now for each api call, wrap it into an object that will
* handle the average computation, the array of start
* timestamps and the array of end timestamps
*/
ApiCall apiCall = app.getHashMap().get(values[0]);
// Create and object for wrapping starting and ending timestamps
if (apiCall == null) {
apiCall = app.getNewApiCall();
}
/*
* If the line is a start then parse the last value to a long
* and add it to the object wrapper to further compute for the
* average
*/
if (values[1].equals("start")) {
apiCall.addStart(Long.parseLong(values[2]));
}
/*
* Else it is a end timestamp then parse the last value of the
* line to a long and add it to the object wrapper to further
* compute for the average
*/
else {
apiCall.addEnd(Long.parseLong(values[2]));
}
// Add the new incremented object wrapper to the API key
app.getHashMap().put(values[0], apiCall);
}
}
/*
* Stream hashmap entries (API keys) and print the key and the average
* value for each
*/
app.getHashMap().entrySet().forEach(
ks -> System.out.println(
ks.getKey()+" average="+ks.getValue().getAverage()
)
);
}
}
希望能有所帮助。任务很好,到目前为止你都尝试了什么?在谷歌的采访中被问到,我的解决方案被拒绝了。所以,想知道如何以优化的方式解决这个问题。您的解决方案看起来如何?你能把它添加到你的问题中吗?用Java还是只使用linux shell?@v78两种解决方案的平均值都是一样的。我的回答完全一样,但谷歌面试官拒绝了,我不知道为什么
/**
* ApiCall
* Class allowing to collect starting timestamps and ending timestamp for
* an API call. Compute the average on each addition.
* @author user
* @since 12 sept. 2019
*/
public class ApiCall {
private List<Long> start;
private List<Long> end;
private double average;
public ApiCall() {
start = new ArrayList<>();
end = new ArrayList<>();
}
public void addStart(Long l) {
start.add(l);
setAverage(computeAverage());
}
public void addEnd(Long l) {
end.add(l);
setAverage(computeAverage());
}
public double getAverage() {
return this.average;
}
private void setAverage(Double average) {
this.average = average;
}
private double computeAverage() {
return
(
end.stream().mapToLong(Long::longValue).average().orElse(0.0)
- start.stream().mapToLong(Long::longValue).average().orElse(0.0)
);
}
}
get_bar average=2.22222223E9
get_foo average=2.22222225E9