Skip to content

Commit 72fd06c

Browse files
committed
added simple benchmark
1 parent 6b5a3b4 commit 72fd06c

File tree

2 files changed

+107
-33
lines changed

2 files changed

+107
-33
lines changed
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package cz.mallat.uasparser;
2+
3+
import java.io.IOException;
4+
import java.io.InputStream;
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
/**
9+
* Performance tests
10+
*
11+
* Copyright: Copyright (c) 09.10.2012 <br>
12+
* Company: Braintags GmbH <br>
13+
*
14+
* @author mremme
15+
*/
16+
public class Benchmark {
17+
18+
public static InputStream getIni() {
19+
return OnlineUpdater.getVendoredInputStream();
20+
}
21+
22+
public static void main(String[] args) {
23+
24+
try {
25+
List<UASparser> parserList = new ArrayList<UASparser>();
26+
parserList.add(new UASparser(getIni()));
27+
parserList.add(new SingleThreadedUASparser(getIni()));
28+
parserList.add(new MultithreadedUASparser(getIni()));
29+
//parserList.add(new OnlineUpdateUASparser());
30+
31+
List<String> uaList = new ArrayList<String>();
32+
uaList.add("user-agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.12) Gecko/20050922 Fedora/1.0.7-1.1.fc3 Firefox/1.0.7");
33+
uaList.add("user-agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; .NET CLR 1.1.4322)");
34+
uaList.add("WhatWeb/0.4.7");
35+
uaList.add("check_http/v1.4.16 (nagios-plugins 1.4.16)");
36+
uaList.add("Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)");
37+
38+
List<String> expectedTypes = new ArrayList<String>();
39+
expectedTypes.add("Browser");
40+
expectedTypes.add("Browser");
41+
expectedTypes.add("unknown");
42+
expectedTypes.add("Other");
43+
expectedTypes.add("Robot");
44+
45+
for (UASparser uaParser : parserList) {
46+
performTest(uaParser, uaList);
47+
}
48+
49+
} catch (Exception e) {
50+
e.printStackTrace();
51+
}
52+
53+
}
54+
55+
private static final void performTest(UASparser uaParser, List<String> uaList) throws IOException {
56+
long startTime = System.currentTimeMillis();
57+
58+
int i = 0;
59+
60+
while (i++ < 5000) {
61+
for (String tmpString : uaList) {
62+
UserAgentInfo info = uaParser.parse(tmpString);
63+
//System.out.println("getType: " + info.getType());
64+
}
65+
}
66+
long newTime = System.currentTimeMillis() - startTime;
67+
System.out.println(uaParser.getClass().getSimpleName() + ": " + newTime);
68+
69+
}
70+
71+
private static final void checkThreadSafe(final UASparser uaParser, final List<String> uaList,
72+
final List<String> expectedType, final int threadCount, final int runs) {
73+
74+
List<Runnable> threads = new ArrayList<Runnable>();
75+
for (int i = 0; i < threadCount; i++) {
76+
Runnable runnable = new Runnable() {
77+
78+
@Override
79+
public void run() {
80+
int r = 0;
81+
while (r++ < runs) {
82+
for (int k = 0; k < uaList.size(); k++) {
83+
String uaString = uaList.get(k);
84+
String expected = expectedType.get(k);
85+
try {
86+
UserAgentInfo info = uaParser.parse(uaString);
87+
if (!info.getType().equals(expected))
88+
throw new IllegalArgumentException("not expected: " + info.getType() + " / " + expected);
89+
} catch (IOException e) {
90+
throw new RuntimeException(e);
91+
}
92+
}
93+
}
94+
System.out.println("finished Thread " + Thread.currentThread().getName());
95+
}
96+
};
97+
threads.add(runnable);
98+
}
99+
100+
int i = 0;
101+
for (Runnable runnable : threads) {
102+
new Thread(runnable, "Thread " + i++).start();
103+
}
104+
}
105+
106+
107+
}

src/test/java/cz/mallat/uasparser/TestPerf.java

Lines changed: 0 additions & 33 deletions
This file was deleted.

0 commit comments

Comments
 (0)