电气自动化技术网

线程上下文切换的性能损耗测试分析

时间:2014-06-20 来源:51Testing软件测试网 编辑:编辑部 点击:次 字体设置:
线程上下文切换的性能损耗到底有多少,请先看看下面列出的程序:

  ThreadTester是所有Tester的基类。所有的Tester都干的是同样一件事情,把counter增加到100000000,每次只能加1。

  1: public abstract class ThreadTester

  2: {

  3: public const long MAX_COUNTER_NUMBER = 100000000;

  4:

  5: private long _counter = 0;

  6:

  7: //获得计数

  8: public virtual long GetCounter()

  9: {

  10: return this._counter;

  11: }

  12:

  13: //增加计数器

  14: protected virtual void IncreaseCounter()

  15: {

  16: this._counter += 1;

  17: }

  18:

  19: //启动测试

  20: public abstract void Start();

  21:

  22: //获得Counter从开始增加到现在的数字所耗的时间

  23: public abstract long GetElapsedMillisecondsOfIncreaseCounter();

  24:

  25: //测试是否正在运行

  26: public abstract bool IsTesterRunning();

  27: }

  SingleThreadTester是单线程计数。

  1: class SingleThreadTester : ThreadTester

  2: {

  3: private Stopwatch _aStopWatch = new Stopwatch();

  4:

  5: public override void Start()

  6: {

  7: _aStopWatch.Start();

  8:

  9: Thread aThread = new Thread(() => WorkInThread());

  10: aThread.Start();

  11: }

  12:

  13: public override long GetElapsedMillisecondsOfIncreaseCounter()

  14: {

  15: return this._aStopWatch.ElapsedMilliseconds;

  16: }

  17:

  18: public override bool IsTesterRunning()

  19: {

  20: return _aStopWatch.IsRunning;

  21: }

  22:

  23: private void WorkInThread()

  24: {

  25: while (true)

  26: {

  27: if (this.GetCounter() > ThreadTester.MAX_COUNTER_NUMBER)

  28: {

  29: _aStopWatch.Stop();

  30: break;

  31: }

  32:

  33: this.IncreaseCounter();

  34: }

  35: }

  36: }

  TwoThreadSwitchTester是两个线程交替计数。

  1: class TwoThreadSwitchTester : ThreadTester

  2: {

  3: private Stopwatch _aStopWatch = new Stopwatch();

  4: private AutoResetEvent _autoResetEvent = new AutoResetEvent(false);

  5:

  6: public override void Start()

  7: {

  8: _aStopWatch.Start();

  9:

  10: Thread aThread1 = new Thread(() => Work1InThread());

  11: aThread1.Start();

  12:

  13: Thread aThread2 = new Thread(() => Work2InThread());

  14: aThread2.Start();

  15: }

  16:

  17: public override long GetElapsedMillisecondsOfIncreaseCounter()

  18: {

  19: return this._aStopWatch.ElapsedMilliseconds;

  20: }

  21:

  22: public override bool IsTesterRunning()

  23: {

  24: return _aStopWatch.IsRunning;

  25: }

  26:

  27: private void Work1InThread()

  28: {

  29: while (true)

  30: {

  31: _autoResetEvent.WaitOne();

  32:

  33: this.IncreaseCounter();

  34:

  35: if (this.GetCounter() > ThreadTester.MAX_COUNTER_NUMBER)

  36: {

  37: _aStopWatch.Stop();

  38: break;

  39: }

  40:

  41: _autoResetEvent.Set();

  42: }

  43: }

  44:

  45: private void Work2InThread()

  46: {

  47: while (true)

  48: {

  49: _autoResetEvent.Set();

  50: _autoResetEvent.WaitOne();

  51: this.IncreaseCounter();

  52:

  53: if (this.GetCounter() > ThreadTester.MAX_COUNTER_NUMBER)

  54: {

  55: _aStopWatch.Stop();

  56: break;

  57: }

  58: }

  59: }

  60: }

  MultiThreadTester可以指定线程数,多个线程争抢计数。

  1: class MultiThreadTester : ThreadTester

  2: {

  3: private Stopwatch _aStopWatch = new Stopwatch();

  4: private readonly int _threadCount = 0;

  5: private readonly object _counterLock = new object();

  6:

  7: public MultiThreadTester(int threadCount)

  8: {

  9: this._threadCount = threadCount;

  10: }

  11:

  12: public override void Start()

  13: {

  14: _aStopWatch.Start();

  15:

  16: for (int i = 0; i < _threadCount; i++)

  17: {

  18: Thread aThread = new Thread(() => WorkInThread());

  19: aThread.Start();

  20: }

  21: }

  22:

  23: public override long GetElapsedMillisecondsOfIncreaseCounter()

  24: {

  25: return this._aStopWatch.ElapsedMilliseconds;

  26: }

  27:

  28: public override bool IsTesterRunning()

  29: {

  30: return _aStopWatch.IsRunning;

  31: }

  32:

  33: private void WorkInThread()

  34: {

  35: while (true)

  36: {

  37: lock (_counterLock)

  38: {

  39: if (this.GetCounter() > ThreadTester.MAX_COUNTER_NUMBER)

  40: {

  41: _aStopWatch.Stop();

  42: break;

  43: }

  44:

  45: this.IncreaseCounter();

  46: }

  47: }

  48: }

  49: }

  Program的Main函数中,根据用户的选择来决定执行哪个测试类。

  1: class Program

  2: {

  3: static void Main(string[] args)

  4: {

  5:

  6: string inputText = GetUserChoice();

  7:

  8: while (!"4".Equals(inputText))

  9: {

  10: ThreadTester tester = GreateThreadTesterByInputText(inputText);

  11: tester.Start();

  12:

  13: while (true)

  14: {

  15: Console.WriteLine(GetStatusOfThreadTester(tester));

  16: if (!tester.IsTesterRunning())

  17: {

  18: break;

  19: }

  20: Thread.Sleep(100);

  21: }

  22:

  23: inputText = GetUserChoice();

  24: }

  25:

  26: Console.Write("Click enter to exit...");

  27: }

  28:

  29: private static string GetStatusOfThreadTester(ThreadTester tester)

  30: {

  31: return string.Format("[耗时{0}ms] counter = {1}, {2}",

  32: tester.GetElapsedMillisecondsOfIncreaseCounter(), tester.GetCounter(),

  33: tester.IsTesterRunning() ? "running" : "stopped");

  34: }

  35:

  36: private static ThreadTester GreateThreadTesterByInputText(string inputText)

  37: {

  38: switch (inputText)

  39: {

  40: case "1":

  41: return new SingleThreadTester();

顶一下
(0)
0%
踩一下
(0)
0%
打 印】【顶 部】【关 闭】【挑 错
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
验证码:点击我更换图片
版权与免责声明:
①凡本网注明"来源:电气自动化技术网"的所有作品,版权均属于电气自动化技术网,转载请必须注明出处“电气自动化技术网http://www.dqjsw.com.cn”。违反者本网将追究相关法律责任。
②本网转载并注明自其它来源的作品,目的在于传递更多信息,并不代表本网赞同其观点或证实其内容的真实性,不承担此类作品侵权行为的直接责任及连带责任。其他媒体、网站或个人从本网转载时,必须保留本网注明的作品来源,并自负版权等法律责任。
③如涉及作品内容、版权等问题,请在作品发表之日起一周内与本网联系,否则视为放弃相关权利。
>> 相关文章
栏目导航:互感器 | 智能电网 | 无功补偿 | 电容器 | 变压器 | 电源 | 避雷器 | 电抗器 | opc | 三菱PLC | 西门子PLC | 欧姆龙PLC | PLC视频教程 | 电工基础知识
© www.dqjsw.com.cn 2008-2013 版权所有 ICP网站备案:渝ICP备11003104号