-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
822 lines (698 loc) · 34.8 KB
/
main.py
File metadata and controls
822 lines (698 loc) · 34.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
import os
import glob
import fcntl
import subprocess
import time
import json
import argparse
from threading import Thread
from queue import Queue
from concurrent.futures import ThreadPoolExecutor, as_completed
import signal, atexit, sys
from env.aut_env import AndroidAppEnv
from appium_manager import get_appium_manager
from env.mutil_aut_env import multi_AppEnv
from agent.Q import QLearningAgent
from agent.Random import RandomAgent
from agent.ResourceQLearning import ResourceQLearning
from apk.apk import install_apk_if_needed, install_apk, uninstall_app, apktool, component_extract, install_apks, uninstall_apps
from agent import global_data
from utils import (
get_logger,
get_device_name,
get_android_version,
_ensure_app_dir,
get_next_run_index,
count_existing_runs,
list_missing_run_indices,
build_missing_tasks,
count_valid_activities_files,
login_to_app,
)
from agent.Q import QLearningAgent
import time
from resources.resource import init_resource, append_resource
# Create log directory
if not os.path.exists('logs'):
os.makedirs('logs')
# Main logger
main_logger = get_logger()
SUPPORTED_ALGOS = [
"random",
# "monkey",
"q_res_v2",
"agebot_r",
"agebot_b",
"agebot_e",
"q_cov",
]
ALGO_NAME_ALIASES = {
"agebot-r": "agebot_r",
"agebot_r": "agebot_r",
"agebot-b": "agebot_b",
"agebot_b": "agebot_b",
"agebot-e": "agebot_e",
"agebot_e": "agebot_e",
}
def _parse_csv(raw: str | None) -> list[str]:
if not raw:
return []
return [item.strip() for item in raw.split(',') if item.strip()]
def _resolve_algos(raw_algos: str) -> list[str]:
if not raw_algos:
return ["q_res"]
parsed = _parse_csv(raw_algos)
if not parsed:
return ["q_res"]
if len(parsed) == 1 and parsed[0].lower() == 'all':
return SUPPORTED_ALGOS.copy()
canonical = []
for al in parsed:
key = al.strip().lower()
canonical_name = ALGO_NAME_ALIASES.get(key, key)
canonical.append(canonical_name)
unknown = [al for al in canonical if al not in SUPPORTED_ALGOS]
if unknown:
raise ValueError(
f"Unknown algorithm: {unknown}. Available algorithms: {SUPPORTED_ALGOS}"
)
return canonical
def parallel_single_main(al_list, apps, duration, devices_name, appium_manager=None):
"""
Multi-device parallel execution: for each (app, algo), fill up to rounds results.
- Use unified numbering result/<app>/<N>/ as run directory.
- If an app/algorithm hasn't reached rounds times, loop to reassign that app's tasks.
"""
max_outer_loops = 10 # Safety limit to prevent infinite retry from exceptions
outer = 0
app_names = list(apps.keys()) if isinstance(apps, dict) else list(apps)
while outer < max_outer_loops:
if appium_manager is not None:
try:
appium_manager.recover_unhealthy(max_restart=10)
main_logger.info(f"Appium status (round start): {appium_manager.summary()}")
except Exception as e:
main_logger.warning(f"Failed to get Appium status: {e}")
# Calculate missing tasks
tasks_list = build_missing_tasks(al_list, app_names, rounds)
if not tasks_list:
main_logger.info("All (app, algo) have reached target rounds, ending assignment.")
break
# Sort by (run_idx, app, algo): prioritize different algorithms, then different apps, finally increase repeat count
# task = (app, al, run_idx)
_algo_order = {al: i for i, al in enumerate(al_list)}
tasks_list.sort(key=lambda task: (task[2], task[0], _algo_order.get(task[1], 0)))
main_logger.info(f"Missing tasks count: {len(tasks_list)}, sorted by (run_idx, app, algo).")
main_logger.info(f"Starting this round of parallel assignment (round {outer+1})")
# Create task queue and failed task record
task_queue = Queue()
failed_tasks = [] # Record failed tasks
failed_tasks_lock = __import__('threading').Lock()
for app, al, run_idx in tasks_list:
task_queue.put((app, al, run_idx))
def device_worker(device_index, device_name):
logger = get_logger(device_name)
logger.info(f"Parallel worker thread started: device={device_name}, index={device_index}")
while True:
try:
app, al, run_idx = task_queue.get_nowait()
except Exception:
break
success = False
max_retries = 3 # Max retries per task
retry_count = 0
while retry_count < max_retries and not success:
try:
if retry_count > 0:
logger.info(f"Device {device_name} retry attempt {retry_count}: {app}/{al} run={run_idx}")
else:
logger.info(f"Device {device_name} starting execution: {app}/{al} run={run_idx}")
if al == "monkey":
single_main(al, app, 3600*10, device_index=device_index, devices_name=devices_name, run_index=run_idx)
else:
single_main(al, app, duration, device_index=device_index, devices_name=devices_name, run_index=run_idx)
# Verify result file is generated
result_file = os.path.join('result', app, str(run_idx), f'{al}_bug_report.json')
if os.path.exists(result_file):
logger.info(f"Device {device_name} completed: {app}/{al} run={run_idx}")
success = True
else:
logger.warning(f"Device {device_name} execution completed but result file missing: {app}/{al} run={run_idx}")
retry_count += 1
time.sleep(5) # Wait 5 seconds before retry
except Exception as e:
logger.error(f"Device {device_name} running {app}/{al} run={run_idx} error (attempt {retry_count+1}/{max_retries}): {e}", exc_info=True)
retry_count += 1
if retry_count < max_retries:
time.sleep(10) # Wait 10 seconds before retry
# If all retries failed, record failed task
if not success:
with failed_tasks_lock:
failed_tasks.append((app, al, run_idx))
logger.error(f"Task ultimately failed, recorded: {app}/{al} run={run_idx}")
task_queue.task_done()
logger.info(f"Parallel worker thread ended: device={device_name}")
# Start threads
threads = []
for idx, dn in enumerate(devices_name):
t = Thread(target=device_worker, args=(idx, dn), daemon=False)
threads.append(t)
t.start()
# Wait for completion
task_queue.join()
for t in threads:
t.join()
# Report failed tasks
if failed_tasks:
main_logger.warning(f"This round has {len(failed_tasks)} failed tasks:")
for app, al, run_idx in failed_tasks:
main_logger.warning(f" - {app}/{al} run={run_idx}")
# Write failed tasks to log file for later analysis
failed_log_path = os.path.join('logs', 'failed_tasks.json')
try:
if os.path.exists(failed_log_path):
with open(failed_log_path, 'r', encoding='utf-8') as f:
all_failed = json.load(f)
else:
all_failed = []
all_failed.extend([
{'app': app, 'algo': al, 'run_idx': run_idx, 'timestamp': time.time()}
for app, al, run_idx in failed_tasks
])
with open(failed_log_path, 'w', encoding='utf-8') as f:
json.dump(all_failed, f, ensure_ascii=False, indent=4)
main_logger.info(f"Failed tasks recorded to: {failed_log_path}")
except Exception as e:
main_logger.error(f"Error recording failed tasks: {e}")
else:
main_logger.info("All tasks in this round completed successfully")
outer += 1
def parallel_unconditional_main(al_list, apps, duration, devices_name):
"""
Multi-device parallel execution, for each (app, algo) ensure 3 independent activities.json files exist.
- Check existing valid activities.json files (file exists and format is correct)
- If less than 3, continue running single_main until satisfied
- Tasks are randomly shuffled for better load balancing
"""
import random
app_names = list(apps.keys()) if isinstance(apps, dict) else list(apps)
max_outer_loops = 20 # Max outer loop count to prevent infinite loop
outer = 0
while outer < max_outer_loops:
# 1. Build task list: check each (app, algo) whether it has 3 valid activities.json files
tasks_list = []
algo_status = {} # Record completion status for each algorithm
for app in app_names:
for al in al_list:
# Count existing valid activities.json files for this app/algo combination
valid_count = count_valid_activities_files(app, al)
algo_key = f"{app}/{al}"
algo_status[algo_key] = valid_count
# If less than 3 valid files, need to continue execution
needed_runs = max(0, 3 - valid_count)
if needed_runs > 0:
# Find max index for this app/algo (check both activities and bug_report files)
pattern_activities = os.path.join('result', app, '*', f'{al}_activities.json')
pattern_bug_report = os.path.join('result', app, '*', f'{al}_bug_report.json')
existing_files = glob.glob(pattern_activities) + glob.glob(pattern_bug_report)
if existing_files:
# Extract all existing run indices
existing_indices = []
for file_path in existing_files:
# Extract index from path: result/app/N/al_*.json
parts = file_path.split(os.sep)
try:
idx = int(parts[-2]) # Second-to-last part is the index
existing_indices.append(idx)
except (ValueError, IndexError):
continue
# Deduplicate and start from max index + 1
existing_indices = list(set(existing_indices))
start_idx = max(existing_indices) + 1 if existing_indices else 1
else:
# No existing files, start from 1
start_idx = 1
# Assign needed runs for this (app, al)
for i in range(needed_runs):
tasks_list.append((app, al, start_idx + i))
main_logger.info(f"App {app} algo {al}: has {valid_count}/3 valid data, will execute indices {start_idx} to {start_idx + needed_runs - 1}")
# Check if all algorithms have satisfied the condition
if not tasks_list:
main_logger.info("All (app, algo) have satisfied 3 valid activities.json files condition, ending execution.")
main_logger.info(f"Final statistics: {algo_status}")
break
main_logger.info(f"Starting round {outer + 1} parallel assignment, total tasks: {len(tasks_list)}")
main_logger.info(f"Current completion status: {algo_status}")
# 2. Randomly shuffle tasks for cross-device load balancing
random.shuffle(tasks_list)
main_logger.info(f"Tasks randomly shuffled, starting execution")
main_logger.info(f"Tasks randomly shuffled, starting execution")
# 3. Create task queue and failed task record
task_queue = Queue()
failed_tasks = [] # Record failed tasks
failed_tasks_lock = __import__('threading').Lock()
for app, al, run_idx in tasks_list:
task_queue.put((app, al, run_idx))
def device_worker(device_index, device_name):
logger = get_logger(device_name)
logger.info(f"Parallel worker thread started (activities validation mode): device={device_name}, index={device_index}")
while True:
try:
app, al, run_idx = task_queue.get_nowait()
except Exception:
break # Queue is empty
success = False
max_retries = 3 # Max retries per task
retry_count = 0
while retry_count < max_retries and not success:
try:
if retry_count > 0:
logger.info(f"Device {device_name} retry attempt {retry_count}: {app}/{al} run={run_idx}")
else:
logger.info(f"Device {device_name} starting execution: {app}/{al} run={run_idx}")
single_main(al, app, duration, device_index=device_index, devices_name=devices_name, run_index=run_idx)
# Verify result file is generated and activities.json is valid JSON file
activities_file = os.path.join('result', app, str(run_idx), f'{al}_activities.json')
bug_report_file = os.path.join('result', app, str(run_idx), f'{al}_bug_report.json')
if os.path.exists(activities_file) and os.path.exists(bug_report_file):
# Validate activities.json validity
try:
with open(activities_file, 'r', encoding='utf-8') as f:
activities_data = json.load(f)
if isinstance(activities_data, dict):
logger.info(f"Device {device_name} completed (valid data): {app}/{al} run={run_idx}, contains {len(activities_data)} activities")
success = True
else:
logger.warning(f"Device {device_name} execution completed but activities.json format incorrect: {app}/{al} run={run_idx}")
retry_count += 1
time.sleep(5)
except Exception as e:
logger.warning(f"Device {device_name} execution completed but activities.json parsing failed: {app}/{al} run={run_idx}, error: {e}")
retry_count += 1
time.sleep(5)
else:
logger.warning(f"Device {device_name} execution completed but result file missing: {app}/{al} run={run_idx}")
retry_count += 1
time.sleep(5)
except Exception as e:
logger.error(f"Device {device_name} running {app}/{al} run={run_idx} error (attempt {retry_count+1}/{max_retries}): {e}", exc_info=True)
retry_count += 1
if retry_count < max_retries:
time.sleep(10) # Wait 10 seconds before retry
# If all retries failed, record failed task
if not success:
with failed_tasks_lock:
failed_tasks.append((app, al, run_idx))
logger.error(f"Task ultimately failed, recorded: {app}/{al} run={run_idx}")
task_queue.task_done()
logger.info(f"Parallel worker thread ended (activities validation mode): device={device_name}")
# Start threads
threads = []
for idx, dn in enumerate(devices_name):
t = Thread(target=device_worker, args=(idx, dn), daemon=False)
threads.append(t)
t.start()
# Wait for all tasks to complete
task_queue.join()
for t in threads:
t.join()
# Report failed tasks
if failed_tasks:
main_logger.warning(f"Round {outer + 1} has {len(failed_tasks)} failed tasks:")
for app, al, run_idx in failed_tasks:
main_logger.warning(f" - {app}/{al} run={run_idx}")
# Write failed tasks to log file
failed_log_path = os.path.join('logs', 'failed_tasks_activities_validation.json')
try:
if os.path.exists(failed_log_path):
with open(failed_log_path, 'r', encoding='utf-8') as f:
all_failed = json.load(f)
else:
all_failed = []
all_failed.extend([
{'app': app, 'algo': al, 'run_idx': run_idx, 'timestamp': time.time(), 'round': outer + 1}
for app, al, run_idx in failed_tasks
])
with open(failed_log_path, 'w', encoding='utf-8') as f:
json.dump(all_failed, f, ensure_ascii=False, indent=4)
main_logger.info(f"Failed tasks recorded to: {failed_log_path}")
except Exception as e:
main_logger.error(f"Error recording failed tasks: {e}")
else:
main_logger.info(f"Round {outer + 1} all tasks completed successfully")
outer += 1
# If there are still incomplete tasks, rest briefly before next round
if outer < max_outer_loops:
main_logger.info(f"Preparing to enter round {outer + 1} check...")
time.sleep(2)
# Final statistics after loop ends
if outer >= max_outer_loops:
main_logger.warning(f"Reached max loop count {max_outer_loops}, forcing end")
main_logger.info("All parallel_unconditional_main tasks completed.")
# Final statistics report
final_status = {}
for app in app_names:
for al in al_list:
valid_count = count_valid_activities_files(app, al)
algo_key = f"{app}/{al}"
final_status[algo_key] = valid_count
if valid_count >= 3:
main_logger.info(f"✓ {algo_key}: {valid_count}/3 valid data (completed)")
else:
main_logger.warning(f"✗ {algo_key}: {valid_count}/3 valid data (incomplete)")
main_logger.info(f"Final statistics: {final_status}")
def single_main(al, app, duration=3600, device_index=0, devices_name=None, run_index: int | None = None):
devices_name = devices_name or []
device_name = devices_name[device_index] if devices_name else "default"
device_logger = get_logger(devices_name[device_index] if devices_name else "default", app)
apk_path = apps[app]
apktool(apk_path)
package, main_activity, activities = component_extract(
f'{apk_path.split(".")[0]}/AndroidManifest.xml')
device_logger.info(f"Starting single device test - App: {app}, Algorithm: {al}")
# Get Android version for this device
local_android_version = get_android_version(device_name) if devices_name else "11"
# If run_index is specified, check existence first, skip if already exists
if run_index is not None:
existing = os.path.join('result', app, str(run_index), f'{al}_bug_report.json')
if os.path.exists(existing):
device_logger.info(f"Result already exists, skipping run: {existing}")
return
# Single run (unified numbering, no subdirectory by device)
if not install_apk_if_needed(apk_path, device_name, package):
device_logger.error(f"App {app} installation failed, skipping this app")
return
env = None
installed = True
if app == "aurora.store":
main_activity = "MainActivity"
if app == "fossify_calendar":
main_activity = "org.fossify.calendar.activities.MainActivity"
if app == "taz":
# taz app needs special handling, ensure full Activity path is used
if not main_activity.startswith("de.thecode.android.tazreader"):
main_activity = f"de.thecode.android.tazreader.{main_activity}" if "." not in main_activity else main_activity
start_time = time.time()
run_idx = run_index if run_index is not None else None
try:
if al == 'q_res':
env = AndroidAppEnv(app, package, main_activity, 'resources', activities, local_android_version,
device_name,
ports[device_index], start_time, al.split('_')[-1], algo_name=al)
agent = QLearningAgent(env, duration=duration)
agent.learn(start_time)
elif al in ('q_res_v2', 'agebot_r', 'agebot_b', 'agebot_e'):
# q_res_v2 and its ablation experiments:
# - agebot_r: remove smoothing and normalization from resource sensitivity reward
# - agebot_b: remove bug reward component
# - agebot_e: fixed epsilon-greedy (no decay)
env = AndroidAppEnv(app, package, main_activity, 'resources', activities, local_android_version,
device_name,
ports[device_index], start_time, 'all', algo_name=al)
agent_kwargs = {}
if al == 'agebot_e':
agent_kwargs.update({
'epsilon': 0.1,
'epsilon_decay': 1.0,
'epsilon_min': 0.1,
})
agent = QLearningAgent(env, duration=duration, **agent_kwargs)
agent.learn(start_time)
elif al == 'q_cov':
env = AndroidAppEnv(app, package, main_activity, 'cov', activities, local_android_version,
device_name,
ports[device_index], start_time, al.split('_')[-1], fsm_enabled=True, algo_name=al)
agent = QLearningAgent(env, duration=duration)
agent.learn(start_time)
elif al == 'random':
env = AndroidAppEnv(app, package, main_activity, 'cov', activities, local_android_version,
device_name,
ports[device_index], start_time, al.split('_')[-1], fsm_enabled=False, algo_name=al)
agent = RandomAgent(env, duration=duration)
agent.learn(start_time)
# elif al == 'auto_res':
# env = AndroidAppEnv(app, package, main_activity, 'auto', activities, local_android_version,
# device_name,
# ports[device_index], start_time, al.split('_')[-1], fsm_enabled=True)
# agent = QLearningAgent(env, duration=duration)
# agent.learn(start_time)
elif al == 'monkey':
# Initialize environment to get driver and launch app
env = AndroidAppEnv(
app,
package,
main_activity,
'monkey',
activities,
local_android_version,
device_name,
ports[device_index],
start_time,
'all',
fsm_enabled=False,
algo_name=al,
)
# Launch adb monkey random events, interval 500ms
events = 1000000 # Large enough event count, controlled by duration
throttle_ms = 500
monkey_cmd = (
f"adb -s {device_name} shell monkey -p {package} --throttle {throttle_ms} --pct-majornav 0 --pct-syskeys 0 --pct-anyevent 0 -v {events}"
)
device_logger.info(f"Starting monkey: {monkey_cmd}")
monkey_proc = subprocess.Popen(monkey_cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
# Resource collection: sample every 10s, continue for duration seconds
sample_interval = 10
resources = init_resource(package, device_name)
next_sample = time.time() + sample_interval
while True:
now = time.time()
if now - start_time >= duration:
break
if now >= next_sample:
try:
append_resource(package, resources, device_name)
device_logger.info("Resource sampling completed at {:.2f}s".format(now - start_time))
except Exception as e:
device_logger.warning(f"Resource sampling failed: {e}")
next_sample += sample_interval
time.sleep(0.2)
# End monkey process
try:
monkey_proc.terminate()
time.sleep(1)
except Exception:
pass
# Try again to kill monkey on device side (fallback)
try:
subprocess.run(f"adb -s {device_name} shell pkill -9 monkey", shell=True)
except Exception:
pass
# Save resource data file
run_idx = run_index if run_index is not None else get_next_run_index(app)
run_dir = os.path.join('result', app, str(run_idx))
os.makedirs(run_dir, exist_ok=True)
resource_path = os.path.join(run_dir, f'{al}_resources.json')
meta = {
'package': package,
'device': device_name,
'duration_sec': duration,
'sample_interval_sec': sample_interval,
'throttle_ms': throttle_ms,
'start_ts': start_time,
'end_ts': time.time(),
}
with open(resource_path, 'w', encoding='utf-8') as f:
json.dump({'meta': meta, 'resources': resources}, f, ensure_ascii=False, indent=4)
device_logger.info(f"Resource data saved: {resource_path}")
# Determine run index to save: prefer passed run_index, otherwise assign new number
run_idx = run_index if run_index is not None else get_next_run_index(app)
run_dir = os.path.join('result', app, str(run_idx))
os.makedirs(run_dir, exist_ok=True)
# Save bug_report
try:
with open(os.path.join(run_dir, f'{al}_bug_report.json'), 'w', encoding='utf-8') as json_file:
json.dump(getattr(env, 'bug_report', {}), json_file, ensure_ascii=False, indent=4)
except Exception:
pass
# Save activities
try:
activities_first_visit = {}
for activity, visits in getattr(env, 'list_activities', {}).items():
if visits:
first_visit = visits[0]
activities_first_visit[activity] = {
"timestamp": first_visit[1],
}
with open(os.path.join(run_dir, f'{al}_activities.json'), 'w', encoding='utf-8') as json_file:
json.dump(activities_first_visit, json_file, ensure_ascii=False, indent=4)
# Write view coverage info to app root directory
app_dir = _ensure_app_dir(app)
with open(os.path.join(app_dir, 'views.json'), 'w', encoding='utf-8') as json_file:
json.dump(getattr(env, 'widget_list', {}), json_file, ensure_ascii=False, indent=4)
except Exception:
pass
finally:
try:
if env is not None:
env.close()
except Exception:
pass
if installed:
try:
uninstall_app(package, device_name)
except Exception:
pass
duration_val = time.time() - start_time
device_logger.info(f"Single test completed - App: {app}, Algorithm: {al}, run_id: {run_idx}, duration: {duration_val:.2f}s")
def multi_main(n):
main_logger.info(f"Starting multi-agent test - Agent count: {n}")
for app, apk_path in apps.items():
apktool(apk_path)
package, Main_activity, activities = component_extract(
f'{apk_path.split(".")[0]}/AndroidManifest.xml')
install_apks(apk_path, devices_name)
for j in range(3):
global_data.init_data(package, devices_name[0])
global_data.init_widget_list(app)
main_logger.info(f"Multi-agent test started - App: {app}")
start_time = time.time()
envs = []
agents = []
tasks = []
for i in range(n):
envs.append(multi_AppEnv(package, Main_activity, activities, android_version, devices_name[i], ports[i], start_time, n))
agents.append(ResourceQLearning(envs[i]))
tasks.append(Thread(target=agents[i].learn, args=(start_time, i + 1)))
tasks[i].start()
for i in range(n):
tasks[i].join()
if not os.path.exists('result'):
os.mkdir('result')
if not os.path.exists(f'result/{app}'):
os.mkdir(f'result/{app}')
if not os.path.exists(f'result/{app}/{j + 1}'):
os.mkdir(f'result/{app}/{j + 1}')
bug_report = global_data.bug_report
with open(f'result/{app}/{j + 1}/multi_{n}_bug_report.json', 'w', encoding='utf-8') as json_file:
json.dump(bug_report, json_file, ensure_ascii=False, indent=4)
widget_list = global_data.get_widget_list()
with open(f'result/{app}/views.json', 'w', encoding='utf-8') as json_file:
json.dump(widget_list, json_file, ensure_ascii=False, indent=4)
duration = time.time() - start_time
main_logger.info(f"Multi-agent test completed - App: {app}, duration: {duration:.2f}s")
uninstall_apps(package, devices_name)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Android automation test entry point')
parser.add_argument(
'--parallel',
action=argparse.BooleanOptionalAction,
default=True,
help='Enable multi-device parallel mode (default: enabled)'
)
parser.add_argument(
'--algos',
type=str,
default='q_res',
help='Algorithm list, comma separated; or pass all to enable all supported algorithms'
)
parser.add_argument(
'--rounds',
type=int,
default=3,
help='Execution rounds per experiment configuration (default: 3)'
)
parser.add_argument(
'--duration',
type=int,
default=3600,
help='Single run duration in seconds (default: 3600)'
)
parser.add_argument(
'--apps',
type=str,
default='',
help='Run only specified apps (comma separated), empty means all apps'
)
parser.add_argument(
'--list-algos',
action='store_true',
help='Print supported algorithms and exit'
)
args = parser.parse_args()
if args.list_algos:
print('Supported algos:')
for algo in SUPPORTED_ALGOS:
print(f'- {algo}')
sys.exit(0)
rounds = args.rounds # Execution rounds per experiment configuration
als = _resolve_algos(args.algos)
run_duration = args.duration
N = global_data.N # Classification count for multi-agent
apps = global_data.apps
selected_apps = _parse_csv(args.apps)
if selected_apps:
unknown_apps = [app for app in selected_apps if app not in apps]
if unknown_apps:
raise ValueError(
f"Unknown app: {unknown_apps}. Available apps: {list(apps.keys())}"
)
apps = {app: apps[app] for app in selected_apps}
device_index = 0
devices_name = get_device_name() # Device names
android_version = get_android_version(devices_name[0]) if devices_name else "11" # Android version
# Ports managed by AppiumManager
desired_ports = ['4723', '4725', '4727', '4729', '4731', '4733']
devices_name = devices_name[:len(desired_ports)] # Limit max device count to available ports
USE_PARALLEL = args.parallel
# Non-parallel mode only needs one Appium instance and one device
if USE_PARALLEL and len(devices_name) > 1:
active_ports = desired_ports[:len(devices_name)]
else:
active_ports = desired_ports[:1]
devices_name = devices_name[:1] if devices_name else devices_name
appium_manager = get_appium_manager(active_ports, use_jitless=False, auto_start=False, health_interval=10)
appium_manager.start_all()
ports = appium_manager.get_ports()
if not appium_manager.wait_until_healthy(timeout=60, poll_interval=1.0, startup_grace=12):
main_logger.error("Appium not ready after startup, terminating this test to avoid batch Connection refused")
raise RuntimeError("Appium manager initialization failed")
appium_manager.start_health_monitor()
def _graceful_exit(signum=None, frame=None):
try:
main_logger.warning(f"Received exit signal {signum}, preparing to close AppiumManager")
appium_manager.shutdown_all()
finally:
sys.exit(0)
for _sig in (signal.SIGINT, signal.SIGTERM):
signal.signal(_sig, _graceful_exit)
atexit.register(lambda: appium_manager.shutdown_all())
USE_PARALLEL = args.parallel
main_logger.info("Android automation test started")
main_logger.info(
f"Test config - Algorithms: {als}, App count: {len(apps)}, Device count: {len(devices_name)}, "
f"Parallel mode: {USE_PARALLEL}, rounds: {rounds}, duration: {run_duration}s"
)
main_logger.info(f"Appium initial status: {appium_manager.summary()}")
try:
if N == 1:
if USE_PARALLEL and len(devices_name) > 1:
main_logger.info("Using multi-device parallel mode")
parallel_single_main(als, apps, run_duration, devices_name, appium_manager=appium_manager)
else:
main_logger.info("Using single device serial mode")
for app in apps:
for al in als:
miss_idxs = list_missing_run_indices(app, al, rounds)
for ridx in miss_idxs:
single_main(al, app, run_duration, device_index=0, devices_name=devices_name, run_index=ridx)
else:
main_logger.info("Using multi-agent mode")
for app in apps:
multi_main(N)
main_logger.info("Android automation test completed")
finally:
# Ensure all Appium instances are closed on exit
try:
appium_manager.shutdown_all()
except Exception:
pass