[20190219]xargs -P实现并行执行.txt

[20190219]xargs -P实现并行执行.txt


--//上午做测试,链接http://blog.itpub.net/267265/viewspace-2636342/,我的执行脚本,使用host read -p 'wait finish...'.

--//这样很蛮烦,因为许多测试需要5分钟完成,我必须时不时看看脚本完成是否完成,非常繁琐,是否可以通过shell内部的wait等后台

--//任务完成,自动下一步操作.


--//google : xargs wait ,发现如下链接:

https://stackoverflow.com/questions/51410472/bash-wait-for-process-substitution-subshell-to-finish

https://serverfault.com/questions/819028/bash-script-wait-for-all-xargs-processes-to-be-finished

https://stackoverflow.com/questions/30924350/use-xargs-to-wait-for-enter-key

https://unix.stackexchange.com/questions/477971/call-a-command-wait-then-execute-another-command


--//看了许多链接感觉都不是很好,都需要写shell脚本.我仔细看了xargs文档,发现都忘了xargs支持-P参数可以实现并行操作.


$ time seq 10 | xargs -I{} -n1  bash -c "sleep {} "


real    0m55.040s

user    0m0.010s

sys     0m0.026s


--//这样脚本需要55秒完成.

$ echo {1..10} | sed -e 's/ /+/g'  | bc -l

55


$ time seq 10 | xargs -I{} -n1 -P10  bash -c "sleep {} "

real    0m10.008s

user    0m0.010s

sys     0m0.026s

--//如果并行,这样就需要10秒完成.

--//修改脚本如下:


$ cat cc.txt

delete from job_times;

commit ;

drop table t purge;

create table t as select rownum id from dual ;

execute sys.dbms_stats.gather_table_stats ( OwnName => user,TabName => 't',Estimate_Percent => NULL,Method_Opt => 'FOR ALL COLUMNS SIZE 1 ',Cascade => True ,No_Invalidate => false);

alter procedure do_work compile ;

alter procedure do_work1 compile ;

host sleep 3


host seq &&1 | xargs -I{} -n1 -P &&1 bash -c "sqlplus -s -l  scott/&&2 <<< \"execute do_work(&&3,'null')\" "  > /dev/null 2>&1

host sleep 1


create unique index pk_t on t(id);

alter table t modify (id  not null);

host seq &&1 | xargs -I{} -n1 -P &&1 bash -c "sqlplus -s -l  scott/&&2 <<< \"execute do_work(&&3,'notnull')\" "  > /dev/null 2>&1

host sleep 1


host seq &&1 | xargs -I{} -n1 -P &&1 bash -c "sqlplus -s -l  scott/&&2 <<< \"execute do_work1(&&3,'id=1_unique_index')\" "  > /dev/null 2>&1

host sleep 1


drop index pk_t ;

create index pk_t on t(id);

host seq &&1 | xargs -I{} -n1 -P &&1 bash -c "sqlplus -s -l  scott/&&2 <<< \"execute do_work1(&&3,'id=1_index')\" "  > /dev/null 2>&1

host sleep 1


alter table t result_cache (mode force);

host seq &&1 | xargs -I{} -n1 -P &&1 bash -c "sqlplus -s -l  scott/&&2 <<< \"execute do_work(&&3,'result_cache')\" "  > /dev/null 2>&1

host sleep 1


--//因为这样完成在启动新的任务,不用时不时查看.

--//重复测试看看:

SCOTT@book> @ cc.txt 50 book 1e6

....


--//测试结果如下:

SCOTT@book> select method,count(*),round(avg(TIME_ELA),0),sum(TIME_ELA) from job_times group by method order by 3 ;

METHOD                 COUNT(*) ROUND(AVG(TIME_ELA),0) SUM(TIME_ELA)

-------------------- ---------- ---------------------- -------------

result_cache                 50                   8542        427111

id=1_unique_index            50                   9526        476293

null                         50                  10761        538056

id=1_index                   50                  29453       1472659

notnull                      50                  30639       1531925


--//10g下的测试结果:

SCOTT@test> select method,count(*),round(avg(TIME_ELA),0),sum(TIME_ELA) from job_times group by method order by 3 ;

METHOD                 COUNT(*) ROUND(AVG(TIME_ELA),0) SUM(TIME_ELA)

-------------------- ---------- ---------------------- -------------

id=1_unique_index            50                   4829        241474

notnull                      50                  33711       1685566

id=1_index                   50                  34647       1732330

null                         50                  38203       1910151


--//细节就不再说明了.


来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/267265/viewspace-2636382/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/267265/viewspace-2636382/

你可能感兴趣的:([20190219]xargs -P实现并行执行.txt)