This example shows how to change the performance profile when creating
a 51Degrees device detection engine.
It also includes a simple method of benchmarking the engine that
can illustrate the performance differences.
Note that benchmarking is a complex area and this is not a sophisticated
solution. It is simply intended to demonstrate the approximate, relative
performance of the pre-configured profiles.
30 import multiprocessing
as mp
33 from fiftyone_devicedetection_onpremise.devicedetection_onpremise_pipelinebuilder
import DeviceDetectionOnPremisePipelineBuilder
34 from fiftyone_devicedetection_examples.example_utils
import ExampleUtils
36 data_file = ExampleUtils.find_file(
"51Degrees-LiteV4.1.hash")
38 pipeline = DeviceDetectionOnPremisePipelineBuilder(
39 data_file_path=data_file,
41 performance_profile=
'MaxPerformance',
42 add_javascript_builder =
False,
43 restricted_properties = [
"ismobile"],
45 auto_update=
False).build()
50 def process_user_agent(user_agent):
53 flowdata = pipeline.create_flowdata()
56 flowdata.evidence.add(
"header.user-agent", user_agent)
66 if flowdata.device.ismobile.has_value():
67 return flowdata.device.ismobile.value()
71 def process_user_agent_list(user_agent_list, list_number, output, skip=False):
77 for user_agent
in user_agent_list:
80 result = process_user_agent(user_agent[0])
82 results[
"unknown"] += 1
84 results[
"mobile"] += 1
86 results[
"notmobile"] += 1
88 output.put(results, list_number)
91 def run(skip = False):
100 for x
in range(threads):
101 processes.append(mp.Process(target=process_user_agent_list,
102 args=(split_lists[x], x, output, skip)))
116 results = [output.get()
for p
in processes]
122 return {
"time": total,
"result": results}
124 if __name__ ==
"__main__":
127 with open(
'fiftyone_devicedetection_onpremise/device-detection-cxx/device-detection-data/20000 User Agents.csv', newline=
'')
as file:
128 reader = csv.reader(file)
129 user_agents = list(reader)
131 number_of_user_agents = len(user_agents)
133 print(
"Processing " + str(number_of_user_agents) +
" user agents")
137 threads = mp.cpu_count()
139 print(
"Using " + str(threads) +
" threads")
141 chunk_size = int(number_of_user_agents / threads)
144 split_lists = [user_agents[x:x+chunk_size]
145 for x
in range(0, len(user_agents), chunk_size)]
147 calibration = run(skip=
True)
149 real = run(skip=
False)
151 real_time = real[
"time"] - calibration[
"time"]
153 print(
"Total time (seconds): " + str(real_time) +
" seconds")
154 print (
"Time per user agent (ms): " + str((real_time / number_of_user_agents) * 1000))
162 for result
in real[
"result"]:
163 final_result[
"unknown"] += result[
"unknown"]
164 final_result[
"mobile"] += result[
"mobile"]
165 final_result[
"notmobile"] += result[
"notmobile"]
167 print(
"Results", final_result)